1
1
package org .archive .format .text .html ;
2
2
3
- import org .archive .format .text .html .CDATALexer ;
4
- import org .archive .format .text .html .NodeUtils ;
5
3
import org .htmlparser .Node ;
6
4
import org .htmlparser .lexer .Page ;
7
- //import org.htmlparser.nodes.RemarkNode;
8
5
import org .htmlparser .nodes .TagNode ;
9
6
import org .htmlparser .nodes .TextNode ;
10
7
import org .htmlparser .util .ParserException ;
@@ -72,20 +69,38 @@ public void testInCSS() throws ParserException {
72
69
assertFalse (l .inJS ());
73
70
assertTrue (NodeUtils .isCloseTagNodeNamed (n , "STYLE" ));
74
71
}
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
+ }
75
102
76
103
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
-
89
104
assertJSContentWorks ("//<!--\n foo bar baz\n //-->" );
90
105
assertJSContentWorks ("<!-- foo bar baz -->" );
91
106
assertJSContentWorks ("//<!-- foo bar baz -->" );
@@ -94,9 +109,22 @@ public void testInJSComment() throws ParserException {
94
109
assertJSContentWorks ("if(1 < 2) { foo(); } " );
95
110
assertJSContentWorks ("if(1 <n) { foo(); } " );
96
111
assertJSContentWorks ("document.write(\" <b>bold</b>\" ); " );
97
- assertJSContentWorks ("document.write(\" <script>bold</script>\" ); " );
112
+ assertJSContentWorks ("document.write(\" <script>bold<\\ /script>\" ); " );
98
113
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 ");
100
128
}
101
129
102
130
private void assertJSContentWorks (String js ) throws ParserException {
0 commit comments