Wiki source code of Step 2: Evaluation of the CSS minifiers
Last modified by Lucas Charpentier on 2026/10/30 11:03
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | Step 2 of [[Replacement for the yuicompressor maven plugin>>Proposal.Replacementfortheyuicompressormavenplugin]], run on 2026-10-30 against xwiki-platform master. The harness, the fixtures and the raw results are attached to this page. | ||
| 2 | |||
| 3 | {{toc/}} | ||
| 4 | |||
| 5 | == Summary == | ||
| 6 | |||
| 7 | * **YUI Compressor breaks a lot of current CSS**, including two bugs that are live in XWiki Standard today, see [[Live bugs in XWiki Standard>>||anchor="HLivebugsinXWikiStandard"]]. | ||
| 8 | * **No candidate can replace YUI on both paths.** Half of the build-time CSS files are Velocity templates. The real CSS parsers, css4j and ph-css, either refuse them or break them silently: css4j turns ##{{{$theme.textPrimaryColor}}}## into ##{{{\$theme\.textPrimaryColor}}}##, which Velocity then outputs as it is. | ||
| 9 | * **Runtime, after Velocity: css4j is the most correct and the fastest.** The only 2 defects found are serialization bugs, easy to report upstream. | ||
| 10 | * **Build time: stripping the comments and collapsing the white space never broke anything** in the corpus. It saves 24% gzipped, against 28% for YUI, about 2 KB in total. | ||
| 11 | * Direction to discuss in step 3: css4j at runtime, keeping the source unminified on a parse error; a comment and white space stripper at build time. Later, optionally: minify the Velocity-rendered skin files at runtime too, so that css4j becomes the only engine. | ||
| 12 | |||
| 13 | == Method == | ||
| 14 | |||
| 15 | === Corpus === | ||
| 16 | |||
| 17 | 175 style sheets: | ||
| 18 | |||
| 19 | * the 64 build-time CSS files listed in the [[inventory>>Proposal.Replacementfortheyuicompressormavenplugin.Inventory.WebHome]], 33 of them Velocity templates; | ||
| 20 | * the 76 non-empty style sheet extension objects of type CSS found in the XARs, 44 of them parsed with Velocity; | ||
| 21 | * 35 fixtures: the cases of XWIKI-21246, XWIKI-22206 and yui/yuicompressor#330, plus modern CSS: nesting, relative colors, ##@supports##, ##@container##, ##@layer##, ##@scope##, ##@property##, ##attr()## with ##type()##, ##if()##, custom properties, math functions. | ||
| 22 | |||
| 23 | The LESS objects are left out: less4j already compresses its output before YUI runs. | ||
| 24 | |||
| 25 | === Scenarios === | ||
| 26 | |||
| 27 | * **Runtime**: Velocity renders the CSS, then the engine minifies it. This is what happens to skin extensions. | ||
| 28 | * **Build time**: the engine minifies the raw template, then Velocity renders it. This is what happens to the skin files. | ||
| 29 | |||
| 30 | Velocity runs with stubs: ##$theme## returns one color for every key, ##$xwiki.getSkinFile## returns a URL, ##~#template## outputs nothing. Other references and macros stay unrendered, the same way in the reference and in the result. | ||
| 31 | |||
| 32 | === Oracle === | ||
| 33 | |||
| 34 | Chromium 149 parses the reference, which is the unminified rendering, and the result, with ##CSSStyleSheet.replaceSync##. Rules are matched by selector or at-rule header, then compared declaration by declaration. A different value counts as **equivalent** when both give the same computed value on a probe element, for example ##bold## and ##700##. Anything else, or a lost rule or declaration, counts as **changed**. | ||
| 35 | |||
| 36 | The changed sheets were then reviewed by hand. A sheet counts as **invisible only** when all its changes have no visible effect, for example ##{{{outline: none}}}## turned into ##{{{outline:0}}}##: the computed outline width differs, but no outline is drawn either way. | ||
| 37 | |||
| 38 | The control engine, ##strip##, gives no difference at all on the 175 sheets, which shows that the oracle does not report white space or comments as changes. | ||
| 39 | |||
| 40 | === Engines === | ||
| 41 | |||
| 42 | |=Engine|=Version tested|=Latest release|=License|=Notes | ||
| 43 | |YUI Compressor|2.4.8|2.4.8, July 2013|BSD|Baseline. Embeds Rhino 1.7R2. | ||
| 44 | |css4j|6.2|6.2, December 2025|BSD 3-clause|Real CSS parser. Already a platform dependency. Published on its own Maven repository, not on Maven Central. | ||
| 45 | |ph-css|8.2.3|8.2.3, October 2026|Apache 2|Real CSS parser. Pulls about 8 ph-commons JARs. The error handler API used here is deprecated for removal. | ||
| 46 | |logicsquad minifier|2.0|2.0, October 2026|BSD 2-clause|Token-based, port of CSSMin. No dependency. Maven plugin 1.5. | ||
| 47 | |Lightning CSS|1.33|1.33.0, July 2026|MPL 2.0|Rust, Node binding. Runtime scenario only, as a size reference. | ||
| 48 | |strip|n/a|n/a|n/a|Control written for this evaluation, about 60 lines: removes the comments except ##{{{/*!}}}## ones and collapses the white space, keeping new lines and strings. | ||
| 49 | |||
| 50 | Every engine was tested in its latest release. The ##yuicompressor-maven-plugin## we use to run YUI at build time had its last release, 1.5.1, in November 2014, and its GitHub repository is archived. The repositories of css4j, ph-css, logicsquad minifier and Lightning CSS all received commits in October 2026. | ||
| 51 | |||
| 52 | css4j and ph-css run in two modes: **safe** keeps the source unminified when the parser reports an error, **lenient** ignores the errors. | ||
| 53 | |||
| 54 | == Results == | ||
| 55 | |||
| 56 | === Correctness, runtime scenario === | ||
| 57 | |||
| 58 | |=Engine|=Unchanged|=Equivalent|=Invisible only|=Changed|=Kept unminified | ||
| 59 | |YUI|135|18|6|16|0 | ||
| 60 | |logicsquad|133|37|1|4|0 | ||
| 61 | |css4j safe|163|9|1|2|9 | ||
| 62 | |css4j lenient|159|9|1|6|0 | ||
| 63 | |ph-css safe|166|5|1|3|19 | ||
| 64 | |ph-css lenient|160|5|1|9|9 | ||
| 65 | |strip|175|0|0|0|0 | ||
| 66 | |||
| 67 | The sheets kept unminified are also counted as unchanged. | ||
| 68 | |||
| 69 | Lightning CSS merges and rewrites rules, for example media queries into range syntax, which this oracle reports as lost rules. Its 52 changed sheets are mostly such rewrites and were not reviewed one by one. | ||
| 70 | |||
| 71 | Real defects behind the changed sheets: | ||
| 72 | |||
| 73 | * **YUI**: | ||
| 74 | ** declarations dropped because spaces around ##+## and ##-## are removed in ##calc()##, also inside ##min()##, ##max()## and ##clamp()##, or inside relative colors: XWIKI-21246; | ||
| 75 | ** spaces removed inside an SVG data URL: XWIKI-22206; | ||
| 76 | ** ##0s## turned into ##0##, which drops the whole ##transition##; | ||
| 77 | ** keyframe selector ##0%## turned into ##0##, which drops the keyframe; | ||
| 78 | ** ##{{{flex: 1 1 0%}}}## turned into ##{{{1 1 0px}}}##, which is not the same flex basis; | ||
| 79 | ** ##{{{@container card (width > 400px)}}}##, ##{{{@supports (not (...))}}}## and ##{{{@scope (.a) to (.b)}}}## lose spaces and stop matching; | ||
| 80 | ** ##{{{@property}}}## with ##{{{initial-value: 0deg}}}## turned into ##0##, which drops the rule; | ||
| 81 | ** ##{{{font-size: 1.0em}}}## turned into ##{{{font-size: 1.0}}}##, and ##{{{& :hover}}}## into ##{{{&:hover}}}##, see below. | ||
| 82 | * **logicsquad**: an empty custom property, ##{{{--empty: ;}}}##, swallows the next declaration; same ##{{{flex: 1 1 0%}}}## bug as YUI. Its 2 other changed sheets come from Velocity macros the stub does not render. | ||
| 83 | * **css4j**: keyframe selector ##0%## serialized as ##0##; ##{{{@supports selector(:has(a))}}}## serialized without its space. | ||
| 84 | * **ph-css**: ##0s## turned into ##0##; ##{{{flex: 1 1 0%}}}## bug; ##{{{@property}}}## ##{{{initial-value: 0deg}}}## turned into ##0##. It also refuses many modern constructs, ##if()##, ##attr()## with ##type()##, ##clamp()##, and keeps those sheets unminified. | ||
| 85 | |||
| 86 | The invisible changes are YUI rewriting ##{{{outline: none}}}## and ##{{{background: none}}}##, as described in the build-time scenario below, and the other engines shortening a color inside a custom property, for example ##white## into ##~#fff##. | ||
| 87 | |||
| 88 | In lenient mode, css4j and ph-css silently drop what they do not understand. ph-css drops the declarations using ##clamp()##, ##if()## or ##attr()## with ##type()##. css4j outputs nothing at all for a sheet starting with an ##@import## that uses ##layer()## and ##supports()##. | ||
| 89 | |||
| 90 | === Correctness, build-time scenario === | ||
| 91 | |||
| 92 | |=Engine|=Unchanged|=Equivalent|=Invisible only|=Changed|=Template broken|=Kept unminified | ||
| 93 | |YUI|47|12|5|0|0|0 | ||
| 94 | |logicsquad|47|14|0|0|3|0 | ||
| 95 | |css4j safe|60|3|0|1|0|31 | ||
| 96 | |css4j lenient|30|3|0|31|0|0 | ||
| 97 | |ph-css safe|61|3|0|0|0|33 | ||
| 98 | |ph-css lenient|60|3|0|1|0|32 | ||
| 99 | |strip|64|0|0|0|0|0 | ||
| 100 | |||
| 101 | * **YUI** changes no file visibly. Its 5 invisible changes: | ||
| 102 | ** ##{{{outline: none}}}## becomes ##{{{outline:0}}}##. The outline width goes from ##medium## to ##0##, but the outline style stays ##none##, so no outline is drawn. | ||
| 103 | ** ##{{{background: none}}}## becomes ##{{{background:0}}}##, which sets the background position to ##{{{0 center}}}## instead of ##{{{0% 0%}}}##. There is no background image to position, so nothing changes. | ||
| 104 | * **logicsquad** breaks 3 templates. For example it drops the ##]## closing a list in the arguments of the ##~#css3_backgroundLinearGradient## macro in ##dashboard.css##, so Velocity cannot parse the file any more. | ||
| 105 | * **css4j and ph-css in safe mode** refuse about half of the files, the Velocity templates, which then ship unminified. When css4j accepts a template, it can still break it: in ##print.css## it escapes ##{{{$theme.textPrimaryColor}}}## into ##{{{\$theme\.textPrimaryColor}}}##, so the color is lost. | ||
| 106 | * **In lenient mode** they drop or escape the Velocity code: css4j changes 31 of the 64 files. | ||
| 107 | |||
| 108 | === Size === | ||
| 109 | |||
| 110 | Build time, the 64 raw templates: | ||
| 111 | |||
| 112 | |=Engine|=Raw|=Saved|=Gzipped|=Saved | ||
| 113 | |none|136.5 KB| |47.7 KB| | ||
| 114 | |YUI|98.1 KB|28%|34.4 KB|28% | ||
| 115 | |logicsquad|97.9 KB|28%|34.4 KB|28% | ||
| 116 | |css4j safe|119.3 KB|13%|40.9 KB|14% | ||
| 117 | |ph-css safe|122.2 KB|10%|42.0 KB|12% | ||
| 118 | |strip|108.0 KB|21%|36.3 KB|24% | ||
| 119 | |||
| 120 | Runtime, the 76 skin extension objects after Velocity: | ||
| 121 | |||
| 122 | |=Engine|=Raw|=Saved|=Gzipped|=Saved | ||
| 123 | |none|80.3 KB| |31.3 KB| | ||
| 124 | |YUI|54.8 KB|32%|22.5 KB|28% | ||
| 125 | |logicsquad|54.5 KB|32%|22.5 KB|28% | ||
| 126 | |css4j safe|56.8 KB|29%|23.0 KB|27% | ||
| 127 | |ph-css safe|57.3 KB|29%|23.2 KB|26% | ||
| 128 | |Lightning CSS|53.1 KB|34%|21.9 KB|30% | ||
| 129 | |strip|61.7 KB|23%|24.2 KB|23% | ||
| 130 | |||
| 131 | === Speed === | ||
| 132 | |||
| 133 | Time to minify the 175 rendered sheets, 217 KB, once, after warm-up. The machine was shared, so the figures varied between runs; the ranking did not. | ||
| 134 | |||
| 135 | |=Engine|=Time per pass | ||
| 136 | |strip|5 to 10 ms | ||
| 137 | |css4j|30 to 50 ms | ||
| 138 | |logicsquad|40 to 60 ms | ||
| 139 | |ph-css|110 to 140 ms | ||
| 140 | |YUI|150 to 260 ms | ||
| 141 | |||
| 142 | css4j was 3 to 8 times faster than YUI, depending on the run. | ||
| 143 | |||
| 144 | == Support of recent CSS features == | ||
| 145 | |||
| 146 | Each row is one feature, minified at runtime and checked in Chromium against the unminified source. The rows come from the fixtures, plus the two constructs behind the [[live bugs>>||anchor="HLivebugsinXWikiStandard"]]. css4j and ph-css run in safe mode: **kept unminified** means the engine refused the sheet, so it is correct but not smaller. Lightning CSS is left out because its rewrites could not be judged automatically. | ||
| 147 | |||
| 148 | |=Feature|=Example|=YUI|=logicsquad|=css4j|=ph-css | ||
| 149 | |##calc()## with ##+## or ##-##|##{{{calc(50% + 1px)}}}##, ##{{{calc(50% - (100px / 2))}}}##|**Broken**: XWIKI-21246|OK|OK|OK | ||
| 150 | |Operators in ##min()##, ##max()##, ##clamp()##|##{{{clamp(1rem, 0.5rem + 2vw, 2rem)}}}##|**Broken**|OK|OK|Kept unminified | ||
| 151 | |Zero times|##{{{transition: opacity 0s .2s}}}##|**Broken**|OK|OK|**Broken** | ||
| 152 | |SVG in a data URL|##{{{url("data:image/svg+xml,%3Csvg viewBox='0 0 10 10'%3E…")}}}##|**Broken**: XWIKI-22206|OK|OK|OK | ||
| 153 | |Keyframe selector ##0%##|##{{{@keyframes fade { 0% {…} }}}}##|**Broken**|OK|**Broken**|OK | ||
| 154 | |Percentage flex basis|##{{{flex: 1 1 0%}}}##|**Broken**|**Broken**|OK|**Broken** | ||
| 155 | |Number with a unit|##{{{font-size: 1.0em}}}##|**Broken**|OK|OK|OK | ||
| 156 | |Nesting|##{{{& .title}}}##, ##{{{&:hover}}}##|OK|OK|OK|OK | ||
| 157 | |Nesting with a descendant pseudo-class|##{{{& :hover}}}##|**Broken**|OK|OK|OK | ||
| 158 | |##@property##|##{{{initial-value: 0deg}}}##|**Broken**|OK|OK|**Broken** | ||
| 159 | |Named container queries|##{{{@container card (width > 400px)}}}##|**Broken**|OK|OK|OK | ||
| 160 | |##@supports## with ##not##|##{{{@supports (not (display: grid))}}}##|**Broken**|OK|OK|Kept unminified | ||
| 161 | |##@supports selector()##|##{{{@supports selector(:has(a))}}}##|OK|OK|**Broken**|Kept unminified | ||
| 162 | |##@scope##|##{{{@scope (.card) to (.content)}}}##|**Broken**|OK|OK|OK | ||
| 163 | |Relative colors|##{{{oklch(from var(--c) calc(l + 0.1) c h)}}}##|**Broken**|OK|OK|OK | ||
| 164 | |Unusual custom property values|##{{{--empty: ;}}}##, ##{{{--json: { "a": 1 }}}}##|OK|**Broken**|Kept unminified|Kept unminified | ||
| 165 | |##@import## with ##layer()## and ##supports()##|##{{{@import url("theme.css") layer(theme) supports(display: grid)}}}##|OK|OK|Kept unminified|Kept unminified | ||
| 166 | |##attr()## with ##type()##|##{{{attr(data-size type(<length>), 10px)}}}##|OK|OK|OK|Kept unminified | ||
| 167 | |##if()##|##{{{if(style(--scheme: dark): white; else: black)}}}##|OK|OK|OK|Kept unminified | ||
| 168 | |##@layer##, ##@starting-style##, media query ranges, ##:is()##, ##:where()##, ##:has()##, grid, logical properties, ##color-mix()##|n/a|OK|OK|OK|OK | ||
| 169 | |||
| 170 | YUI breaks 13 of these features, logicsquad 2, css4j 2 and ph-css 3. ph-css also refuses 7 of them, so the sheets using them are not minified. | ||
| 171 | |||
| 172 | === Workarounds in XWiki Standard today === | ||
| 173 | |||
| 174 | Developers already write around the YUI bugs: | ||
| 175 | |||
| 176 | * ##TourCode.TourJS## uses ##{{{visibility .001s .2s}}}## instead of ##0s##, with the comment "Using an actual 0s here gets minified to 0, which is incorrect CSS", added for XWIKI-24022 in February 2026. | ||
| 177 | * ##CKEditor.ContentSheet## sets ##transition-delay## and ##transition-property## separately and leaves the duration implicit, for the same reason, added for XWIKI-22383 in September 2026. | ||
| 178 | * ##style.css## and ##cssVariablesInit.css## open with ##{{{/*!}}}## and close the comment right before printing their output, so that YUI keeps their Velocity code intact at build time. As a side effect, YUI never minifies the CSS these two files output. | ||
| 179 | * No workaround exists for ##calc()##: XWIKI-21246 is still open and was reported again as XWIKI-23906 in January 2026. The workaround suggested in the issue, the LESS escape ##{{{~"…"}}}##, does not apply to plain CSS. For SVG data URLs, encoding the spaces as ##%20## avoids XWIKI-22206. | ||
| 180 | |||
| 181 | == Live bugs in XWiki Standard == | ||
| 182 | |||
| 183 | The runtime minification already changes the rendering of these skin extensions, compared to ##?minify=false##: | ||
| 184 | |||
| 185 | * ##{{{.notification-content blockquote { font-size: 1.0em; }}}}## in ##XWiki.Notifications.Code.Macro.NotificationsMacro##, and the same value in ##XWiki.TagCloud##: YUI outputs ##{{{font-size:1.0}}}##, an invalid declaration, so the font size is ignored. | ||
| 186 | * ##{{{& :hover, & :focus, & :focus-within}}}## in ##Main.SolrSearch##: YUI outputs ##{{{&:hover,&:focus,&:focus-within}}}##. The source targets a hovered or focused descendant, the minified code targets the element itself. | ||
| 187 | |||
| 188 | == Limitations == | ||
| 189 | |||
| 190 | * Velocity runs with stubs, not in XWiki: macros like ##~#css3_backgroundLinearGradient## and references like ##$services## stay unrendered. | ||
| 191 | * One browser engine, Chromium. The oracle compares declarations and computed values on a probe element, not the layout of real pages. | ||
| 192 | * The fixtures were written for this evaluation and do not cover everything. | ||
| 193 | |||
| 194 | == Consequences for step 3 == | ||
| 195 | |||
| 196 | * **Runtime**: css4j in safe mode. Report its 2 serialization bugs upstream, the keyframe ##0%## and the ##{{{@supports selector()}}}## space, first. | ||
| 197 | * **Build time**: a real CSS parser cannot work on raw Velocity templates. Options: | ||
| 198 | ** a comment and white space stripper, owned by XWiki, small and safe, for a 4% smaller gain than YUI; | ||
| 199 | ** logicsquad minifier: same gain as YUI, but it breaks 3 templates; | ||
| 200 | ** no build-time minification for the Velocity templates, and minify the rendered skin files at runtime with css4j, as for skin extensions. This gives a single engine, at the cost of a bigger change in how skin files are served. | ||
| 201 | * The webjar CSS can also contain Velocity, when it is loaded with ##{{{'evaluate': true}}}##, so the same constraint applies to the ##webjar## packaging. |