chromium/third_party/blink/web_tests/editing/selection/selection-update-range-after-addrange.html

<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<p>abcdef</p>
<script>
test(() => {
  let selection = getSelection();
  selection.removeAllRanges();
  let range = document.createRange();
  let text = document.querySelector('p').firstChild;
  range.setStart(text, 1);
  range.setEnd(text, 2);

  selection.addRange(range);
  assert_equals(selection.anchorNode, text);
  assert_equals(selection.anchorOffset, 1);
  assert_equals(selection.focusNode, text);
  assert_equals(selection.focusOffset, 2);

  range.setStart(text, 0);
  assert_equals(selection.anchorOffset, 0);
}, 'Mutation of Range after adding it to Selection should update Selection attributes.');

test(() => {
  let selection = getSelection();
  selection.removeAllRanges();
  let range = document.createRange();
  let text = document.querySelector('p').firstChild;
  range.setStart(text, 1);
  range.setEnd(text, 2);

  selection.addRange(range);
  assert_equals(selection.anchorNode, text);
  assert_equals(selection.anchorOffset, 1);
  assert_equals(selection.focusNode, text);
  assert_equals(selection.focusOffset, 2);

  text.parentNode.contentEditable = true;
  range.setStart(text, 0);
  assert_equals(selection.anchorOffset, 0);
  assert_not_equals(document.activeElement, text.parentNode);
}, 'Mutation of Range after adding it to Selection should not focus on editable anchor.');

test(() => {
  let selection = getSelection();
  selection.removeAllRanges();
  let range = document.createRange();
  range.selectNode(document.body);
  selection.addRange(range);
  assert_equals(selection.rangeCount, 1);

  let document2 = document.implementation.createHTMLDocument();
  document2.innerHTML = '<html><body>abc</body></html>';
  range.selectNode(document2.body);
  // TODO(tkent): The specification says nothing about such case. For now, we
  // unregister the Range from the Selection for ease of implementation
  // though Firefox and Edge keep |selection.getRangeAt(0) == range|.
  assert_equals(selection.rangeCount, 0);
}, 'Switching Range document should clear registered Selection.');

test(() => {
  let selection = getSelection();
  selection.removeAllRanges();
  let range = document.createRange();
  range.selectNode(document.body);
  selection.addRange(range);
  assert_equals(selection.rangeCount, 1);

  let span = document.createElement('span');
  span.innerHTML = 'text';
  range.selectNode(span.firstChild);
  // TODO(tkent): The specification says nothing about such case. For now, we
  // unregister the Range from the Selection for ease of implementation
  // though Firefox and Edge keep |selection.getRangeAt(0) == range|.
  assert_equals(selection.rangeCount, 0);
}, 'Updating Range for another root should clear registered Selection.');
</script>