Repository navigation
Add :max_depth option to from_document/2 and from_fragment/2 - #43
Merged
Merged
Conversation
lexbor's tree construction walks the stack of open elements for many tokens, so input that nests elements thousands deep takes time quadratic in its size to parse. The option stops parsing and raises once more elements than that are open.
josevalim
approved these changes
Oct 16, 2026
ericmj
marked this pull request as ready for review
October 16, 2026 17:23
jonatanklosko
approved these changes
Oct 18, 2026
Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This was referenced Oct 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
lexbor walks the stack of open elements for many tokens during tree construction, for example to look for a
pelement in button scope on everydivorulstart tag, and for most end tags. Input that nests elements thousands deep takes time quadratic in its size to parse: nested<div>takes 225 ms at 100 KB and 964 ms at 200 KB with the pinned lexbor (Apple M5 Max). lexbor/lexbor#437 fixes thepcheck upstream, and the end tags remain.:max_depthraises once more elements than the limit are open, where<html>is at depth 1 for documents and the top-level nodes are at depth 1 for fragments. It wraps the tree builder's token callback, so it doesn't need a lexbor change. To install the callback,from_documentandfrom_fragmentnow call lexbor's chunk functions, which is whatlxb_html_document_parse()andlxb_html_document_parse_fragment()do internally.Without the option the output doesn't change: 200,000 random tag-soup documents and 200,000 fragments produce the same HTML as main, and the same holds with
max_depth: 10_000. Withmax_depth: 512, 100 KB of nested<div>raises after 0.1 ms, and inputs that stay under the limit, such as 500 nested<span>followed by 25,000</x>, parse in under 10 ms. Ordinary HTML parses at the same speed with or without the option.