summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/editing/other/restoration.html
blob: 4c53008b41027c4e11734dea068a57ea0e7283db (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<meta charset=utf-8>
<title>Restoration of style tests</title>
<!--
No spec, based on: https://bugzilla.mozilla.org/show_bug.cgi?id=1250805
If the user presses Ctrl+B and then hits Enter and then types text, the text
should still be bold.  Hitting Enter shouldn't make it forget.  And so too for
other commands.
-->
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div contenteditable></div>
<script>
var div = document.querySelector("div");

function doTestInner(cmd, param, startBold) {
  div.innerHTML = startBold ? "<b>foo</b>bar" : "foobar";
  getSelection().collapse(startBold ? div.firstChild.firstChild
                                    : div.firstChild, 3);

  // Set/unset bold, then run command and see if it's still there
  assert_true(document.execCommand("bold", false, ""),
              "execCommand needs to return true for bold");

  assert_true(document.execCommand(cmd, false, param),
              "execCommand needs to return true for " + cmd + " " + param);

  assert_equals(document.queryCommandState("bold"), !startBold,
               "bold state");

  assert_true(document.execCommand("inserttext", false, "x"),
              "execCommand needs to return true for inserttext x");

  // Find the new text node and check that it's actually bold (or not)
  var node = div;
  while (node) {
    if (node.nodeType == Node.TEXT_NODE && node.nodeValue.indexOf("x") != -1) {
      assert_in_array(getComputedStyle(node.parentNode).fontWeight,
                    !startBold ? ["700", "bold"] : ["400", "normal"],
                    "font-weight");
      return;
    }
    if (node.firstChild) {
      node = node.firstChild;
      continue;
    }
    while (node != div && !node.nextSibling) {
      node = node.parentNode;
    }
    if (node == div) {
      assert_unreached("x not found!");
      break;
    }
    node = node.nextSibling;
  }
}

function doTest(cmd, param) {
  if (param === undefined) {
    param = "";
  }

  test(function() {
    doTestInner(cmd, param, true);
  }, cmd + " " + param + " starting bold");

  test(function() {
    doTestInner(cmd, param, false);
  }, cmd + " " + param + " starting not bold");
}

doTest("insertparagraph");
doTest("insertlinebreak");
doTest("delete");
doTest("forwarddelete");
doTest("insertorderedlist");
doTest("insertunorderedlist");
doTest("indent");
// Outdent does nothing here, but should be harmless.
doTest("outdent");
doTest("justifyleft");
doTest("justifyright");
doTest("justifycenter");
doTest("justifyfull");
doTest("formatblock", "div");
doTest("formatblock", "blockquote");
doTest("inserthorizontalrule");
doTest("insertimage", "a");
doTest("inserttext", "bar");
</script>