Skip to content

Commit a3f4518

Browse files
committed
Fix #3 issue and update rust dependencies
[run test]
1 parent 9c365f7 commit a3f4518

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ crate-type = ["cdylib"]
1717
members = ["treedom", "matching"]
1818

1919
[workspace.package]
20-
version = "0.3.0"
20+
version = "0.3.1"
2121
edition = "2021"
2222
readme = "README.md"
2323
license = "MIT"
@@ -34,7 +34,7 @@ panic = "abort"
3434
strip = "symbols"
3535

3636
[dependencies]
37-
pyo3 = { version = "0.24", default-features = false, features = [
37+
pyo3 = { version = "0.25", default-features = false, features = [
3838
"macros",
3939
"extension-module",
4040
] }
@@ -47,7 +47,7 @@ hashbrown = { version = "0.15", default-features = false, features = [
4747
] }
4848

4949
[build-dependencies]
50-
pyo3-build-config = { version = "0.24", features = ["resolve-config"] }
50+
pyo3-build-config = { version = "0.25", features = ["resolve-config"] }
5151

5252
[lints.clippy]
5353
dbg_macro = "warn"

python/markupever/parser.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
class Parser:
77
__slots__ = ("__raw", "__state")
88

9-
def __init__(self, options: typing.Union[_rustlib.HtmlOptions, _rustlib.XmlOptions, typing.Literal["html"], typing.Literal["xml"]] = "html"):
9+
def __init__(
10+
self,
11+
options: typing.Union[
12+
_rustlib.HtmlOptions, _rustlib.XmlOptions, typing.Literal["html"], typing.Literal["xml"]
13+
] = "html",
14+
):
1015
"""
1116
An HTML/XML parser, ready to receive unicode input.
1217
@@ -23,7 +28,7 @@ def __init__(self, options: typing.Union[_rustlib.HtmlOptions, _rustlib.XmlOptio
2328
options = _rustlib.XmlOptions()
2429
else:
2530
raise ValueError(f"invalid parser options: {options!r}")
26-
31+
2732
self.__raw = _rustlib.Parser(options)
2833

2934
# 0 - processing
@@ -113,7 +118,9 @@ def __repr__(self) -> str:
113118

114119
def parse(
115120
content: typing.Union[str, bytes],
116-
options: typing.Union[_rustlib.HtmlOptions, _rustlib.XmlOptions, typing.Literal["html"], typing.Literal["xml"]] = "html",
121+
options: typing.Union[
122+
_rustlib.HtmlOptions, _rustlib.XmlOptions, typing.Literal["html"], typing.Literal["xml"]
123+
] = "html",
117124
) -> TreeDom:
118125
"""
119126
Parses HTML or XML content and returns the parsed document tree.
@@ -132,7 +139,9 @@ def parse(
132139

133140
def parse_file(
134141
path: typing.Union[str, typing.TextIO, typing.BinaryIO],
135-
options: typing.Union[_rustlib.HtmlOptions, _rustlib.XmlOptions, typing.Literal["html"], typing.Literal["xml"]] = "html",
142+
options: typing.Union[
143+
_rustlib.HtmlOptions, _rustlib.XmlOptions, typing.Literal["html"], typing.Literal["xml"]
144+
] = "html",
136145
*,
137146
chunk_size: int = 10240,
138147
) -> TreeDom:

python/tests/test_dom.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,10 @@ def test_serializer():
477477
case.content, markupever.XmlOptions() if case.is_xml else markupever.HtmlOptions()
478478
)
479479
assert dom.serialize(case.indent) == case.expected
480+
481+
482+
def test_add_itself():
483+
tag = markupever.parse("<p>").root().first_child
484+
485+
with pytest.raises(RuntimeError):
486+
tag.attach(tag)

src/tree.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ impl PyTreeDom {
183183
));
184184
}
185185

186+
if parent.id == child.id {
187+
return Err(pyo3::PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
188+
"Cannot append node as a child to itself",
189+
));
190+
}
191+
186192
let mut tree = self_.dom.lock();
187193
let mut parent = tree.get_mut(parent.id).unwrap();
188194

@@ -226,6 +232,12 @@ impl PyTreeDom {
226232
));
227233
}
228234

235+
if parent.id == child.id {
236+
return Err(pyo3::PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
237+
"Cannot append node as a child to itself",
238+
));
239+
}
240+
229241
let mut tree = self_.dom.lock();
230242
let mut parent = tree.get_mut(parent.id).unwrap();
231243

@@ -269,6 +281,12 @@ impl PyTreeDom {
269281
));
270282
}
271283

284+
if parent.id == child.id {
285+
return Err(pyo3::PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
286+
"Cannot append node as a child to itself",
287+
));
288+
}
289+
272290
let mut tree = self_.dom.lock();
273291
let mut parent = tree.get_mut(parent.id).unwrap();
274292

@@ -312,6 +330,12 @@ impl PyTreeDom {
312330
));
313331
}
314332

333+
if parent.id == child.id {
334+
return Err(pyo3::PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
335+
"Cannot append node as a child to itself",
336+
));
337+
}
338+
315339
let mut tree = self_.dom.lock();
316340
let mut parent = tree.get_mut(parent.id).unwrap();
317341

treedom/src/dom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'a> Serializer<'a> {
245245

246246
fn skip_last<T>(mut iter: impl Iterator<Item = T>) -> impl Iterator<Item = T> {
247247
let last = iter.next();
248-
iter.scan(last, |state, item| std::mem::replace(state, Some(item)))
248+
iter.scan(last, |state, item| state.replace(item))
249249
}
250250

251251
impl markup5ever::serialize::Serialize for Serializer<'_> {

0 commit comments

Comments
 (0)