tinymce-obsolete.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ( function( $, QUnit, tinymce ) {
  2. var editor,
  3. text,
  4. count = 0;
  5. QUnit.module( 'tinymce.obsolete', {
  6. beforeEach: function( assert ) {
  7. var done;
  8. if ( ! editor ) {
  9. done = assert.async();
  10. $( document.body ).append( '<textarea id="editor">' );
  11. tinymce.init({
  12. selector: '#editor',
  13. add_unload_trigger : false,
  14. skin: false,
  15. indent : false,
  16. entities : 'raw',
  17. plugins: 'media',
  18. convert_urls : false,
  19. init_instance_callback : function( ed ) {
  20. editor = ed;
  21. done();
  22. }
  23. });
  24. } else {
  25. editor.setContent( '' );
  26. editor.selection.setCursorLocation();
  27. }
  28. },
  29. afterEach: function( assert ) {
  30. count++;
  31. if ( count === assert.test.module.tests.length ) {
  32. editor.remove();
  33. $( '#editor' ).remove();
  34. }
  35. }
  36. } );
  37. /**
  38. * Test whether attribute exists in a HTML string
  39. *
  40. * @param html The HTML string
  41. * @param attr string|object When string, test for the first instance of attr.
  42. * When object, break up the HTML string into individual tags and test for attr in the specified tag.
  43. * Format: { tagName: 'attr1 attr2', ... }
  44. * @return bool
  45. */
  46. function hasAttr( html, attr ) {
  47. var tagName, tags, tag, array, regex, i;
  48. if ( typeof attr === 'string' ) {
  49. return new RegExp( ' \\b' + attr + '\\b' ).test( html );
  50. }
  51. for ( tagName in attr ) {
  52. if ( tags = html.match( new RegExp( '<' + tagName + ' [^>]+>', 'g' ) ) ) {
  53. for ( tag in tags ) {
  54. array = attr[tagName].split(' ');
  55. for ( i in array ) {
  56. regex = new RegExp( '\\b' + array[i] + '\\b' );
  57. if ( regex.test( tags[tag] ) ) {
  58. attr[tagName] = attr[tagName].replace( regex, '' );
  59. }
  60. }
  61. }
  62. if ( attr[tagName].replace( / +/g, '' ).length ) {
  63. return false;
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. // Ref: http://www.w3.org/TR/html5/obsolete.html, http://developers.whatwg.org/obsolete.html
  70. QUnit.test('HTML elements non-conforming to HTML 5.0', function( assert ) {
  71. var testString;
  72. /*
  73. Not supported, deprecated in HTML 4.0 or earlier, and/or proprietary:
  74. applet
  75. bgsound
  76. dir
  77. frame
  78. frameset
  79. noframes
  80. isindex
  81. listing
  82. nextid
  83. noembed
  84. plaintext
  85. rb
  86. xmp
  87. basefont
  88. blink
  89. marquee
  90. multicol
  91. nobr
  92. spacer
  93. The rest are still supported in TinyMCE but "...must not be used by authors".
  94. */
  95. assert.expect(6);
  96. text = 'acronym';
  97. testString = '<p><acronym title="www">WWW</acronym></p>';
  98. editor.setContent( testString );
  99. assert.equal( editor.getContent(), testString, text );
  100. text = 'strike, converted to span';
  101. editor.setContent( '<strike>test</strike>' );
  102. assert.equal( editor.getContent(), '<p><span style="text-decoration: line-through;">test</span></p>', text );
  103. text = 'big';
  104. testString = '<p><big>test</big></p>';
  105. editor.setContent( testString );
  106. assert.equal( editor.getContent(), testString, text );
  107. text = 'center';
  108. testString = '<center>test</center>';
  109. editor.setContent( testString );
  110. assert.equal( editor.getContent(), testString, text );
  111. text = 'font, converted to span';
  112. editor.setContent( '<p><font size="4">test</font></p>' );
  113. assert.equal( editor.getContent(), '<p><span style="font-size: large;">test</span></p>', text );
  114. text = 'tt';
  115. testString = '<p><tt>test</tt></p>';
  116. editor.setContent( testString );
  117. assert.equal( editor.getContent(), testString, text );
  118. });
  119. QUnit.test('Obsolete (but still conforming) HTML attributes', function( assert ) {
  120. var testString;
  121. assert.expect(3);
  122. text = 'border on <img>';
  123. testString = '<p><img src="../../test.gif" alt="" border="5" /></p>';
  124. editor.setContent( testString );
  125. assert.equal( editor.getContent(), testString, text );
  126. text = 'Old style anchors';
  127. testString = '<p><a name="test"></a></p>';
  128. editor.setContent( testString );
  129. assert.equal( editor.getContent(), testString, text );
  130. text = 'maxlength, size on input type="number"';
  131. testString = '<p><input maxlength="5" size="10" type="number" value="" /></p>';
  132. editor.setContent( testString );
  133. assert.ok( hasAttr( editor.getContent(), { input: 'maxlength size' } ), text );
  134. });
  135. QUnit.test('Obsolete attributes in HTML 5.0', function( assert ) {
  136. var testString, text;
  137. assert.expect(22);
  138. text = 'charset, rev, shape, coords on <a> elements';
  139. testString = '<p><a href="javascript;:" charset="en" rev="made" shape="rect" coords="5,5">test</a></p>';
  140. editor.setContent( testString );
  141. assert.ok( hasAttr( editor.getContent(), { a: 'charset rev shape coords' } ), text );
  142. text = 'name, align, hspace, vspace on img elements';
  143. testString = '<p><img src="../../test.gif" alt="" name="test" align="left" hspace="5" vspace="5" /></p>';
  144. editor.setContent( testString );
  145. assert.ok( hasAttr( editor.getContent(), { img: 'name align hspace vspace' } ), text );
  146. text = 'name, align, hspace, vspace, on embed elements';
  147. testString = '<p><embed width="100" height="100" src="test.swf" vspace="5" hspace="5" align="left" name="test"></embed></p>';
  148. editor.setContent( testString );
  149. assert.ok( hasAttr( editor.getContent(), { embed: 'name align hspace vspace' } ), text );
  150. text = 'archive, classid, code, codebase, codetype, declare, standby on object elements';
  151. testString = '<p><object width="100" height="100" classid="clsid" codebase="clsid" standby="standby" codetype="1" code="1" archive="1" declare="declare"></object></p>';
  152. editor.setContent( testString );
  153. assert.ok( hasAttr( editor.getContent(), { object: 'archive classid code codebase codetype declare standby' } ), text );
  154. text = 'type, valuetype on param elements';
  155. testString = '<p><object width="100" height="100"><param type="" valuetype="" /></object></p>';
  156. editor.setContent( testString );
  157. assert.ok( hasAttr( editor.getContent(), { param: 'type valuetype' } ), text );
  158. text = 'align, bgcolor, border, cellpadding, cellspacing, frame, rules, summary, width on table elements';
  159. testString = '<table border="1" summary="" width="100" frame="" rules="" cellspacing="5" cellpadding="5" align="left" bgcolor="blue"><tbody><tr><td>test</td></tr></tbody></table>';
  160. editor.setContent( testString );
  161. assert.ok( hasAttr( editor.getContent(), { table: 'align bgcolor border cellpadding cellspacing frame rules summary width' } ), text );
  162. text = 'align, char, charoff, valign on tbody, thead, and tfoot elements';
  163. testString = '<table><thead align="left" char="" charoff="" valign="top"></thead><tfoot align="left" char="" charoff="" valign="top"></tfoot><tbody align="left" char="" charoff="" valign="top"><tr><th>test</th><td>test</td></tr></tbody></table>';
  164. editor.setContent( testString );
  165. assert.ok( hasAttr( editor.getContent(), {
  166. thead: 'align char charoff valign',
  167. tfoot: 'align char charoff valign',
  168. tbody: 'align char charoff valign'
  169. } ), text );
  170. text = 'axis, align, bgcolor, char, charoff, height, nowrap, valign, width on td and th elements, scope on td elements';
  171. testString = '<table><tbody><tr><th axis="" align="left" char="" charoff="" valign="top" nowrap="nowrap" bgcolor="blue" width="100" height="10">test</th><td axis="" align="left" char="" charoff="" valign="top" nowrap="nowrap" bgcolor="blue" width="100" height="10" scope="">test</td></tr></tbody></table>';
  172. editor.setContent( testString );
  173. assert.ok( hasAttr( editor.getContent(), {
  174. th: 'axis align bgcolor char charoff height nowrap valign width',
  175. td: 'axis align bgcolor char charoff height nowrap valign width scope'
  176. } ), text );
  177. text = 'align, bgcolor, char, charoff, valign on tr elements';
  178. testString = '<table><tbody><tr align="left" char="" charoff="" valign="top" bgcolor="blue"><td>test</td></tr></tbody></table>';
  179. editor.setContent( testString );
  180. assert.ok( hasAttr( editor.getContent(), { tr: 'align bgcolor char charoff valign' } ), text );
  181. text = 'clear on br elements';
  182. testString = '<p>test<br clear="all" />test</p>';
  183. editor.setContent( testString );
  184. assert.equal( editor.getContent(), testString, text );
  185. text = 'align on caption elements';
  186. testString = '<table><caption align="left">test</caption><tbody><tr><td>test</td></tr></tbody></table>';
  187. editor.setContent( testString );
  188. assert.equal( editor.getContent(), testString, text );
  189. text = 'align, char, charoff, valign, width on col elements';
  190. testString = '<table><colgroup><col width="100" align="left" char="a" charoff="1" valign="top" /><col /></colgroup><tbody><tr><td>test</td><td>test</td></tr></tbody></table>';
  191. editor.setContent( testString );
  192. assert.ok( hasAttr( editor.getContent(), { col: 'align char charoff valign width' } ), text );
  193. text = 'align on div, h1—h6, input, legend, p elements';
  194. testString = '<div align="left">1</div><h3 align="left">1</h3><p align="left">1</p><form><fieldset><legend align="left">test</legend><input type="text" align="left" /></fieldset></form>';
  195. editor.setContent( testString );
  196. assert.equal( editor.getContent(), testString, text );
  197. text = 'compact on dl elements';
  198. testString = '<dl compact="compact"><dd>1</dd></dl>';
  199. editor.setContent( testString );
  200. assert.equal( editor.getContent(), testString, text );
  201. text = 'align, hspace, vspace on embed elements';
  202. testString = '<p><embed width="100" height="100" vspace="5" hspace="5" align="left"></embed></p>';
  203. editor.setContent( testString );
  204. assert.ok( hasAttr( editor.getContent(), { embed: 'align hspace vspace' } ), text );
  205. text = 'align, noshade, size, width on hr elements';
  206. testString = '<hr align="left" noshade="noshade" size="1" width="100" />';
  207. editor.setContent( testString );
  208. assert.ok( hasAttr( editor.getContent(), { hr: 'align noshade size width' } ), text );
  209. text = 'align, frameborder, marginheight, marginwidth, scrolling on iframe elements';
  210. testString = '<p><iframe width="100" height="100" frameborder="1" marginwidth="5" marginheight="5" scrolling="" align="left"></iframe></p>';
  211. editor.setContent( testString );
  212. assert.ok( hasAttr( editor.getContent(), { iframe: 'align frameborder marginheight marginwidth scrolling' } ), text );
  213. text = 'type on li elements';
  214. testString = '<ul><li type="disc">test</li></ul>';
  215. editor.setContent( testString );
  216. assert.equal( editor.getContent(), testString, text );
  217. text = 'align, border, hspace, vspace on object elements';
  218. testString = '<p><object width="100" height="100" border="1" vspace="5" hspace="5" align="left"></object></p>';
  219. editor.setContent( testString );
  220. assert.ok( hasAttr( editor.getContent(), { object: 'align border hspace vspace' } ), text );
  221. text = 'compact on ol elements';
  222. testString = '<ol compact="compact"><li>test</li></ol>';
  223. editor.setContent( testString );
  224. assert.equal( editor.getContent(), testString, text );
  225. text = 'compact, type on ul elements';
  226. testString = '<ul type="disc" compact="compact"><li>test</li></ul>';
  227. editor.setContent( testString );
  228. assert.ok( hasAttr( editor.getContent(), { ul: 'compact type' } ), text );
  229. text = 'width on pre elements';
  230. testString = '<pre width="100">1</pre>';
  231. editor.setContent( testString );
  232. assert.equal( editor.getContent(), testString, text );
  233. });
  234. } )( window.jQuery, window.QUnit, window.tinymce );