1 package com.opensymphony.module.sitemesh.html.tokenizer; 2 3 import com.opensymphony.module.sitemesh.html.Tag; 4 import com.opensymphony.module.sitemesh.html.Text; 5 import junit.framework.TestCase; 6 7 public class TagTokenizerTest extends TestCase { 8 9 private MockTokenHandler handler; 10 11 protected void setUp() throws Exception { 12 super.setUp(); 13 handler = new MockTokenHandler(); 14 } 15 16 public void testSplitsTagsFromText() { 17 handler.expectTag(Tag.OPEN, "hello"); 19 handler.expectText("cruel"); 20 handler.expectTag(Tag.OPEN, "world"); 21 handler.expectTag(Tag.OPEN, "and"); 22 handler.expectText("some stuff"); 23 TagTokenizer tokenizer = new TagTokenizer("<hello>cruel<world><and>some stuff"); 25 tokenizer.start(handler); 26 handler.verify(); 28 } 29 30 public void testDistinguishesBetweenOpenCloseAndEmptyTags() { 31 handler.expectTag(Tag.OPEN, "open"); 33 handler.expectTag(Tag.CLOSE, "close"); 34 handler.expectTag(Tag.EMPTY, "empty"); 35 TagTokenizer tokenizer = new TagTokenizer("<open></close><empty/>"); 37 tokenizer.start(handler); 38 handler.verify(); 40 } 41 42 public void testTreatsCommentsAsText() { 43 handler.expectText("hello world "); 45 handler.expectText("<!-- how are<we> \n -doing? -->"); 46 handler.expectText("good\n bye."); 47 handler.expectTag(Tag.OPEN, "br"); 48 TagTokenizer tokenizer = new TagTokenizer("hello world <!-- how are<we> \n -doing? -->good\n bye.<br>"); 50 tokenizer.start(handler); 51 handler.verify(); 53 } 54 55 public void testExtractsUnquotedAttributesFromTag() { 56 handler.expectTag(Tag.OPEN, "hello", new String []{"name", "world", "foo", "boo"}); 58 TagTokenizer tokenizer = new TagTokenizer("<hello name=world foo=boo>"); 60 tokenizer.start(handler); 61 handler.verify(); 63 } 64 65 public void testExtractsQuotedAttributesFromTag() { 66 handler.expectTag(Tag.OPEN, "hello", new String []{"name", "the world", "foo", "boo"}); 68 TagTokenizer tokenizer = new TagTokenizer("<hello name=\"the world\" foo=\"boo\">"); 70 tokenizer.start(handler); 71 handler.verify(); 73 } 74 75 public void testHandlesMixedQuoteTypesInAttributes() { 76 handler.expectTag(Tag.OPEN, "hello", new String []{"name", "it's good", "foo", "say \"boo\""}); 78 TagTokenizer tokenizer = new TagTokenizer("<hello name=\"it's good\" foo=\'say \"boo\"'>"); 80 tokenizer.start(handler); 81 handler.verify(); 83 } 84 85 public void testHandlesHtmlStyleEmptyAttributes() { 86 handler.expectTag(Tag.OPEN, "hello", new String []{"isgood", null, "and", null, "stuff", null}); 88 TagTokenizer tokenizer = new TagTokenizer("<hello isgood and stuff>"); 90 tokenizer.start(handler); 91 handler.verify(); 93 } 94 95 public void testSupportsWhitespaceInElements() { 96 handler.expectTag(Tag.OPEN, "hello", new String []{"somestuff", "good", "foo", null, "x", "long\n string"}); 98 handler.expectTag(Tag.EMPTY, "empty"); 99 handler.expectTag(Tag.OPEN, "HTML", new String []{"notonnewline", "yo", "newline", "hello", "anotherline", "bye"}); 100 TagTokenizer tokenizer = new TagTokenizer("" 102 + "<hello \n somestuff = \ngood \n foo \nx=\"long\n string\" >" 103 + "<empty />" 104 + "<HTML notonnewline=yo newline=\n" 105 + "hello anotherline=\n" 106 + "\"bye\">"); 107 tokenizer.start(handler); 108 handler.verify(); 110 111 112 } 113 114 public void testExposesOriginalTagToHandler() { 115 final String originalTag = "<hello \n somestuff = \ngood \n foo \nx=\"long\n string\" >"; 118 TagTokenizer tokenizer = new TagTokenizer("some text" + originalTag + "more text"); 119 final boolean[] called = {false}; 121 tokenizer.start(new TokenHandler() { 122 123 public boolean shouldProcessTag(String name) { 124 return true; 125 } 126 127 public void tag(Tag tag) { 128 assertEquals(originalTag, tag.getContents()); 129 called[0] = true; 130 } 131 132 public void text(Text text) { 133 } 135 136 public void warning(String message, int line, int column) { 137 fail("Encountered error " + message); 138 } 139 }); 140 141 assertTrue("tag() never called", called[0]); 142 } 143 144 public void testAllowsSlashInUnquotedAttribute() { 145 handler.expectTag(Tag.OPEN, "something", new String []{"type", "text/html"}); 147 TagTokenizer tokenizer = new TagTokenizer("<something type=text/html>"); 149 tokenizer.start(handler); 150 handler.verify(); 152 } 153 154 public void testAllowsTrailingQuoteOnAttribute() { 155 handler.expectTag(Tag.OPEN, "something", new String []{"type", "bl'ah\""}); 157 TagTokenizer tokenizer = new TagTokenizer("<something type=bl'ah\">"); 159 tokenizer.start(handler); 160 handler.verify(); 162 } 163 164 public void testAllowsAwkwardCharsInElementAndAttribute() { 165 handler.expectTag(Tag.OPEN, "name:space", new String []{"foo:bar", "x:y%"}); 167 handler.expectTag(Tag.EMPTY, "a_b-c$d", new String []{"b_b-c$d", "c_b=c$d"}); 168 handler.expectTag(Tag.OPEN, "a", new String []{"href", "/exec/obidos/flex-sign-in/ref=pd_nfy_gw_si/026-2634699-7306802?opt=a&page=misc/login/flex-sign-in-secure.html&response=tg/new-for-you/new-for-you/-/main"}); 169 TagTokenizer tokenizer = new TagTokenizer("" 171 + "<name:space foo:bar=x:y%>" 172 + "<a_b-c$d b_b-c$d=c_b=c$d />" 173 + "<a HREF=/exec/obidos/flex-sign-in/ref=pd_nfy_gw_si/026-2634699-7306802?opt=a&page=misc/login/flex-sign-in-secure.html&response=tg/new-for-you/new-for-you/-/main>"); 174 tokenizer.start(handler); 175 handler.verify(); 177 178 } 179 180 public void testTreatsXmpCdataScriptAndProcessingInstructionsAsText() { 181 handler.expectText("<script language=jscript> if (a < b & > c)\n alert(); </script>"); 183 handler.expectText("<xmp><evil \n<stuff<</xmp>"); 184 handler.expectText("<?some stuff ?>"); 185 handler.expectText("<![CDATA[ evil<>> <\n ]]>"); 186 handler.expectText("<SCRIPT>stuff</SCRIPT>"); 187 handler.expectText("<!DOCTYPE html PUBLIC \\\"-//W3C//DTD HTML 4.01 Transitional//EN\\\">"); 188 TagTokenizer tokenizer = new TagTokenizer("" 190 + "<script language=jscript> if (a < b & > c)\n alert(); </script>" 191 + "<xmp><evil \n<stuff<</xmp>" 192 + "<?some stuff ?>" 193 + "<![CDATA[ evil<>> <\n ]]>" 194 + "<SCRIPT>stuff</SCRIPT>" 195 + "<!DOCTYPE html PUBLIC \\\"-//W3C//DTD HTML 4.01 Transitional//EN\\\">"); 196 tokenizer.start(handler); 197 handler.verify(); 199 } 200 201 246 247 public void testIgnoresEvilMalformedPairOfAngleBrackets() { 248 handler.expectTag(Tag.OPEN, "good"); 250 TagTokenizer tokenizer = new TagTokenizer("<>< ><good><>"); 252 tokenizer.start(handler); 253 handler.verify(); 255 } 256 257 public void testDoesNotTryToParseTagsUnlessTheHandlerCares() { 258 handler = new MockTokenHandler() { 260 public boolean shouldProcessTag(String name) { 261 return name.equals("good"); 262 } 263 }; 264 handler.expectTag(Tag.OPEN, "good"); 266 handler.expectText("<bad>"); 267 handler.expectTag(Tag.CLOSE, "good"); 268 handler.expectText("<![bad]-->"); 269 TagTokenizer tokenizer = new TagTokenizer("<good><bad></good><![bad]-->"); 271 tokenizer.start(handler); 272 handler.verify(); 274 } 275 276 public void testParsesMagicCommentBlocks() { 277 handler.expectTag(Tag.OPEN_MAGIC_COMMENT, "if", new String [] {"gte", null, "mso", null, "9", null}); 279 handler.expectTag(Tag.OPEN, "stuff"); 280 handler.expectTag(Tag.CLOSE_MAGIC_COMMENT, "endif"); 281 TagTokenizer tokenizer = new TagTokenizer("<!--[if gte mso 9]><stuff><![endif]-->"); 283 tokenizer.start(handler); 284 handler.verify(); 286 287 } 288 } 289 290 | Popular Tags |