Skip to content

Commit

Permalink
Update cssparser to the latest version, relax nesting rules and enabl…
Browse files Browse the repository at this point in the history
…e by default

Closes parcel-bundler#576
  • Loading branch information
devongovett committed Sep 17, 2023
1 parent b645c7f commit a8b7ea0
Show file tree
Hide file tree
Showing 29 changed files with 852 additions and 803 deletions.
24 changes: 15 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ substitute_variables = ["visitor", "into_owned"]

[dependencies]
serde = { version = "1.0.123", features = ["derive"], optional = true }
cssparser = "0.29.1"
cssparser = "0.33.0"
cssparser-color = "0.1.0"
parcel_selectors = { version = "0.26.1", path = "./selectors" }
itertools = "0.10.1"
smallvec = { version = "1.7.0", features = ["union"] }
Expand Down
1 change: 0 additions & 1 deletion c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ pub extern "C" fn lightningcss_stylesheet_parse(
let code = unsafe { std::str::from_utf8_unchecked(slice) };
let warnings = Arc::new(RwLock::new(Vec::new()));
let mut flags = ParserFlags::empty();
flags.set(ParserFlags::NESTING, options.nesting);
flags.set(ParserFlags::CUSTOM_MEDIA, options.custom_media);
let opts = ParserOptions {
filename: if options.filename.is_null() {
Expand Down
12 changes: 7 additions & 5 deletions examples/custom_at_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ use lightningcss::{
properties::custom::{Token, TokenOrValue},
rules::{style::StyleRule, CssRule, CssRuleList, Location},
selector::{Component, Selector},
stylesheet::{ParserFlags, ParserOptions, PrinterOptions, StyleSheet},
stylesheet::{ParserOptions, PrinterOptions, StyleSheet},
targets::Browsers,
traits::{AtRuleParser, ToCss},
values::{color::CssColor, length::LengthValue},
values::{
color::{CssColor, RGBA},
length::LengthValue,
},
vendor_prefix::VendorPrefix,
visit_types,
visitor::{Visit, VisitTypes, Visitor},
Expand All @@ -22,7 +25,6 @@ fn main() {
let source = std::fs::read_to_string(&args[1]).unwrap();
let opts = ParserOptions {
filename: args[1].clone(),
flags: ParserFlags::NESTING,
..Default::default()
};

Expand Down Expand Up @@ -266,8 +268,8 @@ impl<'a, 'i> Visitor<'i, AtRule> for ApplyVisitor<'a, 'i> {
match token {
TokenOrValue::Function(f) if f.name == "theme" => match f.arguments.0.first() {
Some(TokenOrValue::Token(Token::String(s))) => match s.as_ref() {
"blue-500" => *token = TokenOrValue::Color(CssColor::RGBA(RGBA::new(0, 0, 255, 255))),
"red-500" => *token = TokenOrValue::Color(CssColor::RGBA(RGBA::new(255, 0, 0, 255))),
"blue-500" => *token = TokenOrValue::Color(CssColor::RGBA(RGBA::new(0, 0, 255, 1.0))),
"red-500" => *token = TokenOrValue::Color(CssColor::RGBA(RGBA::new(255, 0, 0, 1.0))),
_ => {}
},
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ crate-type = ["cdylib"]
[dependencies]
serde = { version = "1.0.123", features = ["derive"] }
serde_bytes = "0.11.5"
cssparser = "0.29.1"
cssparser = "0.33.0"
lightningcss = { path = "../", features = ["nodejs", "serde", "visitor"] }
parcel_sourcemap = { version = "2.1.1", features = ["json"] }
serde-detach = "0.0.1"
Expand Down
4 changes: 0 additions & 4 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,6 @@ impl<'a> Into<PseudoClasses<'a>> for &'a OwnedPseudoClasses {
#[derive(Serialize, Debug, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
struct Drafts {
#[serde(default)]
nesting: bool,
#[serde(default)]
custom_media: bool,
}
Expand Down Expand Up @@ -740,7 +738,6 @@ fn compile<'i>(

let res = {
let mut flags = ParserFlags::empty();
flags.set(ParserFlags::NESTING, matches!(drafts, Some(d) if d.nesting));
flags.set(ParserFlags::CUSTOM_MEDIA, matches!(drafts, Some(d) if d.custom_media));
flags.set(
ParserFlags::DEEP_SELECTOR_COMBINATOR,
Expand Down Expand Up @@ -867,7 +864,6 @@ fn compile_bundle<
let drafts = config.drafts.as_ref();
let non_standard = config.non_standard.as_ref();
let mut flags = ParserFlags::empty();
flags.set(ParserFlags::NESTING, matches!(drafts, Some(d) if d.nesting));
flags.set(ParserFlags::CUSTOM_MEDIA, matches!(drafts, Some(d) if d.custom_media));
flags.set(
ParserFlags::DEEP_SELECTOR_COMBINATOR,
Expand Down
8 changes: 0 additions & 8 deletions node/test/customAtRules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ test('mixin', () => {
@apply color;
}
`),
drafts: { nesting: true },
targets: { chrome: 100 << 16 },
customAtRules: {
mixin: {
Expand Down Expand Up @@ -171,9 +170,6 @@ test('style block', () => {
}
}
`),
drafts: {
nesting: true
},
targets: {
chrome: 105 << 16
},
Expand Down Expand Up @@ -217,9 +213,6 @@ test('style block top level', () => {
}
}
`),
drafts: {
nesting: true
},
customAtRules: {
test: {
body: 'style-block'
Expand Down Expand Up @@ -289,7 +282,6 @@ test('bundler', () => {
let res = bundle({
filename: 'tests/testdata/apply.css',
minify: true,
drafts: { nesting: true },
targets: { chrome: 100 << 16 },
customAtRules: {
mixin: {
Expand Down
3 changes: 0 additions & 3 deletions node/test/transform.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ test('can enable features without targets', () => {
filename: 'test.css',
code: Buffer.from('.foo { .bar { color: red }}'),
minify: true,
drafts: {
nesting: true
},
include: Features.Nesting
});

Expand Down
3 changes: 0 additions & 3 deletions node/test/visitor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@ test('apply', () => {
let res = transform({
filename: 'test.css',
minify: true,
drafts: {
nesting: true
},
code: Buffer.from(`
--toolbar-theme {
color: white;
Expand Down
2 changes: 1 addition & 1 deletion selectors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jsonschema = ["serde", "schemars"]

[dependencies]
bitflags = "2.2.1"
cssparser = "0.29"
cssparser = "0.33.0"
fxhash = "0.2"
log = "0.4"
phf = "0.10"
Expand Down
12 changes: 0 additions & 12 deletions selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1991,18 +1991,6 @@ where
input.reset(&state);
}

// In the implicit nesting mode, selectors may not start with an ident or function token.
if nesting_requirement == NestingRequirement::Implicit {
let state = input.state();
match input.next()? {
Token::Ident(..) | Token::Function(..) => {
return Err(input.new_custom_error(SelectorParseErrorKind::MissingNestingPrefix));
}
_ => {}
}
input.reset(&state);
}

let mut builder = SelectorBuilder::default();

'outer_loop: loop {
Expand Down
30 changes: 22 additions & 8 deletions src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ impl<'i> DeclarationBlock<'i> {
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let mut important_declarations = DeclarationList::new();
let mut declarations = DeclarationList::new();
let mut parser = DeclarationListParser::new(
input,
PropertyDeclarationParser {
important_declarations: &mut important_declarations,
declarations: &mut declarations,
options,
},
);
let mut decl_parser = PropertyDeclarationParser {
important_declarations: &mut important_declarations,
declarations: &mut declarations,
options,
};
let mut parser = RuleBodyParser::new(input, &mut decl_parser);
while let Some(res) = parser.next() {
if let Err((err, _)) = res {
if options.error_recovery {
Expand Down Expand Up @@ -435,6 +433,22 @@ impl<'a, 'o, 'i> AtRuleParser<'i> for PropertyDeclarationParser<'a, 'o, 'i> {
type Error = ParserError<'i>;
}

impl<'a, 'o, 'i> QualifiedRuleParser<'i> for PropertyDeclarationParser<'a, 'o, 'i> {
type Prelude = ();
type QualifiedRule = ();
type Error = ParserError<'i>;
}

impl<'a, 'o, 'i> RuleBodyItemParser<'i, (), ParserError<'i>> for PropertyDeclarationParser<'a, 'o, 'i> {
fn parse_qualified(&self) -> bool {
false
}

fn parse_declarations(&self) -> bool {
true
}
}

pub(crate) fn parse_declaration<'i, 't>(
name: CowRcStr<'i>,
input: &mut cssparser::Parser<'i, 't>,
Expand Down
Loading

0 comments on commit a8b7ea0

Please sign in to comment.