1 27 package org.htmlparser.tests.tagTests; 28 29 import org.htmlparser.Text; 30 import org.htmlparser.tags.HeadTag; 31 import org.htmlparser.tags.Html; 32 import org.htmlparser.tags.StyleTag; 33 import org.htmlparser.tests.ParserTestCase; 34 import org.htmlparser.util.ParserException; 35 36 public class StyleTagTest extends ParserTestCase { 37 38 static 39 { 40 System.setProperty ("org.htmlparser.tests.tagTests.StyleTagTest", "StyleTagTest"); 41 } 42 43 public StyleTagTest(String name) { 44 super(name); 45 } 46 47 public void testToHTML() throws ParserException { 48 String html = "<style>a.h{background-color:#ffee99}</style>"; 49 createParser(html); 50 parseAndAssertNodeCount(1); 51 assertTrue(node[0] instanceof StyleTag); 52 StyleTag styleTag = (StyleTag)node[0]; 53 assertEquals("Raw String",html,styleTag.toHtml()); 54 } 55 56 60 public void testToHtmlAttributes() throws ParserException { 61 String style = "<STYLE type=\"text/css\">\n"+ 62 "<!--"+ 63 "{something....something}"+ 64 "-->"+ 65 "</STYLE>"; 66 createParser(style); 67 parseAndAssertNodeCount(1); 68 assertTrue(node[0] instanceof StyleTag); 69 StyleTag styleTag = (StyleTag)node[0]; 70 assertStringEquals("toHtml",style,styleTag.toHtml()); 71 } 72 73 public void testScan() throws ParserException 74 { 75 createParser("<STYLE TYPE=\"text/css\"><!--\n\n"+ 76 "</STYLE>","http://www.yle.fi/"); 77 parseAndAssertNodeCount(1); 78 } 79 80 public void testScanBug() throws ParserException { 81 createParser("<html><head><title>Yahoo!</title><base HREF=http://www.yahoo.com/ target=_top><meta http-equiv=\"PICS-Label\" content='(PICS-1.1 \"http://www.icra.org/ratingsv02.html\" l r (cz 1 lz 1 nz 1 oz 1 vz 1) gen true for \"http://www.yahoo.com\" r (cz 1 lz 1 nz 1 oz 1 vz 1) \"http://www.rsac.org/ratingsv01.html\" l r (n 0 s 0 v 0 l 0) gen true for \"http://www.yahoo.com\" r (n 0 s 0 v 0 l 0))'><style>a.h{background-color:#ffee99}</style></head>", 82 "http://www.google.com/test/index.html"); 83 parseAndAssertNodeCount(1); 84 assertTrue("First node should be a HTML tag", node[0] instanceof Html); 85 Html html = (Html)node[0]; 86 assertTrue("HTML tag should have one child", 1 == html.getChildCount ()); 87 assertTrue("First child should be a HEAD tag", html.childAt (0) instanceof HeadTag); 88 HeadTag head = (HeadTag)html.childAt (0); 89 assertTrue("HEAD tag should have four children", 4 == head.getChildCount ()); 90 assertTrue("Fourth child should be a STYLE tag", head.childAt (3) instanceof StyleTag); 91 StyleTag styleTag = (StyleTag)head.childAt (3); 92 assertEquals("Style Code","a.h{background-color:#ffee99}",styleTag.getStyleCode()); 93 } 94 95 98 public void testScanBug2() throws ParserException { 99 createParser("<STYLE TYPE=\"text/css\"><!--\n\n"+ 100 "input{font-family: arial, helvetica, sans-serif; font-size:11px;}\n\n"+ 101 "i {font-family: times; font-size:10pt; font-weight:normal;}\n\n"+ 102 ".ruuhka {font-family: arial, helvetica, sans-serif; font-size:11px;}\n\n"+ 103 ".paalinkit {font-family: arial, helvetica, sans-serif; font-size:12px;}\n\n"+ 104 ".shortselect{font-family: arial, helvetica, sans-serif; font-size:12px; width:130;}\n\n"+ 105 ".cityselect{font-family: arial, helvetica, sans-serif; font-size:11px; width:100;}\n\n"+ 106 ".longselect{font-family: arial, helvetica, sans-serif; font-size:12px;}\n\n"+ 107 "---></STYLE>","http://www.yle.fi/"); 108 parseAndAssertNodeCount(1); 109 assertTrue(node[0] instanceof StyleTag); 110 } 111 112 115 public void testScanBug3() throws ParserException { 116 String expectedCode = "<!--\nbody,td,a,p,.h{font-family:arial,sans-serif;} .h{font-size: 20px;} .h{color:} .q{text-decoration:none; color:#0000cc;}\n//-->"; 117 createParser("<html><head><META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=ISO-8859-1\"><title>Google</title><style>"+ 118 expectedCode+ 119 "</style>","http://www.yle.fi/"); 120 parseAndAssertNodeCount(1); 121 assertTrue("First node should be a HTML tag", node[0] instanceof Html); 122 Html html = (Html)node[0]; 123 assertTrue("HTML tag should have one child", 1 == html.getChildCount ()); 124 assertTrue("First child should be a HEAD tag", html.childAt (0) instanceof HeadTag); 125 HeadTag head = (HeadTag)html.childAt (0); 126 assertTrue("HEAD tag should have three children", 3 == head.getChildCount ()); 127 assertTrue("Third child should be a STYLE tag", head.childAt (2) instanceof StyleTag); 128 StyleTag styleTag = (StyleTag)head.childAt (2); 129 assertStringEquals("Expected Style Code",expectedCode,styleTag.getStyleCode()); 130 } 131 132 135 public void testStyleChildren () throws ParserException 136 { 137 String style = 138 "\nbody {color:white}\n" + 139 "<!--\n" + 140 ".teliabox {\n" + 141 "color: #A9014E;\n" + 142 "text-align: center;\n" + 143 "background-image:url(hallo.gif);\n" + 144 "}\n" + 145 "-->"; 146 String html = 147 "<style type=\"text/css\" media=\"screen\">" + 148 style + 149 "</style>"; 150 StyleTag tag; 151 Text string; 152 153 createParser (html); 154 parseAndAssertNodeCount (1); 155 assertTrue ("Node should be a STYLE tag", node[0] instanceof StyleTag); 156 tag = (StyleTag)node[0]; 157 assertTrue ("STYLE tag should have one child", 1 == tag.getChildCount ()); 158 assertTrue ("Child should be a StringNode", tag.getChild (0) instanceof Text); 159 string = (Text)tag.getChild (0); 160 assertStringEquals ("Style text incorrect", style, string.toHtml ()); 161 } 162 } 163
| Popular Tags
|