Skip to content

Truly independent web browser

4.0 KB
Raw Blame
<!DOCTYPE html>
<script src="include.js"></script>
<style>
    body {
        font: 20px SerenitySans, sans-serif;
        line-height: 32px;
    }
</style>
<div id="word">ordinary</div>
<div id="words">two words</div>
<div id="nbsp">two&nbsp;words</div>
<div id="url">ladybird.org/path?x=1</div>
<div id="prevented">prevented</div>
<div id="existing">existing selection</div>
<div id="other">other target</div>
<a id="anchor" href="https://example.com/">anchor selection</a>
<img id="image" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==" style="width: 20px; height: 20px;">
<div id="none" style="user-select: none;">hidden.org</div>
<script>
    function rightClick(element) {
        const rect = element.getBoundingClientRect();
        rightClickAt(rect.left + 8, rect.top + rect.height / 2);
    }

    function rightClickAt(x, y) {
        internals.click(x, y, 1, internals.BUTTON_RIGHT);
    }

    function textRect(node, start, end) {
        const range = document.createRange();
        range.setStart(node, start);
        range.setEnd(node, end);
        return range.getBoundingClientRect();
    }

    function selectedText() {
        return internals.selectedTextForClipboard();
    }

    test(() => {
        rightClick(word);
        println(`plain word: "${selectedText()}"`);

        getSelection().removeAllRanges();
        const wordRect = textRect(word.firstChild, 0, 8);
        rightClickAt(wordRect.right + 20, wordRect.top + wordRect.height / 2);
        println(`next to word: "${selectedText()}"`);

        getSelection().removeAllRanges();
        const lastGlyphRect = textRect(words.firstChild, 2, 3);
        rightClickAt(lastGlyphRect.right - 2, lastGlyphRect.top + lastGlyphRect.height / 2);
        println(`end of word: "${selectedText()}"`);

        getSelection().removeAllRanges();
        const spaceRect = textRect(words.firstChild, 3, 4);
        rightClickAt(spaceRect.left + spaceRect.width / 2, spaceRect.top + spaceRect.height / 2);
        println(`between words: "${selectedText()}"`);

        getSelection().removeAllRanges();
        const nbspRect = textRect(nbsp.firstChild, 3, 4);
        rightClickAt(nbspRect.left + nbspRect.width / 2, nbspRect.top + nbspRect.height / 2);
        println(`between words with a non-breaking space: "${selectedText()}"`);

        getSelection().removeAllRanges();
        const lineRect = word.getBoundingClientRect();
        rightClickAt(wordRect.left + 8, lineRect.top + 1);
        println(`above glyphs within line box: "${selectedText()}"`);

        getSelection().removeAllRanges();
        rightClick(url);
        println(`url token: "${selectedText()}"`);

        getSelection().removeAllRanges();
        prevented.addEventListener("contextmenu", event => event.preventDefault());
        rightClick(prevented);
        println(`canceled context menu: "${selectedText()}"`);

        const range = document.createRange();
        range.setStart(existing.firstChild, 0);
        range.setEnd(existing.firstChild, 8);
        getSelection().removeAllRanges();
        getSelection().addRange(range);
        rightClick(existing);
        println(`existing selection preserved: "${selectedText()}"`);

        getSelection().removeAllRanges();
        getSelection().addRange(range);
        rightClick(other);
        println(`existing selection replaced: "${selectedText()}"`);

        const anchorRange = document.createRange();
        anchorRange.setStart(anchor.firstChild, 0);
        anchorRange.setEnd(anchor.firstChild, 6);
        getSelection().removeAllRanges();
        getSelection().addRange(anchorRange);
        rightClick(anchor);
        println(`anchor selection preserved: "${selectedText()}"`);

        getSelection().removeAllRanges();
        getSelection().addRange(range);
        rightClick(image);
        println(`image preserves selection: "${selectedText()}"`);

        getSelection().removeAllRanges();
        rightClick(none);
        println(`user-select none: "${selectedText()}"`);
    });
</script>