2015-06-15 09:42:48 -05:00
|
|
|
/*global expect, describe, afterEach, beforeEach, it */
|
2015-06-16 04:49:21 -05:00
|
|
|
expect = expect.clone()
|
|
|
|
.addType({
|
|
|
|
name: 'SquireRTE',
|
|
|
|
base: 'object',
|
|
|
|
identify: function (value) {
|
|
|
|
return value instanceof Squire;
|
|
|
|
},
|
|
|
|
inspect: function (value) {
|
|
|
|
return 'Squire RTE: ' + value.getHTML();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.addAssertion('[not] to contain HTML', function (expect, editor, expectedValue) {
|
|
|
|
this.errorMode = 'bubble';
|
2015-06-16 05:39:31 -05:00
|
|
|
var actualHTML = editor.getHTML().replace(/<br>/g, '');
|
|
|
|
// BR tags are inconsistent across browsers. Removing them allows cross-browser testing.
|
|
|
|
expect(actualHTML, '[not] to be', expectedValue);
|
2015-06-16 04:49:21 -05:00
|
|
|
});
|
|
|
|
|
2015-06-15 09:42:48 -05:00
|
|
|
describe('Squire RTE', function () {
|
2015-06-16 04:49:21 -05:00
|
|
|
var doc, editor;
|
2015-06-15 09:42:48 -05:00
|
|
|
beforeEach(function () {
|
2015-06-16 04:49:21 -05:00
|
|
|
var iframe = document.getElementById('testFrame');
|
|
|
|
doc = iframe.contentDocument;
|
|
|
|
editor = new Squire(doc);
|
|
|
|
});
|
|
|
|
|
|
|
|
function selectAll(editor) {
|
|
|
|
var range = doc.createRange();
|
2015-06-16 05:39:31 -05:00
|
|
|
range.setStart(doc.body.childNodes.item(0), 0);
|
|
|
|
range.setEnd(doc.body.childNodes.item(0), doc.body.childNodes.item(0).childNodes.length);
|
2015-06-16 04:49:21 -05:00
|
|
|
editor.setSelection(range);
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:39:31 -05:00
|
|
|
describe('removeAllFormatting', function () {
|
2015-06-16 04:49:21 -05:00
|
|
|
// Trivial cases
|
|
|
|
it('removes inline styles', function () {
|
2015-06-16 05:39:31 -05:00
|
|
|
var startHTML = '<div><i>one</i> <b>two</b> <u>three</u> <sub>four</sub> <sup>five</sup></div>';
|
2015-06-16 04:49:21 -05:00
|
|
|
editor.setHTML(startHTML);
|
|
|
|
expect(editor, 'to contain HTML', startHTML);
|
|
|
|
selectAll(editor);
|
|
|
|
editor.removeAllFormatting();
|
2015-06-16 05:39:31 -05:00
|
|
|
expect(editor, 'to contain HTML', '<div>one two three four five</div>');
|
2015-06-16 04:49:21 -05:00
|
|
|
});
|
|
|
|
it('removes block styles', function () {
|
2015-06-16 05:39:31 -05:00
|
|
|
var startHTML = '<div><blockquote>one</blockquote><ul><li>two</li></ul>' +
|
|
|
|
'<ol><li>three</li></ol><table><tbody><tr><th>four</th><td>five</td></tr></tbody></table></div>';
|
2015-06-16 04:49:21 -05:00
|
|
|
editor.setHTML(startHTML);
|
|
|
|
expect(editor, 'to contain HTML', startHTML);
|
|
|
|
selectAll(editor);
|
|
|
|
editor.removeAllFormatting();
|
2015-06-16 05:39:31 -05:00
|
|
|
var expectedHTML = '<div><div>one</div><div>two</div><div>three</div><div>four</div><div>five</div></div>';
|
|
|
|
expect(editor, 'to contain HTML', expectedHTML);
|
2015-06-16 04:49:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Potential bugs
|
|
|
|
it('removes styles that begin inside the range', function () {
|
2015-06-16 05:39:31 -05:00
|
|
|
var startHTML = '<div>one <i>two three four five</i></div>';
|
2015-06-16 04:49:21 -05:00
|
|
|
editor.setHTML(startHTML);
|
|
|
|
expect(editor, 'to contain HTML', startHTML);
|
|
|
|
var range = doc.createRange();
|
|
|
|
range.setStart(doc.body.childNodes.item(0), 0);
|
|
|
|
range.setEnd(doc.getElementsByTagName('i').item(0).childNodes.item(0), 4);
|
|
|
|
editor.removeAllFormatting(range);
|
2015-06-16 05:39:31 -05:00
|
|
|
expect(editor, 'to contain HTML', '<div>one two <i>three four five</i></div>');
|
2015-06-16 04:49:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('removes styles that end inside the range', function () {
|
2015-06-16 05:39:31 -05:00
|
|
|
var startHTML = '<div><i>one two three four</i> five</div>';
|
2015-06-16 04:49:21 -05:00
|
|
|
editor.setHTML(startHTML);
|
|
|
|
expect(editor, 'to contain HTML', startHTML);
|
|
|
|
var range = doc.createRange();
|
|
|
|
range.setStart(doc.getElementsByTagName('i').item(0).childNodes.item(0), 13);
|
|
|
|
range.setEnd(doc.body.childNodes.item(0), doc.body.childNodes.item(0).childNodes.length);
|
|
|
|
editor.removeAllFormatting(range);
|
|
|
|
expect(editor, 'to contain HTML', '<div><i>one two three</i> four five</div>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes styles enclosed by the range', function () {
|
2015-06-16 05:39:31 -05:00
|
|
|
var startHTML = '<div>one <i>two three four</i> five</div>';
|
2015-06-16 04:49:21 -05:00
|
|
|
editor.setHTML(startHTML);
|
|
|
|
expect(editor, 'to contain HTML', startHTML);
|
|
|
|
var range = doc.createRange();
|
|
|
|
range.setStart(doc.body.childNodes.item(0), 0);
|
|
|
|
range.setEnd(doc.body.childNodes.item(0), doc.body.childNodes.item(0).childNodes.length);
|
|
|
|
editor.removeAllFormatting(range);
|
|
|
|
expect(editor, 'to contain HTML', '<div>one two three four five</div>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes styles enclosing the range', function () {
|
2015-06-16 05:39:31 -05:00
|
|
|
var startHTML = '<div><i>one two three four five</i></div>';
|
2015-06-16 04:49:21 -05:00
|
|
|
editor.setHTML(startHTML);
|
|
|
|
expect(editor, 'to contain HTML', startHTML);
|
|
|
|
var range = doc.createRange();
|
|
|
|
range.setStart(doc.getElementsByTagName('i').item(0).childNodes.item(0), 4);
|
|
|
|
range.setEnd(doc.getElementsByTagName('i').item(0).childNodes.item(0), 18);
|
|
|
|
editor.removeAllFormatting(range);
|
2015-06-16 05:39:31 -05:00
|
|
|
expect(editor, 'to contain HTML', '<div><i>one </i>two three four<i> five</i></div>');
|
2015-06-16 04:49:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('removes nested styles and closes tags correctly', function () {
|
2015-06-16 05:39:31 -05:00
|
|
|
var startHTML = '<table><tbody><tr><td>one</td></tr><tr><td>two</td><td>three</td></tr><tr><td>four</td><td>five</td></tr></tbody></table>';
|
2015-06-16 04:49:21 -05:00
|
|
|
editor.setHTML(startHTML);
|
|
|
|
expect(editor, 'to contain HTML', startHTML);
|
|
|
|
var range = doc.createRange();
|
|
|
|
range.setStart(doc.getElementsByTagName('td').item(1), 0);
|
|
|
|
range.setEnd(doc.getElementsByTagName('td').item(2), doc.getElementsByTagName('td').item(2).childNodes.length);
|
|
|
|
editor.removeAllFormatting(range);
|
2015-06-16 05:39:31 -05:00
|
|
|
expect(editor, 'to contain HTML', '<table><tbody><tr><td>one</td></tr></tbody></table>' +
|
|
|
|
'<div>two</div>' +
|
|
|
|
'<div>three</div>' +
|
|
|
|
'<table><tbody><tr><td>four</td><td>five</td></tr></tbody></table>');
|
2015-06-16 04:49:21 -05:00
|
|
|
});
|
2015-06-15 09:42:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
editor = null;
|
2015-06-16 04:49:21 -05:00
|
|
|
var iframe = document.getElementById('testFrame');
|
2015-06-16 05:39:31 -05:00
|
|
|
iframe.src = 'blank.html';
|
2015-06-15 09:42:48 -05:00
|
|
|
});
|
|
|
|
});
|