garyhurtz / html2json.py

Tools for building HTML and parsing HTML into JSON in Python, and rendering HTML from JSON using Jinja.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect _parse_to_element implementation

garyhurtz opened this issue · comments

In one project I found an issue

<section><h1>text</h1></section>

parses to

<section>text</section>

the fix is below:

` def _parse_to_element(cls, soup):
"""
Parse soup into a (possibly nested) Element

    :param soup:
    :return: Element
    """

    children = list(soup.children)

    # if an element has one child and it is a string, it is held in soup.string
    # else soup.string is None
    if len(children) == 1 and not children[0].name:
        return Element(soup.name, soup.string, soup.attrs)`