我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用html5lib.constants.namespaces()。
def reparentChildren(self, newParent): while self.element.contents: child = self.element.contents[0] child.extract() if isinstance(child, Tag): newParent.appendChild( Element(child, self.soup, namespaces["html"])) else: newParent.appendChild( TextNode(child, self.soup))
def getNameTuple(self): if self.namespace == None: return namespaces["html"], self.name else: return self.namespace, self.name
def test_namespace_html_elements_0(self): parser = html5parser.HTMLParser(namespaceHTMLElements=True) doc = parser.parse("<html></html>") self.assert_(doc.childNodes[0].namespace == namespaces["html"])
def getNodeDetails(self, node): if isinstance(node, BeautifulSoup): # Document or DocumentFragment return (_base.DOCUMENT,) elif isinstance(node, Declaration): # DocumentType string = unicode(node.string) #Slice needed to remove markup added during unicode conversion, #but only in some versions of BeautifulSoup/Python if string.startswith('<!') and string.endswith('>'): string = string[2:-1] m = self.doctype_regexp.match(string) #This regexp approach seems wrong and fragile #but beautiful soup stores the doctype as a single thing and we want the seperate bits #It should work as long as the tree is created by html5lib itself but may be wrong if it's #been modified at all #We could just feed to it a html5lib tokenizer, I guess... assert m is not None, "DOCTYPE did not match expected format" name = m.group('name') publicId = m.group('publicId') if publicId is not None: systemId = m.group('systemId1') else: systemId = m.group('systemId2') return _base.DOCTYPE, name, publicId or "", systemId or "" elif isinstance(node, Comment): string = unicode(node.string) if string.startswith('<!--') and string.endswith('-->'): string = string[4:-3] return _base.COMMENT, string elif isinstance(node, unicode): # TextNode return _base.TEXT, node elif isinstance(node, Tag): # Element return (_base.ELEMENT, namespaces["html"], node.name, dict(node.attrs).items(), node.contents) else: return _base.UNKNOWN, node.__class__.__name__
def reparentChildren(self, newParent): while self.element.contents: child = self.element.contents[0] child.extract() if isinstance(child, Tag): newParent.appendChild(Element(child, self.soup, namespaces["html"])) else: newParent.appendChild(TextNode(child, self.soup))
def elementInScope(self, target, variant=None): #If we pass a node in we match that. if we pass a string #match any node with that name exactNode = hasattr(target, "nameTuple") listElementsMap = { None:(scopingElements, False), "button":(scopingElements | set([(namespaces["html"], "button")]), False), "list":(scopingElements | set([(namespaces["html"], "ol"), (namespaces["html"], "ul")]), False), "table":(set([(namespaces["html"], "html"), (namespaces["html"], "table")]), False), "select":(set([(namespaces["html"], "optgroup"), (namespaces["html"], "option")]), True) } listElements, invert = listElementsMap[variant] for node in reversed(self.openElements): if (node.name == target and not exactNode or node == target and exactNode): return True elif (invert ^ (node.nameTuple in listElements)): return False assert False # We should never reach this point