summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/uievents/textInput/support/common.js
blob: 25c39da3e2b66da3e8fd15f88a4a1a47b1433ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function elDesc(el) {
  let rv = `<${el.localName}`;
  if (el.hasAttribute('contenteditable')) {
    rv += ` contenteditable="${el.getAttribute('contenteditable')}"`;
  }
  if (el.hasAttribute('type')) {
    rv += ` type="${el.getAttribute('type')}"`;
  }
  rv += `>`;
  return rv;
}

function setSelection(el, selectionStart, selectionEnd) {
  if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
    el.selectionStart = selectionStart;
    el.selectionEnd = selectionEnd;
  } else {
    const s = getSelection();
    s.removeAllRanges();
    const r = new Range();
    r.setStart(el.firstChild || el, selectionStart);
    r.setEnd(el.firstChild || el, selectionEnd);
    s.addRange(r);
  }
}

function getValue(el) {
  if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
    return el.value;
  }
  return el.innerHTML;
}

const keyMapping = {
  "Enter": "\uE006",
  "Backspace": "\uE003",
  "Delete": "\uE017",
};