Skip to content

Commit

Permalink
Quote bugfix (#47)
Browse files Browse the repository at this point in the history
related: [query_selector cannot parse meta[property=og:title]](#46)

Co-authored-by: mehmetcan <2010841+mehmetcansahin@users.noreply.github.com>
  • Loading branch information
mehmetcansahin and mehmetcansahin committed Aug 25, 2022
1 parent 106fa4a commit 9fe1305
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/queryselector/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,24 @@ impl<'a> Parser<'a> {
}
Some(b'=') => {
self.stream.advance();
let quote = self.stream.expect_oneof_and_skip(&[b'"', b'\'']);
let value = self.read_identifier();
if let Some(quote) = quote {
// Only require the given quote if the value starts with a quote
self.stream.expect_and_skip(quote)?;
}
self.stream.expect_and_skip(b']')?;
Selector::AttributeValue(attribute, value)
}
Some(c @ b'~' | c @ b'^' | c @ b'$' | c @ b'*') => {
self.stream.advance();
self.stream.expect_and_skip(b'=')?;
let quote = self.stream.expect_oneof_and_skip(&[b'"', b'\'']);
let value = self.read_identifier();
if let Some(quote) = quote {
// Only require the given quote if the value starts with a quote
self.stream.expect_and_skip(quote)?;
}
self.stream.expect_and_skip(b']')?;
match c {
b'~' => Selector::AttributeValueWhitespacedContains(attribute, value),
Expand Down
29 changes: 29 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,35 @@ mod query_selector {
"PASS"
);
}

#[test]
fn query_selector_with_quote() {
let input = r#"<div><meta property="og:title" content="hello" /></div>"#;
let dom = parse(input, ParserOptions::default()).unwrap();
let parser = dom.parser();
let node_option = dom
.query_selector(r#"meta[property="og:title"]"#)
.and_then(|mut iter| iter.next());
let value = if let Some(node) = node_option {
Some(
node.get(parser)
.unwrap()
.as_tag()
.unwrap()
.attributes()
.get("content")
.flatten()
.unwrap()
.try_as_utf8_str()
.unwrap()
.to_string(),
)
} else {
None
};

assert_eq!(value, Some("hello".to_string()));
}
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub fn is_ident(c: u8) -> bool {
|| (b'a'..=b'z').contains(&c)
|| c == b'-'
|| c == b'_'
|| c == b':'
}

#[inline(always)]
Expand Down

0 comments on commit 9fe1305

Please sign in to comment.