From 9b6623190dbff5fa89425ab50a4ebc4faa656a3d Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Wed, 28 Dec 2022 17:09:55 -0500 Subject: [PATCH] allow escaped non-alphanumeric bytes in media queries --- src/ast/stmt.rs | 1 + src/evaluate/visitor.rs | 10 +++++++--- src/parse/base.rs | 1 - src/parse/stylesheet.rs | 3 +++ tests/media.rs | 9 +++++++++ 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/ast/stmt.rs b/src/ast/stmt.rs index 4953fc7..950bd97 100644 --- a/src/ast/stmt.rs +++ b/src/ast/stmt.rs @@ -96,6 +96,7 @@ pub(crate) struct AstEach { #[derive(Debug, Clone)] pub(crate) struct AstMedia { pub query: Interpolation, + pub query_span: Span, pub body: Vec, pub span: Span, } diff --git a/src/evaluate/visitor.rs b/src/evaluate/visitor.rs index 7c6de04..6f1bec6 100644 --- a/src/evaluate/visitor.rs +++ b/src/evaluate/visitor.rs @@ -1222,10 +1222,14 @@ impl<'a> Visitor<'a> { Some(queries) } - fn visit_media_queries(&mut self, queries: Interpolation) -> SassResult> { + fn visit_media_queries( + &mut self, + queries: Interpolation, + span: Span, + ) -> SassResult> { let resolved = self.perform_interpolation(queries, true)?; - CssMediaQuery::parse_list(&resolved, self.span_before) + CssMediaQuery::parse_list(&resolved, span) } fn visit_media_rule(&mut self, media_rule: AstMedia) -> SassResult> { @@ -1237,7 +1241,7 @@ impl<'a> Visitor<'a> { .into()); } - let queries1 = self.visit_media_queries(media_rule.query)?; + let queries1 = self.visit_media_queries(media_rule.query, media_rule.query_span)?; // todo: superfluous clone? let queries2 = self.media_queries.clone(); let merged_queries = queries2 diff --git a/src/parse/base.rs b/src/parse/base.rs index 9383316..dc37a3f 100644 --- a/src/parse/base.rs +++ b/src/parse/base.rs @@ -387,7 +387,6 @@ pub(crate) trait BaseParser<'a> { while let Some(tok) = self.toks().peek() { match tok.kind { '\\' => { - self.toks_mut().next(); buffer.push_str(&self.parse_escape(true)?); wrote_newline = false; } diff --git a/src/parse/stylesheet.rs b/src/parse/stylesheet.rs index a96922c..391e5b9 100644 --- a/src/parse/stylesheet.rs +++ b/src/parse/stylesheet.rs @@ -981,12 +981,15 @@ pub(crate) trait StylesheetParser<'a>: BaseParser<'a> + Sized { } fn parse_media_rule(&mut self, start: usize) -> SassResult { + let query_start = self.toks().cursor(); let query = self.parse_media_query_list()?; + let query_span = self.toks_mut().span_from(query_start); let body = self.with_children(Self::parse_statement)?.node; Ok(AstStmt::Media(AstMedia { query, + query_span, body, span: self.toks_mut().span_from(start), })) diff --git a/tests/media.rs b/tests/media.rs index efd1b6b..e02f392 100644 --- a/tests/media.rs +++ b/tests/media.rs @@ -561,6 +561,15 @@ test!( }", "@media (url) {\n a {\n color: red;\n }\n}\n" ); +test!( + escaped_nullbyte_in_query, + r#"@media (min-width:\0) { + a { + color: red; + } + }"#, + "@media (min-width: \\0 ) {\n a {\n color: red;\n }\n}\n" +); error!( media_query_has_quoted_closing_paren, r#"@media ('a)'w) {