2025-03-18 19:28:35 +01:00
|
|
|
<!DOCTYPE html>
|
2025-01-10 13:28:32 +01:00
|
|
|
<script src="../include.js"></script>
|
|
|
|
|
<div contenteditable="true">foobar</div>
|
|
|
|
|
<script>
|
|
|
|
|
test(() => {
|
2025-07-23 11:43:21 +02:00
|
|
|
const selection = getSelection();
|
|
|
|
|
const reportSelection = () => {
|
|
|
|
|
if (selection.rangeCount === 0) {
|
|
|
|
|
println('No range.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const range = selection.getRangeAt(0);
|
|
|
|
|
println(`${range.startContainer.nodeName} ${range.startOffset} - ${range.endContainer.nodeName} ${range.endOffset}`);
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-10 13:28:32 +01:00
|
|
|
var divElm = document.querySelector('div');
|
2025-01-23 10:50:00 +01:00
|
|
|
divElm.addEventListener('input', (e) => println('input triggered'));
|
2025-01-10 13:28:32 +01:00
|
|
|
|
|
|
|
|
// Put cursor between 'foo' and 'bar'
|
2025-07-23 11:43:21 +02:00
|
|
|
selection.setBaseAndExtent(divElm.childNodes[0], 3, divElm.childNodes[0], 3);
|
2025-01-10 13:28:32 +01:00
|
|
|
|
|
|
|
|
// Insert text
|
|
|
|
|
document.execCommand('insertText', false, 'baz');
|
2025-07-23 11:43:21 +02:00
|
|
|
reportSelection();
|
|
|
|
|
|
|
|
|
|
// Insert Unicode
|
|
|
|
|
document.execCommand('insertText', false, '🙂');
|
|
|
|
|
reportSelection();
|
2025-01-10 13:28:32 +01:00
|
|
|
|
|
|
|
|
println(divElm.innerHTML);
|
|
|
|
|
});
|
|
|
|
|
</script>
|