Truly independent web browser
LibWeb: Don’t generate ::before or ::after inside form controls
sideshowbarker354509d21493
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rsindex 4555f464005..4325dac7f41 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs@@ -2288,6 +2288,7 @@ fn update_principal_node_descendants(// Add the ::before pseudo-element before walking normal children.if update.kind.is_element()&& layout_node_can_have_children+ && box_can_have_generated_children(host, update.mirror_identity, layout_node)&& !content_visibility_hidden&& !context.has_svg_root{@@ -2499,7 +2500,10 @@ fn update_principal_node_descendants(}}- if update.update_pseudo_elements_in_place && !should_create_layout_node {+ if update.update_pseudo_elements_in_place+ && !should_create_layout_node+ && box_can_have_generated_children(host, update.mirror_identity, layout_node)+ {state.ancestor_stack.push(layout_node);let placed = create_pseudo_element(host,@@ -2544,17 +2548,41 @@ fn create_marker_and_after_boxes();assert!(placed.is_none());}- let placed = create_pseudo_element(- host,- state,- element,- FfiPseudoElement::After,- Some(FfiInsertionMode::Append),- );- assert!(placed.is_none());+ if box_can_have_generated_children(host, element, layout_node) {+ let placed = create_pseudo_element(+ host,+ state,+ element,+ FfiPseudoElement::After,+ Some(FfiInsertionMode::Append),+ );+ assert!(placed.is_none());+ }assert!(state.ancestor_stack.pop().is_some());}+/// Whether the box of `element` can hold the boxes of its ::before and ::after pseudo-elements. A form control whose+/// content the user agent supplies holds none: a text control, a range input, an input element shown as a button, or a+/// select.+fn box_can_have_generated_children(host: &TreeBuilderHost<'_>, element: StyleNodeID, layout_node: LayoutNode) -> bool {+ use crate::css::style::bridge::element_construction_fact::IS_HTML_SELECT_ELEMENT;+ let data = host.data(layout_node);+ if matches!(+ data.kind.get(),+ NodeKind::TextInputBox | NodeKind::TextAreaBox | NodeKind::RangeInputBox+ ) {+ return false;+ }+ if node_facts::has_flag(data, NodeFlag::IsHtmlInputElement)+ && node_facts::has_flag(data, NodeFlag::UsesButtonLayout)+ {+ return false;+ }+ !host+ .arena()+ .with_style_store(|engine| engine.element_construction_facts(element) & IS_HTML_SELECT_ELEMENT != 0)+}+/// Empties the child list of a box that keeps its place while its children are placed again: frees the element's/// `::before`, `::after` and `::marker` boxes and the anonymous wrappers around inline runs, and detaches every other/// box, which [`may_rebuild_child_list`] made sure belongs to a DOM child. Those boxes keep their subtrees, anddiff --git a/Tests/LibWeb/Layout/expected/form-controls-generate-no-before-and-after.txt b/Tests/LibWeb/Layout/expected/form-controls-generate-no-before-and-after.txtnew file mode 100644index 00000000000..65aaa6499f1--- /dev/null+++ b/Tests/LibWeb/Layout/expected/form-controls-generate-no-before-and-after.txt@@ -0,0 +1,95 @@+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 92 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 76 0+0+8] children: inline+ frag 0 from TextInputBox start: 0, length: 0, rect: [13,31 159.6875x14] baseline: 13.984375+ frag 1 from TextNode start: 0, length: 1, rect: [177.6875,29 8x16] baseline: 12.796875+ " "+ frag 2 from TextAreaBox start: 0, length: 0, rect: [188.6875,11 159.6875x28] baseline: 34+ frag 3 from TextNode start: 0, length: 1, rect: [351.375,29 8x16] baseline: 12.796875+ " "+ frag 4 from BlockContainer start: 0, length: 0, rect: [368.375,31 43.5x14] baseline: 13.984375+ frag 5 from TextNode start: 0, length: 1, rect: [420.875,29 8x16] baseline: 12.796875+ " "+ frag 6 from BlockContainer start: 0, length: 0, rect: [437.875,31 44.09375x14] baseline: 13.984375+ frag 7 from TextNode start: 0, length: 1, rect: [490.96875,29 8x16] baseline: 12.796875+ " "+ frag 8 from RangeInputBox start: 0, length: 0, rect: [500.96875,26 129x16] baseline: 18+ frag 9 from TextNode start: 0, length: 1, rect: [631.96875,29 8x16] baseline: 12.796875+ " "+ frag 10 from BlockContainer start: 0, length: 0, rect: [648.96875,31 62.609375x14] baseline: 13.984375+ frag 11 from BlockContainer start: 0, length: 0, rect: [17,51 135.65625x30] baseline: 21.984375+ TextInputBox <input.g> at [13,31] inline-block [0+2+3 159.6875 3+2+0] [0+2+1 14 1+2+0] [BFC] children: not-inline+ Box <div> at [13,31] flex-container(row) [0+0+0 159.6875 0+0+0] [0+0+0 14 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [13,31] flex-item [0+0+0 159.6875 0+0+0] [0+0+0 14 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 4, rect: [13,31 26.78125x14] baseline: 10.984375+ "text"+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)+ TextAreaBox <textarea.g> at [188.6875,11] inline-block [0+1+2 159.6875 2+1+0] [0+1+2 28 2+1+0] [BFC] children: not-inline+ Box <div> at [188.6875,11] flex-container(row) [0+0+0 159.6875 0+0+0] [0+0+0 14 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [188.6875,11] flex-item [0+0+0 159.6875 0+0+0] [0+0+0 14 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 4, rect: [188.6875,11 30.1875x14] baseline: 10.984375+ "area"+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)+ BlockContainer <input.g> at [368.375,31] inline-block [0+2+7 43.5 7+2+0] [0+2+1 14 1+2+0] [BFC] children: not-inline+ BlockContainer <(anonymous)> at [368.375,31] flex-container(column) [0+0+0 43.5 0+0+0] [0+0+0 14 0+0+0] [FFC] children: not-inline+ BlockContainer <(anonymous)> at [368.375,31] flex-item [0+0+0 43.5 0+0+0] [0+0+0 14 0+0+0] [BFC] children: inline+ frag 0 from BlockContainer start: 0, length: 0, rect: [368.375,31 43.5x14] baseline: 10.984375+ BlockContainer <span> at [368.375,31] inline-block [0+0+0 43.5 0+0+0] [0+0+0 14 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 6, rect: [368.375,31 43.5x14] baseline: 10.984375+ "submit"+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)+ BlockContainer <input.g> at [437.875,31] inline-block [0+2+7 44.09375 7+2+0] [0+2+1 14 1+2+0] [BFC] children: not-inline+ BlockContainer <(anonymous)> at [437.875,31] flex-container(column) [0+0+0 44.09375 0+0+0] [0+0+0 14 0+0+0] [FFC] children: not-inline+ BlockContainer <(anonymous)> at [437.875,31] flex-item [0+0+0 44.09375 0+0+0] [0+0+0 14 0+0+0] [BFC] children: inline+ frag 0 from BlockContainer start: 0, length: 0, rect: [437.875,31 44.09375x14] baseline: 10.984375+ BlockContainer <span> at [437.875,31] inline-block [0+0+0 44.09375 0+0+0] [0+0+0 14 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 6, rect: [437.875,31 44.09375x14] baseline: 10.984375+ "button"+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)+ RangeInputBox <input.g> at [500.96875,26] inline-block [2+0+0 129 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline+ Box <div> at [500.96875,26] flex-container(row) [0+0+0 129 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [500.96875,32] positioned flex-item [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [500.96875,32] positioned [0+0+0 64.5 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [565.46875,26] positioned [64.5+0+0 16 0+0+48.5] [-6+0+0 16 0+0+0] children: not-inline+ TextNode <#text> (not painted)+ BlockContainer <select.g> at [648.96875,31] inline-block [0+1+8 62.609375 8+1+0] [0+1+2 14 2+1+0] [BFC] children: not-inline+ Box <div> at [648.96875,31] positioned flex-container(row) [0+0+0 62.609375 0+0+0] [0+0+0 14 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [648.96875,31] flex-item [0+0+0 42.609375 0+0+20] [0+0+0 14 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [648.96875,31] [0+0+0 42.609375 0+0+0] [0+0+0 14 0+0+0] children: inline+ frag 0 from TextNode start: 0, length: 6, rect: [648.96875,31 42.609375x14] baseline: 10.984375+ "option"+ TextNode <#text> (not painted)+ BlockContainer <div> at [648.96875,45] floating [0+0+0 42.609375 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [648.96875,45] [0+0+0 42.609375 0+0+0] [0+0+0 14 0+0+0] children: inline+ frag 0 from TextNode start: 0, length: 6, rect: [648.96875,45 42.609375x14] baseline: 10.984375+ "option"+ TextNode <#text> (not painted)+ BlockContainer <div> at [695.578125,30] positioned [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline+ frag 0 from SVGSVGBox start: 0, length: 0, rect: [695.578125,30 16x16] baseline: 16+ SVGSVGBox <svg> at [695.578125,30] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] viewport-transform=[0.6666667 0 0 0.6666667 0 0] [SVG] children: not-inline+ SVGGeometryBox <path> at [6,8.578125] [0+0+0 12 0+0+0] [0+0+0 7.421875 0+0+0] children: not-inline+ TextNode <#text> (not painted)+ BlockContainer <button.g> at [17,51] inline-block [0+2+7 135.65625 7+2+0] [0+2+1 30 1+2+0] [BFC] children: not-inline+ BlockContainer <(anonymous)> at [17,51] flex-container(column) [0+0+0 135.65625 0+0+0] [0+0+0 30 0+0+0] [FFC] children: not-inline+ BlockContainer <(anonymous)> at [17,51] flex-item [0+0+0 135.65625 0+0+0] [0+0+0 30 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 14, rect: [24.78125,59 99.984375x14] baseline: 10.984375+ "button element"+ frag 1 from BlockContainer start: 0, length: 0, rect: [132.765625,59 11.890625x14] baseline: 18.984375+ InlineNode <(anonymous)> at [17,59] [0+0+0 7.78125 0+0+0] [0+0+0 14 0+0+0]+ frag 0 from GeneratedTextNode start: 0, length: 1, rect: [17,59 7.78125x14] baseline: 10.984375+ "B"+ GeneratedTextNode <(anonymous)> (not painted)+ TextNode <#text> (not painted)+ BlockContainer <(anonymous)> at [132.765625,59] inline-block [0+0+8 11.890625 8+0+0] [0+0+8 14 8+0+0] [BFC] children: inline+ frag 0 from GeneratedTextNode start: 0, length: 1, rect: [132.765625,59 11.890625x14] baseline: 10.984375+ "A"+ GeneratedTextNode <(anonymous)> (not painted)+ TextNode <#text> (not painted)++SC for Viewport<#document> [0,0 800x600] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x92] (z-index: auto)+ SC for BlockContainer<DIV> [565.46875,26 16x16] (z-index: auto), has_transformdiff --git a/Tests/LibWeb/Layout/input/form-controls-generate-no-before-and-after.html b/Tests/LibWeb/Layout/input/form-controls-generate-no-before-and-after.htmlnew file mode 100644index 00000000000..49ee192bffa--- /dev/null+++ b/Tests/LibWeb/Layout/input/form-controls-generate-no-before-and-after.html@@ -0,0 +1,12 @@+<!DOCTYPE html>+<style>+ .g::before { content: "B"; }+ .g::after { content: "A"; display: inline-block; padding: 8px; }+</style>+<input class="g" value="text">+<textarea class="g">area</textarea>+<input class="g" type="submit" value="submit">+<input class="g" type="button" value="button">+<input class="g" type="range">+<select class="g"><option>option</option></select>+<button class="g">button element</button>diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-pseudo/pseudo-replaced-elements.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-pseudo/pseudo-replaced-elements.txtnew file mode 100644index 00000000000..9fe35d5e225--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-pseudo/pseudo-replaced-elements.txt@@ -0,0 +1,30 @@+Harness status: OK++Found 24 tests++17 Pass+7 Fail+Pass INPUT text+Pass INPUT date+Pass INPUT time+Pass INPUT datetime-local+Pass INPUT checkbox+Pass INPUT radio+Pass INPUT file+Pass INPUT range+Pass INPUT color+Pass INPUT hidden+Pass INPUT search+Fail VIDEO true+Pass VIDEO+Pass INPUT appearance: none; checkbox+Pass INPUT appearance: none; radio+Fail INPUT appearance: none; text+Fail INPUT appearance: none; date+Fail INPUT appearance: none; time+Fail INPUT appearance: none; datetime-local+Pass INPUT appearance: none; file+Fail INPUT appearance: none; range+Pass INPUT appearance: none; color+Pass INPUT appearance: none; hidden+Fail INPUT appearance: none; search\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-pseudo/pseudo-replaced-elements.html b/Tests/LibWeb/Text/input/wpt-import/css/css-pseudo/pseudo-replaced-elements.htmlnew file mode 100644index 00000000000..c79f1efd56a--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-pseudo/pseudo-replaced-elements.html@@ -0,0 +1,77 @@+<!doctype html>+<meta charset="utf-8">+<link rel="author" href="mailto:[email protected]" title="Emilio Cobos Álvarez">+<link rel="author" href="https://mozilla.org" title="Mozilla">+<link rel="help" href="https://issues.chromium.org/issues/365052666">+<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1911253">+<link rel="help" href="https://drafts.csswg.org/css-pseudo/#generated-content">+<!--+ https://drafts.csswg.org/css-pseudo/#generated-content:+ Also as with regular child elements, the ::before and ::after pseudo-elements+ are suppressed when their parent, the originating element, is replaced.+-->+<script src="../../resources/testharness.js"></script>+<script src="../../resources/testharnessreport.js"></script>+<title>Replaced elements don't generate before / after CSS pseudo-elements</title>+<style>+ input::before,+ video::before,+ progress::before {+ content: "X";+ display: block;+ /* Not resolvable if box is not generated */+ width: 10%;+ }+ span {+ display: block;+ /* Not resolvable if box is not generated */+ width: 10%;+ }+</style>+<input type=text>+<input type=date>+<input type=time>+<input type=datetime-local>+<input type=checkbox>+<input type=radio>+<input type=file>+<input type=range>+<input type=color>+<input type=hidden>+<input type=search>+<video controls></video>+<video></video>+<progress></progress>+<!-- These are special since they are no longer replaced with appearance: none -->+<input style="appearance: none" type=checkbox>+<input style="appearance: none" type=radio>+<!-- These are not special -->+<input style="appearance: none" type=text>+<input style="appearance: none" type=date>+<input style="appearance: none" type=time>+<input style="appearance: none" type=datetime-local>+<input style="appearance: none" type=file>+<input style="appearance: none" type=range>+<input style="appearance: none" type=color>+<input style="appearance: none" type=hidden>+<input style="appearance: none" type=search>+<progress></progress>+<script>+for (let element of document.querySelectorAll("input, video")) {+ test(function() {+ const child = element.appendChild(document.createElement("span"));+ const childWidth = getComputedStyle(child).width;+ const hasChildBox = childWidth.endsWith("px");++ const pseudoWidth = getComputedStyle(element, "::before").width;+ const hasPseudoBox = pseudoWidth.endsWith("px");++ assert_equals(hasChildBox, hasPseudoBox, "Should only generate a box for pseudo-elements if it generates a box for child elements");+ if (hasChildBox || hasPseudoBox) {+ assert_equals(childWidth, pseudoWidth, "Child and pseudo sizes should match");+ }+ const expectedBox = element.style.appearance == "none" && (element.type == "checkbox" || element.type == "radio");+ assert_equals(hasPseudoBox, expectedBox, "Should only generate a box for appearance: none checkboxes/radio buttons");+ }, `${element.tagName} ${element.style.cssText} ${element.type || element.controls || ""}`);+}+</script>