1 27 package org.htmlparser.tests.tagTests; 28 29 import org.htmlparser.PrototypicalNodeFactory; 30 import org.htmlparser.tags.ScriptTag; 31 import org.htmlparser.tests.ParserTestCase; 32 import org.htmlparser.util.ParserException; 33 34 public class ScriptTagTest extends ParserTestCase{ 35 36 static 37 { 38 System.setProperty ("org.htmlparser.tests.tagTests.ScriptTagTest", "ScriptTagTest"); 39 } 40 41 public ScriptTagTest(String name) 42 { 43 super(name); 44 } 45 46 public void testCreation() throws ParserException 47 { 48 String testHtml = "<SCRIPT>Script Code</SCRIPT>"; 49 createParser(testHtml,"http://localhost/index.html"); 50 parseAndAssertNodeCount(1); 51 assertTrue("Node should be a script tag",node[0] instanceof ScriptTag); 52 ScriptTag scriptTag = (ScriptTag)node[0]; 53 assertEquals("Script Tag Begin",0,scriptTag.getStartPosition ()); 54 assertEquals("Script Tag End",28,scriptTag.getEndTag ().getEndPosition ()); 55 assertEquals("Script Tag Code","Script Code",scriptTag.getScriptCode()); 56 } 57 58 public void testToHTML() throws ParserException { 59 createParser("<SCRIPT>document.write(d+\".com\")</SCRIPT>"); 60 parseAndAssertNodeCount(1); 61 assertTrue("Node should be a script tag",node[0] instanceof ScriptTag); 62 ScriptTag scriptTag = (ScriptTag)node[0]; 64 assertEquals("Expected Raw String","<SCRIPT>document.write(d+\".com\")</SCRIPT>",scriptTag.toHtml()); 65 } 66 67 79 public void testToHTMLWG() throws ParserException 80 { 81 StringBuffer sb2 = new StringBuffer (); 82 sb2.append("<script language=\"javascript\">\r\n"); 83 sb2.append("if(navigator.appName.indexOf(\"Netscape\") != -1)\r\n"); 84 sb2.append(" document.write ('xxx');\r\n"); 85 sb2.append("else\r\n"); 86 sb2.append(" document.write ('yyy');\r\n"); 87 sb2.append("</script>"); 88 String expectedHTML = sb2.toString(); 89 90 StringBuffer sb1 = new StringBuffer (); 91 sb1.append("<body>"); 92 sb1.append(expectedHTML); 93 sb1.append("\r\n"); 94 String testHTML1 = sb1.toString(); 95 96 createParser(testHTML1); 97 parser.setNodeFactory (new PrototypicalNodeFactory (new ScriptTag ())); 98 parseAndAssertNodeCount(3); 99 assertTrue("Node should be a script tag",node[1] 100 instanceof ScriptTag); 101 ScriptTag scriptTag = (ScriptTag)node[1]; 103 assertStringEquals("Expected Script Code",expectedHTML,scriptTag.toHtml()); 104 } 105 106 public void testParamExtraction() throws ParserException { 107 StringBuffer sb1 = new StringBuffer (); 108 sb1.append("<script SRC=\"/adb.js\" language=\"javascript\">\r\n"); 109 sb1.append("if(navigator.appName.indexOf(\"Netscape\") != -1)\r\n"); 110 sb1.append(" document.write ('xxx');\r\n"); 111 sb1.append("else\r\n"); 112 sb1.append(" document.write ('yyy');\r\n"); 113 sb1.append("</script>\r\n"); 114 createParser(sb1.toString()); 115 parseAndAssertNodeCount(2); 116 assertTrue("Node should be a script tag",node[0] instanceof ScriptTag); 117 ScriptTag scriptTag = (ScriptTag)node[0]; 118 assertEquals("Script Src","/adb.js",scriptTag.getAttribute("src")); 119 assertEquals("Script Language","javascript",scriptTag.getAttribute("language")); 120 } 121 122 public void testVariableDeclarations() throws ParserException { 123 StringBuffer sb1 = new StringBuffer (); 124 sb1.append("<script language=\"javascript\">\n"); 125 sb1.append("var lower = '<%=lowerValue%>';\n"); 126 sb1.append("</script>\n"); 127 createParser(sb1.toString()); 128 parseAndAssertNodeCount(2); 129 assertTrue("Node should be a script tag",node[0] instanceof ScriptTag); 130 ScriptTag scriptTag = (ScriptTag)node[0]; 131 assertStringEquals("Script toHTML()","<script language=\"javascript\">\nvar lower = '<%=lowerValue%>';\n</script>",scriptTag.toHtml()); 132 } 133 134 public void testSingleApostropheParsingBug() throws ParserException { 135 String script = "<script SRC='<%=sourceFileName%>'></script>"; 136 createParser(script); 137 parseAndAssertNodeCount(1); 138 assertTrue("Node should be a script tag",node[0] instanceof ScriptTag); 139 ScriptTag scriptTag = (ScriptTag)node[0]; 140 assertStringEquals("Script toHTML()",script,scriptTag.toHtml()); 141 } 142 143 } 144
| Popular Tags
|