Skip to content

Commit 54680ec

Browse files
philipp-spiessadamwathan
authored andcommitted
Validate parens in arbitrary candidates
1 parent 308f9d8 commit 54680ec

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

crates/oxide/src/parser.rs

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,26 @@ impl<'a> Extractor<'a> {
334334
return ValidationResult::Restart;
335335
}
336336

337+
// Only allow parentheses for the shorthand arbitrary custom properties syntax
338+
if let Some(index) = utility.find(b"(") {
339+
let mut skip_parens_check = false;
340+
let start_brace_index = utility.find(b"[");
341+
let end_brace_index = utility.find(b"]");
342+
343+
match (start_brace_index, end_brace_index) {
344+
(Some(start_brace_index), Some(end_brace_index)) => {
345+
if start_brace_index < index && end_brace_index > index {
346+
skip_parens_check = true;
347+
}
348+
}
349+
_ => {}
350+
}
351+
352+
if !skip_parens_check && !utility[index + 1..].starts_with(b"--") {
353+
return ValidationResult::Restart;
354+
}
355+
}
356+
337357
// Pluck out the part that we are interested in.
338358
let utility = &utility[offset..(utility.len() - offset_end)];
339359

@@ -911,9 +931,6 @@ impl<'a> Extractor<'a> {
911931
fn generate_slices(&mut self, candidate: &'a [u8]) -> ParseAction<'a> {
912932
match self.without_surrounding() {
913933
Bracketing::None => ParseAction::SingleCandidate(candidate),
914-
Bracketing::Included(sliceable) if sliceable == candidate => {
915-
ParseAction::SingleCandidate(candidate)
916-
}
917934
Bracketing::Included(sliceable) | Bracketing::Wrapped(sliceable) => {
918935
if candidate == sliceable {
919936
ParseAction::SingleCandidate(candidate)
@@ -1117,7 +1134,7 @@ mod test {
11171134
assert_eq!(candidates, vec!["something"]);
11181135

11191136
let candidates = run(" [feature(slice_as_chunks)]", false);
1120-
assert_eq!(candidates, vec!["feature(slice_as_chunks)"]);
1137+
assert_eq!(candidates, vec!["feature", "slice_as_chunks"]);
11211138

11221139
let candidates = run("![feature(slice_as_chunks)]", false);
11231140
assert!(candidates.is_empty());
@@ -1213,9 +1230,8 @@ mod test {
12131230

12141231
#[test]
12151232
fn ignores_arbitrary_property_ish_things() {
1216-
// FIXME: () are only valid in an arbitrary
12171233
let candidates = run(" [feature(slice_as_chunks)]", false);
1218-
assert_eq!(candidates, vec!["feature(slice_as_chunks)",]);
1234+
assert_eq!(candidates, vec!["feature", "slice_as_chunks",]);
12191235
}
12201236

12211237
#[test]
@@ -1637,7 +1653,6 @@ mod test {
16371653

16381654
#[test]
16391655
fn arbitrary_properties_are_not_picked_up_after_an_escape() {
1640-
_please_trace();
16411656
let candidates = run(
16421657
r#"
16431658
<!-- [!code word:group-has-\\[a\\]\\:block] -->
@@ -1648,4 +1663,48 @@ mod test {
16481663

16491664
assert_eq!(candidates, vec!["!code", "a"]);
16501665
}
1666+
1667+
#[test]
1668+
fn test_find_candidates_in_braces_inside_brackets() {
1669+
let candidates = run(
1670+
r#"
1671+
const classes = [wrapper("bg-red-500")]
1672+
"#,
1673+
false,
1674+
);
1675+
1676+
assert_eq!(
1677+
candidates,
1678+
vec!["const", "classes", "wrapper", "bg-red-500"]
1679+
);
1680+
}
1681+
1682+
#[test]
1683+
fn test_is_valid_candidate_string() {
1684+
assert_eq!(
1685+
Extractor::is_valid_candidate_string(b"foo"),
1686+
ValidationResult::Valid
1687+
);
1688+
assert_eq!(
1689+
Extractor::is_valid_candidate_string(b"foo-(--color-red-500)"),
1690+
ValidationResult::Valid
1691+
);
1692+
assert_eq!(
1693+
Extractor::is_valid_candidate_string(b"bg-[url(foo)]"),
1694+
ValidationResult::Valid
1695+
);
1696+
assert_eq!(
1697+
Extractor::is_valid_candidate_string(b"group-foo/(--bar)"),
1698+
ValidationResult::Valid
1699+
);
1700+
1701+
assert_eq!(
1702+
Extractor::is_valid_candidate_string(b"foo(\"bg-red-500\")"),
1703+
ValidationResult::Restart
1704+
);
1705+
assert_eq!(
1706+
Extractor::is_valid_candidate_string(b"foo-("),
1707+
ValidationResult::Restart
1708+
);
1709+
}
16511710
}

0 commit comments

Comments
 (0)