Skip to content

Markdown: bound container nesting to Chromium's 512-element parser limit - #1122

Merged
Hona merged 5 commits into
devfrom
deep-markdown-bound
Nov 18, 2026
Merged

Hona merged 5 commits into
devfrom
deep-markdown-bound

Conversation

@Hona

@Hona Hona commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

In Blink's HTML parser (HTMLConstructionSite::AttachLater, kMaximumHTMLParserDOMTreeDepth = 512 including <html> and <body>), elements nest up to 511 open elements under <body> (255 nested bullet or ordered list levels, 1 quote + 255 nested list levels, 10 quotes + 250 list levels, or 510 non-empty <blockquote>s; text nodes still attach to the current element). Past that bound, deeper list items flatten as sibling items in the innermost list (or open a single 1-level list when a list starts inside a deep quote so bullets and task checkboxes stay intact), and deeper block quotes merge their inner blocks into the last quote (510 on Windows, macOS, and Linux; 385 on web and other unguarded targets — so 386..510 quotes draw differently on the web than natively). Neither flattening bound displays a visual marker; only when a single container accumulates more than 64 flattened items or blocks past the bound are the remaining lines grouped into one selectable, searchable plain block preceded by an unselectable "N more nested lines" (rune::Label::MoreNestedLines) marker.

flowchart LR
  P["pulldown-cmark events"] --> B{"MAX_ELEMENT_DEPTH = 511<br/>(+ 770-flex-container bound outside Win/macOS/Linux)"}
  B -->|"Fits (or first list in Quote)"| N["Push Container::List + Item / Quote"]
  B -->|"Past bound (items 1..=64)"| F["Flatten as siblings in last List / merge into last Quote<br/>(no marker at bound)"]
  B -->|"Past bound (item 65+)"| T["Keep all remaining lines in 1 UTF-8-chunked plain block<br/>+ unselectable 'N more nested lines' a11y marker"]
  N & F & T --> R["Iterative Dirs + Renderer + plain_iter<br/>(O(1) call stack in rune-kit)"]
Loading
  • Blink 511-element bound & unguarded-target layout cap (crates/rune-kit/src/markdown/parser.rs, markdown.rs): Builder::depth() + 2 <= MAX_ELEMENT_DEPTH (511) matches Blink on odd quote counts (1 quote + 255 lists nests all 255 levels) and preserves a single list container when a list opens inside a 509/510-deep quote so bullets and - [ ] / - [x] task checkboxes (with exact source byte offsets and Builder::push tasks alignment) render intact. Outside #1120's cfg(any(windows, target_os = "macos", target_os = "linux")) segment gate (wasm32, ios, android, etc.), MAX_UNGUARDED_LAYOUT_DEPTH = 770 preserves all 255 list levels (765), 1 quote + 255 lists (767), and 10 quotes + 250 lists (770) while capping pure <blockquote> nesting at 385 (770 flex containers) so V8's ~984 KiB WebAssembly call stack and 1–2 MiB mobile main threads do not overflow.
  • Lossless 64-item deep-tail grouping (crates/rune/src/labels.rs, crates/rune-kit/src/markdown/{parser,cache,find,stream,plain,render}.rs):
    • Decided strictly from Builder's real past-bound container state (QuoteSlot::Merged / ListSlot::SiblingItems — never inside a code block and never on < 255 nested ordered lists, unwinding any open inner list cleanly so list items never double their marker), items past the 64th flattened item in a container are kept in one plain block (BlockMap::DeepTail), split into 32 KiB chunks on newline bytes or valid UTF-8 character boundaries (is_char_boundary), and preceded by an unselectable Role::Note header carrying rune::Label::MoreNestedLines(n) ("N more nested lines") as its accessibility label only (so neither selection copy nor QuoteFrame copies the marker line as content).
    • The drawn and plainly copied text of the deep-tail block drops each line's leading spaces, tabs, and >, so a line made only of those characters disappears while list markers and [ ] remain as text; every kept byte maps 1-to-1 via SpanMap back to src, so Markdown::find, highlights, and source copy remain exact — identically while streaming (extended in cache.rs with uncapped 2 * src.len() budget accounting) and once settled.
  • Iterative O(1)-stack rendering, linear bidi, collision-free QuotePlainCache, and existing rune-kit checks (crates/rune-kit/src/markdown/{render,bidi,plain}.rs):
    • Renderer::blocks_in_with_dirs, bidi::Dirs::new, plain_iter, and collapsed_top / collapsed_bottom run iteratively on heap stacks in linear time, Flow::cols / Lead::width widen from u8 to usize for copying past 128 levels, and QuotePlainCache skips empty quotes while keying by (quote_block_ptr, first_task, cols, lists) -> (task_delta, breaks, text) so empty Vec<Block> slices never collide or rewind task.
    • Existing rune-kit test program (50 targets with markdown + mermaid): 439 passed, 0 failed (3 ignored), matching dev 23096262 (439 passed, 0 failed).

Side-by-side captures at 1× raster scale (1280×800, light)

Case Reference | Rune | Pixel difference
Bullet list — 20 levels (all 20 levels step 16px/level across x = 324..628) Bullet 20
Bullet list — 60 levels (steps 16px/level to x = 1268 at column edge) Bullet 60
Bullet list — 255 levels (exact bound: 255 nested levels to x = 4388) Bullet 255
Bullet list — 260 levels (past bound: items 255..260 flatten as siblings at x = 4388) Bullet 260
Ordered list — 20 levels (28px/nested level across x = 324..856) Ordered 20
Ordered list — 260 levels (past bound: items 255..260 flatten as siblings at x = 7436, numbered 1..6) Ordered 260
Block quote — 60 levels (60 nested quote rules + paragraphs) Quote 60
Block quote — 515 levels (past bound: quotes past 510 merge into quote 510) Quote 515

Stack peaks across native (8 MiB) & WebAssembly (2 MiB -zstack-size=2097152)

Case Windows x64 (8,388,608 B reserve, without #1120) WebAssembly (2,097,152 B shadow stack — Chromium 159 / Firefox 159 / WebKit 27.2)
list-255 (255 nested lists) 2,060,288 B (1.96 MiB) 1,022,360 B (0.97 MiB, all 3 engines open)
mixed-1q-255ul (1 quote + 255 lists) 2,064,384 B (1.97 MiB) 1,024,760 B (0.98 MiB, all 3 engines open)
mixed-10q-250ul (10 quotes + 250 lists) 2,068,480 B (1.97 MiB) 1,028,360 B (0.98 MiB, all 3 engines open)
quote-510 / quote-511 (510 / 511 quotes) 2,711,552 B (2.59 MiB) 1,028,360 B in Chromium (385 quotes = 770 flex containers; 1,328,360 B at 510 quotes in Firefox & WebKit)

Track open container elements across lists, list items and block quotes up to 510 elements (255 nested lists or 510 nested block quotes, matching Chromium's 512-element HTML parser cap after `<html>` and `<body>` in `DOMParser`), flattening deeper list items as siblings at the final list level and deeper block quotes into the final quote. Widen `Flow::cols` from `u8` to `usize` so copy indentation no longer wraps past 127 levels, and split `collapsed` top and bottom edge scans so single-child nested containers walk linearly instead of branching `2^D` times.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 09:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@Hona

Hona commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

Windows whole suite (-p rune -p rune-kit) on adais, judged against dev's own failures at 32eb7811 (this lane, whole suite) and eb5ae6c9 (FUNCTION's run, rune only) and 3468f3a3 and cca5df6f: #1122 08b48f7c (18 Nov 04:13): 2841 passed, 18 failed in 170 binaries; also on dev: alloc::disabled_trace_hooks_neither_format_nor_allocate, canvas_input::the_middle_button_drags_only_elements_that_take_it, rune::edit::tests::hidden_multiline_pristine_replace_and_delete, rune::edit::tests::pill_edge_cases_words_lines_bidi_and_direct_text_mutation, rune::edit::tests::the_caret_steps_over_a_pill_in_one_move, rune::input::keycaps::tests::repeated_chords_count_and_named_keys_use_glyphs, rune::native_surface::gpu_tests::mirrored_video_is_exact_cropped_centred_and_toggles_without_a_new_frame, rune::native_surface::gpu_tests::video_gpu_colour_sharpness_geometry_and_lifetime, rune::render::breaker::tests::a_refused_blur_pipeline_draws_unblurred, rune::render::layers::cache_tests::live_scale_zoom_sweep_buckets_during_motion_and_settles_crisp, rune::render::layers::rebind_tests::a_texture_switch_binds_no_parked_target_until_one_returns, rune::render::layers::tests::clipped_single_draw_fades_do_not_allocate_targets, rune::runtime::tests::layout_rows::send_flight_follows_the_measured_bubble_while_earlier_rows_land, rune::runtime::tests::picture_scale::a_changing_static_scale_keeps_the_glyph_atlas_bounded, rune::runtime::tests::picture_scale::text_inside_a_scale_draws_at_that_size, rune::runtime::tests::scroll_children::scroll_xy_with_direct_children_scrolls_both_axes_and_warns_on_trailing_growth_sibling, rune::text::macos::smoothing::tests::pipelines_never_share_masks, selectable_text::copy_select_all_and_the_context_menu; none new. Base 47195cfa. Proof 6af31f2d.

@Hona
Hona merged commit 9ccfaba into dev Nov 18, 2026
1 check passed
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