Skip to main content

css_parse/
cursor_compact_write_sink.rs

1use crate::{Cursor, CursorSink, Kind, KindSet, QuoteStyle, SourceCursor, SourceCursorSink, Token};
2
3/// This is a [CursorSink] that wraps a sink (`impl SourceCursorSink`) and on each [CursorSink::append()] call, will write
4/// the contents of the cursor [Cursor] given into the given sink - using the given `&'a str` as the original source.
5/// Some tokens will not be output, and Whitespace tokens will always write out as a single `' '`. It can be used as a
6/// light-weight minifier for ToCursors structs.
7pub struct CursorCompactWriteSink<'a, T: SourceCursorSink<'a>> {
8	source_text: &'a str,
9	sink: T,
10	last_token: Option<Token>,
11	pending: Option<SourceCursor<'a>>,
12	pending_comment: Option<SourceCursor<'a>>,
13}
14
15// Tokens that get buffered as `pending` rather than emitted immediately.
16const PENDING_KINDSET: KindSet = KindSet::new(&[Kind::Semicolon, Kind::Whitespace]);
17// `;` is redundant immediately before/after these.
18const REDUNDANT_SEMI_KINDSET: KindSet = KindSet::new(&[Kind::Semicolon, Kind::RightCurly]);
19// Whitespace immediately before these tokens can always be removed (overrides any
20// `AssociatedWhitespaceRules::EnforceBefore` annotations carried from the source).
21const NO_WHITESPACE_BEFORE_KINDSET: KindSet =
22	KindSet::new(&[Kind::Whitespace, Kind::Colon, Kind::Delim, Kind::LeftCurly, Kind::RightCurly, Kind::Eof]);
23// Whitespace immediately after these tokens can always be removed.
24const NO_WHITESPACE_AFTER_KINDSET: KindSet =
25	KindSet::new(&[Kind::Comma, Kind::RightParen, Kind::RightCurly, Kind::LeftCurly, Kind::Colon]);
26
27impl<'a, T: SourceCursorSink<'a>> CursorCompactWriteSink<'a, T> {
28	pub fn new(source_text: &'a str, sink: T) -> Self {
29		Self { source_text, sink, last_token: None, pending: None, pending_comment: None }
30	}
31
32	/// Would emitting `next` immediately after `last_token` change tokenisation?
33	fn needs_separator(&self, next: Token) -> bool {
34		self.last_token.is_some_and(|t| t.needs_separator_for(next))
35	}
36
37	/// True when the previously-emitted token never needs a separator after it
38	/// (e.g. `,`, `)`, `{`). Whitespace can be dropped and re-injection skipped.
39	fn last_forbids_ws_after(&self) -> bool {
40		self.last_token.is_some_and(|t| t == NO_WHITESPACE_AFTER_KINDSET)
41	}
42
43	fn emit(&mut self, c: SourceCursor<'a>) {
44		self.last_token = Some(c.token());
45		self.sink.append(c);
46	}
47
48	fn write(&mut self, c: SourceCursor<'a>) {
49		if c == Kind::Comment {
50			let can_separate =
51				self.pending.is_none() && self.pending_comment.is_none() && !self.last_forbids_ws_after();
52			if can_separate {
53				self.pending_comment = Some(c);
54			}
55			return;
56		}
57		if self.pending_comment.take().is_some()
58			&& c != Kind::Whitespace
59			&& c != Kind::Eof
60			&& self.needs_separator(c.token())
61		{
62			self.emit(SourceCursor::EMPTY_COMMENT);
63		}
64
65		if c == Kind::Whitespace && self.pending.is_some_and(|c| c == Kind::Semicolon) {
66			return;
67		}
68
69		let suppress_separator = self.last_forbids_ws_after();
70		if let Some(prev) = self.pending.take() {
71			let keep = match prev.token().kind() {
72				Kind::Semicolon => {
73					c != REDUNDANT_SEMI_KINDSET && self.last_token.is_some_and(|t| t != REDUNDANT_SEMI_KINDSET)
74				}
75				_ => !suppress_separator && c != NO_WHITESPACE_BEFORE_KINDSET && self.needs_separator(c.token()),
76			};
77			if keep {
78				self.emit(prev.compact());
79			}
80		}
81
82		if c == PENDING_KINDSET {
83			self.pending = Some(c);
84			return;
85		}
86		if c == Kind::Eof {
87			return;
88		}
89
90		if !suppress_separator && self.needs_separator(c.token()) {
91			self.sink.append(SourceCursor::SPACE);
92		}
93
94		let out = if c == Kind::String { c.with_quotes(QuoteStyle::Double).compact() } else { c.compact() };
95		self.emit(out);
96	}
97}
98
99impl<'a, T: SourceCursorSink<'a>> Drop for CursorCompactWriteSink<'a, T> {
100	fn drop(&mut self) {
101		if let Some(prev) = self.pending.take()
102			&& prev == Kind::Semicolon
103		{
104			self.emit(prev);
105		}
106	}
107}
108
109impl<'a, T: SourceCursorSink<'a>> CursorSink for CursorCompactWriteSink<'a, T> {
110	fn append(&mut self, c: Cursor) {
111		self.write(SourceCursor::from(c, c.str_slice(self.source_text)))
112	}
113}
114
115impl<'a, T: SourceCursorSink<'a>> SourceCursorSink<'a> for CursorCompactWriteSink<'a, T> {
116	fn append(&mut self, c: SourceCursor<'a>) {
117		self.write(c)
118	}
119}
120
121#[cfg(test)]
122mod test {
123	use super::*;
124	use crate::{ComponentValues, EmptyAtomSet, Parser, ToCursors};
125	use bumpalo::Bump;
126	use css_lexer::Lexer;
127
128	macro_rules! assert_format {
129		($before: literal, $after: literal) => {
130			assert_format!(ComponentValues, $before, $after);
131		};
132		($struct: ident, $before: literal, $after: literal) => {
133			let source_text = $before;
134			let bump = Bump::default();
135			let mut sink = String::new();
136			{
137				let mut stream = CursorCompactWriteSink::new(source_text, &mut sink);
138				let lexer = Lexer::new(&EmptyAtomSet::ATOMS, source_text);
139				let mut parser = Parser::new(&bump, source_text, lexer);
140				parser.parse_entirely::<$struct>().with_trivia().to_cursors(&mut stream);
141			}
142			assert_eq!(sink, $after.trim());
143		};
144	}
145
146	#[test]
147	fn test_basic() {
148		assert_format!("foo{bar: baz();}", r#"foo{bar:baz()}"#);
149	}
150
151	#[test]
152	fn test_removes_redundant_semis() {
153		assert_format!("foo{bar: 1;;;;bing: 2;;;}", r#"foo{bar:1;bing:2}"#);
154	}
155
156	#[test]
157	fn normalizes_quotes() {
158		assert_format!("bar:'baz';bing:'quux';x:url('foo')", r#"bar:"baz";bing:"quux";x:url("foo")"#);
159	}
160
161	#[test]
162	fn test_does_not_ignore_whitespace_component_values() {
163		assert_format!("div dialog:modal > td p a", "div dialog:modal > td p a");
164	}
165
166	#[test]
167	fn test_compacts_whitespace() {
168		assert_format!(
169			r#"
170		body   >   div {
171			bar:  baz
172		}
173		"#,
174			"body > div{bar:baz}"
175		);
176	}
177
178	#[test]
179	fn test_does_not_compact_whitespace_resulting_in_new_ident() {
180		assert_format!("12px - 1px", "12px - 1px");
181	}
182
183	#[test]
184	fn test_removes_whitespace_after_comma() {
185		assert_format!("foo(a, b, c)", "foo(a,b,c)");
186		assert_format!("rgb(255, 128, 0)", "rgb(255,128,0)");
187	}
188
189	#[test]
190	fn test_removes_whitespace_after_right_paren() {
191		assert_format!("foo() bar", "foo()bar");
192		assert_format!("rgb(0, 0, 0) solid", "rgb(0,0,0)solid");
193	}
194
195	#[test]
196	fn test_removes_whitespace_after_right_curly() {
197		assert_format!("@media screen{} .foo{}", "@media screen{}.foo{}");
198	}
199
200	#[test]
201	fn test_compacts_numbers_with_leading_zero() {
202		assert_format!("opacity: 0.8", "opacity:.8");
203		assert_format!("opacity: 0.5", "opacity:.5");
204		assert_format!("opacity: 0.123", "opacity:.123");
205	}
206
207	#[test]
208	fn test_compacts_numbers_with_trailing_zeros() {
209		assert_format!("width: 1.0px", "width:1px");
210		assert_format!("width: 1.500px", "width:1.5px");
211		assert_format!("width: 2.000px", "width:2px");
212	}
213
214	#[test]
215	fn test_compacts_numbers_with_sign() {
216		assert_format!("margin: -0.5px", "margin:-.5px");
217		assert_format!("margin: +1.5px", "margin:1.5px");
218		assert_format!("margin: +0.8px", "margin:.8px");
219	}
220
221	#[test]
222	fn test_compacts_edge_case_numbers() {
223		assert_format!("opacity: 0.0", "opacity:0");
224		assert_format!("opacity: 0", "opacity:0");
225		assert_format!("opacity: 1", "opacity:1");
226	}
227
228	#[test]
229	fn test_does_not_change_numbers_without_optimization() {
230		assert_format!("width: 123px", "width:123px");
231		assert_format!("width: .5px", "width:.5px");
232	}
233
234	#[test]
235	fn test_preserves_trailing_semicolons() {
236		assert_format!("foo;", "foo;");
237	}
238
239	#[test]
240	fn test_removes_trailing_semis_when_after_curly() {
241		assert_format!("{foo};", "{foo}");
242	}
243
244	#[test]
245	fn test_drops_trailing_whitespace() {
246		assert_format!("foo  ", "foo");
247		assert_format!("foo; ", "foo;");
248	}
249
250	#[test]
251	fn test_preserves_comment_absence_in_custom_properties() {
252		assert_format!("div{--bar:a/**/b}", "div{--bar:a/**/b}");
253		assert_format!("div { --bar: a/**/b }", "div{--bar:a/**/b}");
254		assert_format!("div{--bar:a /* comment */ b}", "div{--bar:a b}");
255		assert_format!("div{--bar:a/**//**/b}", "div{--bar:a/**/b}");
256		assert_format!("div{--bar:a /* x */ /* y */ b}", "div{--bar:a b}");
257		assert_format!("div{/*comment*/--bar:a}", "div{--bar:a}");
258		assert_format!("div{--bar:a/*comment*/}", "div{--bar:a}");
259		assert_format!("div{--bar:a  /**/  b}", "div{--bar:a b}");
260		assert_format!("@container style(--bar:a/**/b){}", "@container style(--bar:a/**/b){}");
261		assert_format!("@container style(--bar:a/**/b){}", "@container style(--bar:a/**/b){}");
262		assert_format!("foo /**/bar", "foo bar");
263		assert_format!("foo{/**/bar}", "foo{bar}");
264		assert_format!("foo:/**/bar", "foo:bar");
265		assert_format!("foo(/**/bar)", "foo(bar)");
266		assert_format!("foo/**/,bar", "foo,bar");
267		assert_format!("/**/foo", "foo");
268		assert_format!("div{--bar:a/*some really long comment text*/b}", "div{--bar:a/**/b}");
269	}
270
271	#[test]
272	fn test_at_rule_no_space_before_paren() {
273		assert_format!(
274			"@media(prefers-reduced-motion:no-preference){:root{}}",
275			"@media(prefers-reduced-motion:no-preference){:root{}}"
276		);
277		assert_format!(
278			"@media (prefers-reduced-motion:no-preference){:root{}}",
279			"@media(prefers-reduced-motion:no-preference){:root{}}"
280		);
281		assert_format!("@media(min-width:576px){}", "@media(min-width:576px){}");
282	}
283}