1 27 package org.htmlparser.tests.visitorsTests; 28 29 import org.htmlparser.tags.CompositeTag; 30 import org.htmlparser.tags.ScriptTag; 31 import org.htmlparser.Tag; 32 import org.htmlparser.tests.ParserTestCase; 33 import org.htmlparser.visitors.NodeVisitor; 34 35 public class ScriptCommentTest extends ParserTestCase { 36 37 static 38 { 39 System.setProperty ("org.htmlparser.tests.visitorsTests.ScriptCommentTest", "ScriptCommentTest"); 40 } 41 42 private String workingScriptTag = 43 "<script language='javascript'>" 44 + "// I cant handle single quotations\n" 45 + "</script>"; 46 47 private String workingHtml = 48 this.workingScriptTag 49 + "<HTML>" 50 + "</HTML>"; 51 52 private String failingScriptTag = 53 "<script language='javascript'>" 54 + "// I can't handle single quotations.\n" 55 + "</script>"; 56 57 private String failingHtml = 58 this.failingScriptTag 59 + "<HTML>" 60 + "</HTML>"; 61 62 private String failingHtml2 = 63 "<HTML>" 64 + this.failingScriptTag 65 + "</HTML>"; 66 67 private String anotherFailingScriptTag = 68 "<script language='javascript'>" 69 + "/* I can't handle single quotations. */" 70 + "</script>"; 71 72 public ScriptCommentTest(String name) { 73 super(name); 74 } 75 76 public void testTagWorking() throws Exception { 77 createParser(this.workingHtml); 78 ScriptVisitor visitor = new ScriptVisitor(); 79 this.parser.visitAllNodesWith(visitor); 80 String scriptNodeHtml = visitor.scriptTag.toHtml(); 81 assertEquals("Script parsing worked", this.workingScriptTag, scriptNodeHtml); 82 } 83 84 public void testScriptTagNotWorkingOuter() throws Exception { 85 createParser(this.failingHtml); 86 ScriptVisitor visitor = new ScriptVisitor(); 87 this.parser.visitAllNodesWith(visitor); 88 String scriptNodeHtml = visitor.scriptTag.toHtml(); 89 assertEquals("Script parsing not working", this.failingScriptTag, scriptNodeHtml); 90 } 91 92 public void testScriptTagNotWorkingInner() throws Exception { 93 createParser(this.failingHtml2); 94 ScriptVisitor visitor = new ScriptVisitor(); 95 this.parser.visitAllNodesWith(visitor); 96 String scriptNodeHtml = visitor.scriptTag.toHtml(); 97 assertEquals("Script parsing not working", this.failingScriptTag, scriptNodeHtml); 98 } 99 100 public void testScriptTagNotWorkingMultiLine() throws Exception { 101 createParser(this.anotherFailingScriptTag); 102 ScriptVisitor visitor = new ScriptVisitor(); 103 this.parser.visitAllNodesWith(visitor); 104 String scriptNodeHtml = visitor.scriptTag.toHtml(); 105 assertEquals("Script parsing not working", this.anotherFailingScriptTag, scriptNodeHtml); 106 } 107 108 111 public final class ScriptVisitor extends NodeVisitor { 112 113 114 public ScriptTag scriptTag; 115 116 119 public ScriptVisitor() { 120 super(true, true); 121 } 122 123 126 public void visitTag(final Tag n) { 127 if ((null != n.getParent()) 128 || ((n instanceof CompositeTag) 129 && (null == ((CompositeTag) n).getEndTag()))) { 130 131 if (n instanceof ScriptTag) { 132 this.scriptTag = (ScriptTag) n; 133 } 134 } else { 135 if (n instanceof ScriptTag) { 136 this.scriptTag = (ScriptTag) n; 137 } 138 } 139 } 140 } 141 } 142 143 | Popular Tags |