Wordpress/tests/qunit/editor/tinymce/Formatter_remove.html
Andrew Ozz e5bed6707f TinyMCE: update to 4.0.18, see #24067
git-svn-id: https://develop.svn.wordpress.org/trunk@27387 602fd350-edb4-49c9-b593-d223f7449a82
2014-03-04 01:56:57 +00:00

405 lines
18 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Unit tests for remove formatting</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-git.css" type="text/css" />
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<script src="../js/qunit/reporter.js"></script>
<script src="../js/utils.js"></script>
<script src="../js/tinymce_loader.js"></script>
<script>
var editor, rng, format;
QUnit.config.reorder = false;
QUnit.config.autostart = false;
module("Remove formatting", {
autostart: false
});
function getContent() {
return editor.getContent().toLowerCase().replace(/[\r]+/g, '');
};
test('Inline element on selected text', function() {
editor.formatter.register('format', {inline : 'b'});
editor.getBody().innerHTML = '<p><b>1234</b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('b')[0].firstChild, 0);
rng.setEnd(editor.dom.select('b')[0].firstChild, 4);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p>1234</p>', 'Inline element on selected text');
});
test('Inline element on selected text with remove=all', function() {
editor.formatter.register('format', {selector : 'b', remove : 'all'});
editor.getBody().innerHTML = '<p><b title="text">1234</b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('b')[0].firstChild, 0);
rng.setEnd(editor.dom.select('b')[0].firstChild, 4);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p>1234</p>', 'Inline element on selected text with remove=all');
});
test('Inline element on selected text with remove=none', function() {
editor.formatter.register('format', {selector : 'span', styles : {fontWeight : 'bold'}, remove : 'none'});
editor.getBody().innerHTML = '<p><span style="font-weight:bold">1234</span></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('p')[0], 0);
rng.setEnd(editor.dom.select('p')[0], 1);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><span>1234</span></p>', 'Inline element on selected text with remove=none');
});
test('Inline element style where element is format root', function() {
editor.formatter.register('format', {inline : 'span', styles : {fontWeight : 'bold'}});
editor.getBody().innerHTML = '<p><span style="font-weight:bold; color:#FF0000"><em>1234</em></span></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('em')[0].firstChild, 1);
rng.setEnd(editor.dom.select('em')[0].firstChild, 3);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(),
'<p><span style="color: #ff0000; font-weight: bold;">' +
'<em>1</em></span><span style="color: #ff0000;"><em>23</em></span>' +
'<span style=\"color: #ff0000; font-weight: bold;\"><em>4' +
'</em></span></p>',
'Inline element style where element is format root');
});
test('Partially selected inline element text', function() {
editor.formatter.register('format', {inline : 'b'});
editor.getBody().innerHTML = '<p><b>1234</b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('b')[0].firstChild, 2);
rng.setEnd(editor.dom.select('b')[0].firstChild, 4);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><b>12</b>34</p>', 'Partially selected inline element text');
});
test('Partially selected inline element text with children', function() {
editor.formatter.register('format', {inline : 'b'});
editor.getBody().innerHTML = '<p><b><em><span>1234</span></em></b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('span')[0].firstChild, 2);
rng.setEnd(editor.dom.select('span')[0].firstChild, 4);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><b><em><span>12</span></em></b><em><span>34</span></em></p>', 'Partially selected inline element text with children');
});
test('Partially selected inline element text with complex children', function() {
editor.formatter.register('format', {inline : 'span', styles : {fontWeight : 'bold'}});
editor.getBody().innerHTML = '<p><span style="font-weight:bold"><em><span style="color:#ff0000;font-weight:bold">1234</span></em></span></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('span')[1].firstChild, 2);
rng.setEnd(editor.dom.select('span')[1].firstChild, 4);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><span style="font-weight: bold;"><em><span style="color: #ff0000; font-weight: bold;">12</span></em></span><em><span style="color: #ff0000;">34</span></em></p>', 'Partially selected inline element text with complex children');
});
test('Inline elements with exact flag', function() {
editor.formatter.register('format', {inline : 'span', styles : {color : '#ff0000'}, exact : true});
editor.getBody().innerHTML = '<p><span style="font-size:10px;color:#ff0000">1234</span><span style="font-size:10px;color:#00ff00">1234</span></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('p')[0], 0);
rng.setEnd(editor.dom.select('p')[0], 2);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><span style="font-size: 10px;">1234</span><span style="color: #00ff00; font-size: 10px;">1234</span></p>', 'Inline elements with exact flag');
});
test('Inline elements with variables', function() {
editor.formatter.register('format', {inline : 'span', styles : {color : '%color'}, exact : true});
editor.getBody().innerHTML = '<p><span style="font-size:10px;color:#ff0000">1234</span><span style="font-size:10px;color:#00ff00">1234</span></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('p')[0], 0);
rng.setEnd(editor.dom.select('p')[0], 2);
editor.selection.setRng(rng);
editor.formatter.remove('format', {color : '#ff0000'});
equal(getContent(), '<p><span style="font-size: 10px;">1234</span><span style="color: #00ff00; font-size: 10px;">1234</span></p>', 'Inline elements on selected text with variables');
});
test('Inline elements with functions and variables', function() {
editor.formatter.register('format', {
inline : 'span',
styles : {
color : function(vars) {
return vars.color + "00";
}
},
exact : true
});
editor.getBody().innerHTML = '<p><span style="font-size:10px;color:#ff0000">1234</span><span style="font-size:10px;color:#00ff00">1234</span></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('p')[0], 0);
rng.setEnd(editor.dom.select('p')[0], 2);
editor.selection.setRng(rng);
editor.formatter.remove('format', {
color : '#ff00'
});
equal(getContent(), '<p><span style="font-size: 10px;">1234</span><span style="color: #00ff00; font-size: 10px;">1234</span></p>', 'Inline elements with functions and variables');
});
test('End within start element', function() {
editor.formatter.register('format', {inline : 'b'});
editor.getBody().innerHTML = '<p><b>1234<b>5678</b></b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('p')[0], 0);
rng.setEnd(editor.dom.select('b')[0], 2);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p>12345678</p>', 'End within start element');
});
test('Start and end within similar format 1', function() {
editor.formatter.register('format', {inline : 'b'});
editor.getBody().innerHTML = '<p><b><em><b>1234<b>5678</b></b></em></b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('em')[0], 0);
rng.setEnd(editor.dom.select('b')[1], 2);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><em>12345678</em></p>', 'Start and end within similar format 1');
});
test('Start and end within similar format 2', function() {
editor.formatter.register('format', {inline : 'b'});
editor.getBody().innerHTML = '<p><b><em><b>1234</b><b>5678</b></em></b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('em')[0], 0);
rng.setEnd(editor.dom.select('em')[0], 1);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><em>1234</em><b><em><b>5678</b></em></b></p>', 'Start and end within similar format 2');
});
test('Start and end within similar format 3', function() {
editor.formatter.register('format', {inline : 'b'});
editor.getBody().innerHTML = '<p><b><em><b>1234</b></em></b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('em')[0], 0);
rng.setEnd(editor.dom.select('em')[0], 1);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><em>1234</em></p>', 'Start and end within similar format 3');
});
test('End within start', function() {
editor.formatter.register('format', {inline : 'b'});
editor.getBody().innerHTML = '<p><b><em>x<b>abc</b>y</em></b></p>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('p')[0], 0);
rng.setEnd(editor.dom.select('b')[1].firstChild, 3);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><em>x</em><em>abc</em><b><em>y</em></b></p>', 'End within start');
});
test('Remove block format', function() {
editor.formatter.register('format', {block : 'h1'});
editor.getBody().innerHTML = '<h1>text</h1>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('h1')[0].firstChild, 0);
rng.setEnd(editor.dom.select('h1')[0].firstChild, 4);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p>text</p>', 'Remove block format');
});
test('Remove wrapper block format', function() {
editor.formatter.register('format', {block : 'blockquote', wrapper : true});
editor.getBody().innerHTML = '<blockquote><p>text</p></blockquote>';
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('p')[0].firstChild, 0);
rng.setEnd(editor.dom.select('p')[0].firstChild, 4);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p>text</p>', 'Remove wrapper block format');
});
test('Remove span format within block with style', function() {
editor.formatter.register('format', {selector : 'span', attributes : ['style', 'class'], remove : 'empty', split : true, expand : false, deep : true});
rng = editor.dom.createRng();
editor.getBody().innerHTML = '<p style="color:#ff0000"><span style="color:#00ff00">text</span></p>';
rng.setStart(editor.dom.select('span')[0].firstChild, 1);
rng.setEnd(editor.dom.select('span')[0].firstChild, 3);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p style="color: #ff0000;"><span style="color: #00ff00;">t</span>ex<span style="color: #00ff00;">t</span></p>', 'Remove span format within block with style');
});
test('Remove and verify start element', function() {
editor.formatter.register('format', {inline : 'b'});
rng = editor.dom.createRng();
editor.getBody().innerHTML = '<p><b>text</b></p>';
rng.setStart(editor.dom.select('b')[0].firstChild, 1);
rng.setEnd(editor.dom.select('b')[0].firstChild, 3);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), '<p><b>t</b>ex<b>t</b></p>');
equal(editor.selection.getStart().nodeName, 'P');
});
test('Remove with selection collapsed ensure correct caret position', function() {
var content = '<p>test</p><p>testing</p>';
editor.formatter.register('format', {block : 'p'});
rng = editor.dom.createRng();
editor.getBody().innerHTML = content;
rng.setStart(editor.dom.select('p')[0].firstChild, 4);
rng.setEnd(editor.dom.select('p')[0].firstChild, 4);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(getContent(), content);
equal(editor.selection.getStart(), editor.dom.select('p')[0]);
});
test('Caret format at middle of text', function() {
editor.setContent('<p><b>abc</b></p>');
editor.formatter.register('format', {inline: 'b'});
setSelection('b', 1, 'b', 1);
editor.formatter.remove('format');
equal(editor.getContent(), '<p>abc</p>');
});
test('Caret format at end of text', function() {
editor.setContent('<p><b>abc</b></p>');
editor.formatter.register('format', {inline: 'b'});
setSelection('b', 3, 'b', 3);
editor.formatter.remove('format');
type('d');
equal(editor.getContent(), '<p><b>abc</b>d</p>');
});
test('Caret format at end of text inside other format', function() {
editor.setContent('<p><em><b>abc</b></em></p>');
editor.formatter.register('format', {inline: 'b'});
setSelection('b', 3, 'b', 3);
editor.formatter.remove('format');
type('d');
equal(editor.getContent(), '<p><em><b>abc</b>d</em></p>');
});
test('Caret format at end of text inside other format with text after 1', function() {
editor.setContent('<p><em><b>abc</b></em>e</p>');
editor.formatter.register('format', {inline: 'b'});
setSelection('b', 3, 'b', 3);
editor.formatter.remove('format');
type('d');
equal(editor.getContent(), '<p><em><b>abc</b>d</em>e</p>');
});
test('Caret format at end of text inside other format with text after 2', function() {
editor.setContent('<p><em><b>abc</b></em>e</p>');
editor.formatter.register('format', {inline: 'em'});
setSelection('b', 3, 'b', 3);
editor.formatter.remove('format');
type('d');
equal(editor.getContent(), '<p><em><b>abc</b></em><b>d</b>e</p>');
});
test('Caret format on second word in table cell', function() {
editor.setContent('<table><tbody><tr><td>one <b>two</b></td></tr></tbody></table>');
editor.formatter.register('format', {inline: 'b'});
setSelection('b', 2, 'b', 2);
editor.formatter.remove('format');
equal(editor.getContent(), '<table><tbody><tr><td>one two</td></tr></tbody></table>');
});
test('contentEditable: false on start and contentEditable: true on end', function() {
var rng;
editor.formatter.register('format', {inline: 'b'});
editor.setContent('<p>abc</p><p contenteditable="false"><b>def</b></p><p><b>ghj</b></p>');
rng = editor.dom.createRng();
rng.setStart(editor.dom.select('b')[0].firstChild, 0);
rng.setEnd(editor.dom.select('b')[1].firstChild, 3);
editor.selection.setRng(rng);
editor.formatter.remove('format');
equal(editor.getContent(), '<p>abc</p><p><b>def</b></p><p>ghj</p>', 'Text in last paragraph is not bold');
});
test('contentEditable: true on start and contentEditable: false on end', function() {
editor.formatter.register('format', {inline: 'b'});
editor.setContent('<p>abc</p><p><b>def</b></p><p contenteditable="false"><b>ghj</b></p>');
setSelection('p:nth-child(2) b', 0, 'p:last b', 3);
editor.formatter.remove('format');
equal(editor.getContent(), '<p>abc</p><p>def</p><p><b>ghj</b></p>', 'Text in first paragraph is not bold');
});
test('contentEditable: true inside contentEditable: false', function() {
editor.formatter.register('format', {inline: 'b'});
editor.setContent('<p>abc</p><p contenteditable="false"><span contenteditable="true"><b>def</b></span></p>');
setSelection('b', 0, 'b', 3);
editor.formatter.remove('format');
equal(editor.getContent(), '<p>abc</p><p><span>def</span></p>', 'Text is not bold');
});
test('remove format block on contentEditable: false block', function() {
editor.formatter.register('format', {block: 'h1'});
editor.setContent('<p>abc</p><h1 contenteditable="false">def</h1>');
setSelection('h1:nth-child(2)', 0, 'h1:nth-child(2)', 3);
editor.formatter.remove('format');
equal(editor.getContent(), '<p>abc</p><h1>def</h1>', 'H1 is still not h1');
});
/*
test('Remove format bug 1', function() {
editor.setContent('<p><b>ab<em>cde</em>fgh</b></p>');
editor.formatter.register('format', {inline: 'b'});
setSelection('em', 0, 'em', 2);
editor.formatter.remove('format');
equal(editor.getContent(), '<p><b>ab</b><em>cd</em><b><em>e</em>fgh</b></p>');
});
*/
// WP
var url = document.location.href.substring( 0, document.location.href.lastIndexOf('tinymce/') );
tinymce.init({
mode : "exact",
elements : "elm1",
// WP
external_plugins: {
noneditable: url + 'external-plugins/noneditable/plugin.js'
},
// WP end
indent : false,
add_unload_trigger : false,
theme_advanced_styles : 'test1=test1;test2=test2',
valid_elements : '@[contenteditable|id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur],strong,b,em,i,strike,u,#p,-ol[type|compact],-ul[type|compact],-li,br,img[longdesc|usemap|src|border|alt=|title|hspace|vspace|width|height|align],-sub,-sup,-blockquote[cite],-table[border|cellspacing|cellpadding|width|frame|rules|height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],object[classid|width|height|codebase|*],param[name|value],embed[type|width|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value|tabindex|accesskey],kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],q[cite],samp,select[disabled|multiple|name|size],small,textarea[cols|rows|disabled|name|readonly],tt,var,big',
fix_list_elements : 0,
fix_table_elements : 0,
entities : 'raw',
valid_styles : {
'*' : 'color,font-size,font-family,background-color,font-weight,font-style,text-decoration,float,margin,margin-top,margin-right,margin-bottom,margin-left,display'
},
disable_nodechange: true,
init_instance_callback : function(ed) {
editor = ed;
QUnit.start();
}
});
</script>
</head>
<body>
<h1 id="qunit-header">Unit tests for text formatting</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<textarea id="elm1" name="elm1"></textarea>
<div>
<a href="javascript:alert(tinymce.EditorManager.get('elm1').getContent({format : 'raw'}));">[getRawContents]</a>
<a href="javascript:alert(tinymce.EditorManager.get('elm1').getContent());">[getContents]</a>
</div>
</body>
</html>