Skip to content

AssertionError caused by weird/malformed HTML #34

@ThiefMaster

Description

@ThiefMaster

Easiest way to reproduce is this minimal example:

from weasyprint import HTML
HTML(string='$<a_{b}>$').write_pdf()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/weasyprint/__init__.py", line 265, in write_pdf
    self.render(font_config, counter_style, **options)
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/weasyprint/__init__.py", line 222, in render
    return Document._render(self, font_config, counter_style, options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/weasyprint/document.py", line 256, in _render
    context = cls._build_layout_context(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/weasyprint/document.py", line 235, in _build_layout_context
    style_for = get_all_computed_styles(
                ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/weasyprint/css/__init__.py", line 1113, in get_all_computed_styles
    for sheet in find_stylesheets(
                 ^^^^^^^^^^^^^^^^^
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/weasyprint/css/__init__.py", line 246, in find_stylesheets
    for wrapper in wrapper_element.query_all('style', 'link'):
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/cssselect2/tree.py", line 298, in <genexpr>
    if any(test(element) for test in tests))
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/cssselect2/tree.py", line 298, in <genexpr>
    if any(test(element) for test in tests))
           ^^^^^^^^^^^^^
  File "<string>", line 1, in <lambda>
  File "/home/adrian/.pyenv/versions/3.12.11/lib/python3.12/functools.py", line 998, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/cssselect2/tree.py", line 333, in local_name
    namespace_url, local_name = _split_etree_tag(self.etree_element.tag)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adrian/dev/indico/py3/env-312/lib/python3.12/site-packages/cssselect2/tree.py", line 406, in _split_etree_tag
    assert tag[0] == '{'
           ^^^^^^^^^^^^^
AssertionError

Yes, it's obviously invalid HTML that should have ideally been escaped before passing it to HTML(), but the error is still weird.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions