public void testParentConnections() throws ParserException { String tag1 = "<custom>"; String tag2 = "<custom>something</custom>"; String tag3 = "</custom>"; createParser(tag1 + tag2 + tag3); parser.setNodeFactory( new PrototypicalNodeFactory( new Tag[] { new CustomTag(false), new AnotherTag(false), })); parseAndAssertNodeCount(3); CustomTag customTag = (CustomTag) node[0]; assertStringEquals("first custom tag html", tag1 + "</custom>", customTag.toHtml()); assertNull("first custom tag should have no parent", customTag.getParent()); customTag = (CustomTag) node[1]; assertStringEquals("second custom tag html", tag2, customTag.toHtml()); assertNull("second custom tag should have no parent", customTag.getParent()); Node firstChild = customTag.childAt(0); assertType("firstChild", Text.class, firstChild); Node parent = firstChild.getParent(); assertNotNull("first child parent should not be null", parent); assertSame("parent and custom tag should be the same", customTag, parent); Tag endTag = (Tag) node[2]; assertStringEquals("third custom tag html", tag3, endTag.toHtml()); assertNull("end tag should have no parent", endTag.getParent()); }
/** @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; } } }
@Override public void visitTag(Tag tag) { Element e = Document.get().createElement(tag.getTagName()); map.put(tag, e); for (Object o : tag.getAttributesEx()) { Attribute a = (Attribute) o; if ("id".equalsIgnoreCase(a.getName())) { e.setId(a.getValue()); } else if ("style".equalsIgnoreCase(a.getName())) { processStyle(e, a.getValue()); } else if ("class".equalsIgnoreCase(a.getName())) { e.setClassName(a.getValue()); } else if (!a.isEmpty() && !a.isWhitespace() && a.isValued()) { e.setAttribute(a.getName(), a.getValue()); } } Element parent = getParent(tag.getParent()); parent.appendChild(e); }