Skip to content

Truly independent web browser

LibWeb: Disable editing state commands outside of editable content

Andreas Kling15e89ab6a5a4
diff --git a/Libraries/LibWeb/Editing/ExecCommand.cpp b/Libraries/LibWeb/Editing/ExecCommand.cpp
index f801be22a0d..c04b4e56f43 100644
--- a/Libraries/LibWeb/Editing/ExecCommand.cpp
+++ b/Libraries/LibWeb/Editing/ExecCommand.cpp
@@ -421,11 +421,15 @@ static bool command_is_enabled(Document& document, Utf16FlyString const& command
// Among commands defined in this specification, those listed in Miscellaneous commands are always enabled, except
// for the cut command and the paste command.
// NOTE: cut and paste are actually in the Clipboard commands section
+ // INTEROP: Browsers do not enable these commands if the active range's start node is neither editable nor an
+ // editing host.
if (command.is_one_of_ignoring_ascii_case(
Editing::CommandNames::defaultParagraphSeparator,
Editing::CommandNames::styleWithCSS,
- Editing::CommandNames::useCSS))
- return true;
+ Editing::CommandNames::useCSS)) {
+ auto active_range = Editing::active_range(document);
+ return !active_range || active_range->start_container()->is_editable_or_editing_host();
+ }
// INTEROP: The spec lists undo and redo among the always-enabled miscellaneous commands, but in Chromium
// queryCommandEnabled("undo") is true only when there is a step to undo, and execCommand("undo") on an
diff --git a/Tests/LibWeb/Text/expected/wpt-import/editing/other/exec-command-without-editable-element.tentative.txt b/Tests/LibWeb/Text/expected/wpt-import/editing/other/exec-command-without-editable-element.tentative.txt
index 8f9e97d3b14..c49c9dc06c7 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/editing/other/exec-command-without-editable-element.tentative.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/editing/other/exec-command-without-editable-element.tentative.txt
@@ -1,9 +1,9 @@
Harness status: OK
-Found 384 tests
+Found 382 tests
-360 Pass
-24 Fail
+378 Pass
+4 Fail
Pass ChildDocument.execCommand(bold, false, bold) with a[b]c: calling execCommand
Pass ChildDocument.execCommand(bold, false, bold) with a[b]c: checking event on focused document
Pass ChildDocument.execCommand(bold, false, bold) with a[b]c: checking event on executed document
@@ -174,25 +174,24 @@ Pass ChildDocument.execCommand(formatblock, false, div) with a[b]c: checking eve
Pass ChildDocument.execCommand(formatblock, false, div) with a[b]c: checking event on executed document
Pass ChildDocument.execCommand(formatblock, false, div) with a[b]c: checking result content in focused document
Pass ChildDocument.execCommand(formatblock, false, div) with a[b]c: checking result content in executed document
-Fail ChildDocument.execCommand(styleWithCSS, false, true): calling execCommand to initialize
+Pass ChildDocument.execCommand(styleWithCSS, false, true): calling execCommand to initialize
Pass ChildDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document after initializing
Pass ChildDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on focus document after initializing
-Fail ChildDocument.execCommand(styleWithCSS, false, true): calling execCommand to set state
-Fail ChildDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document
+Pass ChildDocument.execCommand(styleWithCSS, false, true): calling execCommand to set state
+Pass ChildDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document
Pass ChildDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on focused document
-Fail ChildDocument.execCommand(styleWithCSS, false, true): calling queryCommandValue on executed document
-Fail ChildDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document after making executed document editable
-Fail ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling execCommand to initialize
+Pass ChildDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document after making executed document editable
+Pass ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling execCommand to initialize
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on executed document after initializing
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on focus document after initializing
-Fail ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling execCommand to set state
+Pass ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling execCommand to set state
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on executed document
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on focused document
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on executed document after making executed document editable
-Fail ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling execCommand to initialize
+Pass ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling execCommand to initialize
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on executed document after initializing
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on focus document after initializing
-Fail ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling execCommand to set state
+Pass ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling execCommand to set state
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on executed document
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on focused document
Pass ChildDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on executed document after making executed document editable
@@ -366,25 +365,24 @@ Pass ParentDocument.execCommand(formatblock, false, div) with a[b]c: checking ev
Pass ParentDocument.execCommand(formatblock, false, div) with a[b]c: checking event on executed document
Pass ParentDocument.execCommand(formatblock, false, div) with a[b]c: checking result content in focused document
Pass ParentDocument.execCommand(formatblock, false, div) with a[b]c: checking result content in executed document
-Fail ParentDocument.execCommand(styleWithCSS, false, true): calling execCommand to initialize
+Pass ParentDocument.execCommand(styleWithCSS, false, true): calling execCommand to initialize
Pass ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document after initializing
-Fail ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on focus document after initializing
-Fail ParentDocument.execCommand(styleWithCSS, false, true): calling execCommand to set state
-Fail ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document
-Fail ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on focused document
-Fail ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandValue on executed document
-Fail ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document after making executed document editable
-Fail ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling execCommand to initialize
+Pass ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on focus document after initializing
+Pass ParentDocument.execCommand(styleWithCSS, false, true): calling execCommand to set state
+Pass ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document
+Pass ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on focused document
+Pass ParentDocument.execCommand(styleWithCSS, false, true): calling queryCommandState on executed document after making executed document editable
+Pass ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling execCommand to initialize
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on executed document after initializing
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on focus document after initializing
-Fail ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling execCommand to set state
+Pass ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling execCommand to set state
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on executed document
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on focused document
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, div): calling queryCommandState on executed document after making executed document editable
-Fail ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling execCommand to initialize
+Pass ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling execCommand to initialize
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on executed document after initializing
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on focus document after initializing
-Fail ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling execCommand to set state
+Pass ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling execCommand to set state
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on executed document
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on focused document
Pass ParentDocument.execCommand(defaultParagraphSeparator, false, p): calling queryCommandState on executed document after making executed document editable
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-styleWithCSS.html b/Tests/LibWeb/Text/input/Editing/execCommand-styleWithCSS.html
index 58f1a194bb6..e38dadf6979 100644
--- a/Tests/LibWeb/Text/input/Editing/execCommand-styleWithCSS.html
+++ b/Tests/LibWeb/Text/input/Editing/execCommand-styleWithCSS.html
@@ -3,10 +3,11 @@
<div contenteditable="true">foobar</div>
<script>
test(() => {
+ const divElm = document.querySelector('div');
const range = document.createRange();
+ range.setStart(divElm, 0);
getSelection().addRange(range);
- const divElm = document.querySelector('div');
const report = () => println(`Div contents: "${divElm.innerHTML}" bold state: ${document.queryCommandState('bold')}`);
report();
diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-useCSS.html b/Tests/LibWeb/Text/input/Editing/execCommand-useCSS.html
index 450544be737..9123fbf5490 100644
--- a/Tests/LibWeb/Text/input/Editing/execCommand-useCSS.html
+++ b/Tests/LibWeb/Text/input/Editing/execCommand-useCSS.html
@@ -3,10 +3,11 @@
<div contenteditable="true">foobar</div>
<script>
test(() => {
+ const divElm = document.querySelector('div');
const range = document.createRange();
+ range.setStart(divElm, 0);
getSelection().addRange(range);
- const divElm = document.querySelector('div');
const report = () => println(`Div contents: "${divElm.innerHTML}" bold state: ${document.queryCommandState('bold')}`);
report();