KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > scannersTests > ScriptScannerTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/scannersTests/ScriptScannerTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/03/12 17:53:11 $
10
// $Revision: 1.59 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests.scannersTests;
28
29 import java.util.Hashtable JavaDoc;
30
31 import org.htmlparser.Node;
32 import org.htmlparser.Parser;
33 import org.htmlparser.filters.TagNameFilter;
34 import org.htmlparser.lexer.Lexer;
35 import org.htmlparser.scanners.ScriptDecoder;
36 import org.htmlparser.tags.BodyTag;
37 import org.htmlparser.tags.ScriptTag;
38 import org.htmlparser.tests.ParserTestCase;
39 import org.htmlparser.util.NodeIterator;
40 import org.htmlparser.util.NodeList;
41 import org.htmlparser.util.ParserException;
42
43 public class ScriptScannerTest extends ParserTestCase
44 {
45     static
46     {
47         System.setProperty ("org.htmlparser.tests.scannersTests.ScriptScannerTest", "ScriptScannerTest");
48     }
49
50     public ScriptScannerTest(String JavaDoc name) {
51         super(name);
52     }
53
54     public void testScan() throws ParserException
55     {
56         String JavaDoc testHtml = "<SCRIPT>document.write(d+\".com\")</SCRIPT>";
57         createParser(testHtml,"http://www.google.com/test/index.html");
58         parseAndAssertNodeCount(1);
59         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
60         // Check the data in the applet tag
61
ScriptTag scriptTag = (ScriptTag)node[0];
62         assertStringEquals("Expected Script Code","document.write(d+\".com\")",scriptTag.getScriptCode());
63         assertStringEquals("script tag html",testHtml,scriptTag.toHtml());
64     }
65
66     /**
67      * Test javascript tag attributes.
68      * Bug reported by Gordon Deudney 2002-03-27
69      * Upon parsing :
70      * &lt;SCRIPT LANGUAGE="JavaScript"
71      * SRC="../js/DetermineBrowser.js"&gt;&lt;/SCRIPT&gt;
72      * the SRC data cannot be retrieved.
73      */

74     public void testScanBug() throws ParserException
75     {
76         String JavaDoc src = "../js/DetermineBrowser.js";
77         createParser("<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"" + src + "\"></SCRIPT>","http://www.google.com/test/index.html");
78         parseAndAssertNodeCount(1);
79         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
80         // Check the data in the applet tag
81
ScriptTag scriptTag = (ScriptTag)node[0];
82         Hashtable JavaDoc table = scriptTag.getAttributes();
83         String JavaDoc srcExpected = (String JavaDoc)table.get("SRC");
84         assertEquals("Expected SRC value",src,srcExpected);
85     }
86
87     /**
88      * Test script code.
89      * Bug check by Wolfgang Germund 2002-06-02
90      * Upon parsing :
91      * &lt;script language="javascript"&gt;
92      * if(navigator.appName.indexOf("Netscape") != -1)
93      * document.write ('xxx');
94      * else
95      * document.write ('yyy');
96      * &lt;/script&gt;
97      * check getScriptCode().
98      */

99     public void testScanBugWG() throws ParserException
100     {
101         StringBuffer JavaDoc sb2 = new StringBuffer JavaDoc();
102         sb2.append("\r\nif(navigator.appName.indexOf(\"Netscape\") != -1)\r\n");
103         sb2.append(" document.write ('xxx');\r\n");
104         sb2.append("else\r\n");
105         sb2.append(" document.write ('yyy');\r\n");
106         String JavaDoc testHTML2 = sb2.toString();
107
108         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
109         sb1.append("<body><script language=\"javascript\">");
110         sb1.append(testHTML2);
111         sb1.append("</script>");
112         String JavaDoc testHTML1 = sb1.toString();
113
114         createParser(testHTML1,"http://www.google.com/test/index.html");
115         parseAndAssertNodeCount(1);
116         assertTrue("Node should be a body tag", node[0] instanceof BodyTag);
117         BodyTag body = (BodyTag)node[0];
118         assertTrue("Node should have one child", 1 == body.getChildCount ());
119         assertTrue("Child should be a script tag", body.getChild (0) instanceof ScriptTag);
120         // Check the data in the script tag
121
ScriptTag scriptTag = (ScriptTag)body.getChild (0);
122         String JavaDoc s = scriptTag.getScriptCode();
123         assertStringEquals("Expected Script Code",testHTML2,s);
124     }
125
126     public void testScanScriptWithLinks() throws ParserException
127     {
128         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
129         sb1.append("<script type=\"text/javascript\">\r\n"+
130             "<A HREF=\"http://thisisabadlink.com\">\r\n"+
131             "</script>\r\n");
132         String JavaDoc testHTML1 = new String JavaDoc(sb1.toString());
133
134         createParser(testHTML1,"http://www.hardwareextreme.com/");
135         parseAndAssertNodeCount(2);
136         assertTrue("Node should be a script tag",node[0]
137         instanceof ScriptTag);
138     }
139
140     public void testScanScriptWithComments() throws ParserException {
141         String JavaDoc expectedCode = "\n<!--\n"+
142                           " function validateForm()\n"+
143                           " {\n"+
144                           " var i = 10;\n"+
145                           " if(i < 5)\n"+
146                           " i = i - 1 ; \n"+
147                           " return true;\n"+
148                           " }\n"+
149                           "// -->\n";
150         createParser("<SCRIPT Language=\"JavaScript\">"+expectedCode+
151                           "</SCRIPT>","http://www.hardwareextreme.com/");
152         parseAndAssertNodeCount(1);
153         assertTrue("Node should be a script tag",node[0]
154         instanceof ScriptTag);
155         // Check the data in the applet tag
156
ScriptTag scriptTag = (ScriptTag)node[0];
157         String JavaDoc scriptCode = scriptTag.getScriptCode();
158         assertStringEquals("Expected Code",expectedCode,scriptCode);
159     }
160
161     /**
162      * Submitted by Dhaval Udani - reproducing bug 664404
163      * @throws ParserException
164      */

165     public void testScriptTagComments() throws
166     ParserException
167     {
168         String JavaDoc testHtml =
169         "<SCRIPT LANGUAGE=\"JavaScript\">\r\n"+
170             "<!--\r\n"+
171             "// -->\r\n"+
172         "</SCRIPT>";
173         createParser(testHtml);
174         parseAndAssertNodeCount(1);
175         ScriptTag scriptTag = (ScriptTag)node[0];
176         assertStringEquals("scriptag html",testHtml,scriptTag.toHtml());
177     }
178
179     /**
180      * Duplicates bug reported by James Moliere - whereby,
181      * if script tags are generated by script code, the parser
182      * interprets them as real tags. The problem was that the
183      * string parser was not moving to the ignore state on encountering double
184      * quotes (only single quotes were previously accepted).
185      * @throws Exception
186      */

187     public void testScriptTagsGeneratedByScriptCode() throws Exception JavaDoc {
188         createParser(
189             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 " +
190             "Transitional//EN\">" +
191             "<html>" +
192             "<head>" +
193             "<title>Untitled Document</title>" +
194             "<meta http-equiv=\"Content-Type\" content=\"text/html; " +
195             "charset=iso-8859-1\">" +
196             "</head>" +
197             "<script language=\"JavaScript\">" +
198             "document.write(\"<script " +
199             "language=\\\"JavaScript\\\">\");" +
200             "document.write(\"function onmousedown" +
201             "(event)\");" +
202             "document.write(\"{ // do something\"); " +
203             "document.write(\"}\"); " +
204             "// parser thinks this is the end tag.\n" +
205             "document.write(\"</script>\");" +
206             "</script>" +
207             "<body>" +
208             "</body>" +
209             "</html>"
210         );
211         Node scriptNodes [] =
212             parser.extractAllNodesThatAre(ScriptTag.class);
213         assertType(
214             "scriptnode",
215             ScriptTag.class,
216             scriptNodes[0]
217         );
218         ScriptTag scriptTag = (ScriptTag)scriptNodes[0];
219         assertStringEquals(
220             "script code",
221             "document.write(\"<script " +
222             "language=\\\"JavaScript\\\">\");" +
223             "document.write(\"function onmousedown" +
224             "(event)\");" +
225             "document.write(\"{ // do something\"); " +
226             "document.write(\"}\"); " +
227             "// parser thinks this is the end tag.\n" +
228             "document.write(\"</script>\");",
229             scriptTag.getScriptCode()
230         );
231
232     }
233
234     public void testScriptCodeExtraction() throws ParserException {
235         createParser(
236             "<SCRIPT language=JavaScript>" +
237             "document.write(\"<a HREF=\"1.htm\"><img SRC=\"1.jpg\" " +
238             "width=\"80\" height=\"20\" border=\"0\"></a>\");" +
239             "</SCRIPT>"
240         );
241         parseAndAssertNodeCount(1);
242         assertType("script",ScriptTag.class,node[0]);
243         ScriptTag scriptTag = (ScriptTag)node[0];
244         assertStringEquals(
245             "script code",
246             "document.write(\"<a HREF=\"1.htm\"><img SRC=\"1.jpg\" " +
247             "width=\"80\" height=\"20\" border=\"0\"></a>\");",
248             scriptTag.getScriptCode()
249         );
250     }
251
252     public void testScriptCodeExtractionWithMultipleQuotes() throws ParserException {
253         createParser(
254             "<SCRIPT language=JavaScript>" +
255             "document.write(\"<a HREF=\\\"1.htm\\\"><img SRC=\\\"1.jpg\\\" " +
256             "width=\\\"80\\\" height=\\\"20\\\" border=\\\"0\\\"></a>\");" +
257             "</SCRIPT>"
258         );
259         parseAndAssertNodeCount(1);
260         assertType("script",ScriptTag.class,node[0]);
261         ScriptTag scriptTag = (ScriptTag)node[0];
262         assertStringEquals(
263             "script code",
264             "document.write(\"<a HREF=\\\"1.htm\\\"><img SRC=\\\"1.jpg\\\" " +
265             "width=\\\"80\\\" height=\\\"20\\\" border=\\\"0\\\"></a>\");",
266             scriptTag.getScriptCode()
267         );
268     }
269
270     public void testScriptWithinComments() throws Exception JavaDoc {
271         createParser(
272             "<script language=\"JavaScript1.2\">" +
273             "\n" +
274             "var linkset=new Array()" +
275             "\n" +
276             "var ie4=document.all&&navigator.userAgent.indexOf(\"Opera\")==-1" +
277             "\n" +
278             "var ns6=document.getElementById&&!document.all" +
279             "\n" +
280             "var ns4=document.layers" +
281             "\n" +
282             "\n" +
283             "\n" +
284             "function showmenu(e,which){" +
285             "\n" +
286             "\n" +
287             "\n" +
288             "if (!document.all&&!document.getElementById&&!document.layers)" +
289             "\n" +
290             "return" +
291             "\n" +
292             "\n" +
293             "\n" +
294             "clearhidemenu()" +
295             "\n" +
296             "\n" +
297             "\n" +
298             "menuobj=ie4? document.all.popmenu : ns6? document.getElementById(\"popmenu\") : ns4? document.popmenu : \"\"\n" +
299             "\n" +
300             "menuobj.thestyle=(ie4||ns6)? menuobj.style : menuobj" +
301             "\n" +
302             "\n" +
303             "\n" +
304             "if (ie4||ns6)" +
305             "\n" +
306             "menuobj.innerHTML=which" +
307             "\n" +
308             "else{" +
309             "\n" +
310             "menuobj.document.write('<layer name=gui bgColor=#E6E6E6 width=165 onmouseover=\"clearhidemenu()\" onmouseout=\"hidemenu()\">'+which+'</layer>')" +
311             "\n" +
312             "menuobj.document.close()" +
313             "\n" +
314             "}" +
315             "\n" +
316             "\n" +
317             "\n" +
318             "menuobj.contentwidth=(ie4||ns6)? menuobj.offsetWidth : menuobj.document.gui.document.width" +
319             "\n" +
320             "menuobj.contentheight=(ie4||ns6)? menuobj.offsetHeight : menuobj.document.gui.document.height" +
321             "\n" +
322             "eventX=ie4? event.clientX : ns6? e.clientX : e.x" +
323             "\n" +
324             "eventY=ie4? event.clientY : ns6? e.clientY : e.y" +
325             "\n" +
326             "\n" +
327             "\n" +
328             "//Find out how close the mouse is to the corner of the window" +
329             "\n" +
330             "var rightedge=ie4? document.body.clientWidth-eventX : window.innerWidth-eventX" +
331             "\n" +
332             "var bottomedge=ie4? document.body.clientHeight-eventY : window.innerHeight-eventY" +
333             "\n" +
334             "\n" +
335             "\n" +
336             "//if the horizontal distance isn't enough to accomodate the width of the context menu" +
337             "\n" +
338             "if (rightedge < menuobj.contentwidth)" +
339             "\n" +
340             "//move the horizontal position of the menu to the left by it's width" +
341             "\n" +
342             "menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX-menuobj.contentwidth : ns6? window.pageXOffset+eventX-menuobj.contentwidth : eventX-menuobj.contentwidth" +
343             "\n" +
344             "else" +
345             "\n" +
346             "//position the horizontal position of the menu where the mouse was clicked" +
347             "\n" +
348             "menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX : ns6? window.pageXOffset+eventX : eventX" +
349             "\n" +
350             "\n" +
351             "\n" +
352             "//same concept with the vertical position" +
353             "\n" +
354             "if (bottomedge<menuobj.contentheight)" +
355             "\n" +
356             "menuobj.thestyle.top=ie4? document.body.scrollTop+eventY-menuobj.contentheight : ns6? window.pageYOffset+eventY-menuobj.contentheight : eventY-menuobj.contentheight" +
357             "\n" +
358             "else" +
359             "\n" +
360             "menuobj.thestyle.top=ie4? document.body.scrollTop+event.clientY : ns6? window.pageYOffset+eventY : eventY" +
361             "\n" +
362             "menuobj.thestyle.visibility=\"visible\"\n" +
363             "\n" +
364             "return false" +
365             "\n" +
366             "}" +
367             "\n" +
368             "\n" +
369             "\n" +
370             "function contains_ns6(a, b) {" +
371             "\n" +
372             "//Determines if 1 element in contained in another- by Brainjar.com" +
373             "\n" +
374             "while (b.parentNode)" +
375             "\n" +
376             "if ((b = b.parentNode) == a)" +
377             "\n" +
378             "return true;" +
379             "\n" +
380             "return false;" +
381             "\n" +
382             "}" +
383             "\n" +
384             "\n" +
385             "\n" +
386             "function hidemenu(){" +
387             "\n" +
388             "if (window.menuobj)" +
389             "\n" +
390             "menuobj.thestyle.visibility=(ie4||ns6)? \"hidden\" : \"hide\"\n" +
391             "\n" +
392             "}" +
393             "\n" +
394             "\n" +
395             "\n" +
396             "function dynamichide(e){" +
397             "\n" +
398             "if (ie4&&!menuobj.contains(e.toElement))" +
399             "\n" +
400             "hidemenu()" +
401             "\n" +
402             "else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))" +
403             "\n" +
404             "hidemenu()" +
405             "\n" +
406             "}" +
407             "\n" +
408             "\n" +
409             "\n" +
410             "function delayhidemenu(){" +
411             "\n" +
412             "if (ie4||ns6||ns4)" +
413             "\n" +
414             "delayhide=setTimeout(\"hidemenu()\",500)" +
415             "\n" +
416             "}" +
417             "\n" +
418             "\n" +
419             "\n" +
420             "function clearhidemenu(){" +
421             "\n" +
422             "if (window.delayhide)" +
423             "\n" +
424             "clearTimeout(delayhide)" +
425             "\n" +
426             "}" +
427             "\n" +
428             "\n" +
429             "\n" +
430             "function highlightmenu(e,state){" +
431             "\n" +
432             "if (document.all)" +
433             "\n" +
434             "source_el=event.srcElement" +
435             "\n" +
436             "else if (document.getElementById)" +
437             "\n" +
438             "source_el=e.target" +
439             "\n" +
440             "if (source_el.className==\"menuitems\"){" +
441             "\n" +
442             "source_el.id=(state==\"on\")? \"mouseoverstyle\" : \"\"\n" +
443             "\n" +
444             "}" +
445             "\n" +
446             "else{" +
447             "\n" +
448             "while(source_el.id!=\"popmenu\"){" +
449             "\n" +
450             "source_el=document.getElementById? source_el.parentNode : source_el.parentElement" +
451             "\n" +
452             "if (source_el.className==\"menuitems\"){" +
453             "\n" +
454             "source_el.id=(state==\"on\")? \"mouseoverstyle\" : \"\"\n" +
455             "\n" +
456             "}" +
457             "\n" +
458             "}" +
459             "\n" +
460             "}" +
461             "\n" +
462             "}" +
463             "\n" +
464             "\n" +
465             "\n" +
466             "if (ie4||ns6)" +
467             "\n" +
468             "document.onclick=hidemenu" +
469             "\n" +
470             "\n" +
471             "\n" +
472             "</script>"
473         );
474         parseAndAssertNodeCount(1);
475
476     }
477
478     /**
479      * There was a bug in the ScriptScanner when there was multiline script and
480      * the last line did not have a newline before the end script tag. For example:
481      *
482      * &lt;script&gt;alert()
483      * alert()&lt;/script&gt;
484      *
485      * Would generate the following "scriptCode()" result:
486      * alert()alert()
487      *
488      * But should actually return:
489      * alert()
490      * alert()
491      *
492      * This was fixed in ScriptScanner, which this test verifies
493      */

494     public void testScriptCodeExtractionWithNewlines() throws ParserException {
495         String JavaDoc scriptContents = "alert()\r\nalert()";
496         createParser("<script>" + scriptContents + "</script>");
497         parseAndAssertNodeCount(1);
498         assertType("script",ScriptTag.class,node[0]);
499         ScriptTag scriptTag = (ScriptTag)node[0];
500         assertStringEquals(
501             "script code",
502             scriptContents,
503             scriptTag.getScriptCode()
504         );
505     }
506
507     /**
508      * Tests a bug in ScriptScanner where a NPE would be thrown if the
509      * script tag was not closed before the document ended.
510      */

511     public void testScanNoEndTag() throws ParserException {
512         createParser("<script>");
513         parseAndAssertNodeCount(1);
514     }
515
516     /**
517      * See bug #741769 ScriptScanner doesn't handle quoted </script> tags
518      */

519     public void testScanQuotedEndTag() throws ParserException
520     {
521         String JavaDoc html = "<SCRIPT language=\"JavaScript\">document.write('</SCRIPT>');</SCRIPT>";
522         createParser(html);
523         parseAndAssertNodeCount(1);
524         assertStringEquals ("Parse error", html, node[0].toHtml ());
525     }
526
527
528
529     public void testScanScriptWithTagsInComment() throws ParserException {
530         String JavaDoc javascript = "\n// This is javascript with <li> tag in the comment\n";
531         createParser("<script>"+ javascript + "</script>");
532         parseAndAssertNodeCount(1);
533         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
534         ScriptTag scriptTag = (ScriptTag)node[0];
535         String JavaDoc scriptCode = scriptTag.getScriptCode();
536         assertStringEquals("Expected Code",javascript,scriptCode);
537     }
538
539     public void testScanScriptWithJavascriptLineEndings() throws ParserException {
540         String JavaDoc javascript =
541             "\n" +
542             "var s = \"This is a string \\\n" +
543             "that spans multiple lines;\"\n";
544         createParser("<script>"+ javascript + "</script>");
545         parseAndAssertNodeCount(1);
546         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
547         ScriptTag scriptTag = (ScriptTag)node[0];
548         String JavaDoc scriptCode = scriptTag.getScriptCode();
549         assertStringEquals("Expected Code",javascript,scriptCode);
550     }
551
552
553     public void testScanScriptWithTags() throws ParserException {
554         String JavaDoc javascript = "\nAnything inside the script tag should be unchanged, even <li> and other html tags\n";
555         createParser("<script>"+ javascript + "</script>");
556         parseAndAssertNodeCount(1);
557         assertTrue("Node should be a script tag",node[0] instanceof ScriptTag);
558         ScriptTag scriptTag = (ScriptTag)node[0];
559         String JavaDoc scriptCode = scriptTag.getScriptCode();
560         assertStringEquals("Expected Code",javascript,scriptCode);
561     }
562
563     /**
564      * See bug #839264 toHtml() parse error in Javascripts with "form" keyword
565      * Contributed by Ivan Wang (xj92wang)
566      */

567     public void testScriptsWithForm ()
568         throws
569             ParserException
570     {
571         String JavaDoc teststring = "<SCRIPT LANGUAGE=\"JAVASCRIPT\">" +
572             "function valForm(frm) { " +
573             " for (n=0; n<this.form.test; n++) this.form.nb++; "+
574             "}"+
575             "</SCRIPT>";
576         StringBuffer JavaDoc htmlBuffer = new StringBuffer JavaDoc ();
577
578         createParser (teststring);
579         for (NodeIterator i = parser.elements (); i.hasMoreNodes ();)
580         {
581             Node tnode = i.nextNode ();
582             htmlBuffer.append (tnode.toHtml ());
583         }
584         assertStringEquals ("bad html", teststring, htmlBuffer.toString ());
585     }
586
587     /**
588      * See bug #902121 StringBean throws NullPointerException
589      * Contributed by Reza Motori (rezamotori)
590      */

591     public void testDecodeScript ()
592         throws ParserException
593     {
594         String JavaDoc plaintext =
595             "<HTML>\n" +
596             "<HEAD>\n" +
597             "<TITLE>Script Encoder Sample Page</TITLE>\n" +
598             "<SCRIPT LANGUAGE=\"JScript.Encode\">\n" +
599             "<!--//\n" +
600             "//Copyright© 1998 Microsoft Corporation. All Rights Reserved.\n" +
601             "//**Start Encode**\r\n" +
602             "function verifyCorrectBrowser(){\r\n" +
603             " if(navigator.appName == \"Microsoft Internet Explorer\")\r\n" +
604             " if (navigator.appVersion.indexOf (\"5.\") >= 0)\r\n" +
605             " return(true);\r\n" +
606             " else\r\n" +
607             " return(false);\r\n" +
608             "}\r\n" +
609             "function getAppropriatePage(){\r\n" +
610             " var str1 = \"Had this been an actual Web site, a page compatible with \";\r\n" +
611             " var str2 = \"browsers other than \";\r\n" +
612             " var str3 = \"Microsoft Internet Explorer 5.0 \";\r\n" +
613             " var str4 = \"would have been loaded.\";\r\n" +
614             " if (verifyCorrectBrowser())\r\n" +
615             " document.write(str1 + str3 + str4);\r\n" +
616             " else\r\n" +
617             " document.write(str1 + str2 + str3 + str4);\r\n" +
618             "}\r\n" +
619             "//-->\r\n" +
620             "</SCRIPT>\n" +
621             "</HEAD>\n" +
622             "<BODY onload=\"getAppropriatePage()\">\n" +
623             "</BODY>\n" +
624             "</HTML>";
625         String JavaDoc cryptext =
626             "<HTML>\n" +
627             "<HEAD>\n" +
628             "<TITLE>Script Encoder Sample Page</TITLE>\n" +
629             "<SCRIPT LANGUAGE=\"JScript.Encode\">\n" +
630             "<!--//\n" +
631             "//Copyright© 1998 Microsoft Corporation. All Rights Reserved.\n" +
632             "//**Start Encode**#@~^ZwIAAA==@#@&0; mDkW P7nDb0zZKD.n1YAMGhk+Dvb`@#@&P,kW`UC7kLlDGDcl22gl:n~{'~Jtr1DGkW6YP&xDnD +OPA62sKD+ME#@#@&P,~~k6PvxC\\rLmYGDcCwa.n.kkWU bx[+X66Pcr*cJ#,@*{~!*@#@&P,P~~,D+D;D `YM;n#p@#@&P~P~n^/n@#@&~P,P~~M+Y;. `Wl^d#I@#@&)@#@&6E ^YbWUPT+O)awDK2DblYKCo`* @#@&~~7l.PkOD8Px~rCl[~Dtr/,8+U,l Pl1Y!CV,n4,/rO~Pm~wmo+,^G:alDk8Vn~SkOt,Ei@#@&~~7lD~dDD+P{~r4.Khk+DkPKOtD~Y4lU~ri@#@&~P7lD,dOD2P{PEHr^MWdW6OP&xOnMx+O~A62VK.D~lRZPJp@#@&~P7l.PkY.*,'PrAW!VN,4C\\P(+nx~sKl[+9 Jp@#@&~,k0~c7+.k6z;W.M+1YAMWSd+M`b#@#@&~~,PNK^Es+xD ADbY`dY.q,_~/D.&,_~dDDcbI@#@&~Psk+@#@&P,PP9W1;:xORSDrO`/D.F,_PkO. ,_,/ODf~3PdYM*#p@#@&N@#@&z&R @*@#@&qrIAAA==^#~@</SCRIPT>\n" +
633             "</HEAD>\n" +
634             "<BODY onload=\"getAppropriatePage()\">\n" +
635             "</BODY>\n" +
636             "</HTML>";
637         Lexer lexer;
638         
639         lexer = new Lexer (cryptext);
640         ScriptDecoder.LAST_STATE = ScriptDecoder.STATE_INITIAL; // read everything
641
try
642         {
643             String JavaDoc result = ScriptDecoder.Decode (lexer.getPage (), lexer.getCursor ());
644             assertStringEquals ("decoding failed", plaintext, result);
645         }
646         finally
647         {
648             ScriptDecoder.LAST_STATE = ScriptDecoder.STATE_DONE;
649         }
650     }
651     
652     /**
653      * See bug #902121 StringBean throws NullPointerException
654      * Contributed by Reza Motori (rezamotori)
655      */

656     public void testDecodePage ()
657         throws ParserException
658     {
659         String JavaDoc url = "http://htmlparser.sourceforge.net/test/EncryptedScriptExample.html";
660         String JavaDoc plaintext =
661             "\r\n" +
662             "var nows = new Date();\r\n" +
663             "var nIndexs = nows.getTime();\r\n" +
664             "document.write(\"<img SRC=\\\"http://www.parsads.com/adserve/scriptinject.asp?F=4&Z=3,4,5,10,12&N=1&U=644&O=&nocache=\" + nIndexs + \"\\\" width=\\\"1\\\" hight=\\\"1\\\"><img SRC=\\\"http://www.parsads.com/adserve/scriptinject.asp?F=4&Z=3,4,5,10,12&N=1&U=643&O=&nocache=\" + nIndexs + \"\\\" width=\\\"1\\\" hight=\\\"1\\\"><img SRC=\\\"http://www.parsads.com/adserve/scriptinject.asp?F=4&Z=3,4,5,10,12&N=1&U=324&O=&nocache=\" + nIndexs + \"\\\" width=\\\"1\\\" hight=\\\"1\\\">\");\r\n";
665         
666         parser = new Parser (url);
667         NodeList scripts = parser.extractAllNodesThatMatch (new TagNameFilter ("SCRIPT"));
668         assertEquals ("wrong number of scripts found", 2, scripts.size ());
669         ScriptTag script = (ScriptTag)scripts.elementAt (1);
670         assertStringEquals ("script not decoded correctly", plaintext, script.getScriptCode ());
671     }
672 }
673
Popular Tags