LibWeb: Implement the "bold" editing command

This commit is contained in:
Jelle Raaijmakers 2025-01-07 14:15:09 +01:00 committed by Andreas Kling
commit a71e999ac8
Notes: github-actions[bot] 2025-01-10 22:37:28 +00:00
6 changed files with 64 additions and 40 deletions

View file

@ -0,0 +1,34 @@
<script src="../include.js"></script>
<div contenteditable="true">foobar</div>
<script>
test(() => {
const range = document.createRange();
getSelection().addRange(range);
const divElm = document.querySelector('div');
const printableSelection = () => {
let activeRange = getSelection().getRangeAt(0);
return `${activeRange.startContainer.nodeName} ${activeRange.startOffset} ${activeRange.endContainer.nodeName} ${activeRange.endOffset}`;
};
const report = () => println(`Div contents: "${divElm.innerHTML}" state: ${document.queryCommandState('bold')} selection: ${printableSelection()}`);
report();
// Make 'bar' bold
range.setStart(divElm.childNodes[0], 3);
range.setEnd(divElm.childNodes[0], 6);
document.execCommand('bold');
report();
// Make 'foo' bold
range.setStart(divElm.childNodes[0], 0);
range.setEnd(divElm.childNodes[0], 3);
document.execCommand('bold');
report();
// Deboldify 'bar'
range.setStart(divElm.childNodes[0].childNodes[1], 0);
range.setEnd(divElm.childNodes[0].childNodes[1], 3);
document.execCommand('bold');
report();
});
</script>

View file

@ -1,32 +0,0 @@
<script src="../include.js"></script>
<div id="text" contenteditable="true"></div>
<script>
test(() => {
let text = document.getElementById("text");
text.focus();
let hello = text.appendChild(document.createTextNode("Hello, world!"));
let boldText = document.createTextNode("I'm totally bold rn");
let boldTag = text.appendChild(document.createElement("b"));
boldTag.appendChild(boldText);
// select the text
let range = document.createRange();
range.selectNodeContents(hello);
let selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
let e = document.execCommand("bold", false, "on");
println(`execCommand("bold") returned ${e}`);
let allTheText = text.innerHTML;
println(allTheText);
println(`queryCommandEnabled("bold") returned ${document.queryCommandEnabled("bold")}`);
println(`queryCommandIndeterm("bold") returned ${document.queryCommandIndeterm("bold")}`);
println(`queryCommandState("bold") returned ${document.queryCommandState("bold")}`);
println(`queryCommandSupported("bold") returned ${document.queryCommandSupported("bold")}`);
println(`queryCommandValue("bold") returned "${document.queryCommandValue("bold")}"`);
});
</script>