Skip to content

Commit 1bedc1c

Browse files
committed
refactor: Use document.select instead of Rule struct
1 parent c39c189 commit 1bedc1c

File tree

2 files changed

+3
-26
lines changed

2 files changed

+3
-26
lines changed

src/lib.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ mod parser;
109109

110110
use cssparser::CowRcStr;
111111
pub use error::InlineError;
112-
use parser::Rule;
113112
use smallvec::{smallvec, SmallVec};
114113
use std::{
115114
borrow::Cow,
@@ -344,20 +343,16 @@ fn process_css(document: &NodeRef, css: &str) -> Result<()> {
344343
cssparser::RuleListParser::new_for_stylesheet(&mut parser, parser::CSSRuleListParser);
345344
for parsed in rule_list {
346345
if let Ok((selector, declarations)) = parsed {
347-
if let Ok(rule) = Rule::new(selector, declarations) {
348-
let matching_elements = document
349-
.inclusive_descendants()
350-
.filter_map(|node| node.into_element_ref())
351-
.filter(|element| rule.selectors.matches(element));
346+
if let Ok(matching_elements) = document.select(selector) {
352347
for matching_element in matching_elements {
353348
// It can be borrowed if the current selector matches <link> tag, that is
354349
// already borrowed in `inline_to`. We can ignore such matches
355350
if let Ok(mut attributes) = matching_element.attributes.try_borrow_mut() {
356351
if let Some(existing_style) = attributes.get_mut("style") {
357-
*existing_style = merge_styles(existing_style, &rule.declarations)?
352+
*existing_style = merge_styles(existing_style, &declarations)?
358353
} else {
359354
let mut final_styles = String::with_capacity(64);
360-
for (name, value) in &rule.declarations {
355+
for (name, value) in &declarations {
361356
final_styles.push_str(name);
362357
final_styles.push(':');
363358
final_styles.push_str(value);

src/parser.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
use kuchiki::Selectors;
2-
31
pub(crate) struct CSSRuleListParser;
42
pub(crate) struct CSSDeclarationListParser;
53

64
pub(crate) type Declaration<'i> = (cssparser::CowRcStr<'i>, &'i str);
75
pub(crate) type QualifiedRule<'i> = (&'i str, Vec<Declaration<'i>>);
86

9-
#[derive(Debug)]
10-
pub(crate) struct Rule<'i> {
11-
pub(crate) selectors: Selectors,
12-
pub(crate) declarations: Vec<Declaration<'i>>,
13-
}
14-
15-
impl<'i> Rule<'i> {
16-
#[inline]
17-
pub(crate) fn new(selectors: &str, declarations: Vec<Declaration<'i>>) -> Result<Rule<'i>, ()> {
18-
Ok(Rule {
19-
selectors: Selectors::compile(selectors)?,
20-
declarations,
21-
})
22-
}
23-
}
24-
257
fn exhaust<'i>(input: &mut cssparser::Parser<'i, '_>) -> &'i str {
268
let start = input.position();
279
while input.next().is_ok() {}

0 commit comments

Comments
 (0)