Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/prompt_toolkit/formatted_text/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def get_current_style() -> str:
def process_node(node: Any) -> None:
"Process node recursively."
for child in node.childNodes:
if child.nodeType == child.TEXT_NODE:
if child.nodeType in (child.TEXT_NODE, child.CDATA_SECTION_NODE):
result.append((get_current_style(), child.data))
else:
elif child.nodeType == child.ELEMENT_NODE:
add_to_name_stack = child.nodeName not in (
"#document",
"html-root",
Expand Down
50 changes: 50 additions & 0 deletions tests/test_html_nodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import annotations

import io

import pytest

from prompt_toolkit import print_formatted_text
from prompt_toolkit.formatted_text import HTML, to_formatted_text


@pytest.mark.parametrize(
"markup, expected",
[
("<!-- hidden -->", []),
("before<!-- hidden -->after", [("", "before"), ("", "after")]),
(
"<b>before<!-- <i>hidden</i> -->after</b>",
[("class:b", "before"), ("class:b", "after")],
),
(
'<style fg="ansired"><!-- hidden --><b>text</b></style>after',
[("class:b fg:ansired", "text"), ("", "after")],
),
("<?note hidden?>visible", [("", "visible")]),
(
"<b>before<?note hidden?>after</b>",
[("class:b", "before"), ("class:b", "after")],
),
("<![CDATA[a < b & c]]>", [("", "a < b & c")]),
(
'<b fg="ansired"><![CDATA[<i>literal</i>]]></b>after',
[("class:b fg:ansired", "<i>literal</i>"), ("", "after")],
),
(
"<b>before<![CDATA[ & ]]><!-- hidden -->after</b>",
[("class:b", "before"), ("class:b", " & "), ("class:b", "after")],
),
],
)
def test_html_non_element_nodes(markup, expected):
assert to_formatted_text(HTML(markup)) == expected


def test_print_html_with_comments_and_cdata():
output = io.StringIO()
print_formatted_text(
HTML("<b>Result: <!-- internal note --><![CDATA[x < 3]]></b>"),
file=output,
)
assert output.getvalue() == "Result: x < 3\r\n"