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.")
3 var testContainer = document.createElement("div");
4 testContainer.contentEditable = true;
5 document.body.appendChild(testContainer);
7 function testSingleToggle(toggleCommand, initialContents, expectedContents)
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);
15 testFailed("one " + toggleCommand + " command converted " + initialContents + " to " + testContainer.innerHTML + ", expected " + expectedContents);
19 function testDoubleToggle(toggleCommand, initialContents, expectedContents)
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);
28 testFailed("two " + toggleCommand + " commands converted " + initialContents + " to " + testContainer.innerHTML + ", expected " + expectedContents);
32 function testCommandAndUndo(command, contents)
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);
41 testFailed(command + " followed by undo converted " + contents + " to " + testContainer.innerHTML);
45 function testCommandAndUndoWithIntermediateFunction(command, initialContents, expectedContents, intermediateFunction)
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);
55 testFailed(command + " followed by undo converted " + initialContents + " to " + testContainer.innerHTML + ", expected " + expectedContents);
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>");
65 // Make sure we correctly remove double-styled tags
66 testSingleToggle("bold", "<b style=\"font-weight: bold\">test</b>", "test");
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
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.
79 testCommandAndUndo("bold", "<b style=\"text-decoration: underline\">test</b>");
80 testCommandAndUndo("italic", "<i style=\"font-weight: bold\">test</i>");
82 testCommandAndUndoWithIntermediateFunction(
84 "<b style=\"text-decoration: underline\">test</b>",
85 "<b style=\"text-decoration: underline\" foo=\"bar\">test</b>",
87 // Change the <span> before undoing it
89 testContainer.firstChild.setAttribute("foo", "bar");
91 testFailed("Exception caught: " + e);
95 testCommandAndUndoWithIntermediateFunction(
97 "<b style=\"text-decoration: underline\">test</b>",
100 // remove the span, undo should fail
101 testContainer.removeChild(testContainer.firstChild);
104 testCommandAndUndoWithIntermediateFunction(
106 "<b style=\"text-decoration: underline\">test</b>",
107 "<span>foobar</span>",
109 // replace the span with a new span, undo should fail
110 testContainer.removeChild(testContainer.firstChild);
111 testContainer.innerHTML = "<span>foobar</span>";
114 testCommandAndUndoWithIntermediateFunction(
116 "<b style=\"text-decoration: underline\">test</b>",
117 "<b style=\"text-decoration: underline\">foobar</b>",
119 // replace the span contents, undo should succeed
120 testContainer.firstChild.innerHTML = "foobar";
123 document.body.removeChild(testContainer);
125 var successfullyParsed = true;