Skip to content

Truly independent web browser

LibWeb: Include option text in innerText

sideshowbarker01a227a5f87b
diff --git a/Libraries/LibWeb/CSS/Default.css b/Libraries/LibWeb/CSS/Default.css
index 99acb0f813f..63be92bb199 100644
--- a/Libraries/LibWeb/CSS/Default.css
+++ b/Libraries/LibWeb/CSS/Default.css
@@ -144,8 +144,8 @@ input[type=image] {
cursor: pointer;
}
-option {
- display: none;
+option, optgroup {
+ display: block;
}
/* Custom <input type="range"> styles */
diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp
index 712cb45c506..4f6196a8e01 100644
--- a/Libraries/LibWeb/Editing/Commands.cpp
+++ b/Libraries/LibWeb/Editing/Commands.cpp
@@ -27,6 +27,7 @@
#include <LibWeb/HTML/HTMLHRElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLLIElement.h>
+#include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/HTMLTableElement.h>
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -2361,15 +2362,27 @@ bool command_insert_paragraph_action(DOM::Document& document, Utf16View)
// https://w3c.github.io/editing/docs/execCommand/#the-inserttext-command
static bool insert_text(DOM::Document& document, Utf16View value, InsertTextContext context)
{
- // 1. Delete the selection, with strip wrappers false.
auto& selection = *document.get_selection();
+ auto range = active_range(document);
+ if (auto* select = as_if<HTML::HTMLSelectElement>(*range->start_container()); select && select->is_editable()
+ && !select->is_inclusive_ancestor_of(range->end_container())) {
+ // AD-HOC: The spec deletes the selection first. But a replacement starting at a select's edge starts
+ // outside the control, and an interior position can't accept text, so reject it before deleting.
+ auto offset = range->start_offset();
+ if (offset != 0 && offset != select->child_count())
+ return true;
+ MUST(range->set_start(*select->parent(), select->index() + (offset != 0 ? 1 : 0)));
+ }
+
+ // 1. Delete the selection, with strip wrappers false.
auto replaces_selection = !selection.is_collapsed();
delete_the_selection(selection, true, false);
canonicalize_collapsed_selection_for_editing(selection);
// 2. If the active range's start node is neither editable nor an editing host, return true.
- auto range = active_range(document);
- if (!range->start_container()->is_editable_or_editing_host())
+ // NB: A control can itself be removed from an editor, but its content isn't an insertion target.
+ range = active_range(document);
+ if (!range->start_container()->is_editable_or_editing_host() || !range->start_container()->in_editable_subtree())
return true;
// 3. If value's length is greater than one:
diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp
index dc132b1f222..faaf847fbbd 100644
--- a/Libraries/LibWeb/HTML/HTMLElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLElement.cpp
@@ -43,7 +43,10 @@
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLLabelElement.h>
#include <LibWeb/HTML/HTMLObjectElement.h>
+#include <LibWeb/HTML/HTMLOptGroupElement.h>
+#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/HTML/HTMLParagraphElement.h>
+#include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/HTML/PopoverTargetAttributes.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -592,9 +595,48 @@ static void apply_white_space_processing_rules(Vector<RenderedTextItem>& items,
}
}
+// https://html.spec.whatwg.org/multipage/dom.html#rendered-text-collection-steps
+static void collect_select_rendered_text(DOM::Node const& node, Vector<RenderedTextItem>& items, bool inside_optgroup = false)
+{
+ // A select's light-DOM children have no layout nodes: Its shadow tree renders the control's labels instead.
+ // Collect the option text separately, retaining the required line breaks for empty options and groups.
+ node.for_each_child([&](auto const& child) {
+ if (auto const* option = as_if<HTMLOptionElement>(child)) {
+ items.append(RequiredLineBreakCount { 1 });
+ // AD-HOC: The spec calls for normal block boxes in options. Use their normalized text here, since the
+ // control renders labels rather than those boxes, and the label attribute isn't part of innerText.
+ items.append(option->text());
+ items.append(RequiredLineBreakCount { 1 });
+ } else if (is<HTMLOptGroupElement>(child)) {
+ if (!inside_optgroup) {
+ items.append(RequiredLineBreakCount { 1 });
+ collect_select_rendered_text(child, items, true);
+ items.append(RequiredLineBreakCount { 1 });
+ }
+ } else {
+ collect_select_rendered_text(child, items, inside_optgroup);
+ }
+ return IterationDecision::Continue;
+ });
+}
+
// https://html.spec.whatwg.org/multipage/dom.html#rendered-text-collection-steps
static Vector<RenderedTextItem> rendered_text_collection_steps(Layout::BegunRead const& read, DOM::Node const& node)
{
+ auto* layout_node = node.layout_node(read);
+ if (auto const* select = as_if<HTMLSelectElement>(node); select && layout_node) {
+ Vector<RenderedTextItem> items;
+ auto const& style_node = as<Layout::NodeWithStyle>(*layout_node);
+ if (style_node.visibility() == CSS::Visibility::Visible && style_node.content_visibility() != CSS::ContentVisibility::Hidden) {
+ if (layout_node->is_atomic_inline())
+ items.append(AtomicInlineStart {});
+ collect_select_rendered_text(*select, items);
+ if (layout_node->is_atomic_inline())
+ items.append(AtomicInlineEnd {});
+ }
+ return items;
+ }
+
// 1. Let items be the result of running the rendered text collection steps with each child node of node in tree
// order, and then concatenating the results to a single list.
Vector<RenderedTextItem> items;
@@ -609,10 +651,12 @@ static Vector<RenderedTextItem> rendered_text_collection_steps(Layout::BegunRead
// 3. If node is not being rendered, then return items.
// For the purpose of this step, the following elements must act as described
// if the computed value of the 'display' property is not 'none':
- // FIXME: - select elements have an associated non-replaced inline CSS box whose child boxes include only those of optgroup and option element child nodes;
- // FIXME: - optgroup elements have an associated non-replaced block-level CSS box whose child boxes include only those of option element child nodes; and
- // FIXME: - option elements have an associated non-replaced block-level CSS box whose child boxes are as normal for non-replaced block-level CSS boxes.
- auto* layout_node = node.layout_node(read);
+ // - select elements have an associated non-replaced inline CSS box whose child boxes include only those of
+ // optgroup and option element descendant nodes;
+ // - optgroup elements have an associated non-replaced block-level CSS box whose child boxes include only those
+ // of option element descendant nodes; and
+ // - option elements have an associated non-replaced block-level CSS box whose child boxes are as normal for
+ // non-replaced block-level CSS boxes.
if (!layout_node)
return items;
if (!layout_node->has_style_or_parent_with_style())
@@ -629,6 +673,14 @@ static Vector<RenderedTextItem> rendered_text_collection_steps(Layout::BegunRead
if (style_node.content_visibility() == CSS::ContentVisibility::Hidden)
return items;
+ if (auto const* option = as_if<HTMLOptionElement>(node)) {
+ items.clear();
+ items.append(RequiredLineBreakCount { 1 });
+ items.append(option->text());
+ items.append(RequiredLineBreakCount { 1 });
+ return items;
+ }
+
// 4. If node is a Text node, then for each CSS text box produced by node, in content order, compute the text of the
// box after application of the CSS 'white-space' processing rules and 'text-transform' rules, set items to the
// list of the resulting strings, and return items. The CSS 'white-space' processing rules are slightly modified:
@@ -693,19 +745,29 @@ Utf16String HTMLElement::get_the_text_steps()
if (!has_layout_box())
return descendant_text_content();
+ if (layout_node(read)->content_visibility() == CSS::ContentVisibility::Hidden)
+ return {};
+
+ if (auto const* option = as_if<HTMLOptionElement>(*this))
+ return option->text();
+
// 2. Let results be a new empty list.
Vector<RenderedTextItem> results;
- // 3. For each child node node of element:
- for_each_child([&](Node const& node) {
- // 1. Let current be the list resulting in running the rendered text collection steps with node.
- // Each item in results will either be a string or a positive integer (a required line break count).
- auto current = rendered_text_collection_steps(read, node);
-
- // 2. For each item item in current, append item to results.
- results.extend(move(current));
- return IterationDecision::Continue;
- });
+ if (is<HTMLSelectElement>(*this)) {
+ collect_select_rendered_text(*this, results);
+ } else {
+ // 3. For each child node node of element:
+ for_each_child([&](Node const& node) {
+ // 1. Let current be the list resulting in running the rendered text collection steps with node.
+ // Each item in results will either be a string or a positive integer (a required line break count).
+ auto current = rendered_text_collection_steps(read, node);
+
+ // 2. For each item item in current, append item to results.
+ results.extend(move(current));
+ return IterationDecision::Continue;
+ });
+ }
// NB: Apply the CSS 'white-space' processing rules to the text that remains unprocessed. The start and the end of
// the element only start and end lines if the element is not inline-level.
diff --git a/Tests/LibWeb/Text/expected/Editing/insert-text-select-boundary.txt b/Tests/LibWeb/Text/expected/Editing/insert-text-select-boundary.txt
new file mode 100644
index 00000000000..dd55070a5cb
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/insert-text-select-boundary.txt
@@ -0,0 +1,18 @@
+single select-start: <span>beforeX</span><span>ter</span>
+single select-middle: <span>before</span><select><option>one</option><option>two</option></select><span>after</span>
+single select-end: <span>before</span><select><option>one</option><option>two</option></select><span>Xter</span>
+single option-start: <span>before</span><select><option>one</option><option>two</option></select><span>after</span>
+single before-select: <span>beforeX</span><span>ter</span>
+single after-select: <span>before</span><select><option>one</option><option>two</option></select><span>Xter</span>
+multiple select-start: <span>beforeX</span><span>ter</span>
+multiple select-middle: <span>before</span><select multiple=""><option>one</option><option>two</option></select><span>after</span>
+multiple select-end: <span>before</span><select multiple=""><option>one</option><option>two</option></select><span>Xter</span>
+multiple option-start: <span>before</span><select multiple=""><option>one</option><option>two</option></select><span>after</span>
+multiple before-select: <span>beforeX</span><span>ter</span>
+multiple after-select: <span>before</span><select multiple=""><option>one</option><option>two</option></select><span>Xter</span>
+single spaced select-start: <span>beforeX</span><span>ter</span>
+single spaced select-middle: <span>before</span><select> <option>one</option><option>two</option> </select><span>after</span>
+single spaced select-end: <span>before</span><select> <option>one</option><option>two</option> </select><span>Xter</span>
+single spaced option-start: <span>before</span><select> <option>one</option><option>two</option> </select><span>after</span>
+single spaced before-select: <span>beforeX</span><span>ter</span>
+single spaced after-select: <span>before</span><select> <option>one</option><option>two</option> </select><span>Xter</span>
diff --git a/Tests/LibWeb/Text/expected/inner-text-option-elements.txt b/Tests/LibWeb/Text/expected/inner-text-option-elements.txt
new file mode 100644
index 00000000000..73c907a2e1d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/inner-text-option-elements.txt
@@ -0,0 +1,21 @@
+dropdown: "before \none\ntwo\n after"
+block select: "before\none\ntwo\nafter"
+listbox: "three\nfour"
+empty select: "ab"
+empty group: "a\nb"
+empty option: "a\nb"
+label and whitespace: "one two\nthree four"
+hidden options: "one\ntwo\nthree"
+hidden select: "ab"
+invisible select: "ab"
+standalone: "a\ngroup\none\nb\ntwo\nc"
+styled standalone option: "a\nb c\nd"
+wrapped options: "one\ntwo"
+standalone option text: "standalone option"
+select text: "one\ntwo"
+option display: block
+optgroup display: block
+multiple text: "one\ntwo"
+updated text: "changed\ntwo"
+invisible select text: "changed\ntwo"
+skipped select text: ""
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/the-innertext-and-outertext-properties/getter.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/the-innertext-and-outertext-properties/getter.txt
index 59efa888528..1006fdd5777 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/the-innertext-and-outertext-properties/getter.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/the-innertext-and-outertext-properties/getter.txt
@@ -1,9 +1,9 @@
Harness status: OK
-Found 272 tests
+Found 273 tests
-254 Pass
-18 Fail
+266 Pass
+7 Fail
Pass Simplest possible test ("<div>abc")
Pass Leading whitespace removed ("<div> abc")
Pass Trailing whitespace removed ("<div>abc ")
@@ -134,28 +134,29 @@ Pass <svg> text contents preserved ("<div><svg><text>abc</text></svg></div>")
Pass <svg><defs> text contents ignored ("<div><svg><defs><text>abc</text></defs></svg></div>")
Pass <svg> non-rendered text ignored ("<div><svg><stop>abc</stop></svg></div>")
Pass <foreignObject> contents preserved ("<svg><foreignObject><span id='target'>abc</span></foreignObject></svg>")
-Fail <select size='1'> contents of options preserved ("<select size='1'><option>abc</option><option>def")
-Fail <select size='2'> contents of options preserved ("<select size='2'><option>abc</option><option>def")
+Pass <select size='1'> contents of options preserved ("<select size='1'><option>abc</option><option>def")
+Pass <select size='2'> contents of options preserved ("<select size='2'><option>abc</option><option>def")
Pass <select size='1'> contents of target option preserved ("<select size='1'><option id='target'>abc</option><option>def")
Pass <select size='2'> contents of target option preserved ("<select size='2'><option id='target'>abc</option><option>def")
Pass empty <select> ("<div>a<select></select>bc")
-Fail empty <optgroup> in <select> ("<div>a<select><optgroup></select>bc")
-Fail empty <option> in <select> ("<div>a<select><option></select>bc")
+Pass empty <optgroup> in <select> ("<div>a<select><optgroup></select>bc")
+Pass empty <option> in <select> ("<div>a<select><option></select>bc")
Pass <select> containing text node child ("<select class='poke'></select>")
Pass <optgroup> containing <optgroup> ("<select><optgroup class='poke-optgroup'></select>")
-Fail <optgroup> containing <option> ("<select><optgroup><option>abc</select>")
+Pass <optgroup> containing <option> ("<select><optgroup><option>abc</select>")
Fail <div> in <option> ("<select><option class='poke-div'>123</select>")
-Fail empty <optgroup> in <div> ("<div>a<optgroup></optgroup>bc")
-Fail <optgroup> in <div> ("<div>a<optgroup>123</optgroup>bc")
-Fail empty <option> in <div> ("<div>a<option></option>bc")
-Fail <option> in <div> ("<div>a<option>123</option>bc")
+Pass empty <optgroup> in <div> ("<div>a<optgroup></optgroup>bc")
+Pass <optgroup> in <div> ("<div>a<optgroup>123</optgroup>bc")
+Pass empty <option> in <div> ("<div>a<option></option>bc")
+Pass <option> in <div> ("<div>a<option>123</option>bc")
+Pass undefined ("<select><option>one</option><div><optgroup label=optgroup><div><option><span>two")
Pass <button> contents preserved ("<div><button>abc")
Pass <fieldset> contents preserved ("<div><fieldset>abc")
Pass <fieldset> <legend> contents preserved ("<div><fieldset><legend>abc")
Pass <input> contents ignored ("<div><input type='text' value='abc'>")
Pass <textarea> contents ignored ("<div><textarea>abc")
-Fail <select size='1'> contents of options preserved ("<div><select size='1'><option>abc</option><option>def")
-Fail <select size='2'> contents of options preserved ("<div><select size='2'><option>abc</option><option>def")
+Pass <select size='1'> contents of options preserved ("<div><select size='1'><option>abc</option><option>def")
+Pass <select size='2'> contents of options preserved ("<div><select size='2'><option>abc</option><option>def")
Pass <iframe> contents ignored ("<div><iframe>abc")
Pass <iframe> subdocument ignored ("<div><iframe src='data:text/html,abc'>")
Pass <audio> contents ignored ("<div><audio>abc")
diff --git a/Tests/LibWeb/Text/input/Editing/insert-text-select-boundary.html b/Tests/LibWeb/Text/input/Editing/insert-text-select-boundary.html
new file mode 100644
index 00000000000..759f1e6b2ee
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/insert-text-select-boundary.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+test(() => {
+ for (const [multiple, spaced] of [[false, false], [true, false], [false, true]]) {
+ for (const start of ["select-start", "select-middle", "select-end", "option-start", "before-select", "after-select"]) {
+ const editor = document.createElement("div");
+ editor.contentEditable = "true";
+ editor.innerHTML = "<span>before</span><select" + (multiple ? " multiple" : "") + ">" + (spaced ? " " : "") + "<option>one</option><option>two</option>" + (spaced ? " " : "") + "</select><span>after</span>";
+ document.body.append(editor);
+ editor.focus();
+ const select = editor.querySelector("select");
+ const after = editor.lastChild.firstChild;
+ const range = new Range();
+ if (start === "select-start") range.setStart(select, 0);
+ if (start === "select-middle") range.setStart(select, select.childNodes.length / 2);
+ if (start === "select-end") range.setStart(select, select.childNodes.length);
+ if (start === "option-start") range.setStart(select.options[0].firstChild, 0);
+ if (start === "before-select") range.setStartBefore(select);
+ if (start === "after-select") range.setStartAfter(select);
+ range.setEnd(after, 2);
+ getSelection().removeAllRanges();
+ getSelection().addRange(range);
+ document.execCommand("insertText", false, "X");
+ println((multiple ? "multiple " : "single ") + (spaced ? "spaced " : "") + start + ": " + editor.innerHTML);
+ editor.remove();
+ }
+ }
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/inner-text-option-elements.html b/Tests/LibWeb/Text/input/inner-text-option-elements.html
new file mode 100644
index 00000000000..73a91c3f4de
--- /dev/null
+++ b/Tests/LibWeb/Text/input/inner-text-option-elements.html
@@ -0,0 +1,54 @@
+<!doctype html>
+<script src="include.js"></script>
+<style>#out { position: absolute; }</style>
+<script>
+test(() => {
+ function check(name, markup, setup) {
+ const div = document.createElement("div");
+ div.innerHTML = markup;
+ document.body.append(div);
+ if (setup) setup(div);
+ println(name + ": " + JSON.stringify(div.innerText));
+ div.remove();
+ }
+ check("dropdown", "before <select><option>one</option><optgroup label=group><option>two</option></optgroup></select> after");
+ check("block select", "before <select style='display:block'><option>one</option><option>two</option></select> after");
+ check("listbox", "<select multiple size=3><option>three</option><option>four</option></select>");
+ check("empty select", "a<select></select>b");
+ check("empty group", "a<select><optgroup></optgroup></select>b");
+ check("empty option", "a<select><option></option></select>b");
+ check("label and whitespace", "<select><option label=label> one two </option><option style='white-space:pre;text-transform:uppercase'> three four </option></select>");
+ check("hidden options", "<select><option style='display:none'>one</option><optgroup style='display:none'><option>two</option></optgroup><option style='visibility:hidden'>three</option></select>");
+ check("hidden select", "a<select style='display:none'><option>one</option></select>b");
+ check("invisible select", "a<select style='visibility:hidden'><option>one</option></select>b");
+ check("standalone", "a<optgroup>group<option>one</option></optgroup>b<option>two</option>c");
+ check("styled standalone option", "a<option style='text-transform:uppercase;white-space:pre'> b c </option>d");
+ check("wrapped options", "<select></select>", div => {
+ const select = div.firstChild;
+ select.innerHTML = "<option>one</option>";
+ const wrapper = document.createElement("div");
+ wrapper.innerHTML = "ignored<optgroup label=group><option>two</option></optgroup>";
+ select.append(wrapper);
+ });
+ const standalone = document.createElement("option");
+ standalone.textContent = " standalone option ";
+ document.body.append(standalone);
+ println("standalone option text: " + JSON.stringify(standalone.innerText));
+ standalone.remove();
+ const select = document.createElement("select");
+ select.innerHTML = "<option>one</option><optgroup><option>two</option></optgroup>";
+ document.body.append(select);
+ println("select text: " + JSON.stringify(select.innerText));
+ println("option display: " + getComputedStyle(select.firstChild).display);
+ println("optgroup display: " + getComputedStyle(select.lastChild).display);
+ select.multiple = true;
+ println("multiple text: " + JSON.stringify(select.innerText));
+ select.options[0].text = "changed";
+ println("updated text: " + JSON.stringify(select.innerText));
+ select.style.visibility = "hidden";
+ println("invisible select text: " + JSON.stringify(select.innerText));
+ select.style.contentVisibility = "hidden";
+ println("skipped select text: " + JSON.stringify(select.innerText));
+ select.remove();
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/the-innertext-and-outertext-properties/getter-tests.js b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/the-innertext-and-outertext-properties/getter-tests.js
index d9b5b063732..f4df99e27f4 100644
--- a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/the-innertext-and-outertext-properties/getter-tests.js
+++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/the-innertext-and-outertext-properties/getter-tests.js
@@ -201,10 +201,12 @@ testText("<select><optgroup class='poke-optgroup'></select>", "", "<optgroup> co
testText("<select><optgroup><option>abc</select>", "abc", "<optgroup> containing <option>");
testText("<select><option class='poke-div'>123</select>", "123\nabc", "<div> in <option>");
testText("<div>a<optgroup></optgroup>bc", "a\nbc", "empty <optgroup> in <div>");
-testText("<div>a<optgroup>123</optgroup>bc", "a\nbc", "<optgroup> in <div>");
+testText("<div>a<optgroup>123</optgroup>bc", "a\n123\nbc", "<optgroup> in <div>");
testText("<div>a<option></option>bc", "a\nbc", "empty <option> in <div>");
testText("<div>a<option>123</option>bc", "a\n123\nbc", "<option> in <div>");
+testText("<select><option>one</option><div><optgroup label=optgroup><div><option><span>two", "one\ntwo");
+
/**** innerText on replaced element children ****/
testText("<div><button>abc", "abc", "<button> contents preserved");