test_nsIHTMLEditor_getParagraphState.html (7499B)
1 <!DOCTYPE> 2 <html> 3 <head> 4 <title>Test for nsIHTMLEditor.getParagraphState()</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" href="/tests/SimpleTest/test.css"> 7 </head> 8 <body> 9 <div id="display"> 10 </div> 11 <div id="content" contenteditable></div> 12 <pre id="test"> 13 </pre> 14 15 <script class="testbody" type="application/javascript"> 16 17 SimpleTest.waitForExplicitFinish(); 18 SimpleTest.waitForFocus(function() { 19 let editor = document.getElementById("content"); 20 let selection = window.getSelection(); 21 let tag, range, mixed = {}; 22 23 editor.focus(); 24 editor.blur(); 25 selection.removeAllRanges(); 26 27 try { 28 tag = getHTMLEditor().getParagraphState(mixed); 29 ok(false, "nsIHTMLEditor.getParagraphState() should throw exception when there is no selection range"); 30 } catch (e) { 31 ok(true, "nsIHTMLEditor.getParagraphState() should throw exception when there is no selection range"); 32 } 33 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"); 40 41 editor.focus(); 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"); 46 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"); 52 53 for (let test of [ 54 {tag: "p", 55 expected: {tag: "p", tagIfEmpty: "p"}}, 56 {tag: "pre", 57 expected: {tag: "pre", tagIfEmpty: "pre"}}, 58 {tag: "h1", 59 expected: {tag: "h1", tagIfEmpty: "h1"}}, 60 {tag: "h2", 61 expected: {tag: "h2", tagIfEmpty: "h2"}}, 62 {tag: "h3", 63 expected: {tag: "h3", tagIfEmpty: "h3"}}, 64 {tag: "h4", 65 expected: {tag: "h4", tagIfEmpty: "h4"}}, 66 {tag: "h5", 67 expected: {tag: "h5", tagIfEmpty: "h5"}}, 68 {tag: "h6", 69 expected: {tag: "h6", tagIfEmpty: "h6"}}, 70 {tag: "address", 71 expected: {tag: "address", tagIfEmpty: "address"}}, 72 {tag: "span", 73 expected: {tag: "", tagIfEmpty: ""}}, 74 {tag: "b", 75 expected: {tag: "", tagIfEmpty: ""}}, 76 {tag: "i", 77 expected: {tag: "", tagIfEmpty: ""}}, 78 {tag: "em", 79 expected: {tag: "", tagIfEmpty: ""}}, 80 {tag: "div", 81 expected: {tag: "", tagIfEmpty: "x"}}, 82 {tag: "section", 83 expected: {tag: "", tagIfEmpty: "x"}}, 84 {tag: "article", 85 expected: {tag: "", tagIfEmpty: "x"}}, 86 {tag: "header", 87 expected: {tag: "", tagIfEmpty: "x"}}, 88 {tag: "main", 89 expected: {tag: "", tagIfEmpty: "x"}}, 90 {tag: "footer", 91 expected: {tag: "", tagIfEmpty: "x"}}, 92 {tag: "aside", 93 expected: {tag: "", tagIfEmpty: "x"}}, 94 {tag: "blockquote", 95 expected: {tag: "", tagIfEmpty: "x"}}, 96 {tag: "form", 97 expected: {tag: "", tagIfEmpty: "x"}}, 98 ]) { 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}>`); 104 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`); 110 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>`); 116 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)`); 122 } 123 124 editor.innerHTML = "<main><h1>header1</h1><section><h2>header2</h2><article><h3>header3</h3><p>paragraph</p><pre>preformat</pre></article></section></main>"; 125 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"); 131 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)"); 137 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"); 143 144 SimpleTest.finish(); 145 }); 146 147 function getHTMLEditor() { 148 var Ci = SpecialPowers.Ci; 149 var editingSession = SpecialPowers.wrap(window).docShell.editingSession; 150 return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsIHTMLEditor); 151 } 152 153 </script> 154 </body> 155 156 </html>