[Htmlparser-cvs] htmlparser/src/org/htmlparser/tests/visitorsTests ScriptCommentTest.java,NONE,1.1.2
Brought to you by:
derrickoswald
From: Derrick O. <der...@us...> - 2004-05-22 20:10:42
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/visitorsTests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29429/src/org/htmlparser/tests/visitorsTests Modified Files: Tag: v1_41 AllTests.java Added Files: Tag: v1_41 ScriptCommentTest.java Log Message: Fix bug# 919738 Text has not been extracted correctly using StringBean and (duplicate) bug #936392 ScriptTag visitor fails for comments with ' by handling single and multiline ecmascript comments in the Lexer class when called with quotesmart true. --- NEW FILE: ScriptCommentTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2004 Jim Arnell // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/visitorsTests/ScriptCommentTest.java,v $ // $Author: derrickoswald $ // $Date: 2004/05/22 20:10:33 $ // $Revision: 1.1.2.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.visitorsTests; import org.htmlparser.tags.CompositeTag; import org.htmlparser.tags.ScriptTag; import org.htmlparser.tags.Tag; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.visitors.NodeVisitor; public class ScriptCommentTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.visitorsTests.ScriptCommentTest", "ScriptCommentTest"); } private String workingScriptTag = "<script language='javascript'>" + "// I cant handle single quotations\n" + "</script>"; private String workingHtml = this.workingScriptTag + "<HTML>" + "</HTML>"; private String failingScriptTag = "<script language='javascript'>" + "// I can't handle single quotations.\n" + "</script>"; private String failingHtml = this.failingScriptTag + "<HTML>" + "</HTML>"; private String failingHtml2 = "<HTML>" + this.failingScriptTag + "</HTML>"; private String anotherFailingScriptTag = "<script language='javascript'>" + "/* I can't handle single quotations. */" + "</script>"; private String failingHtml3 = this.anotherFailingScriptTag + "<HTML>" + "</HTML>"; public ScriptCommentTest(String name) { super(name); } public void testTagWorking() throws Exception { createParser(this.workingHtml); ScriptVisitor visitor = new ScriptVisitor(); this.parser.visitAllNodesWith(visitor); String scriptNodeHtml = visitor.scriptTag.toHtml(); assertEquals("Script parsing worked", this.workingScriptTag, scriptNodeHtml); } public void testScriptTagNotWorkingOuter() throws Exception { createParser(this.failingHtml); ScriptVisitor visitor = new ScriptVisitor(); this.parser.visitAllNodesWith(visitor); String scriptNodeHtml = visitor.scriptTag.toHtml(); assertEquals("Script parsing not working", this.failingScriptTag, scriptNodeHtml); } public void testScriptTagNotWorkingInner() throws Exception { createParser(this.failingHtml2); ScriptVisitor visitor = new ScriptVisitor(); this.parser.visitAllNodesWith(visitor); String scriptNodeHtml = visitor.scriptTag.toHtml(); assertEquals("Script parsing not working", this.failingScriptTag, scriptNodeHtml); } public void testScriptTagNotWorkingMultiLine() throws Exception { createParser(this.anotherFailingScriptTag); ScriptVisitor visitor = new ScriptVisitor(); this.parser.visitAllNodesWith(visitor); String scriptNodeHtml = visitor.scriptTag.toHtml(); assertEquals("Script parsing not working", this.anotherFailingScriptTag, scriptNodeHtml); } /** * Implement test case NodeVisitor. */ public final class ScriptVisitor extends NodeVisitor { /** Keps the only script tag. */ public ScriptTag scriptTag; /** * Creates a new ScriptVisitor object. * * @param hat param. * @param hostString param. * @param direction param. */ public ScriptVisitor() { super(true, true); } /** * @see org.htmlparser.visitors.NodeVisitor */ public void visitTag(final Tag n) { if ((null != n.getParent()) || ((n instanceof CompositeTag) && (null == ((CompositeTag) n).getEndTag()))) { if (n instanceof ScriptTag) { this.scriptTag = (ScriptTag) n; } } else { if (n instanceof ScriptTag) { this.scriptTag = (ScriptTag) n; } } } } } |