<!doctype html>
<html>
<head>
<meta chareset="utf-8">
<meta name="timeout" content="long">
<meta name="variant" content="?styleWithCSS=false">
<meta name="variant" content="?styleWithCSS=true">
<title>Test preserving selection after outdent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="../include/editor-test-utils.js"></script>
</head>
<body>
<div contenteditable></div>
<script>
"use strict";

const editor = document.querySelector("div[contenteditable]");
const utils = new EditorTestUtils(editor);
const styleWithCSS =
  new URLSearchParams(document.location.search).get("styleWithCSS");
document.execCommand("styleWithCSS", false, styleWithCSS);

// Note that it's not scope of this test how browsers to outdent the selected
// content.

// html: Initial HTML which will be set editor.innerHTML, it should contain
//       selection range with a pair of "[" or "{" and "]" or "}".
// expectedSelectedString: After executing "outdent", compared with
//                         getSelection().toString().replace(/[ \n\r]+/g, "")
const tests = [
  {
    html: "<blockquote>a[b]c</blockquote>",
    expectedSelectedString: "b",
  },
  {
    html: "<blockquote><div>a[b]c</div></blockquote>",
    expectedSelectedString: "b",
  },
  {
    html: "<blockquote><div>a[bc</div><div>de]f</div></blockquote>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<blockquote>a[bc</blockquote>" +
          "<blockquote>de]f</blockquote>",
    expectedSelectedString: "bcde",
  },
  {
    html: '<div style="margin-left:15px">a[b]c</div>',
    expectedSelectedString: "b",
  },
  {
    html: '<div style="margin-left:15px">a[bc</div>' +
          '<div style="margin-left:15px">de]f</div>',
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><li>a[b]c</li></ul>",
    expectedSelectedString: "b",
  },
  {
    html: "<ul><li>a[bc</li><li>de]f</li></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ol><ul><li>a[b]c</li></ul></ol>",
    expectedSelectedString: "b",
  },
  {
    html: "<ol><ul><li>a[bc</li><li>de]f</li></ul></ol>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><ul><li>a[b]c</li></ul></ul>",
    expectedSelectedString: "b",
  },
  {
    html: "<ul><ul><li>a[bc</li><li>de]f</li></ul></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ol><li><ul><li>a[b]c</li></ul></li></ol>",
    expectedSelectedString: "b",
  },
  {
    html: "<ol><li><ul><li>a[bc</li><li>de]f</li></ul></li></ol>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><li><ul><li>a[b]c</li></ul></li></ul>",
    expectedSelectedString: "b",
  },
  {
    html: "<ul><li><ul><li>a[bc</li><li>de]f</li></ul></li></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<blockquote><div>a[bc</div></blockquote>" +
          "<ul><ul><li>de]f</li></ul></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<blockquote><div>a[bc</div></blockquote>" +
          "<ol><ul><li>de]f</li></ul></ol>",
    expectedSelectedString: "bcde",
  },
  {
    html: '<div style="margin-left:15px">a[bc</div>' +
          "<ul><ul><li>de]f</li></ul></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<blockquote><div>a[bc</div></blockquote>" +
          "<ul><li><ul><li>de]f</li></ul></li></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<blockquote><div>a[bc</div></blockquote>" +
          "<ol><li><ul><li>de]f</li></ul></li></ol>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ol><ul><li>a[bc</li></ul></ol>" +
          "<blockquote><div>de]f</div></blockquote>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><ul><li>a[bc</li></ul></ul>" +
          '<div style="margin-left:15px">de]f</div>',
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><li><ul><li>a[bc</li></ul></li></ul>" +
          "<blockquote><div>de]f</div></blockquote>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ol><li><ul><li>a[bc</li></ul></li></ol>" +
          "<blockquote><div>de]f</div></blockquote>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><li>a[bc</li></ul>" +
          "<ul><li>de]f</li></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><li>abc</li><li>d[ef</li></ul>" +
          "<ul><li>gh]i</li></ul>",
    expectedSelectedString: "efgh",
  },
  {
    html: "<ul><li>abc</li><li>d[ef</li></ul>" +
          "<ul><li>gh]i</li><li>jkl</li></ul>",
    expectedSelectedString: "efgh",
  },
  {
    html: "<ol><li>a[bc</li></ol>" +
          "<ul><li>de]f</li></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ol><li>abc</li><li>d[ef</li></ol>" +
          "<ul><li>gh]i</li></ul>",
    expectedSelectedString: "efgh",
  },
  {
    html: "<ol><li>abc</li><li>d[ef</li></ol>" +
          "<ul><li>gh]i</li><li>jkl</li></ul>",
    expectedSelectedString: "efgh",
  },
];

for (const t of tests) {
  test(() => {
    utils.setupEditingHost(t.html);
    document.execCommand("outdent");
    assert_equals(
      getSelection().toString().replace(/[ \n\r]+/g, ""),
      t.expectedSelectedString,
      `Result: ${editor.innerHTML}`
    );
  }, `Preserve selection after outdent at ${t.html}`);
}

</script>
</body>
</html>
