Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass by value #85

Merged
merged 5 commits into from
Feb 21, 2024
Merged
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: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
History
=======

0.18.1 (2024-02-21)
-------------------
- Fixed blocks data getting updated due to pass by reference

0.18.0 (2024-02-09)
-------------------
- html.escape all the attributes
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.18.0
current_version = 0.18.1
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="tiptapy",
version="0.18.0", # TODO: why bumpversion works only for single quotes?
version="0.18.1", # TODO: why bumpversion works only for single quotes?
url="https://github.com/scrolltech/tiptapy",
description="Library that generates HTML output from JSON export of tiptap editor",
long_description=open("README.md").read(),
Expand Down
2 changes: 1 addition & 1 deletion tests/data/html/xss.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div data-foo="&quot;/&gt;&lt;script&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;" data-&gt;&lt;script&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;="foo"><p><a foo="&quot;&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;" &gt;&lt;script&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;="foo" target="_blank" rel="noopener nofollow">&lt;script&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;</a></p></div>
<div data-foo="&quot;/&gt;&lt;script&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;" data-&gt;&lt;script&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;="foo"><p><a &gt;&lt;script&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;="foo" foo="&quot;&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;" target="_blank" rel="noopener nofollow">&lt;script&gt;alert(&#x27;pwned!&#x27;);&lt;/script&gt;</a></p></div>
9 changes: 7 additions & 2 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from copy import deepcopy

import pytest

Expand Down Expand Up @@ -87,7 +88,11 @@ def test_html_tag(tag):
Test expected json input with the expected html.
"""
tag_data = json_data[tag]
expected_html = html_data[tag]
tag_data_copy = deepcopy(tag_data)
expected_html = html_data[tag].strip()

renderer = tiptapy.BaseDoc(config)
assert renderer.render(tag_data) == expected_html.strip()
rendered_html = renderer.render(tag_data).strip()

assert rendered_html == expected_html
assert tag_data == tag_data_copy # Test pass by value
18 changes: 6 additions & 12 deletions tiptapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
make_img_src,
)

__version__ = "0.18.0"
__version__ = "0.18.1"

renderers: Dict = {}

Expand Down Expand Up @@ -50,18 +50,12 @@ def escape_values_recursive(node):
skip_key = "html"

if isinstance(node, dict):
items = list(node.items())
for k, v in items:
esc_k = escape(k)
if k != esc_k:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sayanarijit what about this condition?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not required, as it's not updating the same dict anymore. Every key is escaped in new dict.

del node[k]
if esc_k == skip_key:
node[esc_k] = v
else:
node[esc_k] = escape_values_recursive(v)
return {
escape(k): v if k == skip_key else escape_values_recursive(v)
for k, v in node.items()
}
elif isinstance(node, list):
for i, v in enumerate(node):
node[i] = escape_values_recursive(v)
return [escape_values_recursive(x) for x in node]
elif isinstance(node, str):
return escape(node)
return node
Expand Down
Loading