1 27 package org.htmlparser.tests.lexerTests; 28 29 import java.util.Vector ; 30 import org.htmlparser.Node; 31 32 import org.htmlparser.Attribute; 33 import org.htmlparser.PrototypicalNodeFactory; 34 import org.htmlparser.Tag; 35 import org.htmlparser.lexer.PageAttribute; 36 import org.htmlparser.nodes.TagNode; 37 import org.htmlparser.tags.ImageTag; 38 import org.htmlparser.tags.LinkTag; 39 import org.htmlparser.tests.ParserTestCase; 40 import org.htmlparser.util.NodeIterator; 41 import org.htmlparser.util.ParserException; 42 43 public class AttributeTests extends ParserTestCase 44 { 45 static 46 { 47 System.setProperty ("org.htmlparser.tests.lexerTests.AttributeTests", "AttributeTests"); 48 } 49 50 private static final boolean JSP_TESTS_ENABLED = false; 51 private Tag tag; 52 private Vector attributes; 53 54 public AttributeTests (String name) { 55 super(name); 56 } 57 58 public void getParameterTableFor(String tagContents) 59 { 60 getParameterTableFor (tagContents, false); 61 } 62 63 public void getParameterTableFor(String tagContents, boolean dump) 64 { 65 String html; 66 NodeIterator iterator; 67 Node node; 68 69 html = "<" + tagContents + ">"; 70 createParser (html); 71 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 72 try 73 { 74 iterator = parser.elements (); 75 node = iterator.nextNode (); 76 if (node instanceof Tag) 77 { 78 tag = (Tag)node; 79 attributes = tag.getAttributesEx (); 80 if (dump) 81 { 82 for (int i = 0; i < attributes.size (); i++) 83 { 84 System.out.print ("Attribute #" + i); 85 Attribute attribute = (Attribute)attributes.elementAt (i); 86 if (null != attribute.getName ()) 87 System.out.print (" Name: '" + attribute.getName () + "'"); 88 if (null != attribute.getAssignment ()) 89 System.out.print (" Assignment: '" + attribute.getAssignment () + "'"); 90 if (0 != attribute.getQuote ()) 91 System.out.print (" Quote: " + attribute.getQuote ()); 92 if (null != attribute.getValue ()) 93 System.out.print (" Value: '" + attribute.getValue () + "'"); 94 System.out.println (); 95 } 96 System.out.println (); 97 } 98 } 99 else 100 attributes = null; 101 String string = node.toHtml (); 102 assertEquals ("toHtml differs", html, string); 103 assertTrue ("shouldn't be any more nodes", !iterator.hasMoreNodes ()); 104 } 105 catch (ParserException pe) 106 { 107 fail (pe.getMessage ()); 108 } 109 } 110 111 114 public void testConstructors () 115 { 116 Vector attributes; 117 Tag tag; 118 String html; 119 120 attributes = new Vector (); 121 attributes.add (new Attribute ("wombat", null)); 123 attributes.add (new Attribute (" ")); 125 attributes.add (new Attribute ("label", "The civil war.")); 127 attributes.add (new Attribute (" ")); 128 attributes.add (new Attribute ("frameborder", "= ", "no")); 130 attributes.add (new Attribute (" ")); 131 attributes.add (new Attribute ("name", "=", "topFrame", '"')); 133 tag = new TagNode (null, 0, 0, attributes); 134 html = "<wombat label=\"The civil war.\" frameborder= no name=\"topFrame\">"; 135 assertStringEquals ("tag contents", html, tag.toHtml ()); 136 } 137 138 141 public void testProperties () 142 { 143 Attribute attribute; 144 Attribute space; 145 Vector attributes; 146 Tag tag; 147 String html; 148 149 attributes = new Vector (); 150 attribute = new Attribute (); 151 attribute.setName ("wombat"); 152 assertTrue ("should be standalone", attribute.isStandAlone ()); 153 assertTrue ("should not be whitespace", !attribute.isWhitespace ()); 154 assertTrue ("should not be valued", !attribute.isValued ()); 155 assertTrue ("should not be empty", !attribute.isEmpty ()); 156 attributes.add (attribute); 157 space = new Attribute (); 158 space.setValue (" "); 159 assertTrue ("should not be standalone", !space.isStandAlone ()); 160 assertTrue ("should be whitespace", space.isWhitespace ()); 161 assertTrue ("should be valued", space.isValued ()); 162 assertTrue ("should not be empty", !space.isEmpty ()); 163 attributes.add (space); 164 attribute = new Attribute (); 165 attribute.setName ("label"); 166 attribute.setAssignment ("="); 167 attribute.setRawValue ("The civil war."); 168 assertTrue ("should not be standalone", !attribute.isStandAlone ()); 169 assertTrue ("should not be whitespace", !attribute.isWhitespace ()); 170 assertTrue ("should be valued", attribute.isValued ()); 171 assertTrue ("should not be empty", !attribute.isEmpty ()); 172 attributes.add (attribute); 173 attributes.add (space); 174 attribute = new Attribute (); 175 attribute.setName ("frameborder"); 176 attribute.setAssignment ("= "); 177 attribute.setRawValue ("no"); 178 attributes.add (attribute); 179 attributes.add (space); 180 attribute = new Attribute (); 181 attribute.setName ("name"); 182 attribute.setAssignment ("="); 183 attribute.setValue ("topFrame"); 184 attribute.setQuote ('"'); 185 assertTrue ("should not be standalone", !attribute.isStandAlone ()); 186 assertTrue ("should not be whitespace", !attribute.isWhitespace ()); 187 assertTrue ("should be valued", attribute.isValued ()); 188 assertTrue ("should not be empty", !attribute.isEmpty ()); 189 attributes.add (attribute); 190 tag = new TagNode (null, 0, 0, attributes); 191 html = "<wombat label=\"The civil war.\" frameborder= no name=\"topFrame\">"; 192 assertStringEquals ("tag contents", html, tag.toHtml ()); 193 } 194 195 198 public void testConstructors2 () 199 { 200 Vector attributes; 201 Tag tag; 202 String html; 203 204 attributes = new Vector (); 205 attributes.add (new PageAttribute ("wombat", null)); 207 attributes.add (new PageAttribute (" ")); 209 attributes.add (new PageAttribute ("label", "The civil war.")); 211 attributes.add (new PageAttribute (" ")); 212 attributes.add (new PageAttribute ("frameborder", "= ", "no")); 214 attributes.add (new PageAttribute (" ")); 215 attributes.add (new PageAttribute ("name", "=", "topFrame", '"')); 217 tag = new TagNode (null, 0, 0, attributes); 218 html = "<wombat label=\"The civil war.\" frameborder= no name=\"topFrame\">"; 219 assertStringEquals ("tag contents", html, tag.toHtml ()); 220 } 221 222 225 public void testProperties2 () 226 { 227 Attribute attribute; 228 Attribute space; 229 Vector attributes; 230 Tag tag; 231 String html; 232 233 attributes = new Vector (); 234 attribute = new PageAttribute (); 235 attribute.setName ("wombat"); 236 assertTrue ("should be standalone", attribute.isStandAlone ()); 237 assertTrue ("should not be whitespace", !attribute.isWhitespace ()); 238 assertTrue ("should not be valued", !attribute.isValued ()); 239 assertTrue ("should not be empty", !attribute.isEmpty ()); 240 attributes.add (attribute); 241 space = new PageAttribute (); 242 space.setValue (" "); 243 assertTrue ("should not be standalone", !space.isStandAlone ()); 244 assertTrue ("should be whitespace", space.isWhitespace ()); 245 assertTrue ("should be valued", space.isValued ()); 246 assertTrue ("should not be empty", !space.isEmpty ()); 247 attributes.add (space); 248 attribute = new PageAttribute (); 249 attribute.setName ("label"); 250 attribute.setAssignment ("="); 251 attribute.setRawValue ("The civil war."); 252 assertTrue ("should not be standalone", !attribute.isStandAlone ()); 253 assertTrue ("should not be whitespace", !attribute.isWhitespace ()); 254 assertTrue ("should be valued", attribute.isValued ()); 255 assertTrue ("should not be empty", !attribute.isEmpty ()); 256 attributes.add (attribute); 257 attributes.add (space); 258 attribute = new PageAttribute (); 259 attribute.setName ("frameborder"); 260 attribute.setAssignment ("= "); 261 attribute.setRawValue ("no"); 262 assertTrue ("should not be standalone", !attribute.isStandAlone ()); 263 assertTrue ("should not be whitespace", !attribute.isWhitespace ()); 264 assertTrue ("should be valued", attribute.isValued ()); 265 assertTrue ("should not be empty", !attribute.isEmpty ()); 266 attributes.add (attribute); 267 attributes.add (space); 268 attribute = new PageAttribute (); 269 attribute.setName ("name"); 270 attribute.setAssignment ("="); 271 attribute.setValue ("topFrame"); 272 attribute.setQuote ('"'); 273 assertTrue ("should not be standalone", !attribute.isStandAlone ()); 274 assertTrue ("should not be whitespace", !attribute.isWhitespace ()); 275 assertTrue ("should be valued", attribute.isValued ()); 276 assertTrue ("should not be empty", !attribute.isEmpty ()); 277 attributes.add (attribute); 278 tag = new TagNode (null, 0, 0, attributes); 279 html = "<wombat label=\"The civil war.\" frameborder= no name=\"topFrame\">"; 280 assertStringEquals ("tag contents", html, tag.toHtml ()); 281 } 282 283 286 public void testParseParameters() { 287 getParameterTableFor("a b = \"c\""); 288 assertEquals("Value","c",((Attribute)(attributes.elementAt (2))).getValue ()); 289 } 290 291 294 public void testParseTokenValues() { 295 getParameterTableFor("a b = \"'\""); 296 assertEquals("Value","'",((Attribute)(attributes.elementAt (2))).getValue ()); 297 } 298 299 302 public void testParseEmptyValues() { 303 getParameterTableFor("a b = \"\""); 304 assertEquals("Value","",((Attribute)(attributes.elementAt (2))).getValue ()); 305 } 306 307 312 public void testParseMissingEqual() { 313 getParameterTableFor("a b\"c\""); 314 assertEquals("NameC", "b\"c\"", ((Attribute)(attributes.elementAt (2))).getName ()); 315 } 316 317 320 public void testTwoParams(){ 321 getParameterTableFor("PARAM NAME=\"Param1\" VALUE=\"Somik\""); 322 assertEquals("Param1","Param1",((Attribute)(attributes.elementAt (2))).getValue ()); 323 assertEquals("Somik","Somik",((Attribute)(attributes.elementAt (4))).getValue ()); 324 } 325 326 329 public void testPlainParams(){ 330 getParameterTableFor("PARAM NAME=Param1 VALUE=Somik"); 331 assertEquals("Param1","Param1",((Attribute)(attributes.elementAt (2))).getValue ()); 332 assertEquals("Somik","Somik",((Attribute)(attributes.elementAt (4))).getValue ()); 333 } 334 335 338 public void testValueMissing() { 339 getParameterTableFor("INPUT type=\"checkbox\" name=\"Authorize\" value=\"Y\" checked"); 340 assertEquals("Name of Tag","INPUT",((Attribute)(attributes.elementAt (0))).getName ()); 341 assertEquals("Type","checkbox",((Attribute)(attributes.elementAt (2))).getValue ()); 342 assertEquals("Name","Authorize",((Attribute)(attributes.elementAt (4))).getValue ()); 343 assertEquals("Value","Y",((Attribute)(attributes.elementAt (6))).getValue ()); 344 assertEquals("Checked",null,((Attribute)(attributes.elementAt (8))).getValue ()); 345 } 346 347 352 public void testIncorrectSpaceKeyBug() { 353 getParameterTableFor("TEXTAREA name=\"Remarks\" "); 354 assertEquals("There should only be two attributes",4,attributes.size()); 356 assertEquals("Expected name","TEXTAREA",((Attribute)(attributes.elementAt (0))).getName ()); 358 assertEquals("Expected value 1", "Remarks",((Attribute)(attributes.elementAt (2))).getValue ()); 359 } 360 361 364 public void testNullTag(){ 365 getParameterTableFor("INPUT type="); 366 assertEquals("Name of Tag","INPUT",((Attribute)(attributes.elementAt (0))).getName ()); 367 assertNull("Type",((Attribute)(attributes.elementAt (2))).getValue ()); 368 } 369 370 373 public void testAttributeWithSpuriousEqualTo() { 374 getParameterTableFor( 375 "a class=rlbA HREF=/news/866201.asp?0sl=-32" 376 ); 377 assertStringEquals( 378 "href", 379 "/news/866201.asp?0sl=-32", 380 ((Attribute)(attributes.elementAt (4))).getValue () 381 ); 382 } 383 384 387 public void testQuestionMarksInAttributes() { 388 getParameterTableFor( 389 "a HREF=\"mailto:sam@neurogrid.com?subject=Site Comments\"" 390 ); 391 assertStringEquals( 392 "href", 393 "mailto:sam@neurogrid.com?subject=Site Comments", 394 ((Attribute)(attributes.elementAt (2))).getValue () 395 ); 396 assertStringEquals( 397 "tag name", 398 "a", 399 ((Attribute)(attributes.elementAt (0))).getName () 400 ); 401 } 402 403 413 public void testEmptyTag () { 414 getParameterTableFor(""); 415 assertNull ("<> is not a tag",attributes); 416 } 417 418 423 public void testJspWithinAttributes() { 424 if (JSP_TESTS_ENABLED) 425 { 426 getParameterTableFor( 427 "a HREF=\"<%=Application(\"sURL\")%>/literature/index.htm" 428 ); 429 assertStringEquals( 430 "href", 431 "<%=Application(\"sURL\")%>/literature/index.htm", 432 ((Attribute)(attributes.elementAt (2))).getValue () 433 ); 434 } 435 } 436 437 441 public void testScriptedTag () { 442 getParameterTableFor("body onLoad=defaultStatus=''"); 443 String name = ((Attribute)(attributes.elementAt (0))).getName (); 444 assertNotNull ("No Tag.TAGNAME", name); 445 assertStringEquals("tag name parsed incorrectly", "body", name); 446 String value = ((Attribute)(attributes.elementAt (2))).getValue (); 447 assertStringEquals ("parameter parsed incorrectly", "defaultStatus=''", value); 448 } 449 450 455 public void testStandaloneAttribute () 456 { 457 getParameterTableFor ("INPUT DISABLED"); 458 assertStringEquals("Standalone attribute not parsed","DISABLED",((Attribute)(attributes.elementAt (2))).getName ()); 459 assertNull ("Standalone attribute has non-null value",((Attribute)(attributes.elementAt (2))).getValue ()); 460 } 461 462 465 public void testMissingAttribute () 466 { 467 getParameterTableFor ("INPUT DISABLED="); 468 assertStringEquals("Empty attribute has no attribute","DISABLED",((Attribute)(attributes.elementAt (2))).getName ()); 469 assertEquals ("Attribute has non-blank value",null,((Attribute)(attributes.elementAt (2))).getValue ()); 470 } 471 472 476 public void testRule1 () 477 { 478 getParameterTableFor ("tag att = other=fred"); 479 assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ()); 480 assertEquals ("Attribute has wrong value", "other=fred", ((Attribute)(attributes.elementAt (2))).getValue ()); 481 for (int i = 0; i < attributes.size (); i++) 482 assertTrue ("No attribute should be called =", !((Attribute)(attributes.elementAt (2))).getName ().equals ("=")); 483 } 484 485 488 public void testRule2 () 489 { 490 getParameterTableFor ("tag att =value other=fred"); 491 assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ()); 492 assertEquals ("Attribute has wrong value", "value", ((Attribute)(attributes.elementAt (2))).getValue ()); 493 for (int i = 0; i < attributes.size (); i++) 494 assertTrue ("No attribute should be called =value", !((Attribute)(attributes.elementAt (2))).getName ().equals ("=value")); 495 assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (4))).getName ()); 496 assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (4))).getValue ()); 497 } 498 499 502 public void testRule3 () 503 { 504 getParameterTableFor ("tag att= \"value\" other=fred"); 505 assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ()); 506 assertEquals ("Attribute has wrong value", "value", ((Attribute)(attributes.elementAt (2))).getValue ()); 507 for (int i = 0; i < attributes.size (); i++) 508 assertTrue ("No attribute should be called \"value\"", !((Attribute)(attributes.elementAt (2))).getName ().equals ("\"value\"")); 509 assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (4))).getName ()); 510 assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (4))).getValue ()); 511 } 512 513 517 public void testRule4 () 518 { 519 getParameterTableFor ("tag att=\"va\"lue\" other=fred"); 520 assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ()); 521 assertEquals ("Attribute has wrong value", "va", ((Attribute)(attributes.elementAt (2))).getValue ()); 522 for (int i = 0; i < attributes.size (); i++) 523 assertTrue ("No attribute should be called va\"lue", !((Attribute)(attributes.elementAt (2))).getName ().equals ("va\"lue")); 524 assertStringEquals("Attribute missing","att",((Attribute)(attributes.elementAt (2))).getName ()); 525 assertStringEquals("Attribute not parsed","lue\"",((Attribute)(attributes.elementAt (3))).getName ()); 526 assertNull ("Attribute has wrong value", ((Attribute)(attributes.elementAt (3))).getValue ()); 527 assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (5))).getName ()); 528 assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (5))).getValue ()); 529 } 530 531 535 public void testRule5 () 536 { 537 getParameterTableFor ("tag att='va'lue' other=fred"); 538 assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ()); 539 assertEquals ("Attribute has wrong value", "va", ((Attribute)(attributes.elementAt (2))).getValue ()); 540 for (int i = 0; i < attributes.size (); i++) 541 assertTrue ("No attribute should be called va'lue", !((Attribute)(attributes.elementAt (2))).getName ().equals ("va'lue")); 542 assertStringEquals("Attribute not parsed","lue'",((Attribute)(attributes.elementAt (3))).getName ()); 543 assertNull ("Attribute has wrong value", ((Attribute)(attributes.elementAt (3))).getValue ()); 544 assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (5))).getName ()); 545 assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (5))).getValue ()); 546 } 547 548 569 public void testSrcAndAlt () throws ParserException 570 { 571 String html = "<img SRC=\"images/first\" alt=\"first\">"; 572 573 createParser (html); 574 parseAndAssertNodeCount (1); 575 assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag); 576 ImageTag img = (ImageTag)node[0]; 577 assertTrue ("bad source", "images/first".equals (img.getImageURL ())); 578 assertTrue ("bad alt", "first".equals (img.getAttribute ("alt"))); 579 assertStringEquals ("toHtml()", html, img.toHtml ()); 580 } 581 582 585 public void testSrcAndEmptyAlt () throws ParserException 586 { 587 String html = "<img SRC=\"images/second\" alt=\"\">"; 588 589 createParser (html); 590 parseAndAssertNodeCount (1); 591 assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag); 592 ImageTag img = (ImageTag)node[0]; 593 assertTrue ("bad source", "images/second".equals (img.getImageURL ())); 594 assertTrue ("bad alt", "".equals (img.getAttribute ("alt"))); 595 assertStringEquals ("toHtml()", html, img.toHtml ()); 596 } 597 598 601 public void testAltAndSrc () throws ParserException 602 { 603 String html = "<img alt=\"third\" SRC=\"images/third\">"; 604 605 createParser (html); 606 parseAndAssertNodeCount (1); 607 assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag); 608 ImageTag img = (ImageTag)node[0]; 609 assertTrue ("bad source", "images/third".equals (img.getImageURL ())); 610 assertTrue ("bad alt", "third".equals (img.getAttribute ("alt"))); 611 assertStringEquals ("toHtml()", html, img.toHtml ()); 612 } 613 614 617 public void testEmptyAltAndSrc () throws ParserException 618 { 619 String html = "<img alt=\"\" SRC=\"images/third\">"; 620 621 createParser (html); 622 parseAndAssertNodeCount (1); 623 assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag); 624 ImageTag img = (ImageTag)node[0]; 625 assertTrue ("bad source", "images/third".equals (img.getImageURL ())); 626 assertTrue ("bad alt", "".equals (img.getAttribute ("alt"))); 627 assertStringEquals ("toHtml()", html, img.toHtml ()); 628 } 629 630 633 public void testPredicates () throws ParserException 634 { 635 String html1 = "<img alt=\"\" SRC=\"images/third\" readonly>"; 636 String html2 = "<img SRC=\"images/third\" readonly alt=\"\">"; 637 String html3 = "<img readonly alt=\"\" SRC=\"images/third\">"; 638 String htmls[] = { html1, html2, html3 }; 639 640 for (int i = 0; i < htmls.length; i++) 641 { 642 createParser (htmls[i]); 643 parseAndAssertNodeCount (1); 644 assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag); 645 ImageTag img = (ImageTag)node[0]; 646 Attribute src = img.getAttributeEx ("src"); 647 Attribute alt = img.getAttributeEx ("alt"); 648 Attribute readonly = img.getAttributeEx ("readonly"); 649 assertTrue ("src whitespace", !src.isWhitespace ()); 650 assertTrue ("src not valued", src.isValued ()); 651 assertTrue ("src empty", !src.isEmpty ()); 652 assertTrue ("src standalone", !src.isStandAlone ()); 653 assertTrue ("alt whitespace", !alt.isWhitespace ()); 654 assertTrue ("alt valued", !alt.isValued ()); 655 assertTrue ("alt empty", !alt.isEmpty ()); 656 assertTrue ("alt standalone", !alt.isStandAlone ()); 657 assertTrue ("readonly whitespace", !readonly.isWhitespace ()); 658 assertTrue ("readonly valued", !readonly.isValued ()); 659 assertTrue ("readonly empty", !readonly.isEmpty ()); 660 assertTrue ("readonly not standalone", readonly.isStandAlone ()); 661 src.setName ("SRC"); 663 assertTrue ("setName() failed", "SRC=\"images/third\"".equals (src.toString ())); 664 assertTrue ("src whitespace", !src.isWhitespace ()); 665 assertTrue ("src not valued", src.isValued ()); 666 assertTrue ("src empty", !src.isEmpty ()); 667 assertTrue ("src standalone", !src.isStandAlone ()); 668 alt.setName ("ALT"); 669 assertTrue ("setName() failed", "ALT=\"\"".equals (alt.toString ())); 670 assertTrue ("alt whitespace", !alt.isWhitespace ()); 671 assertTrue ("alt valued", !alt.isValued ()); 672 assertTrue ("alt empty", !alt.isEmpty ()); 673 assertTrue ("alt standalone", !alt.isStandAlone ()); 674 readonly.setName ("READONLY"); 675 assertTrue ("setName() failed", "READONLY".equals (readonly.toString ())); 676 assertTrue ("readonly whitespace", !readonly.isWhitespace ()); 677 assertTrue ("readonly valued", !readonly.isValued ()); 678 assertTrue ("readonly empty", !readonly.isEmpty ()); 679 assertTrue ("readonly not standalone", readonly.isStandAlone ()); 680 src.setAssignment (" = "); 682 assertTrue ("setAssignment() failed", "SRC = \"images/third\"".equals (src.toString ())); 683 assertTrue ("src whitespace", !src.isWhitespace ()); 684 assertTrue ("src not valued", src.isValued ()); 685 assertTrue ("src empty", !src.isEmpty ()); 686 assertTrue ("src standalone", !src.isStandAlone ()); 687 alt.setAssignment (" = "); 688 assertTrue ("setAssignment() failed", "ALT = \"\"".equals (alt.toString ())); 689 assertTrue ("alt whitespace", !alt.isWhitespace ()); 690 assertTrue ("alt valued", !alt.isValued ()); 691 assertTrue ("alt empty", !alt.isEmpty ()); 692 assertTrue ("alt standalone", !alt.isStandAlone ()); 693 readonly.setAssignment ("="); 694 assertTrue ("setAssignment() failed", "READONLY=".equals (readonly.toString ())); 695 assertTrue ("readonly whitespace", !readonly.isWhitespace ()); 696 assertTrue ("readonly valued", !readonly.isValued ()); 697 assertTrue ("readonly not empty", readonly.isEmpty ()); 698 assertTrue ("readonly standalone", !readonly.isStandAlone ()); 699 createParser (htmls[i]); 701 parseAndAssertNodeCount (1); 702 assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag); 703 img = (ImageTag)node[0]; 704 src = img.getAttributeEx ("src"); 705 alt = img.getAttributeEx ("alt"); 706 readonly = img.getAttributeEx ("readonly"); 707 src.setValue ("cgi-bin/redirect"); 708 assertTrue ("setValue() failed", "src=\"cgi-bin/redirect\"".equals (src.toString ())); 709 assertTrue ("src whitespace", !src.isWhitespace ()); 710 assertTrue ("src not valued", src.isValued ()); 711 assertTrue ("src empty", !src.isEmpty ()); 712 assertTrue ("src standalone", !src.isStandAlone ()); 713 alt.setValue ("no image"); 714 assertTrue ("setValue() failed", "alt=\"no image\"".equals (alt.toString ())); 715 assertTrue ("alt whitespace", !alt.isWhitespace ()); 716 assertTrue ("alt not valued", alt.isValued ()); 717 assertTrue ("alt empty", !alt.isEmpty ()); 718 assertTrue ("alt standalone", !alt.isStandAlone ()); 719 readonly.setValue ("true"); assertTrue ("setValue() failed", "readonlytrue".equals (readonly.toString ())); 721 assertTrue ("readonly whitespace", !readonly.isWhitespace ()); 722 assertTrue ("readonly not valued", readonly.isValued ()); 723 assertTrue ("readonly empty", !readonly.isEmpty ()); 724 assertTrue ("readonly standalone", !readonly.isStandAlone ()); 725 readonly.setAssignment ("="); 726 assertTrue ("setAssignment() failed", "readonly=true".equals (readonly.toString ())); 727 assertTrue ("readonly whitespace", !readonly.isWhitespace ()); 728 assertTrue ("readonly not valued", readonly.isValued ()); 729 assertTrue ("readonly empty", !readonly.isEmpty ()); 730 assertTrue ("readonly standalone", !readonly.isStandAlone ()); 731 } 732 } 733 734 737 public void testSetQuote () throws ParserException 738 { 739 String html = "<img alt=\"\" SRC=\"images/third\" toast>"; 740 741 createParser (html); 742 parseAndAssertNodeCount (1); 743 assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag); 744 ImageTag img = (ImageTag)node[0]; 745 Attribute src = img.getAttributeEx ("src"); 746 src.setQuote ('\0'); 747 assertTrue ("setQuote('\\0') failed", "src=images/third".equals (src.toString ())); 748 src.setQuote ('\''); 749 assertTrue ("setQuote('\\'') failed", "src='images/third'".equals (src.toString ())); 750 } 751 752 755 public void testNoSpace () throws ParserException 756 { 757 String id = "A19012_00002"; 758 String rawid = "\"" + id + "\""; 759 String cls = "BuyLink"; 760 String rawcls = "\"" + cls + "\""; 761 String href = "http://www.someplace.com/buyme.html"; 762 String rawhref = "\"" + href + "\""; 763 String html = "<a id=" + rawid + "class=" + rawcls + " HREF=" + rawhref + ">Pick me.</a>"; 764 createParser (html); 765 parseAndAssertNodeCount (1); 766 assertTrue ("Node should be an LinkTag", node[0] instanceof LinkTag); 767 LinkTag link = (LinkTag)node[0]; 768 Vector attributes = link.getAttributesEx (); 769 assertEquals ("Incorrect number of attributes", 6, attributes.size ()); 770 assertStringEquals ("id wrong", rawid, link.getAttributeEx ("id").getRawValue ()); 771 assertStringEquals ("class wrong", rawcls, link.getAttributeEx ("class").getRawValue ()); 772 assertStringEquals ("href wrong", rawhref, link.getAttributeEx ("href").getRawValue ()); 773 assertStringEquals ("id wrong", id, link.getAttributeEx ("id").getValue ()); 774 assertStringEquals ("class wrong", cls, link.getAttributeEx ("class").getValue ()); 775 assertStringEquals ("href wrong", href, link.getAttributeEx ("href").getValue ()); 776 } 777 } 778 | Popular Tags |