<!doctype html>
<html>
<head>
<meta chareset="utf-8">
<meta name="timeout" content="long">
<meta name="variant" content="?styleWithCSS=false&command=justifyCenter">
<meta name="variant" content="?styleWithCSS=false&command=justifyFull">
<meta name="variant" content="?styleWithCSS=false&command=justifyLeft">
<meta name="variant" content="?styleWithCSS=false&command=justifyRight">
<meta name="variant" content="?styleWithCSS=true&command=justifyCenter">
<meta name="variant" content="?styleWithCSS=true&command=justifyFull">
<meta name="variant" content="?styleWithCSS=true&command=justifyLeft">
<meta name="variant" content="?styleWithCSS=true&command=justifyRight">
<title>Test preserving selection after justifying selected content</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 searchParams = new URLSearchParams(document.location.search);
const styleWithCSS = searchParams.get("styleWithCSS");
const command = searchParams.get("command");
document.execCommand("styleWithCSS", false, styleWithCSS);

// Note that it's not scope of this test how browsers to align 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\t]+/g, "")
const tests = [
  {
    html: "<div>a[b]c</div>",
    expectedSelectedString: "b",
  },
  {
    html: "<address>a[b]c</address>",  // <address> cannot have align attribute
    expectedSelectedString: "b",
  },
  {
    html: "<div>a[bc</div>" +
          "<div>de]f</div>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<div>a[bc</div>" +
          "<address>de]f</address>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<address>a[bc</address>" +
          "<div>de]f</div>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<address>a[bc</address>" +
          "<address>de]f</address>",
    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: "<ul><li>a[bc</li><li>de]f</li><li>ghi</li></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><li>abc</li><li>d[ef</li><li>gh]i</li></ul>",
    expectedSelectedString: "efgh",
  },
  {
    html: "<ul><li>abc</li><li>d[e]f</li><li>ghi</li></ul>",
    expectedSelectedString: "e",
  },
  {
    html: "<ul><li>a[bc</li></ul>" +
          "<div>de]f</div>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<ul><li>abc</li><li>d[ef</li></ul>" +
          "<div>gh]i</div>",
    expectedSelectedString: "efgh",
  },
  {
    html: "<div>a[bc</div>" +
          "<ul><li>de]f</li></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<div>a[bc</div>" +
          "<ul><li>de]f</li><li>ghi</li></ul>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<table><tr><td>a[b]c</td></tr></table>",
    expectedSelectedString: "b",
  },
  {
    html: "<table><tr><td>a[bc</td><td>de]f</td></tr></table>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<table><tr><td>a[bc</td></tr><tr><td>de]f</td></tr></table>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<div>a[bc</div>" +
          "<table><tr><td>de]f</td></tr></table>",
    expectedSelectedString: "bcde",
  },
  {
    html: "<table><tr><td>a[bc</td></tr></table>" +
          "<div>de]f</div>",
    expectedSelectedString: "bcde",
  },
];

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

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