272 lines
11 KiB
HTML
272 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Unit tests for check 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/tinymce_loader.js"></script>
|
|
<script src="../js/utils.js"></script>
|
|
<script>
|
|
var editor, inlineEditor, rng, format;
|
|
|
|
QUnit.config.reorder = false;
|
|
QUnit.config.autostart = false;
|
|
module("Check formatting", {
|
|
autostart: false
|
|
});
|
|
|
|
function getContent() {
|
|
return editor.getContent().toLowerCase().replace(/[\r\n]+/g, '');
|
|
};
|
|
|
|
test('Selected style element text', function() {
|
|
editor.formatter.register('bold', {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);
|
|
ok(editor.formatter.match('bold'), 'Selected style element text');
|
|
});
|
|
|
|
test('Selected style element with css styles', function() {
|
|
editor.formatter.register('color', {inline : 'span', styles : {color : '#ff0000'}});
|
|
editor.getBody().innerHTML = '<p><span style="color:#ff0000">1234</span></p>';
|
|
rng = editor.dom.createRng();
|
|
rng.setStart(editor.dom.select('span')[0].firstChild, 0);
|
|
rng.setEnd(editor.dom.select('span')[0].firstChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(editor.formatter.match('color'), 'Selected style element with css styles');
|
|
});
|
|
|
|
test('Selected style element with attributes', function() {
|
|
editor.formatter.register('fontsize', {inline : 'font', attributes : {size : '7'}});
|
|
editor.getBody().innerHTML = '<p><font size="7">1234</font></p>';
|
|
rng = editor.dom.createRng();
|
|
rng.setStart(editor.dom.select('font')[0].firstChild, 0);
|
|
rng.setEnd(editor.dom.select('font')[0].firstChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(editor.formatter.match('fontsize'), 'Selected style element with attributes');
|
|
});
|
|
|
|
test('Selected style element text multiple formats', function() {
|
|
editor.formatter.register('multiple', [
|
|
{inline : 'b'},
|
|
{inline : 'strong'}
|
|
]);
|
|
editor.getBody().innerHTML = '<p><strong>1234</strong></p>';
|
|
rng = editor.dom.createRng();
|
|
rng.setStart(editor.dom.select('strong')[0].firstChild, 0);
|
|
rng.setEnd(editor.dom.select('strong')[0].firstChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(editor.formatter.match('multiple'), 'Selected style element text multiple formats');
|
|
});
|
|
|
|
test('Selected complex style element', function() {
|
|
editor.formatter.register('complex', {inline : 'span', styles : {fontWeight : 'bold'}});
|
|
editor.getBody().innerHTML = '<p><span style="color:#ff0000; font-weight:bold">1234</span></p>';
|
|
rng = editor.dom.createRng();
|
|
rng.setStart(editor.dom.select('span')[0].firstChild, 0);
|
|
rng.setEnd(editor.dom.select('span')[0].firstChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(editor.formatter.match('complex'), 'Selected complex style element');
|
|
});
|
|
|
|
test('Selected non style element text', function() {
|
|
editor.formatter.register('bold', {inline : 'b'});
|
|
editor.getBody().innerHTML = '<p>1234</p>';
|
|
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);
|
|
ok(!editor.formatter.match('bold'), 'Selected non style element text');
|
|
});
|
|
|
|
test('Selected partial style element (start)', function() {
|
|
editor.formatter.register('bold', {inline : 'b'});
|
|
editor.getBody().innerHTML = '<p><b>1234</b>5678</p>';
|
|
rng = editor.dom.createRng();
|
|
rng.setStart(editor.dom.select('b')[0].firstChild, 0);
|
|
rng.setEnd(editor.dom.select('p')[0].lastChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(editor.formatter.match('bold'), 'Selected partial style element (start)');
|
|
});
|
|
|
|
test('Selected partial style element (end)', function() {
|
|
editor.formatter.register('bold', {inline : 'b'});
|
|
editor.getBody().innerHTML = '<p>1234<b>5678</b></p>';
|
|
rng = editor.dom.createRng();
|
|
rng.setStart(editor.dom.select('p')[0].firstChild, 0);
|
|
rng.setEnd(editor.dom.select('b')[0].lastChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(!editor.formatter.match('bold'), 'Selected partial style element (end)');
|
|
});
|
|
|
|
test('Selected element text with parent inline element', function() {
|
|
editor.formatter.register('bold', {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, 0);
|
|
rng.setEnd(editor.dom.select('span')[0].firstChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(editor.formatter.match('bold'), 'Selected element text with parent inline element');
|
|
});
|
|
|
|
test('Selected element match with variable', function() {
|
|
editor.formatter.register('complex', {inline : 'span', styles : {color : '%color'}});
|
|
editor.getBody().innerHTML = '<p><span style="color:#ff0000">1234</span></p>';
|
|
rng = editor.dom.createRng();
|
|
rng.setStart(editor.dom.select('span')[0].firstChild, 0);
|
|
rng.setEnd(editor.dom.select('span')[0].firstChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(editor.formatter.match('complex', {color : '#ff0000'}), 'Selected element match with variable');
|
|
});
|
|
|
|
test('Selected element match with variable and function', function() {
|
|
editor.formatter.register('complex', {
|
|
inline : 'span',
|
|
styles : {
|
|
color : function(vars) {
|
|
return vars.color + '00';
|
|
}
|
|
}
|
|
});
|
|
|
|
editor.getBody().innerHTML = '<p><span style="color:#ff0000">1234</span></p>';
|
|
rng = editor.dom.createRng();
|
|
rng.setStart(editor.dom.select('span')[0].firstChild, 0);
|
|
rng.setEnd(editor.dom.select('span')[0].firstChild, 4);
|
|
editor.selection.setRng(rng);
|
|
ok(editor.formatter.match('complex', {color : '#ff00'}), 'Selected element match with variable and function');
|
|
});
|
|
|
|
test('formatChanged simple format', function() {
|
|
var newState, newArgs;
|
|
|
|
editor.formatter.formatChanged('bold', function(state, args) {
|
|
newState = state;
|
|
newArgs = args;
|
|
});
|
|
|
|
editor.getBody().innerHTML = '<p>text</p>';
|
|
setSelection('p', 0, 'p', 4);
|
|
|
|
// Check apply
|
|
editor.formatter.apply('bold');
|
|
editor.nodeChanged();
|
|
ok(newState);
|
|
equal(newArgs.format, 'bold');
|
|
equal(newArgs.node, editor.getBody().firstChild.firstChild);
|
|
equal(newArgs.parents.length, 2);
|
|
|
|
// Check remove
|
|
editor.formatter.remove('bold');
|
|
editor.nodeChanged();
|
|
ok(!newState);
|
|
equal(newArgs.format, 'bold');
|
|
equal(newArgs.node, editor.getBody().firstChild);
|
|
equal(newArgs.parents.length, 1);
|
|
});
|
|
|
|
test('formatChanged complex format', function() {
|
|
var newState, newArgs;
|
|
|
|
editor.formatter.register('complex', {inline : 'span', styles : {color : '%color'}});
|
|
|
|
editor.formatter.formatChanged('complex', function(state, args) {
|
|
newState = state;
|
|
newArgs = args;
|
|
}, true);
|
|
|
|
editor.getBody().innerHTML = '<p>text</p>';
|
|
setSelection('p', 0, 'p', 4);
|
|
|
|
// Check apply
|
|
editor.formatter.apply('complex', {color: '#FF0000'});
|
|
editor.nodeChanged();
|
|
ok(newState);
|
|
equal(newArgs.format, 'complex');
|
|
equal(newArgs.node, editor.getBody().firstChild.firstChild);
|
|
equal(newArgs.parents.length, 2);
|
|
|
|
// Check remove
|
|
editor.formatter.remove('complex', {color: '#FF0000'});
|
|
editor.nodeChanged();
|
|
ok(!newState);
|
|
equal(newArgs.format, 'complex');
|
|
equal(newArgs.node, editor.getBody().firstChild);
|
|
equal(newArgs.parents.length, 1);
|
|
});
|
|
|
|
test('Match format on div block in inline mode', function() {
|
|
inlineEditor.setContent('<p>a</p><p>b</p>');
|
|
inlineEditor.execCommand('SelectAll');
|
|
ok(!inlineEditor.formatter.match('div'), 'Formatter.match on div says true');
|
|
});
|
|
|
|
tinymce.init({
|
|
mode : "exact",
|
|
elements : "elm1",
|
|
add_unload_trigger : false,
|
|
theme_advanced_styles : 'test1=test1;test2=test2',
|
|
valid_elements : '@[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'
|
|
},
|
|
init_instance_callback : function(ed) {
|
|
editor = ed;
|
|
|
|
if (inlineEditor) {
|
|
QUnit.start();
|
|
}
|
|
}
|
|
});
|
|
|
|
var url = document.location.href.substring( 0, document.location.href.lastIndexOf('tinymce/') );
|
|
|
|
tinymce.init({
|
|
selector: "#elm2",
|
|
inline: true,
|
|
external_plugins: {
|
|
noneditable: url + 'external-plugins/noneditable/plugin.js'
|
|
},
|
|
add_unload_trigger: false,
|
|
indent: false,
|
|
theme_advanced_styles: 'test1=test1;test2=test2',
|
|
forced_root_block: '',
|
|
convert_fonts_to_spans: false,
|
|
disable_nodechange: true,
|
|
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'
|
|
},
|
|
init_instance_callback: function(ed) {
|
|
inlineEditor = ed;
|
|
|
|
if (editor) {
|
|
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>
|
|
<div id="elm2"></div>
|
|
</body>
|
|
</html>
|