/*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( "