Skip to content

Truly independent web browser

2.0 KB
Raw Blame
<!doctype html>
<script src="../include.js"></script>
<style>
@font-face {
    font-family: lato;
    src: url("../../Assets/Lato-Bold.ttf");
}
#text {
    font: 36px lato;
    padding: 0;
    border: 0;
    margin: 0;
}
</style>
<!-- This text has an "ffi" ligature in the Lato Bold font. -->
<div id="text" contenteditable>office</div>
<script>
promiseTest(async () => {
    await document.fonts.ready;

    const div = document.getElementById("text");
    div.focus();

    const text_node = div.firstChild;
    const range = document.createRange();
    const rect_of = (start, end) => {
        range.setStart(text_node, start);
        range.setEnd(text_node, end);
        return range.getBoundingClientRect();
    };

    const o_rect = rect_of(0, 1);
    const ligature_rect = rect_of(1, 4);
    const c_rect = rect_of(4, 5);
    const e_rect = rect_of(5, 6);

    const click_y = o_rect.top + o_rect.height / 2;

    const click = (x, label) => {
        internals.click(x, click_y);
        println(`${label}: focusOffset=${getSelection().focusOffset}`);
    };

    // Click a quarter of the way into each character's rect so the click is unambiguously closer to the character's
    // left edge, avoiding any tie-breaking behavior.
    click(o_rect.left + o_rect.width * 0.25, "left quarter of 'o'");
    click(c_rect.left + c_rect.width * 0.25, "left quarter of 'c'");
    click(e_rect.left + e_rect.width * 0.25, "left quarter of 'e'");

    // The "ffi" ligature renders as a single glyph covering three graphemes.
    // Click a quarter of the way into each third of the ligature to verify that hit testing snaps to each interior
    // grapheme boundary instead of jumping to the cluster start or end.
    const third = ligature_rect.width / 3;
    click(ligature_rect.left + third * 0.25, "left quarter of first 'f' (in ligature)");
    click(ligature_rect.left + third + third * 0.25, "left quarter of second 'f' (in ligature)");
    click(ligature_rect.left + third * 2 + third * 0.25, "left quarter of 'i' (in ligature)");
});
</script>