var DOM = tinymce.DOM; module("tinymce.dom.Serializer", { setupModule: function() { document.getElementById('view').innerHTML = '
'; } }); test('Schema rules', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(8); ser.setRules('@[id|title|class|style],div,img[src|alt|-style|border],span,hr'); DOM.setHTML('test', 'testtest
'); equal(ser.serialize(DOM.get('test')), '
testtest
', 'Global rule'); ser.setRules('*a[*],em/i[*],strong/b[*i*]'); DOM.setHTML('test', 'testtest2test3'); equal(ser.serialize(DOM.get('test')), 'testtest2test3', 'Wildcard rules'); ser.setRules('br,hr,input[type|name|value],div[id],span[id],strong/b,a,em/i,a[!href|!name],img[src|border=0|title={$uid}]'); DOM.setHTML('test', '

abc123123linkno'); equal(ser.serialize(DOM.get('test')), '


abc123123linkno
', 'Output name and attribute rules'); ser.setRules('a[href|target<_blank?_top|title:forced value]'); DOM.setHTML('test', 'linktest2'); equal(ser.serialize(DOM.get('test')), 'linktest2'); ser.setRules('img[src|border=0|alt=]'); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), '', 'Default attribute with empty value'); ser.setRules('img[src|border=0|alt=],*[*]'); DOM.setHTML('test', '
'); equal(ser.serialize(DOM.get('test')), '

'); ser = new tinymce.dom.Serializer({ valid_elements : 'img[src|border=0|alt=]', extended_valid_elements : 'div[id],img[src|alt=]' }); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), '
'); ser = new tinymce.dom.Serializer({invalid_elements : 'hr,br'}); DOM.setHTML('test', '

'); equal(ser.serialize(DOM.get('test')), '
'); }); test('Entity encoding', function() { var ser; expect(4); ser = new tinymce.dom.Serializer({entity_encoding : 'numeric'}); DOM.setHTML('test', '<>&" åäö'); equal(ser.serialize(DOM.get('test')), '
<>&" åäö
'); ser = new tinymce.dom.Serializer({entity_encoding : 'named'}); DOM.setHTML('test', '<>&" åäö'); equal(ser.serialize(DOM.get('test')), '
<>&" åäö
'); ser = new tinymce.dom.Serializer({entity_encoding : 'named+numeric', entities : '160,nbsp,34,quot,38,amp,60,lt,62,gt'}); DOM.setHTML('test', '<>&" åäö'); equal(ser.serialize(DOM.get('test')), '
<>&" åäö
'); ser = new tinymce.dom.Serializer({entity_encoding : 'raw'}); DOM.setHTML('test', '<>&" åäö'); equal(ser.serialize(DOM.get('test')), '
<>&"\u00a0\u00e5\u00e4\u00f6
'); }); test('Form elements (general)', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(5); ser.setRules('form[method],label[for],input[type|name|value|checked|disabled|readonly|length|maxlength],select[multiple],option[value|selected],textarea[name|disabled|readonly]'); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', '
'); equal(ser.serialize(DOM.get('test')), '
'); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); }); test('Form elements (checkbox)', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(4); ser.setRules('form[method],label[for],input[type|name|value|checked|disabled|readonly|length|maxlength],select[multiple],option[value|selected]'); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); }); test('Form elements (select)', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(7); ser.setRules('form[method],label[for],input[type|name|value|checked|disabled|readonly|length|maxlength],select[multiple],option[value|selected]'); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); }); test('List elements', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(5); ser.setRules('ul[compact],ol,li'); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), ''); DOM.setHTML('test', '
  1. a
    1. b
    2. c
  2. e
'); equal(ser.serialize(DOM.get('test')), '
  1. a
    1. b
    2. c
  2. e
'); }); test('Tables', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(4); ser.setRules('table,tr,td[nowrap]'); DOM.setHTML('test', '
'); equal(ser.serialize(DOM.get('test')), '
'); DOM.setHTML('test', '
'); equal(ser.serialize(DOM.get('test')), '
'); DOM.setHTML('test', '
'); equal(ser.serialize(DOM.get('test')), '
'); DOM.setHTML('test', '
'); equal(ser.serialize(DOM.get('test')), '
'); }); test('Styles', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(1); ser.setRules('*[*]'); DOM.setHTML('test', 'test'); equal(ser.serialize(DOM.get('test')), '
test
'); }); test('Comments', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(1); ser.setRules('*[*]'); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')), '
'); }); test('Non HTML elements and attributes', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(2); ser.setRules('*[*]'); ser.schema.addValidChildren('+div[prefix:test]'); DOM.setHTML('test', '
test
'); equal(ser.serialize(DOM.get('test'), {getInner : 1}), '
test
'); DOM.setHTML('test', 'test1Testtest2'); equal(ser.serialize(DOM.get('test')), '
test1Testtest2
'); }); test('Padd empty elements', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(1); ser.setRules('#p'); DOM.setHTML('test', '

test

'); equal(ser.serialize(DOM.get('test')), '

test

 

'); }); test('Remove empty elements', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(1); ser.setRules('-p'); DOM.setHTML('test', '

test

'); equal(ser.serialize(DOM.get('test')), '

test

'); }); test('Pre/post process events', function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); expect(3); ser.setRules('div[id],span[id|class],a[href],b[class]'); ser.onPreProcess = function(o) { equal(o.test, 'abc'); DOM.setAttrib(o.node.getElementsByTagName('span')[0], 'class', 'abc'); }; ser.onPostProcess = function(o) { equal(o.test, 'abc'); o.content = o.content.replace(//g, ''); }; DOM.setHTML('test', 'abc123link'); equal(ser.serialize(DOM.get('test'), {test : 'abc'}), '
abc123link
'); }); test('Script with non JS type attribute', 1, function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); ser.setRules('script[type|language|src]'); DOM.setHTML('test', ''); equal(ser.serialize(DOM.get('test')).replace(/\r/g, ''), ''); }); test('Script with tags inside a comment', 1, function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); ser.setRules('script[type|language|src]'); DOM.setHTML('test', '// '); equal(ser.serialize(DOM.get('test')).replace(/\r/g, ''), '// \n// ]]>'); }); test('Script with less than', 1, function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); ser.setRules('script[type|language|src]'); DOM.setHTML('test', '1 < 2;'); equal(ser.serialize(DOM.get('test')).replace(/\r/g, ''), '// '); }); test('Script with type attrib and less than', 1, function() { var ser = new tinymce.dom.Serializer({fix_list_elements : true}); ser.setRules('script[type|language|src]'); DOM.setHTML('test', '1 < 2;'); equal(ser.serialize(DOM.get('test')).replace(/\r/g, ''), '