1 description("Test to make sure queryCommandState returns correct values.")
3 var testContainer = document.createElement("div");
4 testContainer.contentEditable = true;
5 document.body.appendChild(testContainer);
7 function testQueryCommandState(command, contents, selector, expectedState)
9 testContainer.innerHTML = contents;
10 var selected = selector(testContainer);
11 var actualState = document.queryCommandState(command);
12 var actualValue = document.queryCommandValue(command);
13 var action = 'queryCommandState("' + command + '") returns ' + actualState + ' when selecting ' + selected + ' of "' + contents + '"';
14 if (actualState != expectedState)
15 testFailed(action + ', expected ' + expectedState + '');
16 else if (actualValue != actualState.toString())
17 testFailed(action + ' but queryCommandValue returned ' + actualValue);
22 function selectAll(container) {
23 window.getSelection().selectAllChildren(container);
27 function selectSecondWord(container) {
28 window.getSelection().collapse(container, 0);
29 window.getSelection().modify('move', 'forward', 'word');
30 window.getSelection().modify('move', 'forward', 'word');
31 window.getSelection().modify('move', 'backward', 'word');
32 window.getSelection().modify('extend', 'forward', 'word');
36 function selectFirstTwoWords(container) {
37 window.getSelection().collapse(container, 0);
38 window.getSelection().modify('move', 'forward', 'word');
39 window.getSelection().modify('extend', 'forward', 'word');
40 window.getSelection().modify('extend', 'forward', 'word');
41 return 'first two words';
44 function runTests(editingBehavior) {
46 internals.settings.setEditingBehavior(editingBehavior);
47 debug('Tests for ' + editingBehavior)
49 testQueryCommandState("bold", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
50 testQueryCommandState("bold", '<i>hello</i>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
51 testQueryCommandState("bold", '<b>hello</b>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
52 testQueryCommandState("bold", 'hello <b>world</b>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
53 testQueryCommandState("bold", '<b>hello</b> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
54 testQueryCommandState("bold", 'hello <b>world</b> WebKit', selectSecondWord, {'mac': true, 'win': false}[editingBehavior]);
55 testQueryCommandState("bold", '<b>hello</b> world <b>WebKit</b>', selectSecondWord, {'mac': false, 'win': false}[editingBehavior]);
56 testQueryCommandState("bold", '<i>hello <b>hello</b> WebKit</i>', selectSecondWord, {'mac': true, 'win': false}[editingBehavior]);
57 testQueryCommandState("bold", '<b>hello <i>hello</i> WebKit</b>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
58 testQueryCommandState("bold", '<b><div>hello <i>hello</i> WebKit</div></b>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
59 testQueryCommandState("bold", '<b style="font-weight: normal;">hello</b>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
60 testQueryCommandState("bold", '<i style="font-weight: bold;">hello</i>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
61 testQueryCommandState("bold", '<b>hello</b> world <b>WebKit</b>', selectAll, {'mac': true, 'win': false}[editingBehavior]);
62 testQueryCommandState("bold", '<b>hello</b><b> world</b>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
63 testQueryCommandState("bold", '<div><b>hello</b></div><p><b> WebKit</b></p>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
65 testQueryCommandState("italic", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
66 testQueryCommandState("italic", '<b>hello</b>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
67 testQueryCommandState("italic", '<i>hello</i>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
68 testQueryCommandState("italic", '<i>hello</i> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
69 testQueryCommandState("italic", 'hello <i>world</i>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
70 testQueryCommandState("italic", '<i><div>hello world</div></i>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
71 testQueryCommandState("italic", '<div style="font-style: italic">hello <span style="font-style: normal;">hello</span></div>', selectAll, {'mac': true, 'win': false}[editingBehavior]);
73 testQueryCommandState("subscript", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
74 testQueryCommandState("subscript", '<sup>hello</sup>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
75 testQueryCommandState("subscript", '<sub>hello</sub>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
76 testQueryCommandState("subscript", '<sub>hello</sub> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
77 testQueryCommandState("subscript", 'hello <sub>world</sub>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
78 testQueryCommandState("subscript", '<div style="vertical-align: sub;">hello world</div>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
79 testQueryCommandState("subscript", 'hello <span style="vertical-align: sub;">world</span> WebKit', selectSecondWord, {'mac': true, 'win': false}[editingBehavior]);
81 testQueryCommandState("superscript", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
82 testQueryCommandState("superscript", '<sub>hello</sub>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
83 testQueryCommandState("superscript", '<sup>hello</sup>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
84 testQueryCommandState("superscript", '<sup>hello</sup> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
85 testQueryCommandState("superscript", 'hello <sup>world</sup>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
86 testQueryCommandState("superscript", '<span style="vertical-align: super;">hello</span><span style="vertical-align: sub;">world</span>', selectAll, {'mac': true, 'win': false}[editingBehavior]);
87 testQueryCommandState("superscript", 'hello<span style="vertical-align: super;">world</span>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
89 testQueryCommandState("underline", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
90 testQueryCommandState("underline", '<s>hello</s>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
91 testQueryCommandState("underline", '<u>hello</u>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
92 testQueryCommandState("underline", '<u>hello</u> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
93 testQueryCommandState("underline", 'hello <u>world</u>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
94 testQueryCommandState("underline", '<u><div>hello world</div></u>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
95 testQueryCommandState("underline", '<u><s><div>hello world WebKit</div></s></u>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
96 testQueryCommandState("underline", '<s><u>hello</u> world</s> WebKit', selectSecondWord, {'mac': false, 'win': false}[editingBehavior]);
97 testQueryCommandState("underline", '<u><s>hello</s> world</u> WebKit', selectSecondWord, {'mac': true, 'win': false}[editingBehavior]);
98 testQueryCommandState("underline", '<s>hello <u>world</s> WebKit</u>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
100 testQueryCommandState("strikeThrough", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
101 testQueryCommandState("strikeThrough", '<u>hello</u>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
102 testQueryCommandState("strikeThrough", '<s>hello</s>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
103 testQueryCommandState("strikeThrough", '<s>hello</s> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
104 testQueryCommandState("strikeThrough", 'hello <s>world</s>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
105 testQueryCommandState("strikeThrough", '<s><div>hello world</div></s>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
106 testQueryCommandState("strikeThrough", '<s><u><div>hello world WebKit</div></u></s>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
107 testQueryCommandState("strikeThrough", '<u><s>hello</s> world</u> WebKit', selectSecondWord, {'mac': false, 'win': false}[editingBehavior]);
108 testQueryCommandState("strikeThrough", 'hello <s>world WebKit</s>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
109 testQueryCommandState("strikeThrough", 'hello <s>world WebKit</s>', selectFirstTwoWords, {'mac': false, 'win': true}[editingBehavior]);
116 //document.body.removeChild(testContainer);
117 var successfullyParsed = true;