/*global Autolinker, _, describe, beforeEach, afterEach, it, expect, jasmine */ describe( "Autolinker.htmlParser.HtmlParser", function() { var HtmlParser = Autolinker.htmlParser.HtmlParser, CommentNode = Autolinker.htmlParser.CommentNode, ElementNode = Autolinker.htmlParser.ElementNode, EntityNode = Autolinker.htmlParser.EntityNode, TextNode = Autolinker.htmlParser.TextNode, htmlParser; beforeEach( function() { htmlParser = new HtmlParser(); } ); function expectCommentNode( node, offset, text, comment ) { expect( node ).toEqual( jasmine.any( CommentNode ) ); expect( node.getOffset() ).toBe( offset ); expect( node.getText() ).toBe( text ); expect( node.getComment() ).toBe( comment ); } function expectElementNode( node, offset, tagText, tagName, isClosingTag ) { expect( node ).toEqual( jasmine.any( ElementNode ) ); expect( node.getOffset() ).toBe( offset ); expect( node.getText() ).toBe( tagText ); expect( node.getTagName() ).toBe( tagName ); expect( node.isClosing() ).toBe( isClosingTag ); } function expectEntityNode( node, offset, text ) { expect( node ).toEqual( jasmine.any( EntityNode ) ); expect( node.getOffset() ).toBe( offset ); expect( node.getText() ).toBe( text ); } function expectTextNode( node, offset, text ) { expect( node ).toEqual( jasmine.any( TextNode ) ); expect( node.getOffset() ).toBe( offset ); expect( node.getText() ).toBe( text ); } it( "should return an empty array for an empty input string", function() { expect( htmlParser.parse( "" ) ).toEqual( [] ); } ); describe( 'text node handling', function() { it( "should return a single text node if there are no HTML nodes in it", function() { var nodes = htmlParser.parse( "Testing 123" ); expect( nodes.length ).toBe( 1 ); expectTextNode( nodes[ 0 ], 0, 'Testing 123' ); } ); } ); describe( 'HTML comment node handling', function() { it( "should return a single comment node if there is only an HTML comment node in it", function() { var nodes = htmlParser.parse( "" ); expect( nodes.length ).toBe( 1 ); expectCommentNode( nodes[ 0 ], 0, "", "Testing 123" ); } ); it( "should handle a multi-line comment, and trim any amount of whitespace in the comment for the comment's text", function() { var nodes = htmlParser.parse( "" ); expect( nodes.length ).toBe( 1 ); expectCommentNode( nodes[ 0 ], 0, "", "Testing 123" ); } ); it( "should produce 3 nodes for a text node, comment, then text node", function() { var nodes = htmlParser.parse( "Test Test" ); expect( nodes.length ).toBe( 3 ); expectTextNode ( nodes[ 0 ], 0, 'Test ' ); expectCommentNode( nodes[ 1 ], 5, '', 'Comment' ); expectTextNode ( nodes[ 2 ], 21, ' Test' ); } ); it( "should produce 4 nodes for a text node, comment, text node, comment", function() { var nodes = htmlParser.parse( "Test Test " ); expect( nodes.length ).toBe( 4 ); expectTextNode ( nodes[ 0 ], 0, 'Test ' ); expectCommentNode( nodes[ 1 ], 5, '', 'Comment' ); expectTextNode ( nodes[ 2 ], 21, ' Test ' ); expectCommentNode( nodes[ 3 ], 27, '', 'Comment 2' ); } ); } ); describe( 'HTML element node handling', function() { it( "should return a single element node if there is only an HTML element node in it", function() { var nodes = htmlParser.parse( "
" ); expect( nodes.length ).toBe( 1 ); expectElementNode( nodes[ 0 ], 0, '
', 'div', false ); } ); it( "should produce 3 nodes for a text node, element, then text node", function() { var nodes = htmlParser.parse( "Test
Test" ); expect( nodes.length ).toBe( 3 ); expectTextNode ( nodes[ 0 ], 0, 'Test ' ); expectElementNode( nodes[ 1 ], 5, '
', 'div', false ); expectTextNode ( nodes[ 2 ], 10, ' Test' ); } ); it( "should be able to reproduce the input string based on the text that was provided to each returned `HtmlNode`", function() { var inputStr = 'Joe went to ebay.com today, and bought big items', nodes = htmlParser.parse( inputStr ), result = []; for( var i = 0, len = nodes.length; i < len; i++ ) { result.push( nodes[ i ].getText() ); } expect( result.length ).toBe( 11 ); expect( result.join( "" ) ).toBe( inputStr ); } ); it( "should properly handle tags without attributes", function() { var nodes = htmlParser.parse( 'Test1
Test2 Test3
' ); expect( nodes.length ).toBe( 7 ); expectTextNode ( nodes[ 0 ], 0, 'Test1 ' ); expectElementNode( nodes[ 1 ], 6, '
', 'div', false ); expectElementNode( nodes[ 2 ], 11, '', 'span', false ); expectTextNode ( nodes[ 3 ], 17, 'Test2' ); expectElementNode( nodes[ 4 ], 22, '', 'span', true ); expectTextNode ( nodes[ 5 ], 29, ' Test3' ); expectElementNode( nodes[ 6 ], 35, '
', 'div', true ); } ); it( "should properly handle a tag where the attributes start on the " + "next line", function() { var nodes = htmlParser.parse( 'Test Test' ); expect( nodes.length ).toBe( 3 ); expectTextNode ( nodes[ 0 ], 0, 'Test ' ); expectElementNode( nodes[ 1 ], 5, '', 'div', false ); expectTextNode ( nodes[ 2 ], 44, ' Test' ); } ); } ); describe( 'HTML entity handling', function() { it( "should *not* match the & HTML entity, as this may be part of a query string", function() { var nodes = htmlParser.parse( 'Me&You' ); expect( nodes.length ).toBe( 1 ); expectTextNode( nodes[ 0 ], 0, 'Me&You' ); } ); it( "should properly parse a string that begins with an HTML entity node", function() { var nodes = htmlParser.parse( '"Test' ); expect( nodes.length ).toBe( 2 ); expectEntityNode( nodes[ 0 ], 0, '"' ); expectTextNode( nodes[ 1 ], 6, 'Test' ); } ); it( "should properly parse a string that ends with an HTML entity node", function() { var nodes = htmlParser.parse( 'Test"' ); expect( nodes.length ).toBe( 2 ); expectTextNode( nodes[ 0 ], 0, 'Test' ); expectEntityNode( nodes[ 1 ], 4, '"' ); } ); it( "should properly parse a string that begins and ends with an HTML entity node", function() { var nodes = htmlParser.parse( '"Test"' ); expect( nodes.length ).toBe( 3 ); expectEntityNode( nodes[ 0 ], 0, '"' ); expectTextNode( nodes[ 1 ], 6, 'Test' ); expectEntityNode( nodes[ 2 ], 10, '"' ); } ); it( "should properly parse a string that has an HTML entity node in the middle", function() { var nodes = htmlParser.parse( 'Test"Test' ); expect( nodes.length ).toBe( 3 ); expectTextNode( nodes[ 0 ], 0, 'Test' ); expectEntityNode( nodes[ 1 ], 4, '"' ); expectTextNode( nodes[ 2 ], 10, 'Test' ); } ); it( "should properly parse a string that only has an HTML entity node", function() { var nodes = htmlParser.parse( '"' ); expect( nodes.length ).toBe( 1 ); expectEntityNode( nodes[ 0 ], 0, '"' ); } ); } ); describe( 'combination examples', function() { it( "should properly create `HtmlNode` instances for each text/entity/comment/element node encountered, with the proper data filled in on each node", function() { var inputStr = [ '"Joe went to "', 'ebay.com" ', 'today, and bought big items"' ].join( "" ); var nodes = htmlParser.parse( inputStr ); expect( nodes.length ).toBe( 17 ); var i = -1; expectEntityNode ( nodes[ ++i ], 0, '"' ); expectTextNode ( nodes[ ++i ], 6, 'Joe went to ' ); expectEntityNode ( nodes[ ++i ], 18, '"' ); expectElementNode( nodes[ ++i ], 24, '', 'a', false ); expectTextNode ( nodes[ ++i ], 45, 'ebay.com' ); expectElementNode( nodes[ ++i ], 53, '', 'a', true ); expectEntityNode ( nodes[ ++i ], 57, '"' ); expectTextNode ( nodes[ ++i ], 63, ' today,' ); expectEntityNode ( nodes[ ++i ], 70, ' ' ); expectTextNode ( nodes[ ++i ], 76, 'and ' ); expectCommentNode( nodes[ ++i ], 80, '', 'stuff' ); expectTextNode ( nodes[ ++i ], 94, 'bought ' ); expectElementNode( nodes[ ++i ], 101, '', 'b', false ); expectTextNode ( nodes[ ++i ], 104, 'big' ); expectElementNode( nodes[ ++i ], 107, '', 'b', true ); expectTextNode ( nodes[ ++i ], 111, ' items' ); expectEntityNode ( nodes[ ++i ], 117, '"' ); } ); it( 'should match tags of both upper and lower case', function() { var inputStr = [ 'Joe went ', 'to ebay.com today, and purchased ', 'big items' ].join( '' ); var nodes = htmlParser.parse( inputStr ); expect( nodes.length ).toBe( 24 ); var i = -1; expectTextNode ( nodes[ ++i ], 0, 'Joe ' ); expectElementNode( nodes[ ++i ], 4, '', '!doctype', false ); expectCommentNode( nodes[ ++i ], 19, '', 'Comment' ); expectTextNode ( nodes[ ++i ], 35, 'went ' ); expectElementNode( nodes[ ++i ], 40, '', '!doctype', false ); expectTextNode ( nodes[ ++i ], 69, ' to ' ); expectElementNode( nodes[ ++i ], 73, '', 'a', false ); expectTextNode ( nodes[ ++i ], 94, 'ebay.com' ); expectElementNode( nodes[ ++i ], 102, '', 'a', true ); expectTextNode ( nodes[ ++i ], 106, ' today,' ); expectEntityNode ( nodes[ ++i ], 113, ' ' ); expectTextNode ( nodes[ ++i ], 119, 'and ' ); expectElementNode( nodes[ ++i ], 123, '', 'a', false ); expectTextNode ( nodes[ ++i ], 146, 'purchased' ); expectElementNode( nodes[ ++i ], 155, '', 'a', true ); expectTextNode ( nodes[ ++i ], 159, ' ' ); expectElementNode( nodes[ ++i ], 160, '', 'b', false ); expectTextNode ( nodes[ ++i ], 163, 'big' ); expectElementNode( nodes[ ++i ], 166, '', 'b', true ); expectTextNode ( nodes[ ++i ], 170, ' ' ); expectElementNode( nodes[ ++i ], 171, '', 'b', false ); expectCommentNode( nodes[ ++i ], 174, '', 'Comment 2' ); expectTextNode ( nodes[ ++i ], 192, 'items' ); expectElementNode( nodes[ ++i ], 197, '', 'b', true ); } ); } ); it( "should not freeze up the regular expression engine when presented with the input string in issue #54", function() { 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)", nodes = htmlParser.parse( inputStr ); expect( nodes.length ).toBe( 1 ); expectTextNode( nodes[ 0 ], 0, inputStr ); } ); it( "should not freeze up the regular expression engine when presented with the input string in issue #172", function() { var inputStr = '', nodes = htmlParser.parse( inputStr ); expect( nodes.length ).toBe( 1 ); expectTextNode( nodes[ 0 ], 0, inputStr ); } ); it( "should not freeze up the regular expression engine when presented with the input string in issue #204", function() { var inputStr = '', nodes = htmlParser.parse( inputStr ); expect( nodes.length ).toBe( 1 ); expectTextNode( nodes[ 0 ], 0, inputStr ); } ); } );