Added some more tests.
This commit is contained in:
parent
6b75735c20
commit
9f9964a938
2 changed files with 193 additions and 82 deletions
|
@ -16,14 +16,6 @@ class App.Utils
|
|||
ascii = '<div>' + ascii.replace(/\n/g, '</div><div>') + '</div>'
|
||||
ascii.replace(/<div><\/div>/g, '<div><br></div>')
|
||||
|
||||
# htmlEscaped = App.Utils.htmlEscape( rawText )
|
||||
@htmlEscape: ( ascii ) ->
|
||||
ascii.replace(/&/g, '&')
|
||||
.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, '"')
|
||||
.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
|
||||
|
|
|
@ -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 = "<div>test</div>"
|
||||
var should = "test"
|
||||
var result = App.Utils.htmlRemoveTags( $(source) )
|
||||
console.log('RRRR', result);
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<a href=\"some_link\">some link to somewhere</a>"
|
||||
should = "some link to somewhere"
|
||||
result = App.Utils.htmlRemoveTags( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><a href=\"some_link\">some link to somewhere</a></div>"
|
||||
should = "some link to somewhere"
|
||||
result = App.Utils.htmlRemoveTags( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><a href=\"some_link\">some link to somewhere</a> <div><hr></div> <span>123</span> <img src=\"some_image\"/></div>"
|
||||
should = "some link to somewhere 123 "
|
||||
result = App.Utils.htmlRemoveTags( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
});
|
||||
|
||||
// htmlRemoveRichtext
|
||||
test( "htmlRemoveRichtext", function() {
|
||||
|
||||
var source = "<div><a href=\"test\">test</a></div>"
|
||||
var should = "test"
|
||||
var result = App.Utils.htmlRemoveRichtext( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<a href=\"some_link\">some link to somewhere</a>"
|
||||
should = "some link to somewhere"
|
||||
result = App.Utils.htmlRemoveRichtext( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><a href=\"some_link\"></a> test </div>"
|
||||
should = " test "
|
||||
result = App.Utils.htmlRemoveRichtext( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><b></b> test </div>"
|
||||
should = " test "
|
||||
result = App.Utils.htmlRemoveRichtext( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><div><b></b> test </div></div>"
|
||||
should = "<div> test </div>"
|
||||
result = App.Utils.htmlRemoveRichtext( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><div><b></b> test </div><span>123</span></div>"
|
||||
should = "<div> test </div><span>123</span>"
|
||||
result = App.Utils.htmlRemoveRichtext( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><div class=\"xxx\"><b></b> test </div></div>"
|
||||
should = "<div> test </div>"
|
||||
result = App.Utils.htmlRemoveRichtext( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
});
|
||||
|
||||
// htmlClanup
|
||||
test( "htmlClanup", function() {
|
||||
|
||||
var source = "<div><a href=\"test\">test</a></div>"
|
||||
var should = "test"
|
||||
var result = App.Utils.htmlClanup( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<a href=\"some_link\">some link to somewhere</a>"
|
||||
should = "some link to somewhere"
|
||||
result = App.Utils.htmlClanup( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><h1>some link to somewhere</h1></a>"
|
||||
should = "<div>some link to somewhere</div>"
|
||||
result = App.Utils.htmlClanup( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
source = "<div><h1>some link to somewhere</h1><p><hr></p></div>"
|
||||
should = "<div>some link to somewhere</div><p></p><p></p>"
|
||||
result = App.Utils.htmlClanup( $(source) )
|
||||
equal( result.html(), should, source )
|
||||
|
||||
});
|
||||
|
||||
// wrap
|
||||
test( "wrap", function() {
|
||||
|
||||
|
|
Loading…
Reference in a new issue