Skip to content

Commit

Permalink
Added conditional escape parameter to .content_str() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bystroushaak committed Mar 9, 2022
1 parent f7a5638 commit c130c69
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

3.0.12
------
- Added conditional `escape` parameter to `.content_str()` method.

3.0.11
------
- Fixed parent problem with `.__deepcopy__()`.
Expand Down
10 changes: 8 additions & 2 deletions src/dhtmlparser3/tags/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,20 @@ def _parameters_to_str(self) -> str:

return " " + " ".join(parameters)

def content_str(self) -> str:
def content_str(self, escape=False) -> str:
"""
Return everything in between the tags as string.
Args:
escape (bool): Escape the content. Default False.
"""
output = ""
for item in self.content:
if isinstance(item, str):
output += item
if escape:
output += html.escape(item)
else:
output += item
else:
output += item.to_string()

Expand Down
17 changes: 17 additions & 0 deletions tests/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,20 @@ def test_deep_copy():

assert str(tag) == '<div param="1"><h1>test</h1></div>'
assert str(new_tag) == '<div param="2"><h2>test</h2></div>'


def test_weird_msg():
original_str = """<blockquote>Message-ID: &lt;[email protected]&gt;</blockquote>"""
dom = dhtmlparser3.parse(original_str)

content = dom.find("blockquote")[0].content_str().strip()
assert content == "Message-ID: <[email protected]>"

content = dom.find("blockquote")[0].content_str(True).strip()
assert content == "Message-ID: &lt;[email protected]&gt;"

tag = dom.find("blockquote")[0]
assert tag.to_string() == original_str

assert tag.prettify().strip() == original_str

0 comments on commit c130c69

Please sign in to comment.