Skip to content

Fix browser crash when @Highlight value is empty string - #186

Open
michaelomichael wants to merge 2 commits into
kensa-dev:masterfrom
michaelomichael:hanging-on-highlighted-empty-string-parameter
Open

Fix browser crash when @Highlight value is empty string#186
michaelomichael wants to merge 2 commits into
kensa-dev:masterfrom
michaelomichael:hanging-on-highlighted-empty-string-parameter

Conversation

@michaelomichael

Copy link
Copy Markdown
Contributor

Having a field/parameter annotated with @Highlight can cause the browser tab to crash when the value of that field/parameter is an empty string ("").

This is because the generated "highlight" regex pattern will match every single text node due to it matching on the empty string.

This fix ignores any zero-length "highlight" values.

buildHighlightRegex has two further callers, DataTable and FixturesTable,
which pass the result straight to String.split. Returning null broke tsc for
both, and at runtime they guard on highlights.length === 0, which does not
catch [""], so split(null) coerced to the string "null" and swallowed that
word wherever it appeared.

(?!) can never match, so split returns the whole string and test is false.
That keeps the RegExp return type and fixes all three call sites without
threading null through any of them.

Adds tests, which this function had none of.
@paul-brooks

Copy link
Copy Markdown
Contributor

Good find, the diagnosis is spot on: an empty highlight builds a regex that matches every text node and hangs the tab.

One wrinkle. buildHighlightRegex has two other callers, DataTable.tsx:21 and FixturesTable.tsx:20, so the RegExp | null return breaks tsc, and at runtime they guard on highlights.length === 0, which doesn't catch [""]. They'd get null and text.split(null) coerces to the string "null", so that word vanishes and the following text gets highlighted instead.

You're busy, so I've pushed a commit rather than bounce it back. It returns /(?!)/g instead of null when nothing survives the filter, which never matches, so split returns the whole string and test is false. Same fix, no caller changes, no null to thread through. Added tests too since there was no coverage for this.

CI was green on your original despite tsc failing, because :ui:viteBuild shells straight to vite build and skips the type check. Raised separately as #187.

Thanks for both of these.

@michaelomichael

Copy link
Copy Markdown
Contributor Author

Looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants