if (wysihtml5.browser.supported()) { module("wysihtml5.dom.parse", { sanitize: function(html, rules, context, cleanUp, uneditableClass) { return wysihtml5.dom.parse(html, { "rules": rules, "cleanUp": cleanUp, "context": context, "uneditableClass": uneditableClass }); }, equal: function(actual, expected, message) { return QUnit.assert.htmlEqual(actual, expected, message); } }); test("Simple tests using plain tags only", function() { var rules = { tags: { p: "div", script: undefined, div: {} } }; this.equal( this.sanitize("bar", rules), "bar", "Unknown tag gets renamed to span" ); this.equal( this.sanitize("

foo

", rules), "
foo
", "Known tag gets renamed to it's corresponding conversion" ); this.equal( this.sanitize("", rules), "", "Forbidden tag gets correctly removed" ); this.equal( this.sanitize("foobar", rules), "foobar", "Plain text is kept" ); this.equal( this.sanitize("
I'm a table!
"), "I'm a table!", "Passing no conversion renames all into elements" ); this.equal( this.sanitize("

foobar

", { tags: { p: true, br: true } }), "

foobar

", "Didn't rewrite the HTML" ); this.equal( this.sanitize("
foo
"), "foo", "Stripped out comments" ); this.equal( this.sanitize("
foo
", { tags: { article: true } }), "
foo
", "Check html5 tags" ); this.equal( this.sanitize("

html5 doctype

", { tags: { p: true } }), "

html5 doctype

", "Stripped out doctype" ); }); test("Advanced tests using tags and attributes", function() { var rules = { tags: { img: { set_attributes: { alt: "foo", border: "1" }, check_attributes: { src: "url", width: "numbers", height: "numbers", border: "numbers" } }, a: { rename_tag: "i", set_attributes: { title: "" } }, video: undefined, h1: { rename_tag: "h2" }, h2: true, h3: undefined } }; this.equal( this.sanitize( '

take this you snorty little sanitizer

' + '

yes, you!

' + '

i\'m old and ready to die

' + '
' + '
', rules ), '

take this you snorty little sanitizer

' + '

yes, you!

' + 'foofoo' + '' ); }); test("Attribute check of 'url' cleans up", function() { var rules = { tags: { img: { check_attributes: { src: "url" } } } }; this.equal( this.sanitize( '' + '' + '', rules ), '' ); }); test("Attribute check of 'src' cleans up", function() { var rules = { tags: { img: { check_attributes: { src: "src" } } } }; this.equal( this.sanitize( '' + '' + '' + '', rules ), '' + '' + '' + '' ); }); test("Attribute check of 'href' cleans up", function() { var rules = { tags: { a: { check_attributes: { href: "href" } } } }; this.equal( this.sanitize( '' + '' + '' + '' + '' + '', rules ), '' + '' + '' + '' + '' + '' ); }); test("Bug in IE8 where invalid html causes duplicated content", function() { var rules = { tags: { p: true, span: true, div: true } }; var result = this.sanitize('

FOO
', rules); ok(result.indexOf("FOO") === result.lastIndexOf("FOO")); }); test("Bug in IE8 where elements are duplicated when multiple parsed", function() { var rules = { tags: { p: true, span: true, div: true } }; var firstResult = this.sanitize('

foo

', rules); var secondResult = this.sanitize(firstResult, rules); ok(secondResult.indexOf("foo") !== -1); this.equal(firstResult, secondResult); firstResult = this.sanitize('
foo
', rules); secondResult = this.sanitize(firstResult, rules); ok(secondResult.indexOf("foo") !== -1); this.equal(firstResult, secondResult); }); test("Test cleanup mode", function() { var rules = { classes: { a: 1, c: 1 }, tags: { span: true, div: true } }; this.equal( this.sanitize("
foo
", rules, null, true), "
foo
" ); this.equal( this.sanitize("

foo

", rules, null, true), "foo" ); this.equal( this.sanitize('foo', rules, null, true), 'foo', "Empty 'span' is correctly removed" ); this.equal( this.sanitize('1 2 3', rules, null, true), '1 2 3', "Senseless 'span' is correctly removed" ); }); test("Advanced tests for 'img' elements", function() { var rules = { classes: { "wysiwyg-float-right": 1, "wysiwyg-float-left": 1 }, tags: { img: { check_attributes: { width: "numbers", alt: "alt", src: "url", height: "numbers" }, add_class: { align: "align_img" } } } }; this.equal( this.sanitize( 'Christopher Blum', rules ), 'Christopher Blum' ); this.equal( this.sanitize( 'Christopher Blum', rules ), 'Christopher Blum' ); this.equal( this.sanitize( 'Christopher Blum', rules ), 'Christopher Blum' ); this.equal( this.sanitize( 'Christopher Blum', rules ), 'Christopher Blum' ); this.equal( this.sanitize( 'Christopher Blum', rules ), 'Christopher Blum' ); this.equal( this.sanitize( 'Christopher Blum', rules ), 'Christopher Blum' ); this.equal( this.sanitize( '', rules ), '' ); this.equal( this.sanitize( '', rules ), '' ); }); test("Advanced tests for 'br' elements", function() { var rules = { classes: { "wysiwyg-clear-both": 1, "wysiwyg-clear-left": 1, "wysiwyg-clear-right": 1 }, tags: { div: true, br: { add_class: { clear: "clear_br" } } } }; this.equal( this.sanitize( '
foo
bar
', rules ), '
foo
bar
' ); this.equal( this.sanitize( '
foo
bar
', rules ), '
foo
bar
' ); this.equal( this.sanitize( '
foo
bar
', rules ), '
foo
bar
' ); this.equal( this.sanitize( '
', rules ), '
' ); this.equal( this.sanitize( '
', rules ), '
' ); this.equal( this.sanitize( '
', rules ), '
' ); this.equal( this.sanitize( '
', rules ), '
' ); this.equal( this.sanitize( '
', rules ), '
' ); this.equal( this.sanitize( '
', rules ), '
' ); this.equal( this.sanitize( '
', rules ), '
' ); }); test("Advanced tests for 'font' elements", function() { var rules = { classes: { "wysiwyg-font-size-xx-small": 1, "wysiwyg-font-size-small": 1, "wysiwyg-font-size-medium": 1, "wysiwyg-font-size-large": 1, "wysiwyg-font-size-x-large": 1, "wysiwyg-font-size-xx-large": 1, "wysiwyg-font-size-smaller": 1, "wysiwyg-font-size-larger": 1 }, tags: { font: { add_class: { size: "size_font" }, rename_tag: "span" } } }; this.equal( this.sanitize( 'foo', rules ), 'foo' ); this.equal( this.sanitize( 'foo', rules ), 'foo' ); this.equal( this.sanitize( 'foo', rules ), 'foo' ); this.equal( this.sanitize( 'foo', rules ), 'foo' ); this.equal( this.sanitize( 'foo', rules ), 'foo' ); this.equal( this.sanitize( 'foo', rules ), 'foo' ); this.equal( this.sanitize( 'foo', rules ), 'foo' ); this.equal( this.sanitize( 'foo', rules ), 'foo' ); this.equal( this.sanitize( 'foo', rules ), 'foo' ); }); test("Check whether namespaces are handled correctly", function() { var rules = { tags: { p: true } }; this.equal( this.sanitize("foo", rules), "foo", "Unknown tag with namespace gets renamed to span" ); }); test("Check whether classes are correctly treated", function() { var rules = { classes: { a: 1, c: 1 }, tags: { footer: "div" } }; this.equal( this.sanitize('
foo
', rules), 'foo', "Allowed classes 'a' and 'c' are correctly kept and unknown class 'b' is correctly removed." ); this.equal( this.sanitize('
foo
', rules), '
foo
', "Allowed classes 'c' is correctly kept and unknown class 'b' is correctly removed." ); }); test("Check mailto links", function() { var rules = { tags: { a: { check_attributes: { href: "href" } } } }; this.equal( this.sanitize('foo', rules), 'foo', "'mailto:' urls are not stripped" ); }); test("Check anchor links", function() { var rules = { tags: { a: { check_attributes: { href: "href" } } } }; this.equal( this.sanitize('foo', rules), 'foo', "'#'-starting anchor urls are not stripped" ); }); test("Check custom data attributes", function() { var rules = { tags: { span: { check_attributes: { "data-max-width": "numbers" } } } }; this.equal( this.sanitize('foo', rules), 'foo', "custom data attributes are not stripped" ); }); test("Check Firefox misbehavior with tilde characters in urls", function() { var rules = { tags: { a: { set_attributes: { target: "_blank", rel: "nofollow" }, check_attributes: { href: "url" } } } }; // See https://bugzilla.mozilla.org/show_bug.cgi?id=664398 // // In Firefox this: // var d = document.createElement("div"); // d.innerHTML =''; // d.innerHTML; // will result in: // // which is wrong ok( this.sanitize('', rules).indexOf("~") !== -1 ); }); test("Check concatenation of text nodes", function() { var rules = { tags: { span: 1, div: 1 } }; var tree = document.createElement("div"); tree.appendChild(document.createTextNode("foo ")); tree.appendChild(document.createTextNode("bar baz ")); tree.appendChild(document.createTextNode("bam! ")); var span = document.createElement("span"); span.innerHTML = "boobs! hihihi ..."; tree.appendChild(span); var result = this.sanitize(tree, rules); equal(result.childNodes.length, 2); equal(result.innerHTML, "foo bar baz bam! boobs! hihihi ..."); }); test("Check element unwrapping", function() { var rules = { tags: { div: { unwrap: 1 }, span: { unwrap: 1 } } }, input = "
Hi, there!
", output = "Hi, there!"; this.equal(this.sanitize(input, rules), output); }); test("Check spacing when unwrapping elements", function() { var rules = { tags: { table: { unwrap: 1 }, td: { unwrap: 1 }, tr: { unwrap: 1 }, tbody: { unwrap: 1 }, ul: { unwrap: 1 }, li: { unwrap: 1 } } }, input_list = "
  • This
  • is
  • a
  • list
", output_list = "This is a list", input_table = "
Thisisatable
", output_table = "This is a table"; this.equal(this.sanitize(input_list, rules), output_list, "List unwrapping working ok"); this.equal(this.sanitize(input_table, rules), output_table, "Table unwrapping working ok"); }); test("Test valid type check by attributes", function() { var rules = { "type_definitions": { "valid_image_src": { "attrs": { "src": /^[^data\:]/i } } }, "tags": { "img": { "one_of_type": { "valid_image_src": 1 }, "check_attributes": { "src": "src", "height": "numbers", "width": "numbers", "alt": "alt" } } } }, input = 'alt', input_valid = '', input_valid_2 = ''; this.equal(this.sanitize(input, rules), "", "Image with data src gets removed"); this.equal(this.sanitize(input_valid, rules), input_valid, "Valid image is kept"); this.equal(this.sanitize(input_valid_2, rules), input_valid_2, "Valid image is kept2"); }); test("Test valid type definition visible_content_object ", function() { var rules = { "type_definitions": { "visible_content_object": { "methods": { "has_visible_contet": 1 } }, }, "tags": { 'div': { "one_of_type": { "visible_content_object": 1 }, "remove_action": "unwrap", "check_attributes": { "style": "any" } }, 'img': { "check_attributes": { "src": "any" } }, 'span': {} } }, input1 = '
', input2 = '
', input3 = '
', input4 = '
test
', input5 = '
', tester = document.createElement('div'); this.equal(this.sanitize(input1, rules), "", "Empty DIV gets removed"); this.equal(this.sanitize(input2, rules), " ", "DIV with no textual content gets unwrapped"); this.equal(this.sanitize(input3, rules), input3, "DIV with img inside is kept"); this.equal(this.sanitize(input4, rules), input4, "DIV with textual content is kept"); document.body.appendChild(tester); tester.innerHTML = input2; this.equal(this.sanitize(tester, rules).innerHTML, " ", "DIV with no dimesions and in dom gets unwrapped"); tester.innerHTML = input5; this.equal(this.sanitize(tester, rules).innerHTML, input5 , "DIV with dimensions and in dom is kept"); }); test("Test keeping comments ", function() { var rules = { "comments": 1 }, input = 'Test '; this.equal(this.sanitize(input, rules), input, "Comments are kept if configured to keep"); }); }