11package org .archive .format .text .html ;
22
3- import org .archive .format .text .html .CDATALexer ;
4- import org .archive .format .text .html .NodeUtils ;
53import org .htmlparser .Node ;
64import org .htmlparser .lexer .Page ;
7- //import org.htmlparser.nodes.RemarkNode;
85import org .htmlparser .nodes .TagNode ;
96import org .htmlparser .nodes .TextNode ;
107import org .htmlparser .util .ParserException ;
@@ -72,20 +69,38 @@ public void testInCSS() throws ParserException {
7269 assertFalse (l .inJS ());
7370 assertTrue (NodeUtils .isCloseTagNodeNamed (n , "STYLE" ));
7471 }
72+
73+ public void testInCSSEmpty () throws ParserException {
74+ l = makeLexer ("<style></style>" );
75+ assertFalse (l .inCSS ());
76+ assertFalse (l .inJS ());
77+ n = l .nextNode ();
78+ assertFalse (l .inCSS ());
79+ assertFalse (l .inJS ());
80+ assertTrue (NodeUtils .isNonEmptyOpenTagNodeNamed (n , "STYLE" ));
81+ n = l .nextNode ();
82+ assertFalse (l .inCSS ());
83+ assertFalse (l .inJS ());
84+ assertTrue (NodeUtils .isCloseTagNodeNamed (n , "STYLE" ));
85+ }
86+
87+ public void testInCSSBachelorTag () throws ParserException {
88+ l = makeLexer ("<style />" );
89+ assertFalse (l .inCSS ());
90+ assertFalse (l .inJS ());
91+ n = l .nextNode ();
92+ assertFalse (l .inCSS ());
93+ assertFalse (l .inJS ());
94+ assertTrue (NodeUtils .isTagNode (n ));
95+ assertTrue (((TagNode ) n ).isEmptyXmlTag ());
96+ assertEquals (((TagNode ) n ).getTagName (), "STYLE" );
97+ n = l .nextNode ();
98+ assertFalse (l .inCSS ());
99+ assertFalse (l .inJS ());
100+ assertNull (n );
101+ }
75102
76103 public void testInJSComment () throws ParserException {
77-
78- // dumpParse("<script>//<!--\n foo bar baz\n //--></script>");
79- // dumpParse("<script><!-- foo bar baz --></script>");
80- // dumpParse("<script>//<!-- foo bar baz --></script>");
81- // dumpParse("<script><!-- foo bar baz //--></script>");
82- // dumpParse("<script>\n//<!-- foo bar baz\n //--></script>");
83- // dumpParse("<script> if(1 < 2) { foo(); } </script>");
84- // dumpParse("<script> if(1 <n) { foo(); } </script>");
85- // dumpParse("<script> document.write(\"<b>bold</b>\"); </script>");
86- // dumpParse("<script> document.write(\"<script>bold</script>\"); </script>");
87- // dumpParse("<script> <![CDATA[\n if(i<n) { foo() } // content of your Javascript goes here \n ]]> </script>");
88-
89104 assertJSContentWorks ("//<!--\n foo bar baz\n //-->" );
90105 assertJSContentWorks ("<!-- foo bar baz -->" );
91106 assertJSContentWorks ("//<!-- foo bar baz -->" );
@@ -94,9 +109,22 @@ public void testInJSComment() throws ParserException {
94109 assertJSContentWorks ("if(1 < 2) { foo(); } " );
95110 assertJSContentWorks ("if(1 <n) { foo(); } " );
96111 assertJSContentWorks ("document.write(\" <b>bold</b>\" ); " );
97- assertJSContentWorks ("document.write(\" <script>bold</script>\" ); " );
112+ assertJSContentWorks ("document.write(\" <script>bold<\\ /script>\" ); " );
98113 assertJSContentWorks ("<![CDATA[\n if(i<n) { foo() } // a comment \n ]]> " );
99-
114+ assertJSContentWorks ("var script = '<script>alert(\" hello, world!\" )<\\ /script>'; console.log(script); " );
115+ assertJSContentWorks ("\n "
116+ + " var _hmt = _hmt || [];\n "
117+ + " (function() {\n "
118+ + " var hm = document.createElement(\" script\" );\n "
119+ + " hm.src = \" https://#/hm.js?aba99f7fd4116f6c8c3d1650e8f8ec17\" ;\n "
120+ + " var s = document.getElementsByTagName(\" script\" )[0]; \n "
121+ + " s.parentNode.insertBefore(hm, s);\n "
122+ + " })();\n "
123+ + " " );
124+ /*
125+ * The parser fails on unfinished HTML comments inside script or style.
126+ */
127+ // assertJSContentWorks("<!-- foo bar baz ");
100128 }
101129
102130 private void assertJSContentWorks (String js ) throws ParserException {
0 commit comments