Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / editing / execCommand / script-tests / convert-style-elements-to-spans.js
1 description("Test to make sure we do not remove extra styling hidden on html styling elements (b, i, s, etc.) when removing those elements.")
2
3 var testContainer = document.createElement("div");
4 testContainer.contentEditable = true;
5 document.body.appendChild(testContainer);
6
7 function testSingleToggle(toggleCommand, initialContents, expectedContents)
8 {
9     testContainer.innerHTML = initialContents;
10     window.getSelection().selectAllChildren(testContainer);
11     document.execCommand(toggleCommand, false, null);
12     if (testContainer.innerHTML === expectedContents) {
13         testPassed("one " + toggleCommand + " command converted " + initialContents + " to " + expectedContents);
14     } else {
15         testFailed("one " + toggleCommand + " command converted " + initialContents + " to " + testContainer.innerHTML + ", expected " + expectedContents);
16     }
17 }
18
19 function testDoubleToggle(toggleCommand, initialContents, expectedContents)
20 {
21     testContainer.innerHTML = initialContents;
22     window.getSelection().selectAllChildren(testContainer);
23     document.execCommand(toggleCommand, false, null);
24     document.execCommand(toggleCommand, false, null);
25     if (testContainer.innerHTML === expectedContents) {
26         testPassed("two " + toggleCommand + " commands converted " + initialContents + " to " + expectedContents);
27     } else {
28         testFailed("two " + toggleCommand + " commands converted " + initialContents + " to " + testContainer.innerHTML + ", expected " + expectedContents);
29     }
30 }
31
32 function testCommandAndUndo(command, contents)
33 {
34     testContainer.innerHTML = contents;
35     window.getSelection().selectAllChildren(testContainer);
36     document.execCommand(command, false, null);
37     document.execCommand("undo", false, null);
38     if (testContainer.innerHTML === contents) {
39         testPassed(command + " followed by undo, correctly return to " + contents);
40     } else {
41         testFailed(command + " followed by undo converted " + contents + " to " + testContainer.innerHTML);
42     }
43 }
44
45 function testCommandAndUndoWithIntermediateFunction(command, initialContents, expectedContents, intermediateFunction)
46 {
47     testContainer.innerHTML = initialContents;
48     window.getSelection().selectAllChildren(testContainer);
49     document.execCommand(command, false, null);
50     intermediateFunction();
51     document.execCommand("undo", false, null);
52     if (testContainer.innerHTML === expectedContents) {
53         testPassed(command + " followed by undo converted " + initialContents + " to " + expectedContents);
54     } else {
55         testFailed(command + " followed by undo converted " + initialContents + " to " + testContainer.innerHTML + ", expected " + expectedContents);
56     }
57 }
58
59 // It would be nice to expect a <tag>test</tag> result for these, but that's unrealistic.
60 // The first pass removes the implied styling converting the rest to <span style="...">
61 // the second pass cannot be expected to convert an "existing" <span style="text-decoration: underline"> into <u>
62 testSingleToggle("bold", "<b style=\"text-decoration: underline\">test</b>", "<span style=\"text-decoration: underline\">test</span>");
63 testSingleToggle("italic", "<i style=\"font-weight: bold\">test</i>", "<span style=\"font-weight: bold\">test</span>");
64
65 // Make sure we correctly remove double-styled tags
66 testSingleToggle("bold", "<b style=\"font-weight: bold\">test</b>", "test");
67
68 // Make sure we don't remove extra stuff
69 testSingleToggle("bold", "<b foo=\"bar\">test</b>", "<span foo=\"bar\">test</span>");
70 testSingleToggle("bold", "<b style='invalid'>test</b>", "test"); // It's OK to remove invalid styles
71
72 // FIXME: We have to call undo here to prevent WebKit from trying to
73 // undo all of our previous commands at once.  We have no way to
74 // "clear the undo stack" in WebKit.
75 document.execCommand("undo", false, null);
76 // That filled testContainer with lots of junk, but it will be cleared
77 // when testCommandAndUndo is called.
78
79 testCommandAndUndo("bold", "<b style=\"text-decoration: underline\">test</b>");
80 testCommandAndUndo("italic", "<i style=\"font-weight: bold\">test</i>");
81
82 testCommandAndUndoWithIntermediateFunction(
83     "bold",
84     "<b style=\"text-decoration: underline\">test</b>",
85     "<b style=\"text-decoration: underline\" foo=\"bar\">test</b>",
86     function() {
87         // Change the <span> before undoing it
88         try {
89             testContainer.firstChild.setAttribute("foo", "bar");
90         } catch(e) {
91             testFailed("Exception caught: " + e);
92         }
93 });
94
95 testCommandAndUndoWithIntermediateFunction(
96     "bold",
97     "<b style=\"text-decoration: underline\">test</b>",
98     "",
99     function() {
100         // remove the span, undo should fail
101         testContainer.removeChild(testContainer.firstChild);
102 });
103
104 testCommandAndUndoWithIntermediateFunction(
105     "bold",
106     "<b style=\"text-decoration: underline\">test</b>",
107     "<span>foobar</span>",
108     function() {
109         // replace the span with a new span, undo should fail
110         testContainer.removeChild(testContainer.firstChild);
111         testContainer.innerHTML = "<span>foobar</span>";
112 });
113
114 testCommandAndUndoWithIntermediateFunction(
115     "bold",
116     "<b style=\"text-decoration: underline\">test</b>",
117     "<b style=\"text-decoration: underline\">foobar</b>",
118     function() {
119         // replace the span contents, undo should succeed
120         testContainer.firstChild.innerHTML = "foobar";
121 });
122
123 document.body.removeChild(testContainer);
124
125 var successfullyParsed = true;