/*****************************************************************************
*
* Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
*
* This software is distributed under the terms of the Kupu
* License. See LICENSE.txt for license text. For a list of Kupu
* Contributors see CREDITS.txt.
*
*****************************************************************************/
// $Id: test_xhtml.js 15802 2005-08-09 09:06:11Z duncan $
// Various tests for html -> xhtml processing.
function KupuXhtmlTestCase() {
this.name = 'KupuXhtmlTestCase';
this.incontext = function(s) {
return '
test'+s+'';
}
this.verifyResult = function(newdoc, exp) {
var expected = this.incontext(exp);
var actual = Sarissa.serialize(newdoc);
actual = actual.replace('\xa0', ' ');
if (actual == expected)
return;
var context = /test<\/title><\/head>(.*)<\/body><\/html>/;
if (context.test(actual) && context.test(expected)) {
var a = context.exec(actual)[1];
var e = context.exec(expected)[1];
throw('Assertion failed: ' + a + ' != ' + e);
}
throw('Assertion failed: ' + actual + ' != ' + expected);
}
this.conversionTest = function(data, expected) {
var doc = this.doc.documentElement;
var editor = this.editor;
this.body.innerHTML = data;
var xhtmldoc = Sarissa.getDomDocument();
var newdoc = editor._convertToSarissaNode(xhtmldoc, this.doc.documentElement);
this.verifyResult(newdoc, expected);
}
this.setUp = function() {
var iframe = document.getElementById('iframe');
this.doc = iframe.contentWindow.document;
this.body = this.doc.getElementsByTagName('body')[0];
this.doc.getElementsByTagName('title')[0].text = 'test';
this.editor = new KupuEditor(null, {}, null);
};
this.arrayContains = function(ary, test) {
for (var i = 0; i < ary.length; i++) {
if (ary[i]==test) {
return true;
}
}
return false;
}
this.testExclude = function() {
// Check that the exclude functions work as expected.
var validator = new XhtmlValidation(this.editor);
var events = ['onclick', 'ondblclick', 'onmousedown',
'onmouseup', 'onmouseover', 'onmousemove',
'onmouseout', 'onkeypress', 'onkeydown',
'onkeyup'];
var expected = ['onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseout', 'onkeypress', 'onkeyup'];
var actual = validator._exclude(events, 'onmouseover|onmousemove|onkeydown');
this.assertEquals(actual.toString(), expected.toString());
// check is also works with arrays.
actual = validator._exclude(events, ['onmouseover','onmousemove','onkeydown']);
this.assertEquals(actual.toString(), expected.toString());
// Check we have a bgcolor attribute
this.assertTrue(this.arrayContains(validator.tagAttributes.thead, 'charoff'));
this.assertTrue(validator.attrFilters['charoff'] != null);
validator.excludeAttributes(['charoff']);
this.assertTrue(validator.attrFilters['charoff']==null);
this.assertTrue(!this.arrayContains(validator.tagAttributes.thead, 'charoff'));
this.assertTrue(this.arrayContains(validator.tagAttributes.td, 'height'));
this.assertTrue(this.arrayContains(validator.tagAttributes.th, 'height'));
validator.excludeTagAttributes(['table','th'], ['width','height']);
this.assertTrue(this.arrayContains(validator.tagAttributes.td, 'height'));
this.assertFalse(this.arrayContains(validator.tagAttributes.th, 'height'));
}
this.testSet = function() {
var validator = new XhtmlValidation(this.editor);
var set1 = new validator.Set(['a','b','c']);
this.assertTrue(set1.a && set1.b && set1.c);
var set2 = new validator.Set(set1);
this.assertTrue(set2.a && set2.b && set2.c);
}
this.testValidator = function() {
var validator = new XhtmlValidation(this.editor);
var table = validator.States['table'];
var tags = [];
for (var tag in table) {
this.assertEquals(table[tag], 1);
tags.push(tag);
}
this.assertEquals(tags.toString(),
['caption','col','colgroup',
'thead','tfoot','tbody','tr'].toString());
};
this.testConvertToSarissa = function() {
var data = 'This is a test
';
this.conversionTest(data, data);
}
this.testXmlAttrs = function() {
var data = 'This is a test
';
var expected1 = 'This is a test
';
this.conversionTest(data, expected1);
var expected2 = 'This is a test
';
this.editor.xhtmlvalid.excludeAttributes(['xml:lang','xml:space']);
this.conversionTest(data, expected2);
}
this.testConvertToSarissa2 = function() {
var data = 'This is a test
';
this.conversionTest(data, data);
}
this.testbadTags = function() {
var data = '';
var expected = '';
this.conversionTest(data, expected);
}
this.testnbsp = function() {
var data = 'Text with non-break space
';
this.conversionTest(data, data);
};
this.teststyle = function() {
var data = 'Text aligned right
';
var expected = 'Text aligned right
';
var doc = this.doc.documentElement;
var editor = this.editor;
this.body.innerHTML = data;
this.body.firstChild.style.textAlign = 'left';
this.body.firstChild.style.display = 'block';
//alert(this.body.firstChild.style.cssText);
var xhtmldoc = Sarissa.getDomDocument();
var newdoc = editor._convertToSarissaNode(xhtmldoc, this.doc.documentElement);
this.verifyResult(newdoc, expected);
};
this.testclass = function() {
var data = 'This is a test
';
var expected = 'This is a test
';
this.conversionTest(data, expected);
}
this.testclass2 = function() {
var data = 'This is a test
';
var expected = 'This is a test
';
this.conversionTest(data, expected);
}
this.testTable = function() {
// N.B. This table contains text and a tag where they
// aren't legal. Mozilla strips out the
tag but lets the
// text through, IE lets both through.
var data = '
BADTEXT!Col 01 | Col 11 | '+
'Col 21 |
'+
''+
'text | '+
'a | '+
'r |
'+
''+
'more text | '+
'aaa | '+
'rr |
'+
''+
'yet more text | '+
'aaaaa | '+
'rrr |
';
var expected = 'Col 01 | Col 11 | '+
'Col 21 |
'+
''+
'text | '+
'a | '+
'r |
'+
''+
'more text | '+
'aaa | '+
'rr |
'+
''+
'yet more text | '+
'aaaaa | '+
'rrr |
';
this.editor.xhtmlvalid.filterstructure = true;
this.conversionTest(data, expected);
}
this.testCustomAttribute = function() {
var validator = this.editor.xhtmlvalid;
var data = 'This is a test
';
this.assertTrue(validator.tagAttributes.td===validator.tagAttributes.th);
this.editor.xhtmlvalid.includeTagAttributes(['div','td'],['special']);
// Check that shared arrays are no longer shared...
this.assertFalse(validator.tagAttributes.td===validator.tagAttributes.th);
this.assertTrue(this.arrayContains(validator.tagAttributes.td, 'special'));
this.assertFalse(this.arrayContains(validator.tagAttributes.th, 'special'));
this.editor.xhtmlvalid.setAttrFilter(['special']);
this.conversionTest(data, data);
}
this.tearDown = function() {
this.body.innerHTML = '';
};
}
KupuXhtmlTestCase.prototype = new TestCase;