Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/selectors_vm/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::parser::{Selector, SelectorImplDescriptor};
use crate::selectors_vm::{DenseHashSet, MatchId};
use selectors::attr::{AttrSelectorOperator, ParsedCaseSensitivity};
use selectors::attr::{AttrSelectorOperator, ParsedAttrSelectorOperation, ParsedCaseSensitivity};
use selectors::parser::{Combinator, Component, NthType};
use std::fmt::{self, Debug, Formatter};

Expand Down Expand Up @@ -123,9 +123,11 @@ impl From<&Component<SelectorImplDescriptor>> for Condition {
Component::ExplicitNoNamespace => Self::OnTagName(OnTagNameExpr::Unmatchable),
Component::ID(id) => Self::OnAttributes(OnAttributesExpr::Id(id.to_boxed_slice())),
Component::Class(c) => Self::OnAttributes(OnAttributesExpr::Class(c.to_boxed_slice())),
Component::AttributeInNoNamespaceExists { local_name, .. } => Self::OnAttributes(
OnAttributesExpr::AttributeExists(local_name.to_boxed_slice()),
),
Component::AttributeInNoNamespaceExists {
local_name_lower, ..
} => Self::OnAttributes(OnAttributesExpr::AttributeExists(
local_name_lower.to_boxed_slice(),
)),
&Component::AttributeInNoNamespace {
ref local_name,
ref value,
Expand All @@ -139,6 +141,23 @@ impl From<&Component<SelectorImplDescriptor>> for Condition {
operator,
),
)),
Component::AttributeOther(attr) if attr.namespace.is_none() => {
Self::OnAttributes(match &attr.operation {
ParsedAttrSelectorOperation::Exists => {
OnAttributesExpr::AttributeExists(attr.local_name_lower.to_boxed_slice())
}
ParsedAttrSelectorOperation::WithValue {
operator,
case_sensitivity,
value,
} => OnAttributesExpr::AttributeComparisonExpr(AttributeComparisonExpr::new(
attr.local_name_lower.to_boxed_slice(),
value.to_boxed_slice(),
*case_sensitivity,
*operator,
)),
})
}
Component::Nth(data) if data.ty == NthType::Child => Self::OnTagName(
OnTagNameExpr::NthChild(NthChild::new(data.an_plus_b.0, data.an_plus_b.1)),
),
Expand Down Expand Up @@ -406,6 +425,13 @@ mod tests {
negation: false,
},
),
(
"[FOO]",
Expr {
simple_expr: OnAttributesExpr::AttributeExists("foo".into()),
negation: false,
},
),
(
r#"[foo="bar"]"#,
Expr {
Expand All @@ -420,6 +446,20 @@ mod tests {
negation: false,
},
),
(
r#"[FOO="bar"]"#,
Expr {
simple_expr: OnAttributesExpr::AttributeComparisonExpr(
AttributeComparisonExpr {
name: "foo".into(),
value: "bar".into(),
case_sensitivity: ParsedCaseSensitivity::CaseSensitive,
operator: AttrSelectorOperator::Equal,
},
),
negation: false,
},
),
(
r#"[foo*=""]"#,
Expr {
Expand Down Expand Up @@ -821,6 +861,8 @@ mod tests {
SelectorError::UnexpectedTokenInAttribute,
);
assert_err("svg|img", SelectorError::NamespacedSelector);
assert_err("[*|Foo]", SelectorError::NamespacedSelector);
assert_err("[*|Foo=bar]", SelectorError::NamespacedSelector);
assert_err(".foo()", SelectorError::InvalidClassName);
assert_err(":not()", SelectorError::EmptySelector);
assert_err("div + span", SelectorError::UnsupportedCombinator('+'));
Expand Down
1 change: 1 addition & 0 deletions src/selectors_vm/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl SelectorsParser {
| Component::Class(_)
| Component::AttributeInNoNamespaceExists { .. }
| Component::AttributeInNoNamespace { .. } => Ok(()),
Component::AttributeOther(attr) if attr.namespace.is_none() => Ok(()),

Component::Nth(data) => Self::validate_nth(data),

Expand Down
17 changes: 17 additions & 0 deletions src/selectors_vm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,23 @@ mod compiler {
],
);

assert_attr_expr_matches_and_negation_reverses_match(
"[FOO]",
encoding,
&[("<div foo>", true), ("<div FOO=1>", true), ("<div>", false)],
);

assert_attr_expr_matches_and_negation_reverses_match(
r#"[FOO="bar"]"#,
encoding,
&[
("<div foo='bar'>", true),
("<div FOO='bar'>", true),
("<div foo='BAR'>", false),
("<div>", false),
],
);

assert_attr_expr_matches_and_negation_reverses_match(
r#"[foo="barα"]"#,
encoding,
Expand Down
Loading