| 1 |
|
| 2 |
describe( "Autolinker.htmlParser.HtmlParser", function() { |
| 3 |
var HtmlParser = Autolinker.htmlParser.HtmlParser, |
| 4 |
CommentNode = Autolinker.htmlParser.CommentNode, |
| 5 |
ElementNode = Autolinker.htmlParser.ElementNode, |
| 6 |
EntityNode = Autolinker.htmlParser.EntityNode, |
| 7 |
TextNode = Autolinker.htmlParser.TextNode, |
| 8 |
htmlParser; |
| 9 |
|
| 10 |
|
| 11 |
beforeEach( function() { |
| 12 |
htmlParser = new HtmlParser(); |
| 13 |
} ); |
| 14 |
|
| 15 |
|
| 16 |
function expectCommentNode( node, offset, text, comment ) { |
| 17 |
expect( node ).toEqual( jasmine.any( CommentNode ) ); |
| 18 |
expect( node.getOffset() ).toBe( offset ); |
| 19 |
expect( node.getText() ).toBe( text ); |
| 20 |
expect( node.getComment() ).toBe( comment ); |
| 21 |
} |
| 22 |
|
| 23 |
function expectElementNode( node, offset, tagText, tagName, isClosingTag ) { |
| 24 |
expect( node ).toEqual( jasmine.any( ElementNode ) ); |
| 25 |
expect( node.getOffset() ).toBe( offset ); |
| 26 |
expect( node.getText() ).toBe( tagText ); |
| 27 |
expect( node.getTagName() ).toBe( tagName ); |
| 28 |
expect( node.isClosing() ).toBe( isClosingTag ); |
| 29 |
} |
| 30 |
|
| 31 |
function expectEntityNode( node, offset, text ) { |
| 32 |
expect( node ).toEqual( jasmine.any( EntityNode ) ); |
| 33 |
expect( node.getOffset() ).toBe( offset ); |
| 34 |
expect( node.getText() ).toBe( text ); |
| 35 |
} |
| 36 |
|
| 37 |
function expectTextNode( node, offset, text ) { |
| 38 |
expect( node ).toEqual( jasmine.any( TextNode ) ); |
| 39 |
expect( node.getOffset() ).toBe( offset ); |
| 40 |
expect( node.getText() ).toBe( text ); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
it( "should return an empty array for an empty input string", function() { |
| 45 |
expect( htmlParser.parse( "" ) ).toEqual( [] ); |
| 46 |
} ); |
| 47 |
|
| 48 |
|
| 49 |
describe( 'text node handling', function() { |
| 50 |
|
| 51 |
it( "should return a single text node if there are no HTML nodes in it", function() { |
| 52 |
var nodes = htmlParser.parse( "Testing 123" ); |
| 53 |
|
| 54 |
expect( nodes.length ).toBe( 1 ); |
| 55 |
expectTextNode( nodes[ 0 ], 0, 'Testing 123' ); |
| 56 |
} ); |
| 57 |
|
| 58 |
} ); |
| 59 |
|
| 60 |
|
| 61 |
describe( 'HTML comment node handling', function() { |
| 62 |
|
| 63 |
it( "should return a single comment node if there is only an HTML comment node in it", function() { |
| 64 |
var nodes = htmlParser.parse( "<!-- Testing 123 -->" ); |
| 65 |
|
| 66 |
expect( nodes.length ).toBe( 1 ); |
| 67 |
expectCommentNode( nodes[ 0 ], 0, "<!-- Testing 123 -->", "Testing 123" ); |
| 68 |
} ); |
| 69 |
|
| 70 |
|
| 71 |
it( "should handle a multi-line comment, and trim any amount of whitespace in the comment for the comment's text", function() { |
| 72 |
var nodes = htmlParser.parse( "<!-- \n \t\n Testing 123 \n\t \n\n -->" ); |
| 73 |
|
| 74 |
expect( nodes.length ).toBe( 1 ); |
| 75 |
expectCommentNode( nodes[ 0 ], 0, "<!-- \n \t\n Testing 123 \n\t \n\n -->", "Testing 123" ); |
| 76 |
} ); |
| 77 |
|
| 78 |
|
| 79 |
it( "should produce 3 nodes for a text node, comment, then text node", function() { |
| 80 |
var nodes = htmlParser.parse( "Test <!-- Comment --> Test" ); |
| 81 |
|
| 82 |
expect( nodes.length ).toBe( 3 ); |
| 83 |
expectTextNode ( nodes[ 0 ], 0, 'Test ' ); |
| 84 |
expectCommentNode( nodes[ 1 ], 5, '<!-- Comment -->', 'Comment' ); |
| 85 |
expectTextNode ( nodes[ 2 ], 21, ' Test' ); |
| 86 |
} ); |
| 87 |
|
| 88 |
|
| 89 |
it( "should produce 4 nodes for a text node, comment, text node, comment", function() { |
| 90 |
var nodes = htmlParser.parse( "Test <!-- Comment --> Test <!-- Comment 2 -->" ); |
| 91 |
|
| 92 |
expect( nodes.length ).toBe( 4 ); |
| 93 |
expectTextNode ( nodes[ 0 ], 0, 'Test ' ); |
| 94 |
expectCommentNode( nodes[ 1 ], 5, '<!-- Comment -->', 'Comment' ); |
| 95 |
expectTextNode ( nodes[ 2 ], 21, ' Test ' ); |
| 96 |
expectCommentNode( nodes[ 3 ], 27, '<!-- Comment 2 -->', 'Comment 2' ); |
| 97 |
} ); |
| 98 |
|
| 99 |
} ); |
| 100 |
|
| 101 |
|
| 102 |
describe( 'HTML element node handling', function() { |
| 103 |
|
| 104 |
it( "should return a single element node if there is only an HTML element node in it", function() { |
| 105 |
var nodes = htmlParser.parse( "<div>" ); |
| 106 |
|
| 107 |
expect( nodes.length ).toBe( 1 ); |
| 108 |
expectElementNode( nodes[ 0 ], 0, '<div>', 'div', false ); |
| 109 |
} ); |
| 110 |
|
| 111 |
|
| 112 |
it( "should produce 3 nodes for a text node, element, then text node", function() { |
| 113 |
var nodes = htmlParser.parse( "Test <div> Test" ); |
| 114 |
|
| 115 |
expect( nodes.length ).toBe( 3 ); |
| 116 |
expectTextNode ( nodes[ 0 ], 0, 'Test ' ); |
| 117 |
expectElementNode( nodes[ 1 ], 5, '<div>', 'div', false ); |
| 118 |
expectTextNode ( nodes[ 2 ], 10, ' Test' ); |
| 119 |
} ); |
| 120 |
|
| 121 |
|
| 122 |
it( "should be able to reproduce the input string based on the text that was provided to each returned `HtmlNode`", function() { |
| 123 |
var inputStr = 'Joe went to <a href="google.com">ebay.com</a> today, and bought <b>big</b> items', |
| 124 |
nodes = htmlParser.parse( inputStr ), |
| 125 |
result = []; |
| 126 |
|
| 127 |
for( var i = 0, len = nodes.length; i < len; i++ ) { |
| 128 |
result.push( nodes[ i ].getText() ); |
| 129 |
} |
| 130 |
|
| 131 |
expect( result.length ).toBe( 11 ); |
| 132 |
expect( result.join( "" ) ).toBe( inputStr ); |
| 133 |
} ); |
| 134 |
|
| 135 |
|
| 136 |
it( "should properly handle tags without attributes", function() { |
| 137 |
var nodes = htmlParser.parse( 'Test1 <div><span>Test2</span> Test3</div>' ); |
| 138 |
|
| 139 |
expect( nodes.length ).toBe( 7 ); |
| 140 |
expectTextNode ( nodes[ 0 ], 0, 'Test1 ' ); |
| 141 |
expectElementNode( nodes[ 1 ], 6, '<div>', 'div', false ); |
| 142 |
expectElementNode( nodes[ 2 ], 11, '<span>', 'span', false ); |
| 143 |
expectTextNode ( nodes[ 3 ], 17, 'Test2' ); |
| 144 |
expectElementNode( nodes[ 4 ], 22, '</span>', 'span', true ); |
| 145 |
expectTextNode ( nodes[ 5 ], 29, ' Test3' ); |
| 146 |
expectElementNode( nodes[ 6 ], 35, '</div>', 'div', true ); |
| 147 |
} ); |
| 148 |
|
| 149 |
|
| 150 |
it( "should properly handle a tag where the attributes start on the " + |
| 151 |
"next line", |
| 152 |
function() { |
| 153 |
var nodes = htmlParser.parse( 'Test <div\nclass="myClass"\nstyle="color:red"> Test' ); |
| 154 |
|
| 155 |
expect( nodes.length ).toBe( 3 ); |
| 156 |
expectTextNode ( nodes[ 0 ], 0, 'Test ' ); |
| 157 |
expectElementNode( nodes[ 1 ], 5, '<div\nclass="myClass"\nstyle="color:red">', 'div', false ); |
| 158 |
expectTextNode ( nodes[ 2 ], 44, ' Test' ); |
| 159 |
} ); |
| 160 |
|
| 161 |
} ); |
| 162 |
|
| 163 |
|
| 164 |
describe( 'HTML entity handling', function() { |
| 165 |
|
| 166 |
it( "should *not* match the & HTML entity, as this may be part of a query string", function() { |
| 167 |
var nodes = htmlParser.parse( 'Me&You' ); |
| 168 |
|
| 169 |
expect( nodes.length ).toBe( 1 ); |
| 170 |
expectTextNode( nodes[ 0 ], 0, 'Me&You' ); |
| 171 |
} ); |
| 172 |
|
| 173 |
|
| 174 |
it( "should properly parse a string that begins with an HTML entity node", function() { |
| 175 |
var nodes = htmlParser.parse( '"Test' ); |
| 176 |
|
| 177 |
expect( nodes.length ).toBe( 2 ); |
| 178 |
expectEntityNode( nodes[ 0 ], 0, '"' ); |
| 179 |
expectTextNode( nodes[ 1 ], 6, 'Test' ); |
| 180 |
} ); |
| 181 |
|
| 182 |
|
| 183 |
it( "should properly parse a string that ends with an HTML entity node", function() { |
| 184 |
var nodes = htmlParser.parse( 'Test"' ); |
| 185 |
|
| 186 |
expect( nodes.length ).toBe( 2 ); |
| 187 |
expectTextNode( nodes[ 0 ], 0, 'Test' ); |
| 188 |
expectEntityNode( nodes[ 1 ], 4, '"' ); |
| 189 |
} ); |
| 190 |
|
| 191 |
|
| 192 |
it( "should properly parse a string that begins and ends with an HTML entity node", function() { |
| 193 |
var nodes = htmlParser.parse( '"Test"' ); |
| 194 |
|
| 195 |
expect( nodes.length ).toBe( 3 ); |
| 196 |
expectEntityNode( nodes[ 0 ], 0, '"' ); |
| 197 |
expectTextNode( nodes[ 1 ], 6, 'Test' ); |
| 198 |
expectEntityNode( nodes[ 2 ], 10, '"' ); |
| 199 |
} ); |
| 200 |
|
| 201 |
|
| 202 |
it( "should properly parse a string that has an HTML entity node in the middle", function() { |
| 203 |
var nodes = htmlParser.parse( 'Test"Test' ); |
| 204 |
|
| 205 |
expect( nodes.length ).toBe( 3 ); |
| 206 |
expectTextNode( nodes[ 0 ], 0, 'Test' ); |
| 207 |
expectEntityNode( nodes[ 1 ], 4, '"' ); |
| 208 |
expectTextNode( nodes[ 2 ], 10, 'Test' ); |
| 209 |
} ); |
| 210 |
|
| 211 |
|
| 212 |
it( "should properly parse a string that only has an HTML entity node", function() { |
| 213 |
var nodes = htmlParser.parse( '"' ); |
| 214 |
|
| 215 |
expect( nodes.length ).toBe( 1 ); |
| 216 |
expectEntityNode( nodes[ 0 ], 0, '"' ); |
| 217 |
} ); |
| 218 |
|
| 219 |
} ); |
| 220 |
|
| 221 |
|
| 222 |
describe( 'combination examples', function() { |
| 223 |
|
| 224 |
it( "should properly create `HtmlNode` instances for each text/entity/comment/element node encountered, with the proper data filled in on each node", function() { |
| 225 |
var inputStr = [ |
| 226 |
'"Joe went to "', |
| 227 |
'<a href="google.com">ebay.com</a>" ', |
| 228 |
'today, and <!-- stuff -->bought <b>big</b> items"' |
| 229 |
].join( "" ); |
| 230 |
|
| 231 |
var nodes = htmlParser.parse( inputStr ); |
| 232 |
expect( nodes.length ).toBe( 17 ); |
| 233 |
|
| 234 |
var i = -1; |
| 235 |
expectEntityNode ( nodes[ ++i ], 0, '"' ); |
| 236 |
expectTextNode ( nodes[ ++i ], 6, 'Joe went to ' ); |
| 237 |
expectEntityNode ( nodes[ ++i ], 18, '"' ); |
| 238 |
expectElementNode( nodes[ ++i ], 24, '<a href="google.com">', 'a', false ); |
| 239 |
expectTextNode ( nodes[ ++i ], 45, 'ebay.com' ); |
| 240 |
expectElementNode( nodes[ ++i ], 53, '</a>', 'a', true ); |
| 241 |
expectEntityNode ( nodes[ ++i ], 57, '"' ); |
| 242 |
expectTextNode ( nodes[ ++i ], 63, ' today,' ); |
| 243 |
expectEntityNode ( nodes[ ++i ], 70, ' ' ); |
| 244 |
expectTextNode ( nodes[ ++i ], 76, 'and ' ); |
| 245 |
expectCommentNode( nodes[ ++i ], 80, '<!-- stuff -->', 'stuff' ); |
| 246 |
expectTextNode ( nodes[ ++i ], 94, 'bought ' ); |
| 247 |
expectElementNode( nodes[ ++i ], 101, '<b>', 'b', false ); |
| 248 |
expectTextNode ( nodes[ ++i ], 104, 'big' ); |
| 249 |
expectElementNode( nodes[ ++i ], 107, '</b>', 'b', true ); |
| 250 |
expectTextNode ( nodes[ ++i ], 111, ' items' ); |
| 251 |
expectEntityNode ( nodes[ ++i ], 117, '"' ); |
| 252 |
} ); |
| 253 |
|
| 254 |
|
| 255 |
it( 'should match tags of both upper and lower case', function() { |
| 256 |
var inputStr = [ |
| 257 |
'Joe <!DOCTYPE html><!-- Comment -->went <!doctype "blah" "blah blah"> ', |
| 258 |
'to <a href="google.com">ebay.com</a> today, and <A href="purchase.com">purchased</A> ', |
| 259 |
'<b>big</b> <B><!-- Comment 2 -->items</B>' |
| 260 |
].join( '' ); |
| 261 |
var nodes = htmlParser.parse( inputStr ); |
| 262 |
|
| 263 |
expect( nodes.length ).toBe( 24 ); |
| 264 |
|
| 265 |
var i = -1; |
| 266 |
expectTextNode ( nodes[ ++i ], 0, 'Joe ' ); |
| 267 |
expectElementNode( nodes[ ++i ], 4, '<!DOCTYPE html>', '!doctype', false ); |
| 268 |
expectCommentNode( nodes[ ++i ], 19, '<!-- Comment -->', 'Comment' ); |
| 269 |
expectTextNode ( nodes[ ++i ], 35, 'went ' ); |
| 270 |
expectElementNode( nodes[ ++i ], 40, '<!doctype "blah" "blah blah">', '!doctype', false ); |
| 271 |
expectTextNode ( nodes[ ++i ], 69, ' to ' ); |
| 272 |
expectElementNode( nodes[ ++i ], 73, '<a href="google.com">', 'a', false ); |
| 273 |
expectTextNode ( nodes[ ++i ], 94, 'ebay.com' ); |
| 274 |
expectElementNode( nodes[ ++i ], 102, '</a>', 'a', true ); |
| 275 |
expectTextNode ( nodes[ ++i ], 106, ' today,' ); |
| 276 |
expectEntityNode ( nodes[ ++i ], 113, ' ' ); |
| 277 |
expectTextNode ( nodes[ ++i ], 119, 'and ' ); |
| 278 |
expectElementNode( nodes[ ++i ], 123, '<A href="purchase.com">', 'a', false ); |
| 279 |
expectTextNode ( nodes[ ++i ], 146, 'purchased' ); |
| 280 |
expectElementNode( nodes[ ++i ], 155, '</A>', 'a', true ); |
| 281 |
expectTextNode ( nodes[ ++i ], 159, ' ' ); |
| 282 |
expectElementNode( nodes[ ++i ], 160, '<b>', 'b', false ); |
| 283 |
expectTextNode ( nodes[ ++i ], 163, 'big' ); |
| 284 |
expectElementNode( nodes[ ++i ], 166, '</b>', 'b', true ); |
| 285 |
expectTextNode ( nodes[ ++i ], 170, ' ' ); |
| 286 |
expectElementNode( nodes[ ++i ], 171, '<B>', 'b', false ); |
| 287 |
expectCommentNode( nodes[ ++i ], 174, '<!-- Comment 2 -->', 'Comment 2' ); |
| 288 |
expectTextNode ( nodes[ ++i ], 192, 'items' ); |
| 289 |
expectElementNode( nodes[ ++i ], 197, '</B>', 'b', true ); |
| 290 |
} ); |
| 291 |
|
| 292 |
} ); |
| 293 |
|
| 294 |
|
| 295 |
it( "should not freeze up the regular expression engine when presented with the input string in issue #54", function() { |
| 296 |
var inputStr = "Shai ist endlich in Deutschland! Und wir haben gute Nachrichten! <3 Alle, die den Shai-Rasierer kostenlos probieren, machen am Gewinnspiel eines Jahresvorrates Klingen mit. Den Rasierer bekommst Du kostenlos durch diesen Link: http://dorcoshai.de/pb1205ro, und dann machst Du am Gewinnspiel mit! 'Gefallt mir' klicken, wenn Du gern einen Jahresvorrat Shai haben mochtest. (Y)", |
| 297 |
nodes = htmlParser.parse( inputStr ); |
| 298 |
|
| 299 |
expect( nodes.length ).toBe( 1 ); |
| 300 |
expectTextNode( nodes[ 0 ], 0, inputStr ); |
| 301 |
} ); |
| 302 |
|
| 303 |
|
| 304 |
it( "should not freeze up the regular expression engine when presented with the input string in issue #172", function() { |
| 305 |
var inputStr = '<Office%20days:%20Tue.%20&%20Wed.%20(till%2015:30%20hr),%20Thu.%20(till%2017', |
| 306 |
nodes = htmlParser.parse( inputStr ); |
| 307 |
|
| 308 |
expect( nodes.length ).toBe( 1 ); |
| 309 |
expectTextNode( nodes[ 0 ], 0, inputStr ); |
| 310 |
} ); |
| 311 |
|
| 312 |
it( "should not freeze up the regular expression engine when presented with the input string in issue #204", function() { |
| 313 |
var inputStr = '<img src="http://example.com/Foo" border-radius:2px;moz-border-radius:2px;khtml-border-radius:2px;o-border-radius:2px;webkit-border-radius:2px;ms-border-radius:="" 2px; "=" " class=" ">', |
| 314 |
nodes = htmlParser.parse( inputStr ); |
| 315 |
|
| 316 |
expect( nodes.length ).toBe( 1 ); |
| 317 |
expectTextNode( nodes[ 0 ], 0, inputStr ); |
| 318 |
} ); |
| 319 |
|
| 320 |
} ); |