chromium/third_party/blink/web_tests/editing/pasteboard/smart_paste.html

<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../assert_selection.js"></script>
<script>
function doubleClick(x, y) {
    eventSender.leapForward(1000);
    eventSender.mouseMoveTo(x, y);
    eventSender.mouseDown();
    eventSender.mouseUp();
    eventSender.mouseDown();
    eventSender.mouseUp();
}

function doubleClickIn(selection, selector) {
    const element = selection.document.querySelector(selector);
    const mouseX = element.offsetLeft + 5 + selection.document.offsetLeft;
    const mouseY = element.offsetTop + 5 + selection.document.offsetTop;
    doubleClick(mouseX, mouseY);
}

test(() => {
    assert_not_equals(window.eventSender, undefined,
        'This test requires eventSender');

    assert_not_equals(window.internals, undefined,
        'This test requires internals');

    internals.settings.setSmartInsertDeleteEnabled(true);

    [true, false].forEach(selectTrailingWhitespaceEnabled => {
        internals.settings.setSelectTrailingWhitespaceEnabled(selectTrailingWhitespaceEnabled);
        const withOrWithout = selectTrailingWhitespaceEnabled ? 'with' : 'without';

        assert_selection(
            '<div contenteditable>test</div>',
            selection => {
                doubleClickIn(selection, 'div');
                selection.document.execCommand('copy');
                selection.modify('move', 'forward', 'character');
                selection.document.execCommand('paste');
            },
            '<div contenteditable>test\u{00A0}test|</div>',
            `1 Smart paste at end of word ${withOrWithout} trailing whitespace`);

        assert_selection(
            '<div contenteditable>test</div>',
            selection => {
                doubleClickIn(selection, 'div');
                selection.document.execCommand('copy');
                selection.modify('move', 'backward', 'word');
                selection.document.execCommand('paste');
            },
            '<div contenteditable>test |test</div>',
            `2 Smart paste at start of word ${withOrWithout} trailing whitespace`);

        assert_selection(
            '<div contenteditable>foo bar</div>',
            selection => {
                doubleClickIn(selection, 'div');
                selection.document.execCommand('copy');
                selection.modify('move', 'forward', 'character');
                if (selectTrailingWhitespaceEnabled)
                    selection.modify('move', 'backward', 'character');
                selection.document.execCommand('paste');
            },
            selectTrailingWhitespaceEnabled
                ? '<div contenteditable>foo\u{00A0}foo\u{00A0}|\u{00A0}bar</div>'
                : '<div contenteditable>foo\u{00A0}foo|\u{00A0}bar</div>',
            `3 Smart paste when pasting after a word and before a space and another word ${withOrWithout} trailing whitespace`);

        assert_selection(
            '<div contenteditable>foo bar</div>',
            selection => {
                doubleClickIn(selection, 'div');
                selection.document.execCommand('copy');
                selection.modify('move', 'forward', 'character');
                if (!selectTrailingWhitespaceEnabled)
                    selection.modify('move', 'forward', 'character');
                selection.document.execCommand('paste');
            },
            selectTrailingWhitespaceEnabled
                ? '<div contenteditable>foo foo\u{00A0} |bar</div>'
                : '<div contenteditable>foo foo |bar</div>',
            `4 Smart paste when pasting after word and a space before another word ${withOrWithout} trailing whitespace`);

        assert_selection(
            '<div contenteditable>abcd</div>',
            selection => {
                doubleClickIn(selection, 'div');
                selection.document.execCommand('copy');
                selection.modify('move', 'backward', 'word');
                selection.modify('move', 'forward', 'character');
                selection.document.execCommand('paste');
            },
            '<div contenteditable>a\u{00A0}abcd |bcd</div>',
            `5 Smart paste when pasting in the middle of a word ${withOrWithout} trailing whitespace`);

        assert_selection(
            '<div contenteditable>abcd</div>',
            selection => {
                doubleClickIn(selection, 'div');
                selection.document.execCommand('delete');
                selection.document.execCommand('insertText', false, '--');
                selection.modify('move', 'backward', 'character');
                selection.document.execCommand('paste');
            },
            '<div contenteditable>-abcd|-</div>',
            `6 Smart paste when pasting between smart paste exempt characters ${withOrWithout} trailing whitespace`);

        assert_selection(
            '<div contenteditable>abcd</div>',
            selection => {
                doubleClickIn(selection, 'div');
                selection.document.execCommand('delete');
                selection.document.execCommand('insertText', false, '..');
                selection.modify('move', 'backward', 'character');
                selection.document.execCommand('paste');
            },
            '<div contenteditable>.\u{00A0}abcd|.</div>',
            `7 Smart paste when pasting between two periods ${withOrWithout} trailing whitespace`);
    });
}, 'Smart Paste');
</script>