public void testAttributes() throws ParseException { HtmlParseResult result = parse( "<!DOCTYPE html><html><head><title>hello</title></head><body onclick=\"alert()\"></body></html>"); Node root = result.root(); // NodeUtils.dumpTree(root); assertNotNull(root); OpenTag body = ElementUtils.query(root, "html/body"); assertNotNull(body); assertEquals(1, body.attributes().size()); Attribute attr = body.attributes().iterator().next(); assertNotNull(attr); assertEquals("onclick", attr.name().toString()); assertEquals("\"alert()\"", attr.value().toString()); }
@Override public void visit(Element node) { OpenTag tag = (OpenTag) node; for (Attribute id : tag.attributes( new AttributeFilter() { @Override public boolean accepts(Attribute attribute) { return LexerUtils.equals(CLASS_ATTR_NAME, attribute.name(), true, true); } })) { processElements(id, CssElementType.CLASS, classes2files); } }
// Bug 211792 // http://netbeans.org/bugzilla/show_bug.cgi?id=211792 public void testIssue211792() throws ParseException { // ParseTreeBuilder.setLoggerLevel(Level.ALL); String code = "<a href=\"\"</p>"; // 01234567 8 901234 // 0 1 Node root = parse(code).root(); // ElementUtils.dumpTree(root); OpenTag a = ElementUtils.query(root, "html/body/a"); assertNotNull(a); Collection<Attribute> attrs = a.attributes(); assertNotNull(attrs); // for(Attribute attr : attrs) { // System.out.println("from:" + attr.from()); // System.out.println("to:" + attr.to()); // System.out.println("nameoffset:" + attr.nameOffset()); // System.out.println("valueoffset:" + attr.valueOffset()); // System.out.println("name:"+attr.name()); // System.out.println("value:"+attr.value()); // } // properly the number of the attributes should be one, but the // html parser itself serves the three attributes, where first one // is correct, second one is completely // out of the source are and a third one which represents // the close tag after the quotes. assertEquals(2, attrs.size()); Attribute href = attrs.iterator().next(); assertNotNull(href); assertEquals(3, href.from()); assertEquals(10, href.to()); assertEquals(3, href.nameOffset()); assertEquals(8, href.valueOffset()); assertEquals("href", href.name().toString()); assertEquals("\"\"", href.value().toString()); assertEquals("", href.unquotedValue().toString()); }