Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

3.3 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<div id="target" contenteditable style="width:200px;height:200px">hello world</div>
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
<script>
const target = document.getElementById("target");
const events = [];
let phase = "plain";
let inputCount = 0;
const expectedInputs = 2;

function selectWorld() {
  const text = target.firstChild;
  const selection = getSelection();
  selection.removeAllRanges();
  selection.setBaseAndExtent(text, 6, text, 11);
}

function resetTarget() {
  target.textContent = "hello world";
  selectWorld();
  target.focus();
}

function selectionSummary() {
  const selection = getSelection();
  if (selection.rangeCount === 0) {
    return "none";
  }
  const anchor = selection.anchorNode === target ? "target" : selection.anchorNode.nodeName;
  const focus = selection.focusNode === target ? "target" : selection.focusNode.nodeName;
  return anchor + ":" + selection.anchorOffset + ":" + focus + ":" + selection.focusOffset;
}

function selectedText() {
  const selection = getSelection();
  if (selection.rangeCount === 0) {
    return "";
  }
  return selection.getRangeAt(0).toString();
}

function richNodeSummary() {
  const bold = target.querySelector("b");
  const italic = target.querySelector("i");
  return (
    (bold ? bold.textContent : "missing") +
    ":" +
    (italic ? italic.textContent : "missing")
  );
}

window.__setupContenteditableDropPhase = function (nextPhase) {
  phase = nextPhase;
  resetTarget();
};

resetTarget();

const dropped = new Promise(function (resolve) {
  target.addEventListener("drop", function (event) {
    events.push(
      "drop:" +
        phase +
        ":" +
        event.dataTransfer.getData("text") +
        ":" +
        event.dataTransfer.getData("text/html") +
        ":" +
        target.textContent +
        ":" +
        selectedText(),
    );
  });

  target.addEventListener("beforeinput", function () {
    events.push("beforeinput:" + phase + ":" + target.textContent + ":" + selectedText());
  });

  target.addEventListener("input", function () {
    events.push(
      "input:" +
        phase +
        ":" +
        target.textContent +
        ":" +
        selectionSummary() +
        ":" +
        richNodeSummary(),
    );
    inputCount += 1;
    if (inputCount === expectedInputs) {
      resolve();
    }
  });
});

promise_test(async function () {
  await dropped;
  assert_array_equals(
    events,
    [
      "drop:plain:moli::hello world:world",
      "beforeinput:plain:hello world:world",
      "input:plain:hello moli:#text:10:#text:10:missing:missing",
      "drop:html:fallback:<b>rich</b> <i>html</i>:hello world:world",
      "beforeinput:html:hello world:world",
      "input:html:hello rich html:target:4:target:4:rich:html",
    ],
    "contenteditable drop default action should replace selected content and dispatch input",
  );
  assert_equals(target.textContent, "hello rich html", "HTML drop text was inserted");
  assert_equals(target.querySelector("b").textContent, "rich", "HTML drop preserves bold element");
  assert_equals(target.querySelector("i").textContent, "html", "HTML drop preserves italic element");
}, "Input.dispatchDragEvent runs text drop default action for contenteditable hosts");
</script>