Skip to content
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

feat(css/parser): parse highlight #6109

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/swc_css_ast/src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,22 @@ pub enum PseudoElementSelectorChildren {
Ident(Ident),
#[tag("CompoundSelector")]
CompoundSelector(CompoundSelector),
#[tag("CustomHighlightName")]
CustomHighlightName(CustomHighlightName),
}

#[ast_node("CustomHighlightName")]
#[derive(Eq, Hash)]
pub struct CustomHighlightName {
pub span: Span,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub value: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub raw: Option<JsWord>,
}

impl EqIgnoreSpan for CustomHighlightName {
fn eq_ignore_span(&self, other: &Self) -> bool {
self.value == other.value
}
}
16 changes: 16 additions & 0 deletions crates/swc_css_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,21 @@ where
}
}

#[emitter]
fn emit_custom_highlight_name(&mut self, n: &CustomHighlightName) -> Result {
if self.config.minify {
let serialized = serialize_ident(&n.value, n.raw.as_deref(), true);

write_raw!(self, n.span, &serialized);
} else if let Some(raw) = &n.raw {
write_raw!(self, n.span, raw);
} else {
let serialized = serialize_ident(&n.value, n.raw.as_deref(), true);

write_raw!(self, n.span, &serialized);
}
}

#[emitter]
fn emit_custom_ident(&mut self, n: &CustomIdent) -> Result {
if self.config.minify {
Expand Down Expand Up @@ -2350,6 +2365,7 @@ where
PseudoElementSelectorChildren::PreservedToken(n) => emit!(self, n),
PseudoElementSelectorChildren::Ident(n) => emit!(self, n),
PseudoElementSelectorChildren::CompoundSelector(n) => emit!(self, n),
PseudoElementSelectorChildren::CustomHighlightName(n) => emit!(self, n),
}
}

Expand Down
35 changes: 35 additions & 0 deletions crates/swc_css_parser/src/parser/selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,17 @@ where

self.input.skip_ws();
}
"highlight" => {
self.input.skip_ws();

let custom_highlight_name = self.parse()?;

children.push(PseudoElementSelectorChildren::CustomHighlightName(
custom_highlight_name,
));

self.input.skip_ws();
}
_ => {
return Err(Error::new(span, ErrorKind::Ignore));
}
Expand Down Expand Up @@ -1376,3 +1387,27 @@ where
}
}
}

impl<I> Parse<CustomHighlightName> for Parser<I>
where
I: ParserInput,
{
fn parse(&mut self) -> PResult<CustomHighlightName> {
let span = self.input.cur_span();

if !is!(self, Ident) {
return Err(Error::new(span, ErrorKind::Expected("ident token")));
}

match bump!(self) {
Token::Ident { value, raw } => Ok(CustomHighlightName {
span,
value,
raw: Some(raw),
}),
_ => {
unreachable!()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
::highlight(sample) {
background-color: rgba(0, 0, 255, 0.3);
}

:root::highlight(example-highlight) {
background-color: yellow;
color: blue;
}

div::highlight(bar) {
color: red;
}

div::highlight(foo) {
color: green;
}