Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibWebView: Normalize source-code text before highlighting it #3181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AtkinsSJ
Copy link
Member

@AtkinsSJ AtkinsSJ commented Jan 8, 2025

The previous code to determine the SourceDocument's lines was too naive: the source text can contain other newline characters and sequences, and the HTML/CSS/JS syntax highlighters would take those into account when determining what line a token is on. This disagreement would cause incorrect highlighting, or even crashes, if the source doesn't solely
use \n for its newlines.

In order to have everyone agree on what a line is, this patch first processes the source to replace all newlines with \n. The need to copy the source like this is unfortunate, but viewing the source is a rare enough action that this should not cause any noticeable performance problems.

Fixes #3169

AK/Utf8View.cpp Outdated Show resolved Hide resolved
Libraries/LibWebView/SourceHighlighter.cpp Outdated Show resolved Hide resolved
Libraries/LibWebView/SourceHighlighter.cpp Outdated Show resolved Hide resolved
Useful when you need to check for more than one code point, some of
which are multiple bytes. (In which case StringView::contains() will
return wrong results.)
The previous code to determine the SourceDocument's lines was too naive:
the source text can contain other newline characters and sequences, and
the HTML/CSS/JS syntax highlighters would take those into account when
determining what line a token is on. This disagreement would cause
incorrect highlighting, or even crashes, if the source didn't solely use
`\n` for its newlines.

In order to have everyone agree on what a line is, this patch first
processes the source to replace all newlines with `\n`. The need to
copy the source like this is unfortunate, but viewing the source is a
rare enough action that this should not cause any noticeable
performance problems.

As the callers have a String, and we want a String, this also changes
the function parameters to keep the source as a String instead of
converting it to StringView and back.

Fixes LadybirdBrowser#3169
@AtkinsSJ AtkinsSJ force-pushed the view-source-hates-cr branch from 008cd30 to 66f1c43 Compare January 13, 2025 12:05
@AtkinsSJ
Copy link
Member Author

Changes:

  • Use ReadonlySpan for Utf8View::contains_any_of()
  • Pass a String const& around instead of converting it to StringView and back
  • Use named constants for the line-separator and paragraph-separator code points

(Thanks Tim!)

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.

LibWebView: Crashes viewing the source of the acid1 test
2 participants