4 <title>Test for nsIHTMLEditor.getParagraphState()
</title>
5 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
6 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
11 <div id=
"content" contenteditable
></div>
15 <script class=
"testbody" type=
"application/javascript">
17 SimpleTest.waitForExplicitFinish();
18 SimpleTest.waitForFocus(function() {
19 let editor = document.getElementById(
"content");
20 let selection = window.getSelection();
21 let tag, range, mixed = {};
25 selection.removeAllRanges();
28 tag = getHTMLEditor().getParagraphState(mixed);
29 ok(false,
"nsIHTMLEditor.getParagraphState() should throw exception when there is no selection range");
31 ok(true,
"nsIHTMLEditor.getParagraphState() should throw exception when there is no selection range");
34 range = document.createRange();
35 range.setStart(document,
0);
36 selection.addRange(range);
37 tag = getHTMLEditor().getParagraphState(mixed);
38 is(tag,
"x",
"nsIHTMLEditor.getParagraphState() should return \"x\
" when selection range starts from document node");
39 is(mixed.value, false,
"nsIHTMLEditor.getParagraphState() should return false for mixed state when selection range starts from document node");
42 selection.collapse(editor,
0);
43 tag = getHTMLEditor().getParagraphState(mixed);
44 is(tag,
"x",
"nsIHTMLEditor.getParagraphState() should return \"x\
" when the editing host is empty");
45 is(mixed.value, false,
"nsIHTMLEditor.getParagraphState() should return false for mixed state when the editing host is empty");
47 editor.innerHTML =
"foo";
48 selection.collapse(editor.firstChild,
0);
49 tag = getHTMLEditor().getParagraphState(mixed);
50 is(tag,
"",
"nsIHTMLEditor.getParagraphState() should return \"\
" when the editing host has only text node");
51 is(mixed.value, false,
"nsIHTMLEditor.getParagraphState() should return false for mixed state when the editing host has only text node");
55 expected: {tag:
"p", tagIfEmpty:
"p"}},
57 expected: {tag:
"pre", tagIfEmpty:
"pre"}},
59 expected: {tag:
"h1", tagIfEmpty:
"h1"}},
61 expected: {tag:
"h2", tagIfEmpty:
"h2"}},
63 expected: {tag:
"h3", tagIfEmpty:
"h3"}},
65 expected: {tag:
"h4", tagIfEmpty:
"h4"}},
67 expected: {tag:
"h5", tagIfEmpty:
"h5"}},
69 expected: {tag:
"h6", tagIfEmpty:
"h6"}},
71 expected: {tag:
"address", tagIfEmpty:
"address"}},
73 expected: {tag:
"", tagIfEmpty:
""}},
75 expected: {tag:
"", tagIfEmpty:
""}},
77 expected: {tag:
"", tagIfEmpty:
""}},
79 expected: {tag:
"", tagIfEmpty:
""}},
81 expected: {tag:
"", tagIfEmpty:
"x"}},
83 expected: {tag:
"", tagIfEmpty:
"x"}},
85 expected: {tag:
"", tagIfEmpty:
"x"}},
87 expected: {tag:
"", tagIfEmpty:
"x"}},
89 expected: {tag:
"", tagIfEmpty:
"x"}},
91 expected: {tag:
"", tagIfEmpty:
"x"}},
93 expected: {tag:
"", tagIfEmpty:
"x"}},
95 expected: {tag:
"", tagIfEmpty:
"x"}},
97 expected: {tag:
"", tagIfEmpty:
"x"}},
99 editor.innerHTML = `<${test.tag}
></${test.tag}
>`;
100 selection.collapse(editor.firstChild,
0);
101 tag = getHTMLEditor().getParagraphState(mixed);
102 is(tag, test.expected.tagIfEmpty, `nsIHTMLEditor.getParagraphState() should return
"${test.expected.tagIfEmpty}" when the editing host has an empty <${test.tag}
>`);
103 is(mixed.value, false, `nsIHTMLEditor.getParagraphState() should return false for mixed state when the editing host has an empty <${test.tag}
>`);
105 editor.innerHTML = `<${test.tag}
>foo</${test.tag}
>`;
106 selection.collapse(editor.firstChild.firstChild,
0);
107 tag = getHTMLEditor().getParagraphState(mixed);
108 is(tag, test.expected.tag, `nsIHTMLEditor.getParagraphState() should return
"${test.expected.tag}" when the editing host has a <${test.tag}
> which has a text node`);
109 is(mixed.value, false, `nsIHTMLEditor.getParagraphState() should return false for mixed state when the editing host has a <${test.tag}
> which has a text node`);
111 editor.innerHTML = `<${test.tag}
><span>foo
</span></${test.tag}
>`;
112 selection.collapse(editor.firstChild.firstChild.firstChild,
0);
113 tag = getHTMLEditor().getParagraphState(mixed);
114 is(tag, test.expected.tag, `nsIHTMLEditor.getParagraphState() should return
"${test.expected.tag}" when the editing host has a <${test.tag}
> which has a
<span>`);
115 is(mixed.value, false, `nsIHTMLEditor.getParagraphState() should return false for mixed state when the editing host has a <${test.tag}
> which has a
<span>`);
117 editor.innerHTML = `<${test.tag}
>foo</${test.tag}
>`;
118 selection.collapse(editor.firstChild,
1);
119 tag = getHTMLEditor().getParagraphState(mixed);
120 is(tag, test.expected.tag, `nsIHTMLEditor.getParagraphState() should return
"${test.expected.tag}" when the editing host has a <${test.tag}
> which has a text node (selection collapsed at end of the element)`);
121 is(mixed.value, false, `nsIHTMLEditor.getParagraphState() should return false for mixed state when the editing host has a <${test.tag}
> which has a text node (selection collapsed at end of the element)`);
124 editor.innerHTML =
"<main><h1>header1</h1><section><h2>header2</h2><article><h3>header3</h3><p>paragraph</p><pre>preformat</pre></article></section></main>";
126 selection.setBaseAndExtent(document.querySelector(
"[contenteditable] h1").firstChild,
0,
127 document.querySelector(
"[contenteditable] h2").firstChild,
0);
128 tag = getHTMLEditor().getParagraphState(mixed);
129 is(tag,
"h2",
"nsIHTMLEditor.getParagraphState() should return \"h1\
" when between <h1> and <h2> is selected");
130 is(mixed.value, true,
"nsIHTMLEditor.getParagraphState() should return true for mixed state when between <h1> and <h2> is selected");
132 selection.setBaseAndExtent(document.querySelector(
"[contenteditable] h1").firstChild,
0,
133 document.querySelector(
"[contenteditable] h3").firstChild,
0);
134 tag = getHTMLEditor().getParagraphState(mixed);
135 is(tag,
"h3",
"nsIHTMLEditor.getParagraphState() should return \"h3\
" when between <h1> and <h3> is selected (whole of <h2> is also selected)");
136 is(mixed.value, true,
"nsIHTMLEditor.getParagraphState() should return true for mixed state when between <h1> and <h3> is selected (whole of <h2> is also selected)");
138 selection.setBaseAndExtent(document.querySelector(
"[contenteditable] p").firstChild,
0,
139 document.querySelector(
"[contenteditable] pre").firstChild,
0);
140 tag = getHTMLEditor().getParagraphState(mixed);
141 is(tag,
"pre",
"nsIHTMLEditor.getParagraphState() should return \"pre\
" when between <p> and <pre> is selected");
142 is(mixed.value, true,
"nsIHTMLEditor.getParagraphState() should return true for mixed state when between <p> and <pre> is selected");
147 function getHTMLEditor() {
148 var Ci = SpecialPowers.Ci;
149 var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
150 return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsIHTMLEditor);