Skip to content

Commit 1499d58

Browse files
committed
refactor: rename transform_html to set_html_attributes
1 parent 0740166 commit 1499d58

File tree

6 files changed

+20
-20
lines changed

6 files changed

+20
-20
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ pip install djc-core-html-parser
1313
## Usage
1414

1515
```python
16-
from djc_core_html_parser import transform_html
16+
from djc_core_html_parser import set_html_attributes
1717

1818
html = '<div><p>Hello</p></div>'
19-
result, _ = transform_html(
19+
result, _ = set_html_attributes(
2020
html,
2121
# Add attributes to the root elements
2222
root_attributes=['data-root-id'],
@@ -25,7 +25,7 @@ result, _ = transform_html(
2525
)
2626
```
2727

28-
To save ourselves from re-parsing the HTML, `transform_html` returns not just the transformed HTML, but also a dictionary as the second item.
28+
To save ourselves from re-parsing the HTML, `set_html_attributes` returns not just the transformed HTML, but also a dictionary as the second item.
2929

3030
This dictionary contains a record of which HTML attributes were written to which elemenents.
3131

@@ -37,7 +37,7 @@ Then, during the HTML transformation, we check each element for this attribute.
3737
2. Record the attributes that were added to the element, using the value of the watched attribute as the key.
3838

3939
```python
40-
from djc_core_html_parser import transform_html
40+
from djc_core_html_parser import set_html_attributes
4141

4242
html = """
4343
<div data-watch-id="123">
@@ -47,7 +47,7 @@ html = """
4747
</div>
4848
"""
4949

50-
result, captured = transform_html(
50+
result, captured = set_html_attributes(
5151
html,
5252
# Add attributes to the root elements
5353
root_attributes=['data-root-id'],

__init__.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Dict, Optional
22

3-
def transform_html(
3+
def set_html_attributes(
44
html: str,
55
root_attributes: List[str],
66
all_attributes: List[str],
@@ -25,7 +25,7 @@ def transform_html(
2525
2626
Example:
2727
>>> html = '<div><p>Hello</p></div>'
28-
>>> transform_html(html, ['data-root-id'], ['data-v-123'])
28+
>>> set_html_attributes(html, ['data-root-id'], ['data-v-123'])
2929
'<div data-root-id="" data-v-123=""><p data-v-123="">Hello</p></div>'
3030
3131
Raises:

src/html_parser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const VOID_ELEMENTS: [&str; 14] = [
3131
///
3232
/// Example:
3333
/// >>> html = '<div data-id="123"><p>Hello</p></div>'
34-
/// >>> html, captured = transform_html(html, ['data-root-id'], ['data-v-123'], watch_on_attribute='data-id')
34+
/// >>> html, captured = set_html_attributes(html, ['data-root-id'], ['data-v-123'], watch_on_attribute='data-id')
3535
/// >>> print(captured)
3636
/// {'123': ['data-root-id', 'data-v-123']}
3737
///
@@ -41,7 +41,7 @@ const VOID_ELEMENTS: [&str; 14] = [
4141
#[pyo3(
4242
text_signature = "(html, root_attributes, all_attributes, *, check_end_names=False, watch_on_attribute=None)"
4343
)]
44-
pub fn transform_html(
44+
pub fn set_html_attributes(
4545
py: Python,
4646
html: &str,
4747
root_attributes: Vec<String>,

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ mod html_parser;
66
/// A Python module implemented in Rust for high-performance HTML transformation.
77
#[pymodule]
88
fn djc_core_html_parser(_py: Python, m: &PyModule) -> PyResult<()> {
9-
m.add_function(wrap_pyfunction!(html_parser::transform_html, m)?)?;
9+
m.add_function(wrap_pyfunction!(html_parser::set_html_attributes, m)?)?;
1010
Ok(())
1111
}

tests/benchmark.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from statistics import mean, stdev
22
import time
33

4-
from djc_core_html_parser import transform_html
4+
from djc_core_html_parser import set_html_attributes
55

66

77
def generate_large_html(num_elements: int = 1000) -> str:
@@ -85,7 +85,7 @@ def generate_large_html(num_elements: int = 1000) -> str:
8585
for i in range(NUM_ITER): # Run N iterations
8686

8787
start = time.perf_counter()
88-
transform_html(html, root_attributes, all_attributes, watch_on_attribute="data-id")
88+
set_html_attributes(html, root_attributes, all_attributes, watch_on_attribute="data-id")
8989
modify_time = time.perf_counter() - start
9090
modify_times.append(modify_time)
9191

tests/test_html_parser.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# This same set of tests is also found in django-components, to ensure that
22
# this implementation can be replaced with the django-components' pure-python implementation
33

4-
from djc_core_html_parser import transform_html
4+
from djc_core_html_parser import set_html_attributes
55
from typing import Dict, List
66

77

88
def test_basic_transformation():
99
html = "<div><p>Hello</p></div>"
10-
result, _ = transform_html(html, ["data-root"], ["data-all"])
10+
result, _ = set_html_attributes(html, ["data-root"], ["data-all"])
1111
expected = '<div data-root="" data-all=""><p data-all="">Hello</p></div>'
1212
assert result == expected
1313

1414

1515
def test_multiple_roots():
1616
html = "<div>First</div><span>Second</span>"
17-
result, _ = transform_html(html, ["data-root"], ["data-all"])
17+
result, _ = set_html_attributes(html, ["data-root"], ["data-all"])
1818
expected = '<div data-root="" data-all="">First</div><span data-root="" data-all="">Second</span>'
1919
assert result == expected
2020

@@ -42,7 +42,7 @@ def test_complex_html():
4242
</footer>
4343
"""
4444

45-
result, _ = transform_html(html, ["data-root"], ["data-all", "data-v-123"])
45+
result, _ = set_html_attributes(html, ["data-root"], ["data-all", "data-v-123"])
4646
expected = """
4747
<div class="container" id="main" data-root="" data-all="" data-v-123="">
4848
<header class="flex" data-all="" data-v-123="">
@@ -76,7 +76,7 @@ def test_void_elements():
7676
]
7777

7878
for input_html, expected in test_cases:
79-
result, _ = transform_html(input_html, ["data-root"], ["data-v-123"])
79+
result, _ = set_html_attributes(input_html, ["data-root"], ["data-v-123"])
8080
assert result == expected
8181

8282

@@ -89,7 +89,7 @@ def test_html_head_with_meta():
8989
<meta name="description" content="Test">
9090
</head>"""
9191

92-
result, _ = transform_html(html, ["data-root"], ["data-v-123"])
92+
result, _ = set_html_attributes(html, ["data-root"], ["data-v-123"])
9393
expected = """
9494
<head data-root="" data-v-123="">
9595
<meta charset="utf-8" data-v-123=""/>
@@ -110,7 +110,7 @@ def test_watch_attribute():
110110

111111
result: str
112112
captured: Dict[str, List[str]]
113-
result, captured = transform_html(html, ["data-root"], ["data-v-123"], watch_on_attribute="data-id")
113+
result, captured = set_html_attributes(html, ["data-root"], ["data-v-123"], watch_on_attribute="data-id")
114114
expected = """
115115
<div data-id="123" data-root="" data-v-123="">
116116
<p data-v-123="">Regular element</p>
@@ -140,7 +140,7 @@ def test_whitespace_preservation():
140140
<span> Text with spaces </span>
141141
</div>"""
142142

143-
result, _ = transform_html(html, ["data-root"], ["data-all"])
143+
result, _ = set_html_attributes(html, ["data-root"], ["data-all"])
144144
expected = """<div data-root="" data-all="">
145145
<p data-all=""> Hello World </p>
146146
<span data-all=""> Text with spaces </span>

0 commit comments

Comments
 (0)