[ Index ] |
|
Code source de Kupu-1.3.5 |
1 /***************************************************************************** 2 * 3 * Copyright (c) 2003-2005 Kupu Contributors. All rights reserved. 4 * 5 * This software is distributed under the terms of the Kupu 6 * License. See LICENSE.txt for license text. For a list of Kupu 7 * Contributors see CREDITS.txt. 8 * 9 *****************************************************************************/ 10 11 // $Id: test_xhtml.js 15802 2005-08-09 09:06:11Z duncan $ 12 13 // Various tests for html -> xhtml processing. 14 15 function KupuXhtmlTestCase() { 16 this.name = 'KupuXhtmlTestCase'; 17 18 this.incontext = function(s) { 19 return '<html><head><title>test</title></head><body>'+s+'</body></html>'; 20 } 21 this.verifyResult = function(newdoc, exp) { 22 var expected = this.incontext(exp); 23 var actual = Sarissa.serialize(newdoc); 24 actual = actual.replace('\xa0', ' '); 25 if (actual == expected) 26 return; 27 28 var context = /<html><head><title>test<\/title><\/head><body>(.*)<\/body><\/html>/; 29 if (context.test(actual) && context.test(expected)) { 30 var a = context.exec(actual)[1]; 31 var e = context.exec(expected)[1]; 32 throw('Assertion failed: ' + a + ' != ' + e); 33 } 34 throw('Assertion failed: ' + actual + ' != ' + expected); 35 } 36 37 this.conversionTest = function(data, expected) { 38 var doc = this.doc.documentElement; 39 var editor = this.editor; 40 this.body.innerHTML = data; 41 var xhtmldoc = Sarissa.getDomDocument(); 42 var newdoc = editor._convertToSarissaNode(xhtmldoc, this.doc.documentElement); 43 this.verifyResult(newdoc, expected); 44 } 45 46 this.setUp = function() { 47 var iframe = document.getElementById('iframe'); 48 this.doc = iframe.contentWindow.document; 49 this.body = this.doc.getElementsByTagName('body')[0]; 50 this.doc.getElementsByTagName('title')[0].text = 'test'; 51 this.editor = new KupuEditor(null, {}, null); 52 }; 53 54 this.arrayContains = function(ary, test) { 55 for (var i = 0; i < ary.length; i++) { 56 if (ary[i]==test) { 57 return true; 58 } 59 } 60 return false; 61 } 62 this.testExclude = function() { 63 // Check that the exclude functions work as expected. 64 var validator = new XhtmlValidation(this.editor); 65 var events = ['onclick', 'ondblclick', 'onmousedown', 66 'onmouseup', 'onmouseover', 'onmousemove', 67 'onmouseout', 'onkeypress', 'onkeydown', 68 'onkeyup']; 69 var expected = ['onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseout', 'onkeypress', 'onkeyup']; 70 71 var actual = validator._exclude(events, 'onmouseover|onmousemove|onkeydown'); 72 this.assertEquals(actual.toString(), expected.toString()); 73 74 // check is also works with arrays. 75 actual = validator._exclude(events, ['onmouseover','onmousemove','onkeydown']); 76 this.assertEquals(actual.toString(), expected.toString()); 77 78 // Check we have a bgcolor attribute 79 this.assertTrue(this.arrayContains(validator.tagAttributes.thead, 'charoff')); 80 this.assertTrue(validator.attrFilters['charoff'] != null); 81 validator.excludeAttributes(['charoff']); 82 this.assertTrue(validator.attrFilters['charoff']==null); 83 this.assertTrue(!this.arrayContains(validator.tagAttributes.thead, 'charoff')); 84 this.assertTrue(this.arrayContains(validator.tagAttributes.td, 'height')); 85 this.assertTrue(this.arrayContains(validator.tagAttributes.th, 'height')); 86 validator.excludeTagAttributes(['table','th'], ['width','height']); 87 this.assertTrue(this.arrayContains(validator.tagAttributes.td, 'height')); 88 this.assertFalse(this.arrayContains(validator.tagAttributes.th, 'height')); 89 } 90 91 this.testSet = function() { 92 var validator = new XhtmlValidation(this.editor); 93 94 var set1 = new validator.Set(['a','b','c']); 95 this.assertTrue(set1.a && set1.b && set1.c); 96 var set2 = new validator.Set(set1); 97 this.assertTrue(set2.a && set2.b && set2.c); 98 } 99 this.testValidator = function() { 100 var validator = new XhtmlValidation(this.editor); 101 var table = validator.States['table']; 102 var tags = []; 103 for (var tag in table) { 104 this.assertEquals(table[tag], 1); 105 tags.push(tag); 106 } 107 this.assertEquals(tags.toString(), 108 ['caption','col','colgroup', 109 'thead','tfoot','tbody','tr'].toString()); 110 }; 111 112 this.testConvertToSarissa = function() { 113 var data = '<p class="blue">This is a test</p>'; 114 this.conversionTest(data, data); 115 } 116 this.testXmlAttrs = function() { 117 var data = '<pre xml:space="preserve" xml:lang="fr">This is a test</pre>'; 118 var expected1 = '<pre xml:lang="fr" xml:space="preserve">This is a test</pre>'; 119 this.conversionTest(data, expected1); 120 var expected2 = '<pre>This is a test</pre>'; 121 this.editor.xhtmlvalid.excludeAttributes(['xml:lang','xml:space']); 122 this.conversionTest(data, expected2); 123 } 124 this.testConvertToSarissa2 = function() { 125 var data = '<div id="div1">This is a test</div>'; 126 this.conversionTest(data, data); 127 } 128 this.testbadTags = function() { 129 var data = '<div><center>centered</center><p>Test</p><o:p>zzz</o:p></div>'; 130 var expected = '<div>centered<p>Test</p>zzz</div>'; 131 this.conversionTest(data, expected); 132 } 133 this.testnbsp = function() { 134 var data = '<p>Text with <b>non-break</b> space</p>'; 135 this.conversionTest(data, data); 136 }; 137 this.teststyle = function() { 138 var data = '<p style="text-align:right; mso-silly: green">Text aligned right</p>'; 139 var expected = '<p style="text-align: left;">Text aligned right</p>'; 140 var doc = this.doc.documentElement; 141 var editor = this.editor; 142 this.body.innerHTML = data; 143 this.body.firstChild.style.textAlign = 'left'; 144 this.body.firstChild.style.display = 'block'; 145 //alert(this.body.firstChild.style.cssText); 146 var xhtmldoc = Sarissa.getDomDocument(); 147 var newdoc = editor._convertToSarissaNode(xhtmldoc, this.doc.documentElement); 148 this.verifyResult(newdoc, expected); 149 }; 150 this.testclass = function() { 151 var data = '<div class="MsoNormal fred">This is a test</div>'; 152 var expected = '<div class="fred">This is a test</div>'; 153 this.conversionTest(data, expected); 154 } 155 this.testclass2 = function() { 156 var data = '<div class="MsoNormal">This is a test</div>'; 157 var expected = '<div>This is a test</div>'; 158 this.conversionTest(data, expected); 159 } 160 this.testTable = function() { 161 // N.B. This table contains text and a <P> tag where they 162 // aren't legal. Mozilla strips out the <P> tag but lets the 163 // text through, IE lets both through. 164 165 var data = '<TABLE class="listing">BADTEXT!<THEAD><TR><TH>Col 01</TH><TH class=align-center>Col 11</TH>'+ 166 '<TH class=align-right>Col 21</TH></TR></THEAD>'+ 167 '<TBODY><TR>'+ 168 '<TD>text</TD>'+ 169 '<TD class=align-center>a</TD>'+ 170 '<TD class=align-right>r</TD></TR>'+ 171 '<TR>'+ 172 '<TD>more text</TD>'+ 173 '<TD class=align-center>aaa</TD>'+ 174 '<TD class=align-right>rr</TD></TR>'+ 175 '<TR>'+ 176 '<TD>yet more text</TD>'+ 177 '<TD class=align-center>aaaaa</TD>'+ 178 '<TD class=align-right>rrr</TD></TR></TBODY><P></TABLE>'; 179 var expected = '<table class="listing"><thead><tr><th>Col 01</th><th class="align-center">Col 11</th>'+ 180 '<th class="align-right">Col 21</th></tr></thead>'+ 181 '<tbody><tr>'+ 182 '<td>text</td>'+ 183 '<td class="align-center">a</td>'+ 184 '<td class="align-right">r</td></tr>'+ 185 '<tr>'+ 186 '<td>more text</td>'+ 187 '<td class="align-center">aaa</td>'+ 188 '<td class="align-right">rr</td></tr>'+ 189 '<tr>'+ 190 '<td>yet more text</td>'+ 191 '<td class="align-center">aaaaa</td>'+ 192 '<td class="align-right">rrr</td></tr></tbody></table>'; 193 194 this.editor.xhtmlvalid.filterstructure = true; 195 this.conversionTest(data, expected); 196 } 197 this.testCustomAttribute = function() { 198 var validator = this.editor.xhtmlvalid; 199 var data = '<div special="magic">This is a test</div>'; 200 this.assertTrue(validator.tagAttributes.td===validator.tagAttributes.th); 201 this.editor.xhtmlvalid.includeTagAttributes(['div','td'],['special']); 202 // Check that shared arrays are no longer shared... 203 this.assertFalse(validator.tagAttributes.td===validator.tagAttributes.th); 204 this.assertTrue(this.arrayContains(validator.tagAttributes.td, 'special')); 205 this.assertFalse(this.arrayContains(validator.tagAttributes.th, 'special')); 206 this.editor.xhtmlvalid.setAttrFilter(['special']); 207 this.conversionTest(data, data); 208 } 209 210 this.tearDown = function() { 211 this.body.innerHTML = ''; 212 }; 213 } 214 215 KupuXhtmlTestCase.prototype = new TestCase;
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 15:30:41 2007 | par Balluche grâce à PHPXref 0.7 |