Commit graph

80 commits

Author SHA1 Message Date
Andreas Kling
e5d62e9915 LibWeb: Track whether HTMLLinkElement was enabled when created by parser
This information is needed by the script-blocking style sheet logic, and
its absence was causing a WPT test to crash.
2025-04-24 18:26:54 +02:00
Andreas Kling
5085b0dd66 LibWeb: Remove unused HTMLToken::Position::byte_offset 2025-04-20 18:43:11 +02:00
Andrew Kaster
6d11414957 LibWeb: Make storage of CSS::StyleValues const-correct
Now we consistently use `RefPtr<StyleValue const>` for all StyleValues.
2025-04-16 10:41:44 -06:00
Andrew Kaster
8cfac6ed71 LibWeb: Store a SpeculativeHTMLParser on the HTML Parser
The parser was previously added, but unused. Actually attaching one to
the HTML Parser will let us test the limits of Swift interop.
2025-04-16 09:02:27 -06:00
Sam Atkins
9dbeecb73d LibWeb: Correct some spec typos
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Corresponds to 285a58bf30
2025-04-10 04:01:37 +02:00
Andrew Kaster
9ee2473aa4 LibWeb+LibGC: Import GC swift module into LibWeb and an initial user
Start work on a speculative HTML Parser in Swift. This component will
walk ahead of the normal HTML parser looking for fetch() requests to
make while the normal parser is blocked. This work exposed many holes in
the Swift C++ interop component, which have been reported upstream.
2025-04-03 16:47:48 -06:00
Sam Atkins
7367150536 LibWeb/HTML: Update FIXME to not reset form-associated custom elements
Corresponds to e6bdd0557a
2025-04-02 17:28:06 +01:00
Andreas Kling
f1914893e9 LibJS+LibWeb: Remove more uses of DeprecatedFlyString 2025-03-24 22:27:17 +00:00
Ryan Liptak
0b0f47e320 LibWeb: Make named character references more spec-compliant & efficient
There are two changes happening here: a correctness fix, and an
optimization. In theory they are unrelated, but the optimization
actually paves the way for the correctness fix.

Before this commit, the HTML tokenizer would attempt to look for named
character references by checking from after the `&` until the end of
m_decoded_input, which meant that it was unable to recognize things like
named character references that are inserted via `document.write` one
byte at a time. For example, if `&notin;` was written one-byte-at-a-time
with `document.write`, then the tokenizer would only check against `n`
since that's all that would exist at the time of the check and therefore
erroneously conclude that it was an invalid named character reference.

This commit modifies the approach taken for named character reference
matching by using a trie-like structure (specifically, a deterministic
acyclic finite state automaton or DAFSA), which allows for efficiently
matching one-character-at-a-time and therefore it is able to pick up
matching where it left off after each code point is consumed.

Note: Because it's possible for a partial match to not actually develop
into a full match (e.g. `&notindo` which could lead to `&notindot;`),
some backtracking is performed after-the-fact in order to only consume
the code points within the longest match found (e.g. `&notindo` would
backtrack back to `&not`).

With this new approach, `document.write` being called one-byte-at-a-time
is handled correctly, which allows for passing more WPT tests, with the
most directly relevant tests being
`/html/syntax/parsing/html5lib_entities01.html`
and
`/html/syntax/parsing/html5lib_entities02.html`
when run with `?run_type=write_single`. Additionally, the implementation
now better conforms to the language of the spec (and resolves a FIXME)
because exactly the matched characters are consumed and nothing more, so
SWITCH_TO is able to be used as the spec says instead of RECONSUME_IN.

The new approach is also an optimization:

- Instead of a linear search using `starts_with`, the usage of a DAFSA
  means that it is always aware of which characters can lead to a match
  at any given point, and will bail out whenever a match is no longer
  possible.
- The DAFSA is able to take advantage of the note in the section
  `13.5 Named character references` that says "This list is static and
  will not be expanded or changed in the future." and tailor its Node
  struct accordingly to tightly pack each node's data into 32-bits.
  Together with the inherent DAFSA property of redundant node
  deduplication, the amount of data stored for named character reference
  matching is minimized.

In my testing:

- A benchmark tokenizing an arbitrary set of HTML test files was about
  1.23x faster (2070ms to 1682ms).
- A benchmark tokenizing a file with tens of thousands of named
  character references mixed in with truncated named character
  references and arbitrary ASCII characters/ampersands runs about 8x
  faster (758ms to 93ms).
- The size of `liblagom-web.so` was reduced by 94.96KiB.

Some technical details:

A DAFSA (deterministic acyclic finite state automaton) is essentially a
trie flattened into an array, but it also uses techniques to minimize
redundant nodes. This provides fast lookups while minimizing the
required data size, but normally does not allow for associating data
related to each word. However, by adding a count of the number of
possible words from each node, it becomes possible to also use it to
achieve minimal perfect hashing for the set of words (which allows going
from word -> unique index as well as unique index -> word). This allows
us to store a second array of data so that the DAFSA can be used as a
lookup for e.g. the associated code points.

For the Swift implementation, the new NamedCharacterReferenceMatcher
was used to satisfy the previous API and the tokenizer was left alone
otherwise. In the future, the Swift implementation should be updated to
use the same implementation for its NamedCharacterReference state as
the updated C++ implementation.
2025-03-22 16:03:44 +01:00
Sam Atkins
415dd1be06 LibWeb/HTML: Remove "flag" word from usage of "page showing"
Corresponds to 30935f3474
2025-03-14 20:33:25 +00:00
InvalidUsernameException
0e1eb4d4a7 LibWeb: Respect scroll position set by script during page load
When setting scroll position during page load we need to consider
whether we actually have a fragment to scroll to. A script may already
have run at that point and may already have set a scroll position.

If there is an actual fragment to scroll to, it is fine to scroll to
that fragment, since it should take precedence. If we don't have a
fragment however, we should not unnecessarily overwrite the scroll
position set by the script back to (0, 0).

Since this problem is caused by a spec bug, I have tested the behavior
in the three major browsers engines. Unfortunately they do not agree
fully with each other. If there is no fragment at all (e.g. `foo.html`),
all browsers will respect the scroll position set by the script. If
there is a fragment (e.g. `foo.html#bar`), all browsers will set the
scroll position to the fragment element and ignore the one set by
script. However, when the fragment is empty (e.g. `foo.html#`), then
Blink and WebKit will set scroll position to the fragment, while Gecko
will set scroll position from script. Since all of this is ad-hoc
behavior anyway, I simply implemented the Blink/WebKit behavior because
of the majority vote for now.

This fixes a regression introduced in 51102254b5.
2025-03-10 17:14:13 +01:00
Andreas Kling
043e96946f LibWeb: Block rendering until linked stylesheets are loaded
This commit implements the main "render blocking" behavior for link
elements, drastically reducing the amount of FOUC (flash of unstyled
content) we subject our users to.

The document will now block rendering until linked style sheets
referenced by parser-created link elements have loaded (or failed).

Note that we don't yet extend the blocking period until "critical
subresources" such as imported style sheets have been downloaded
as well.
2025-02-27 21:36:40 +01:00
Tim Ledbetter
a187d5f28f LibWeb/SVG: Process script element when its text content changes 2025-02-26 16:08:35 +01:00
Jaycadox
f672c57ca7 LibWeb: Check charset UTF-16LE/BE separately for UTF-8 conversion
Previously, the charset of name "UTF-16BE/LE" would be checked against
when following standards to convert the charset to UTF-8, but in
reality, the charsets "UTF-16BE" and "UTF-16LE" should be checked
separately.

Co-authored-by: Jelle Raaijmakers <jelle@ladybird.org>
2025-02-24 14:51:41 +01:00
Sam Atkins
334c55c999 LibWeb/HTML: Add missing spec text/link for insert_html_element() 2025-02-20 21:55:34 +00:00
Sam Atkins
91fe0e1a1a LibWeb/HTML: Spec-ify reset_the_insertion_mode_appropriately()
It was already to-spec, with a couple of non-behavior-affecting changes,
but now it's clearer that this is the case. :^)
2025-02-20 21:55:34 +00:00
Sam Atkins
d7b4e1f2e3 LibWeb: Add missing text to find_appropriate_place_for_inserting_node() 2025-02-20 21:55:34 +00:00
Andreas Kling
550613e526 LibWeb: Remember when HTML parser should ignore next line feed character
There's a quirk in HTML where the parser should ignore any line feed
character immediately following a `pre` or `textarea` start tag.

This was working fine when we could peek ahead in the input stream and
see the next token, but didn't work in character-at-a-time parsing with
document.write().

This commit adds the "can ignore next line feed character" as a parser
flag that is maintained across invocations, making it work in this
parsing mode as well.

20 new passes in WPT/html/syntax/parsing/ :^)
2025-02-20 14:32:13 +01:00
Andreas Kling
611833429a LibWeb: Lazily merge text nodes when invoking HTML parser incrementally
Instead of always inserting a new text node, we now continue appending
to an extisting text node if the parser's character insertion point is
a suitable text node.

This fixes an issue where multiple invocations of document.write() would
create unnecessary sequences of text nodes. Such sequences are now
merged automatically.

19 new passes in WPT/html/syntax/parsing/ :^)
2025-02-20 14:32:13 +01:00
Andreas Kling
0c0fe09e70 LibWeb: Preserve attributes in "reconstruct active formatting elements"
25 new passes in WPT/html/syntax/parsing/ :^)
2025-02-20 14:32:13 +01:00
Andreas Kling
7549f6842f LibWeb: Ensure "frameset ok" flag is disabled after parsing pre tag
2 new passes in WPT/html/syntax/parsing/ :^)
2025-02-20 14:32:13 +01:00
Andreas Kling
2e59dbd10f LibWeb: Don't process frameset token twice in "in body" insertion mode
We were neglecting to return after handling the `frameset` start tag,
which caused us to process it twice, once properly and once generically.

54 new passes in WPT/html/syntax/parsing/ :^)
2025-02-20 14:32:13 +01:00
Andreas Kling
6f7b865cd1 LibWeb: Let HTML parser handle EOF inserted by document.close()
Before this change, the explicit EOF inserted by document.close() would
instantly abort the parser. This meant that parsing algorithms that ran
as part of the parser unwinding on EOF would never actually run.

591 new passes in WPT/html/syntax/parsing/ :^)

This exposed a problem where the parser would try to insert a root
<html> element on EOF in a document where someone already inserted such
an element via direct DOM manipulation. The parser now gracefully
handles this scenario. It's covered by existing tests (which would
crash without this change.)
2025-02-20 14:32:13 +01:00
Shannon Booth
7441aa34e4 LibWeb/HTML: Bail from HTML parsing when EOF hit on document.close
This fixes a crash in the included test that regressed in 0adf261,
and is hit by the following HTML:

```html
<body></body>
<script>
  const frame = document.body.appendChild(document.createElement("iframe"));
  frame.contentDocument.open();
  const child = frame.contentDocument.createElement("html")
  const html = frame.contentDocument.appendChild(child);
  frame.contentDocument.close();
</script>
```

I am not 100% sure this is fully the correct fix and there are other
cases which would not work properly. But it's definitely an improvement
to make the confuisingly named 'insert_an_eof' function of the tokenizer
actually do something.
2025-02-09 19:20:09 +00:00
Andreas Kling
4f855286d7 LibWeb: Clamp layout content sizes to a max value instead of crashing
We've historically asserted that no "saturated" size values end up as
final metrics for boxes in layout. This always had a chance of producing
false positives, since you can trivially create extremely large boxes
with CSS.

The reason we had those assertions was to catch bugs in our own engine
code where we'd incorrectly end up with non-finite values in layout
algorithms. At this point, we've found and fixed all known bugs of that
nature, and what remains are a bunch of false positives on pages that
create very large scrollable areas, iframes etc.

So, let's change it! We now clamp content width and height of boxes to
17895700 pixels, apparently the same cap as Firefox uses.

There's also the issue of calc() being able to produce non-finite
values. Note that we don't clamp the result of calc() directly, but
instead just clamp values when assigning them to content sizes.

Fixes #645.
Fixes #1236.
Fixes #1249.
Fixes #1908.
Fixes #3057.
2025-02-05 18:28:55 +01:00
Tim Ledbetter
8dfd382e12 LibWeb: Use as_if instead of dynamic_cast in a few places 2025-01-31 14:29:48 +01:00
Sam Atkins
3775f3399c LibWeb: Fix various typos
Corresponds to:
- https://github.com/whatwg/html/pull/10934
- https://github.com/whatwg/html/pull/10950
2025-01-30 15:00:16 +00:00
Timothy Flynn
85b424464a AK+Everywhere: Rename verify_cast to as
Follow-up to fc20e61e72.
2025-01-21 11:34:06 -05:00
Shannon Booth
51102254b5 LibWeb/HTML: Scroll to the fragment before loading the document
Otherwise nowhere ends up scrolling to the fragment specified by the
fragment in document's URL. This fixes ladybird scrolling to the
correct location in the document when navigating to a link that
has a fragment, e.g:

https://html.spec.whatwg.org/multipage/browsing-the-web.html#try-to-scroll-to-the-fragment

As well as use of the :target selector.
2025-01-15 12:43:48 +00:00
Shannon Booth
64eeda6450 LibWeb: Return a representation of an 'Agent' in 'relevant agent'
This makes it more convenient to use the 'relvant agent' concept,
instead of the awkward dynamic casts we needed to do for every call
site.

mutation_observers is also changed to hold a GC::Root instead of raw
GC::Ptr. Somehow this was not causing problems before, but trips up CI
after these changes.
2025-01-11 10:39:48 -05:00
Tim Ledbetter
f8b8c9c4a4 LibWeb: Wait until ReadyState is complete before detaching HTML parser
Previously, the DOM complete time was never being set, as the HTML
parser was detached before `DocumentReadyState` was set to complete.
2025-01-11 11:11:52 +01:00
Tim Ledbetter
8b5e9c2a1d LibWeb: Emit comment token for unterminated bogus comments on EOF 2025-01-11 11:09:47 +01:00
Tim Ledbetter
983540a244 LibWeb: Add missing spec links to HTML parser 2025-01-08 12:05:08 +00:00
Sam Atkins
89296b88a0 LibWeb: Bring fragment parsing up to spec
Corresponds to https://github.com/whatwg/html/pull/10874

Also, parse_fragment() returns ExceptionOr, so stop voiding the error
from append_child().
2025-01-07 16:05:59 +01:00
Ryan Liptak
1ba15e1fa3 LibWeb: Fix hex character references accepting all alphabetic ASCII
Instead of just A-F/a-f, any char A-Z/a-z was being accepted as a valid
hexadecimal digit.
2025-01-07 00:43:41 +01:00
Ryan Liptak
df87a9689c LibWeb: Fix numeric character reference at EOF leaking its last digit
Previously, if the NumericCharacterReferenceEnd state was reached when
current_input_character was None, then the
DONT_CONSUME_NEXT_INPUT_CHARACTER macro would restore back before the
EOF, and allow the next state (after the SWITCH_TO_RETURN_STATE) to
proceed with the last digit of the numeric character reference.

For example, with something like `&#1111`, before this commit the
output would incorrectly be `<code point with the value 1111>1` instead
of just `<code point with the value 1111>`.

Instead of putting the `if (current_input_character.has_value())` check
inside NumericCharacterReferenceEnd directly, it was instead added to
DONT_CONSUME_NEXT_INPUT_CHARACTER, because all usages of the macro
benefit from this check, even if the other existing usage sites don't
exhibit any bugs without it:

- In MarkupDeclarationOpen, if the current_input_character is EOF, then
  the previous character is always `!`, so restoring and then checking
  forward for strings like `--`, `DOCTYPE`, etc won't match and the
  BogusComment state will run one extra time (once for `!` and once
  for EOF) with no practical consequences. With the `has_value()` check,
  BogusComment will only run once with EOF.

- In AfterDOCTYPEName, ConsumeNextResult::RanOutOfCharacters can only
  occur when stopping at the insertion point, and because of how
  the code is structured, it is guaranteed that current_input_character
  is either `P` or `S`, so the `has_value()` check is irrelevant.
2025-01-07 00:43:41 +01:00
Timothy Flynn
4d6a9f9d2d LibWeb: Remove now-unnecessary clang-format off directive 2024-12-28 05:39:32 -08:00
Sam Atkins
4c5a40579a LibWeb: Match spec changes to "create an element" algorithm
Corresponds to https://github.com/whatwg/html/pull/10857

No code changes, only comments.
2024-12-18 19:22:44 +00:00
Sam Atkins
30d23ad090 LibWeb/HTML: Use camelCase for variables in adoption agency comments
This is a spec change but I don't know when it occurred.

Also reformat some of the spec comments for clarity.

No code changes, only comments.
2024-12-18 19:22:44 +00:00
Andrew Kaster
66519af43f LibWeb: Remove some uses of [&] lambda captures for queued tasks
Using a default reference capture for these kinds of tasks is dangerous
and prone to error. Some of the variables should for sure be captured
by value so that we can keep a GC object alive rather than trying to
refer to stack objects.
2024-12-10 07:13:00 +01:00
Jelle Raaijmakers
1514197e36 LibWeb: Remove dom_ from dom_exception_to_throw_completion
We're not converting `WebIDL::DOMException`, but `WebIDL::Exception`
instead.
2024-12-09 20:02:51 -07:00
joshua stein
12442ca430 LibWeb: Remove a misleading duplicate comment in HTMLParser 2024-11-30 07:07:30 +00:00
Psychpsyo
e602578501 LibWeb: Add handling for 'an end tag whose tag name is sarcasm' 2024-11-27 10:42:58 +00:00
Psychpsyo
397579096f LibWeb: Add search element to list of Special tags 2024-11-27 11:00:58 +01:00
Gingeh
0adf261c32 LibWeb: Don't end parsing after reaching the insertion point 2024-11-26 23:50:18 +01:00
Andreas Kling
5c70436cb2 LibWeb: Teach more of HTMLTokenizer to stop at the insertion point
In particular, input character lookahead now knows how to stop at the
insertion point marker if needed.

This makes it possible to do amazing things like having document.write()
insert doctypes one character at a time.
2024-11-24 11:45:23 +01:00
Andreas Kling
69367194a6 LibWeb: Make HTML tokenizer stop at insertion point after state switch
If we reach the insertion point at the same time as we switch to another
tokenizer state, we have to bail immediately without proceeding with the
next code point. Otherwise we'd fetch the next token, get an EOF marker,
and then proceed as if we're at the end of the input stream, even though
more data may be coming (with more calls to document.write()..)
2024-11-23 19:19:31 +01:00
Andreas Kling
1d554f97de LibWeb: Reset the "stop parsing" flag when entering HTML parser
Otherwise we'll always bail after processing one token, which is not
what we want.
2024-11-23 19:19:31 +01:00
Andreas Kling
7309f2a6f9 LibWeb: Add spec comments to HTML parser's "initial" insertion mode 2024-11-23 19:19:31 +01:00
Psychpsyo
f09ed59351 LibWeb: Add the search element 2024-11-19 23:30:43 +00:00