Skip to content

Commit 0740166

Browse files
committed
refactor: revert the feature of expanding self-closing tags
1 parent a10f496 commit 0740166

File tree

3 files changed

+1
-108
lines changed

3 files changed

+1
-108
lines changed

__init__.pyi

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ def transform_html(
44
html: str,
55
root_attributes: List[str],
66
all_attributes: List[str],
7-
expand_empty_elements: Optional[bool] = None,
87
check_end_names: Optional[bool] = None,
98
watch_on_attribute: Optional[str] = None,
109
) -> tuple[str, Dict[str, List[str]]]:
@@ -15,7 +14,6 @@ def transform_html(
1514
html (str): The HTML string to transform. Can be a fragment or full document.
1615
root_attributes (List[str]): List of attribute names to add to root elements only.
1716
all_attributes (List[str]): List of attribute names to add to all elements.
18-
expand_empty_elements (Optional[bool]): Whether to expand self-closing tags into open/close pairs. Defaults to None.
1917
check_end_names (Optional[bool]): Whether to validate matching of end tags. Defaults to None.
2018
watch_on_attribute (Optional[str]): If set, captures which attributes were added to elements with this attribute.
2119

src/html_parser.rs

+1-78
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const VOID_ELEMENTS: [&str; 14] = [
2020
/// html (str): The HTML string to transform. Can be a fragment or full document.
2121
/// root_attributes (List[str]): List of attribute names to add to root elements only.
2222
/// all_attributes (List[str]): List of attribute names to add to all elements.
23-
/// expand_empty_elements (bool, optional): Whether to expand self-closing tags into open/close pairs. Defaults to true.
2423
/// check_end_names (bool, optional): Whether to validate matching of end tags. Defaults to false.
2524
/// watch_on_attribute (str, optional): If set, captures which attributes were added to elements with this attribute.
2625
///
@@ -40,21 +39,19 @@ const VOID_ELEMENTS: [&str; 14] = [
4039
/// ValueError: If the HTML is malformed or cannot be parsed.
4140
#[pyfunction]
4241
#[pyo3(
43-
text_signature = "(html, root_attributes, all_attributes, *, expand_empty_elements=True, check_end_names=False, watch_on_attribute=None)"
42+
text_signature = "(html, root_attributes, all_attributes, *, check_end_names=False, watch_on_attribute=None)"
4443
)]
4544
pub fn transform_html(
4645
py: Python,
4746
html: &str,
4847
root_attributes: Vec<String>,
4948
all_attributes: Vec<String>,
50-
expand_empty_elements: Option<bool>,
5149
check_end_names: Option<bool>,
5250
watch_on_attribute: Option<String>,
5351
) -> PyResult<PyObject> {
5452
let config = HtmlTransformerConfig::new(
5553
root_attributes,
5654
all_attributes,
57-
expand_empty_elements.unwrap_or(true),
5855
check_end_names.unwrap_or(false),
5956
watch_on_attribute,
6057
);
@@ -79,7 +76,6 @@ pub struct HtmlTransformerConfig {
7976
root_attributes: Vec<String>,
8077
all_attributes: Vec<String>,
8178
void_elements: HashSet<String>,
82-
expand_empty_elements: bool,
8379
check_end_names: bool,
8480
watch_on_attribute: Option<String>,
8581
}
@@ -88,7 +84,6 @@ impl HtmlTransformerConfig {
8884
pub fn new(
8985
root_attributes: Vec<String>,
9086
all_attributes: Vec<String>,
91-
expand_empty_elements: bool,
9287
check_end_names: bool,
9388
watch_on_attribute: Option<String>,
9489
) -> Self {
@@ -98,7 +93,6 @@ impl HtmlTransformerConfig {
9893
root_attributes,
9994
all_attributes,
10095
void_elements,
101-
expand_empty_elements,
10296
check_end_names,
10397
watch_on_attribute,
10498
}
@@ -154,7 +148,6 @@ pub fn transform(
154148
) -> Result<(String, Vec<(String, Vec<String>)>), Box<dyn std::error::Error>> {
155149
let mut reader = Reader::from_str(html);
156150
let reader_config = reader.config_mut();
157-
reader_config.expand_empty_elements = config.expand_empty_elements;
158151
reader_config.check_end_names = config.check_end_names;
159152

160153
// We transform the HTML by reading it and writing it simultaneously
@@ -226,7 +219,6 @@ mod tests {
226219
let config = HtmlTransformerConfig::new(
227220
vec!["data-root".to_string()],
228221
vec!["data-all".to_string()],
229-
true,
230222
false,
231223
None,
232224
);
@@ -243,7 +235,6 @@ mod tests {
243235
let config = HtmlTransformerConfig::new(
244236
vec!["data-root".to_string()],
245237
vec!["data-all".to_string()],
246-
true,
247238
false,
248239
None,
249240
);
@@ -262,7 +253,6 @@ mod tests {
262253
let config = HtmlTransformerConfig::new(
263254
vec!["data-root".to_string()],
264255
vec!["data-all".to_string(), "data-v-123".to_string()],
265-
true,
266256
false,
267257
None,
268258
);
@@ -315,7 +305,6 @@ mod tests {
315305
let config = HtmlTransformerConfig::new(
316306
vec!["data-root".to_string()],
317307
vec!["data-v-123".to_string()],
318-
true,
319308
false,
320309
None,
321310
);
@@ -369,7 +358,6 @@ mod tests {
369358
let config = HtmlTransformerConfig::new(
370359
vec!["data-root".to_string()],
371360
vec!["data-v-123".to_string()],
372-
true,
373361
false,
374362
None,
375363
);
@@ -395,75 +383,12 @@ mod tests {
395383
assert!(result.contains("/>"));
396384
}
397385

398-
#[test]
399-
fn test_config_expand_empty_elements() {
400-
// Test with expand_empty_elements = false
401-
let config = HtmlTransformerConfig::new(
402-
vec!["data-root".to_string()],
403-
vec!["data-v-123".to_string()],
404-
false, // Don't expand empty elements
405-
false,
406-
None,
407-
);
408-
409-
let test_cases = [
410-
// Non-void elements should stay self-closing when expand_empty_elements is false
411-
(
412-
"<div/>",
413-
"<div data-root=\"\" data-v-123=\"\"/>"
414-
),
415-
(
416-
"<p/>",
417-
"<p data-root=\"\" data-v-123=\"\"/>"
418-
),
419-
(
420-
"<div><span/></div>",
421-
"<div data-root=\"\" data-v-123=\"\"><span data-v-123=\"\"/></div>"
422-
),
423-
// Void elements should always be self-closing regardless of config
424-
(
425-
"<div><img/><br/></div>",
426-
"<div data-root=\"\" data-v-123=\"\"><img data-v-123=\"\"/><br data-v-123=\"\"/></div>"
427-
),
428-
];
429-
430-
for (input, expected) in test_cases {
431-
let (result, _) = transform(&config, input).unwrap();
432-
assert_eq!(result, expected);
433-
}
434-
435-
// Compare with expand_empty_elements = true
436-
let config = HtmlTransformerConfig::new(
437-
vec!["data-root".to_string()],
438-
vec!["data-v-123".to_string()],
439-
true, // Expand empty elements
440-
false,
441-
None,
442-
);
443-
444-
let expanded_cases = [
445-
("<div/>", "<div data-root=\"\" data-v-123=\"\"></div>"),
446-
("<p/>", "<p data-root=\"\" data-v-123=\"\"></p>"),
447-
// Void elements should still be self-closing
448-
(
449-
"<div><img/></div>",
450-
"<div data-root=\"\" data-v-123=\"\"><img data-v-123=\"\"/></div>",
451-
),
452-
];
453-
454-
for (input, expected) in expanded_cases {
455-
let (result, _) = transform(&config, input).unwrap();
456-
assert_eq!(result, expected);
457-
}
458-
}
459-
460386
#[test]
461387
fn test_config_check_end_names() {
462388
// Test with check_end_names = false (lenient mode)
463389
let config = HtmlTransformerConfig::new(
464390
vec!["data-root".to_string()],
465391
vec!["data-v-123".to_string()],
466-
true,
467392
false, // Don't check end names
468393
None,
469394
);
@@ -483,7 +408,6 @@ mod tests {
483408
let config = HtmlTransformerConfig::new(
484409
vec!["data-root".to_string()],
485410
vec!["data-v-123".to_string()],
486-
true,
487411
true, // Check end names
488412
None,
489413
);
@@ -503,7 +427,6 @@ mod tests {
503427
let config = HtmlTransformerConfig::new(
504428
vec!["data-root".to_string()],
505429
vec!["data-v-123".to_string()],
506-
true,
507430
false,
508431
Some("data-id".to_string()),
509432
);

tests/test_html_parser.py

-28
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,6 @@ def test_html_head_with_meta():
100100
assert result == expected
101101

102102

103-
def test_expand_empty_elements():
104-
# Test with expand_empty_elements=False
105-
test_cases = [
106-
# Non-void elements should stay self-closing when expand_empty_elements is false
107-
("<div/>", '<div data-root="" data-v-123=""/>'),
108-
("<p/>", '<p data-root="" data-v-123=""/>'),
109-
("<div><span/></div>", '<div data-root="" data-v-123=""><span data-v-123=""/></div>'),
110-
# Void elements should always be self-closing regardless of config
111-
("<div><img/><br/></div>", '<div data-root="" data-v-123=""><img data-v-123=""/><br data-v-123=""/></div>'),
112-
]
113-
114-
for input_html, expected in test_cases:
115-
result, _ = transform_html(input_html, ["data-root"], ["data-v-123"], expand_empty_elements=False)
116-
assert result == expected
117-
118-
# Compare with expand_empty_elements=True
119-
expanded_cases = [
120-
("<div/>", '<div data-root="" data-v-123=""></div>'),
121-
("<p/>", '<p data-root="" data-v-123=""></p>'),
122-
# Void elements should still be self-closing
123-
("<div><img/></div>", '<div data-root="" data-v-123=""><img data-v-123=""/></div>'),
124-
]
125-
126-
for input_html, expected in expanded_cases:
127-
result, _ = transform_html(input_html, ["data-root"], ["data-v-123"], expand_empty_elements=True)
128-
assert result == expected
129-
130-
131103
def test_watch_attribute():
132104
html = """
133105
<div data-id="123">

0 commit comments

Comments
 (0)