| |
| __license__ = "MIT" |
|
|
| __all__ = [ |
| "HTML5TreeBuilder", |
| ] |
|
|
| from typing import ( |
| Any, |
| cast, |
| Dict, |
| Iterable, |
| Optional, |
| Sequence, |
| TYPE_CHECKING, |
| Tuple, |
| Union, |
| ) |
| from typing_extensions import TypeAlias |
| from bs4._typing import ( |
| _AttributeValue, |
| _AttributeValues, |
| _Encoding, |
| _Encodings, |
| _NamespaceURL, |
| _RawMarkup, |
| ) |
|
|
| import warnings |
| from bs4.builder import ( |
| DetectsXMLParsedAsHTML, |
| PERMISSIVE, |
| HTML, |
| HTML_5, |
| HTMLTreeBuilder, |
| ) |
| from bs4.element import ( |
| NamespacedAttribute, |
| PageElement, |
| nonwhitespace_re, |
| ) |
| import html5lib |
| from html5lib.constants import ( |
| namespaces, |
| ) |
| from bs4.element import ( |
| Comment, |
| Doctype, |
| NavigableString, |
| Tag, |
| ) |
|
|
| if TYPE_CHECKING: |
| from bs4 import BeautifulSoup |
|
|
| from html5lib.treebuilders import base as treebuilder_base |
|
|
|
|
| class HTML5TreeBuilder(HTMLTreeBuilder): |
| """Use `html5lib <https://github.com/html5lib/html5lib-python>`_ to |
| build a tree. |
| |
| Note that `HTML5TreeBuilder` does not support some common HTML |
| `TreeBuilder` features. Some of these features could theoretically |
| be implemented, but at the very least it's quite difficult, |
| because html5lib moves the parse tree around as it's being built. |
| |
| Specifically: |
| |
| * This `TreeBuilder` doesn't use different subclasses of |
| `NavigableString` (e.g. `Script`) based on the name of the tag |
| in which the string was found. |
| * You can't use a `SoupStrainer` to parse only part of a document. |
| """ |
|
|
| NAME: str = "html5lib" |
|
|
| features: Iterable[str] = [NAME, PERMISSIVE, HTML_5, HTML] |
|
|
| |
| |
| TRACKS_LINE_NUMBERS: bool = True |
|
|
| underlying_builder: "TreeBuilderForHtml5lib" |
| user_specified_encoding: Optional[_Encoding] |
|
|
| def prepare_markup( |
| self, |
| markup: _RawMarkup, |
| user_specified_encoding: Optional[_Encoding] = None, |
| document_declared_encoding: Optional[_Encoding] = None, |
| exclude_encodings: Optional[_Encodings] = None, |
| ) -> Iterable[Tuple[_RawMarkup, Optional[_Encoding], Optional[_Encoding], bool]]: |
| |
| self.user_specified_encoding = user_specified_encoding |
|
|
| |
| |
| |
| for variable, name in ( |
| (document_declared_encoding, "document_declared_encoding"), |
| (exclude_encodings, "exclude_encodings"), |
| ): |
| if variable: |
| warnings.warn( |
| f"You provided a value for {name}, but the html5lib tree builder doesn't support {name}.", |
| stacklevel=3, |
| ) |
|
|
| |
| |
| DetectsXMLParsedAsHTML.warn_if_markup_looks_like_xml(markup, stacklevel=3) |
|
|
| yield (markup, None, None, False) |
|
|
| |
| def feed(self, markup: _RawMarkup) -> None: |
| """Run some incoming markup through some parsing process, |
| populating the `BeautifulSoup` object in `HTML5TreeBuilder.soup`. |
| """ |
| if self.soup is not None and self.soup.parse_only is not None: |
| warnings.warn( |
| "You provided a value for parse_only, but the html5lib tree builder doesn't support parse_only. The entire document will be parsed.", |
| stacklevel=4, |
| ) |
|
|
| |
| |
| parser = html5lib.HTMLParser(tree=self.create_treebuilder) |
| assert self.underlying_builder is not None |
| self.underlying_builder.parser = parser |
| extra_kwargs = dict() |
| if not isinstance(markup, str): |
| |
| |
| |
| extra_kwargs["override_encoding"] = self.user_specified_encoding |
|
|
| doc = parser.parse(markup, **extra_kwargs) |
|
|
| |
| if isinstance(markup, str): |
| |
| |
| doc.original_encoding = None |
| else: |
| original_encoding = parser.tokenizer.stream.charEncoding[0] |
| |
| |
| original_encoding = original_encoding.name |
| doc.original_encoding = original_encoding |
| self.underlying_builder.parser = None |
|
|
| def create_treebuilder( |
| self, namespaceHTMLElements: bool |
| ) -> "TreeBuilderForHtml5lib": |
| """Called by html5lib to instantiate the kind of class it |
| calls a 'TreeBuilder'. |
| |
| :param namespaceHTMLElements: Whether or not to namespace HTML elements. |
| |
| :meta private: |
| """ |
| self.underlying_builder = TreeBuilderForHtml5lib( |
| namespaceHTMLElements, self.soup, store_line_numbers=self.store_line_numbers |
| ) |
| return self.underlying_builder |
|
|
| def test_fragment_to_document(self, fragment: str) -> str: |
| """See `TreeBuilder`.""" |
| return "<html><head></head><body>%s</body></html>" % fragment |
|
|
|
|
| class TreeBuilderForHtml5lib(treebuilder_base.TreeBuilder): |
| soup: "BeautifulSoup" |
| parser: Optional[html5lib.HTMLParser] |
|
|
| def __init__( |
| self, |
| namespaceHTMLElements: bool, |
| soup: Optional["BeautifulSoup"] = None, |
| store_line_numbers: bool = True, |
| **kwargs: Any, |
| ): |
| if soup: |
| self.soup = soup |
| else: |
| warnings.warn( |
| "The optionality of the 'soup' argument to the TreeBuilderForHtml5lib constructor is deprecated as of Beautiful Soup 4.13.0: 'soup' is now required. If you can't pass in a BeautifulSoup object here, or you get this warning and it seems mysterious to you, please contact the Beautiful Soup developer team for possible un-deprecation.", |
| DeprecationWarning, |
| stacklevel=2, |
| ) |
| from bs4 import BeautifulSoup |
|
|
| |
| |
| |
| self.soup = BeautifulSoup( |
| "", "html.parser", store_line_numbers=store_line_numbers, **kwargs |
| ) |
| |
| |
| |
| super(TreeBuilderForHtml5lib, self).__init__(namespaceHTMLElements) |
|
|
| |
| |
| self.parser = None |
| self.store_line_numbers = store_line_numbers |
|
|
| def documentClass(self) -> "Element": |
| self.soup.reset() |
| return Element(self.soup, self.soup, None) |
|
|
| def insertDoctype(self, token: Dict[str, Any]) -> None: |
| name: str = cast(str, token["name"]) |
| publicId: Optional[str] = cast(Optional[str], token["publicId"]) |
| systemId: Optional[str] = cast(Optional[str], token["systemId"]) |
|
|
| doctype = Doctype.for_name_and_ids(name, publicId, systemId) |
| self.soup.object_was_parsed(doctype) |
|
|
| def elementClass(self, name: str, namespace: str) -> "Element": |
| sourceline: Optional[int] = None |
| sourcepos: Optional[int] = None |
| if self.parser is not None and self.store_line_numbers: |
| |
| |
| |
| sourceline, sourcepos = self.parser.tokenizer.stream.position() |
| assert sourcepos is not None |
| sourcepos = sourcepos - 1 |
| tag = self.soup.new_tag( |
| name, namespace, sourceline=sourceline, sourcepos=sourcepos |
| ) |
|
|
| return Element(tag, self.soup, namespace) |
|
|
| def commentClass(self, data: str) -> "TextNode": |
| return TextNode(Comment(data), self.soup) |
|
|
| def fragmentClass(self) -> "Element": |
| """This is only used by html5lib HTMLParser.parseFragment(), |
| which is never used by Beautiful Soup, only by the html5lib |
| unit tests. Since we don't currently hook into those tests, |
| the implementation is left blank. |
| """ |
| raise NotImplementedError() |
|
|
| def getFragment(self) -> "Element": |
| """This is only used by the html5lib unit tests. Since we |
| don't currently hook into those tests, the implementation is |
| left blank. |
| """ |
| raise NotImplementedError() |
|
|
| def appendChild(self, node: "Element") -> None: |
| |
| |
| |
| |
| |
| |
| self.soup.append(node.element) |
|
|
| def getDocument(self) -> "BeautifulSoup": |
| return self.soup |
|
|
| def testSerializer(self, node: "Element") -> None: |
| """This is only used by the html5lib unit tests. Since we |
| don't currently hook into those tests, the implementation is |
| left blank. |
| """ |
| raise NotImplementedError() |
|
|
|
|
| class AttrList(object): |
| """Represents a Tag's attributes in a way compatible with html5lib.""" |
|
|
| element: Tag |
| attrs: _AttributeValues |
|
|
| def __init__(self, element: Tag): |
| self.element = element |
| self.attrs = dict(self.element.attrs) |
|
|
| def __iter__(self) -> Iterable[Tuple[str, _AttributeValue]]: |
| return list(self.attrs.items()).__iter__() |
|
|
| def __setitem__(self, name: str, value: _AttributeValue) -> None: |
| |
| |
| list_attr = self.element.cdata_list_attributes or {} |
| if name in list_attr.get("*", []) or ( |
| self.element.name in list_attr |
| and name in list_attr.get(self.element.name, []) |
| ): |
| |
| |
| if not isinstance(value, list): |
| assert isinstance(value, str) |
| value = self.element.attribute_value_list_class( |
| nonwhitespace_re.findall(value) |
| ) |
| self.element[name] = value |
|
|
| def items(self) -> Iterable[Tuple[str, _AttributeValue]]: |
| return list(self.attrs.items()) |
|
|
| def keys(self) -> Iterable[str]: |
| return list(self.attrs.keys()) |
|
|
| def __len__(self) -> int: |
| return len(self.attrs) |
|
|
| def __getitem__(self, name: str) -> _AttributeValue: |
| return self.attrs[name] |
|
|
| def __contains__(self, name: str) -> bool: |
| return name in list(self.attrs.keys()) |
|
|
|
|
| class BeautifulSoupNode(treebuilder_base.Node): |
| |
| tag: Optional[Tag] |
| string: Optional[NavigableString] |
| soup: "BeautifulSoup" |
| namespace: Optional[_NamespaceURL] |
|
|
| @property |
| def element(self) -> PageElement: |
| assert self.tag is not None or self.string is not None |
| if self.tag is not None: |
| return self.tag |
| else: |
| assert self.string is not None |
| return self.string |
|
|
| @property |
| def nodeType(self) -> int: |
| """Return the html5lib constant corresponding to the type of |
| the underlying DOM object. |
| |
| NOTE: This property is only accessed by the html5lib test |
| suite, not by Beautiful Soup proper. |
| """ |
| raise NotImplementedError() |
|
|
| |
| |
| def cloneNode(self) -> treebuilder_base.Node: |
| raise NotImplementedError() |
|
|
|
|
| class Element(BeautifulSoupNode): |
| namespace: Optional[_NamespaceURL] |
|
|
| def __init__( |
| self, element: Tag, soup: "BeautifulSoup", namespace: Optional[_NamespaceURL] |
| ): |
| self.tag = element |
| self.string = None |
| self.soup = soup |
| self.namespace = namespace |
| treebuilder_base.Node.__init__(self, element.name) |
|
|
| def appendChild(self, node: "BeautifulSoupNode") -> None: |
| string_child: Optional[NavigableString] = None |
| child: PageElement |
| if type(node.string) is NavigableString: |
| |
| |
| string_child = child = node.string |
| else: |
| child = node.element |
| node.parent = self |
|
|
| if ( |
| child is not None |
| and child.parent is not None |
| and not isinstance(child, str) |
| ): |
| node.element.extract() |
|
|
| if ( |
| string_child is not None |
| and self.tag is not None and self.tag.contents |
| and type(self.tag.contents[-1]) is NavigableString |
| ): |
| |
| |
| |
| old_element = self.tag.contents[-1] |
| new_element = self.soup.new_string(old_element + string_child) |
| old_element.replace_with(new_element) |
| self.soup._most_recent_element = new_element |
| else: |
| if isinstance(node, str): |
| |
| child = self.soup.new_string(node) |
|
|
| |
| |
| |
| if self.tag is not None and self.tag.contents: |
| most_recent_element = self.tag._last_descendant(False) |
| elif self.element.next_element is not None: |
| |
| |
| |
| |
| most_recent_element = self.soup._last_descendant() |
| else: |
| most_recent_element = self.element |
|
|
| self.soup.object_was_parsed( |
| child, parent=self.tag, most_recent_element=most_recent_element |
| ) |
|
|
| def getAttributes(self) -> AttrList: |
| assert self.tag is not None |
| return AttrList(self.tag) |
|
|
| |
| |
| _Html5libAttributeName: TypeAlias = Union[str, Tuple[str, str]] |
| |
| |
| _Html5libAttributes: TypeAlias = Dict[_Html5libAttributeName, str] |
|
|
| def setAttributes(self, attributes: Optional[_Html5libAttributes]) -> None: |
| assert self.tag is not None |
| if attributes is not None and len(attributes) > 0: |
| |
| |
| for name, value in list(attributes.items()): |
| if isinstance(name, tuple): |
| new_name = NamespacedAttribute(*name) |
| del attributes[name] |
| attributes[new_name] = value |
|
|
| |
| |
| normalized_attributes = cast(_AttributeValues, attributes) |
|
|
| |
| |
| self.soup.builder._replace_cdata_list_attribute_values( |
| self.name, normalized_attributes |
| ) |
|
|
| |
| |
| for name, value_or_values in list(normalized_attributes.items()): |
| self.tag[name] = value_or_values |
|
|
| |
| |
| |
| |
| |
| self.soup.builder.set_up_substitutions(self.tag) |
|
|
| attributes = property(getAttributes, setAttributes) |
|
|
| def insertText( |
| self, data: str, insertBefore: Optional["BeautifulSoupNode"] = None |
| ) -> None: |
| text = TextNode(self.soup.new_string(data), self.soup) |
| if insertBefore: |
| self.insertBefore(text, insertBefore) |
| else: |
| self.appendChild(text) |
|
|
| def insertBefore( |
| self, node: "BeautifulSoupNode", refNode: "BeautifulSoupNode" |
| ) -> None: |
| assert self.tag is not None |
| index = self.tag.index(refNode.element) |
| if ( |
| type(node.element) is NavigableString |
| and self.tag.contents |
| and type(self.tag.contents[index - 1]) is NavigableString |
| ): |
| |
| old_node = self.tag.contents[index - 1] |
| assert type(old_node) is NavigableString |
| new_str = self.soup.new_string(old_node + node.element) |
| old_node.replace_with(new_str) |
| else: |
| self.tag.insert(index, node.element) |
| node.parent = self |
|
|
| def removeChild(self, node: "Element") -> None: |
| node.element.extract() |
|
|
| def reparentChildren(self, newParent: "Element") -> None: |
| """Move all of this tag's children into another tag.""" |
| |
| |
| |
|
|
| element = self.tag |
| assert element is not None |
| new_parent_element = newParent.tag |
| assert new_parent_element is not None |
| |
| |
| final_next_element = element.next_sibling |
|
|
| new_parents_last_descendant = new_parent_element._last_descendant(False, False) |
| if len(new_parent_element.contents) > 0: |
| |
| |
|
|
| |
| |
| assert new_parents_last_descendant is not None |
| new_parents_last_child = new_parent_element.contents[-1] |
| new_parents_last_descendant_next_element = ( |
| new_parents_last_descendant.next_element |
| ) |
| else: |
| |
| new_parents_last_child = None |
| new_parents_last_descendant_next_element = new_parent_element.next_element |
|
|
| to_append = element.contents |
| if len(to_append) > 0: |
| |
| |
| first_child = to_append[0] |
| if new_parents_last_descendant is not None: |
| first_child.previous_element = new_parents_last_descendant |
| else: |
| first_child.previous_element = new_parent_element |
| first_child.previous_sibling = new_parents_last_child |
| if new_parents_last_descendant is not None: |
| new_parents_last_descendant.next_element = first_child |
| else: |
| new_parent_element.next_element = first_child |
| if new_parents_last_child is not None: |
| new_parents_last_child.next_sibling = first_child |
|
|
| |
| |
| |
| |
| last_childs_last_descendant = to_append[-1]._last_descendant( |
| is_initialized=False, accept_self=True |
| ) |
|
|
| |
| |
| assert last_childs_last_descendant is not None |
| last_childs_last_descendant.next_element = ( |
| new_parents_last_descendant_next_element |
| ) |
| if new_parents_last_descendant_next_element is not None: |
| |
| |
| |
| |
| new_parents_last_descendant_next_element.previous_element = ( |
| last_childs_last_descendant |
| ) |
| last_childs_last_descendant.next_sibling = None |
|
|
| for child in to_append: |
| child.parent = new_parent_element |
| new_parent_element.contents.append(child) |
|
|
| |
| element.contents = [] |
| element.next_element = final_next_element |
|
|
| |
| |
| |
|
|
| |
| |
| def hasContent(self) -> bool: |
| return self.tag is None or len(self.tag.contents) > 0 |
|
|
| |
| |
| def cloneNode(self) -> treebuilder_base.Node: |
| assert self.tag is not None |
| tag = self.soup.new_tag(self.tag.name, self.namespace) |
| node = Element(tag, self.soup, self.namespace) |
| for key, value in self.attributes: |
| node.attributes[key] = value |
| return node |
|
|
| def getNameTuple(self) -> Tuple[Optional[_NamespaceURL], str]: |
| if self.namespace is None: |
| return namespaces["html"], self.name |
| else: |
| return self.namespace, self.name |
|
|
| nameTuple = property(getNameTuple) |
|
|
|
|
| class TextNode(BeautifulSoupNode): |
|
|
| def __init__(self, element: NavigableString, soup: "BeautifulSoup"): |
| treebuilder_base.Node.__init__(self, None) |
| self.tag = None |
| self.string = element |
| self.soup = soup |
|
|