Skip to content

v0.31.0 RuleListParser::new_for_stylesheet not returning all classes #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
j-mendez opened this issue Oct 5, 2023 · 1 comment
Closed

Comments

@j-mendez
Copy link

j-mendez commented Oct 5, 2023

Hi, thank you for the crate! It looks like v0.31.0 using RuleListParser::new_for_stylesheet all of the classes set are not returned.
I have a large stylesheet that parses correctly in v0.25.0 but, the same logic on v0.31.0 only 3 classes out of like 1000 get applied.

Trying to figure out how to debug the issue:

attached is the SelectorList:

use crate::engine::styles::errors::RuleParseErrorKind;
use cssparser::{AtRuleParser, ParseError, QualifiedRuleParser};
use scraper::selector::Simple;
use selectors::parser::ParseRelative;
use std::sync::Arc;

#[derive(Debug)]
pub enum CssRule {
    StyleRule {
        selectors: selectors::SelectorList<Simple>,
        block: Arc<String>,
    },
}

pub struct Parser;

impl<'i> selectors::parser::Parser<'i> for Parser {
    type Impl = Simple;
    type Error = RuleParseErrorKind<'i>;
}

pub type SelectorList = selectors::SelectorList<Simple>;

pub struct RulesParser;

impl<'i> QualifiedRuleParser<'i> for RulesParser {
    type Prelude = SelectorList;
    type QualifiedRule = CssRule;
    type Error = RuleParseErrorKind<'i>;

    fn parse_prelude<'t>(
        &mut self,
        parser: &mut cssparser::Parser<'i, 't>,
    ) -> Result<Self::Prelude, ParseError<'i, Self::Error>> {
        SelectorList::parse(&Parser, parser, ParseRelative::No)
    }

    fn parse_block<'t>(
        &mut self,
        prelude: Self::Prelude,
        _location: &cssparser::ParserState,
        _parser: &mut cssparser::Parser<'i, 't>,
    ) -> Result<Self::QualifiedRule, ParseError<'i, Self::Error>> {
        Ok(CssRule::StyleRule {
            selectors: prelude,
            block: Arc::new(String::new()),
        })
    }
}

impl<'i> AtRuleParser<'i> for RulesParser {
    type Prelude = ();
    type AtRule = CssRule;
    type Error = RuleParseErrorKind<'i>;
}

main:

    let mut _css_parser = cssparser::Parser::new(css_rules);
    let css_rules_parser = cssparser::RuleListParser::new_for_stylesheet(
        &mut _css_parser,
        crate::engine::styles::rules::RulesParser,
    );

    let mut rules = Vec::new();

    for result in css_rules_parser {
        match result {
            Ok(crate::engine::styles::rules::CssRule::StyleRule { selectors, block }) => {
                for selector in selectors.0 {
                    rules.push((selector, block.clone()));
                }
            }
            _ => (),
        };
    }

    println!("CSS RULES: {:?}", rules);

I have an example repo of the issue in this project across two branches kayle. The branch with the correct class list is called victor-stub ( thank you @SimonSapin ).

wasm-pack build --target nodejs to build the wasm crate and in the root folder run yarn test:puppeteer:innate for the test.

@j-mendez
Copy link
Author

j-mendez commented Oct 6, 2023

Was able to get past the issue by splitting the crate and working off the victor-stub, I think an internal dep was causing the issue.

@j-mendez j-mendez closed this as completed Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant