@Overrideprotected boolean process(Token token) { // start tag, end tag, doctype, comment, character, eof switch (token.type) { case StartTag: insert(token.asStartTag()); break; case EndTag: popStackToClose(token.asEndTag()); break; case Comment: insert(token.asComment()); break; case Character: insert(token.asCharacter()); break; case Doctype: insert(token.asDoctype()); break; case EOF: // could put some normalisation here if desired break; default: Validate.fail("Unexpected token type: " + token.type); } return true;}
登入後複製
insertNode的代码大致是这个样子(为了便于展示,对方法进行了一些整合):
1
Element insert(Token.StartTag startTag) { Tag tag = Tag.valueOf(startTag.name()); Element el = newElement(tag, baseUri, startTag.attributes); stack.getLast().appendChild(el); if(startTag.isSelfClosing()) { tokeniser.acknowledgeSelfClosingFlag(); if(!tag.isKnownTag()) // unknown tag, remember this is self closing for output. see above. tag.setSelfClosing(); } else { stack.add(el); } return el;}
privatevoid closeCell(HtmlTreeBuilder tb) { if(tb.inTableScope("td")) tb.process(newToken.EndTag("td")); elsetb.process(newToken.EndTag("th")); // only here if th or td in scope}