From 9f9964a938800d03c8bfdab07d7d21b6cd07adad Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 6 Jan 2015 23:42:49 +0100 Subject: [PATCH] Added some more tests. --- .../app/lib/app_post/utils.js.coffee | 73 ++++--- public/assets/tests/html-utils.js | 202 +++++++++++++----- 2 files changed, 193 insertions(+), 82 deletions(-) diff --git a/app/assets/javascripts/app/lib/app_post/utils.js.coffee b/app/assets/javascripts/app/lib/app_post/utils.js.coffee index be002fe44..d0a703d54 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.js.coffee @@ -16,14 +16,6 @@ class App.Utils ascii = '
' + ascii.replace(/\n/g, '
') + '
' ascii.replace(/
<\/div>/g, '

') - # htmlEscaped = App.Utils.htmlEscape( rawText ) - @htmlEscape: ( ascii ) -> - ascii.replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"') - .replace(/'/g, ''') - # htmlEscapedAndLinkified = App.Utils.linkify( rawText ) @linkify: (ascii) -> window.linkify( ascii ) @@ -78,32 +70,54 @@ class App.Utils else '>' - @htmlRemoveTags: (textarea) -> - # remove tags, keep content - textarea.find('a, div, span, li, ul, ol, a, hr, blockquote, br').replaceWith( -> - $(@).contents() - ) + # htmlEscaped = App.Utils.htmlEscape( rawText ) + @htmlEscape: ( ascii ) -> + ascii.replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, ''') - @htmlRemoveRichtext: (textarea) -> - - # remove style and class - textarea.find('div, span, li, ul, ol, a').removeAttr( 'style' ).removeAttr( 'class' ).removeAttr( 'title' ) + # textWithoutTags = App.Utils.htmlRemoveTags( html ) + @htmlRemoveTags: (html) -> # remove tags, keep content - textarea.find('a, li, ul, ol, a, hr').replaceWith( -> + html.find('div, span, p, li, ul, ol, a, b, u, i, strong, blockquote, h1, h2, h3, h4, h5, h6').replaceWith( -> $(@).contents() ) - @htmlClanup: (textarea) -> - - # remove style and class - textarea.find('div, span, li, ul, ol, a').removeAttr( 'style' ).removeAttr( 'class' ).removeAttr( 'title' ) - # remove tags & content - textarea.find('hr').remove() + html.find('div, span, p, li, ul, ol, a, b, u, i, strong, blockquote, h1, h2, h3, h4, h5, h6, br, hr, img').remove() + + html + + # htmlOnlyWithRichtext = App.Utils.htmlRemoveRichtext( html ) + @htmlRemoveRichtext: (html) -> + + # remove style and class + @_removeAttributes( html ) # remove tags, keep content - textarea.find('a').replaceWith( -> + html.find('li, ul, ol, a, b, u, i, strong, blockquote, h1, h2, h3, h4, h5, h6').replaceWith( -> + $(@).contents() + ) + + # remove tags & content + html.find('li, ul, ol, a, b, u, i, strong, blockquote, h1, h2, h3, h4, h5, h6, br, hr, img').remove() + + html + + # cleanHtmlWithRichText = App.Utils.htmlClanup( html ) + @htmlClanup: (html) -> + + # remove style and class + @_removeAttributes( html ) + + # remove tags & content + html.find('br, hr, img').remove() + + # remove tags, keep content + html.find('a').replaceWith( -> $(@).contents() ) @@ -112,7 +126,7 @@ class App.Utils replacementTag = 'div'; # Replace all a tags with the type of replacementTag - textarea.find('h1, h2, h3, h4, h5, h6').each( -> + html.find('h1, h2, h3, h4, h5, h6').each( -> outer = this.outerHTML; # Replace opening tag @@ -125,4 +139,11 @@ class App.Utils $(@).replaceWith(newTag); ) + html + @_removeAttributes: (html) -> + html.find('div, span, p, li, ul, ol, a, b, u, i, strong, blockquote, h1, h2, h3, h4, h5, h6') + .removeAttr( 'style' ) + .removeAttr( 'class' ) + .removeAttr( 'title' ) + html diff --git a/public/assets/tests/html-utils.js b/public/assets/tests/html-utils.js index 0b3af1ea0..4b6bb427e 100644 --- a/public/assets/tests/html-utils.js +++ b/public/assets/tests/html-utils.js @@ -39,62 +39,6 @@ test( "textCleanup", function() { equal( result, should, source ) -}); - -// htmlEscape -test( "htmlEscape", function() { - - var source = "<" - var should = "<" - var result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = ">" - should = ">" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = "&" - should = "&" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = "&" - should = "&amp;" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = "& ;" - should = "&amp ;" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = "& amp;" - should = "& amp;" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = "'test'" - should = "'test'" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = '"test"' - should = ""test"" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = "<>" - should = "<>" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - source = "<<>" - should = "<&lt;>" - result = App.Utils.htmlEscape( source ) - equal( result, should, source ) - - }); // text2html @@ -160,6 +104,152 @@ test( "linkify", function() { }); +// htmlEscape +test( "htmlEscape", function() { + + var source = "<" + var should = "<" + var result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = ">" + should = ">" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = "&" + should = "&" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = "&" + should = "&amp;" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = "& ;" + should = "&amp ;" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = "& amp;" + should = "& amp;" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = "'test'" + should = "'test'" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = '"test"' + should = ""test"" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = "<>" + should = "<>" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + + source = "<<>" + should = "<&lt;>" + result = App.Utils.htmlEscape( source ) + equal( result, should, source ) + +}); + +// htmlRemoveTags +test( "htmlRemoveTags", function() { + + var source = "
test
" + var should = "test" + var result = App.Utils.htmlRemoveTags( $(source) ) + console.log('RRRR', result); + equal( result.html(), should, source ) + + source = "some link to somewhere" + should = "some link to somewhere" + result = App.Utils.htmlRemoveTags( $(source) ) + equal( result.html(), should, source ) + + source = "
some link to somewhere
" + should = "some link to somewhere" + result = App.Utils.htmlRemoveTags( $(source) ) + equal( result.html(), should, source ) + + source = "
some link to somewhere

123
" + should = "some link to somewhere 123 " + result = App.Utils.htmlRemoveTags( $(source) ) + equal( result.html(), should, source ) + +}); + +// htmlRemoveRichtext +test( "htmlRemoveRichtext", function() { + + var source = "
test
" + var should = "test" + var result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + + source = "some link to somewhere" + should = "some link to somewhere" + result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + + source = "
test
" + should = " test " + result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + + source = "
test
" + should = " test " + result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + + source = "
test
" + should = "
test
" + result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + + source = "
test
123
" + should = "
test
123" + result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + + source = "
test
" + should = "
test
" + result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + +}); + +// htmlClanup +test( "htmlClanup", function() { + + var source = "
test
" + var should = "test" + var result = App.Utils.htmlClanup( $(source) ) + equal( result.html(), should, source ) + + source = "some link to somewhere" + should = "some link to somewhere" + result = App.Utils.htmlClanup( $(source) ) + equal( result.html(), should, source ) + + source = "

some link to somewhere

" + should = "
some link to somewhere
" + result = App.Utils.htmlClanup( $(source) ) + equal( result.html(), should, source ) + + source = "

some link to somewhere


" + should = "
some link to somewhere

" + result = App.Utils.htmlClanup( $(source) ) + equal( result.html(), should, source ) + +}); + // wrap test( "wrap", function() {