You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Look up nth-child rules for table rows and cells directly instead of running a regex over every stylesheet key #526
Part of #520 (step 6). Changes nothing on the page.
For every TR, TD and TH, CssMerger runs preg_match('/TAG>>SELECTORNTHCHILD>>(.*)/', $key) to find the nth-child rules. It does so over every key of CssManager::$CSS, over the top level of the cascade tree and over the current table cascade level (src/Css/CssMerger.php:530, :742, :850). That is three passes of regexes over the whole stylesheet for every cell.
Measured on gravitypdf at b6a07921, PHP 8.5.4. A table of 400 rows and 5 columns, and a stylesheet of class and descendant rules, none of which match a cell. WriteHTML() time, mean of 3 runs with a hot cache:
Rules
Today
With the three passes removed
0
190 ms
192 ms
300
243 ms
185 ms
1,000
354 ms
197 ms
Measure again cold and hot, averaged, before quoting these.
In the same code, CssParser::processCssSelector() counts maxClassDepth over every class in a selector, not per compound (src/Css/CssParser.php:242-251). .a .b .c p sets it to 3 although no compound has more than one class, and rules that can never match (.x:not(.y):not(.z)) count too. Every element with classes then builds combinations up to that size.
Fix.
At parse time, record the nth-child formulas used for each of TR, TD and TH. Skip the lookup when there are none; otherwise look up each TAG>>SELECTORNTHCHILD>>formula key directly in each node.
Count maxClassDepth per compound, and only for rules that are stored.
Tests. nth-child rules still apply as before, from the stylesheet, from a descendant rule and from a table cascade level. The snapshot suite passes with no fixture updated.
Related: #522 changes how nth-child keys are formed, and #528 changes the index they are matched against.
Activity