Skip to content

fix(template): avoid catastrophic backtracking in table-mode detection - #314

Merged
smalluban merged 1 commit into
hybridsjs:mainfrom
dobexx:fix/template-table-regex-backtracking
Sep 17, 2026
Merged

smalluban merged 1 commit into
hybridsjs:mainfrom
dobexx:fix/template-table-regex-backtracking

Conversation

@dobexx

@dobexx dobexx commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #313.

createContents() tests the accumulated static template text at every placeholder against the table-mode regex. Its group ([^<>]|"[^"]*"|'[^']*')* let a quote be consumed either as a plain character or as the start of a quoted string, and a quoted string may span < and >. When the match fails, the engine tries every pairing of quotes: 2^n for n tags with two quoted attributes following a table tag with an attribute (see the timings in #313; a data table followed by a tabbed form never finished compiling).

This change excludes quotes from the plain-character branch ([^<>"']), which makes the three alternatives mutually exclusive, so the engine has exactly one path. The regex decides identically for every table case in the suite (<table>, <table class="x">, <td class="num">, <th title='a > b'>, <thead-element>, closed tables, text after a tag).

Regression test: a table with a quoted attribute followed by 24 attributed buttons and an expression must render in under a second. Against the current regex it takes about 3.4 s in Chrome Headless and fails; with the fix it takes 0 ms. Full suite: 602 of 602 passing, lint and prettier clean.

The regex that decides whether a placeholder sits inside an open table tag
allowed a quote to be consumed either as a plain character or as the start
of a quoted string, so a failed match had to try every pairing of quotes:
2^n for n tags with two quoted attributes after a table tag with an
attribute. A template with a data table followed by a form with a few dozen
attributed tags never finished compiling.

Excluding quotes from the plain-character branch makes the alternatives
mutually exclusive; the decision is unchanged for all table cases.

Closes hybridsjs#313
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 99.874%. remained the same — dobexx:fix/template-table-regex-backtracking into hybridsjs:main

@smalluban
smalluban merged commit 07a8416 into hybridsjs:main Sep 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Catastrophic backtracking in template compiler: table tag with quoted attribute followed by tags with several quoted attributes freezes render

3 participants