Skip to content

Commit 336bd77

Browse files
committed
[PATCH] plugin.api.validate: fix xml_element (streamlink#4514)
Add missing tail attribute and clone child nodes
1 parent ea4d45b commit 336bd77

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/streamlink/plugin/api/validate.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
"""
1717

18-
from copy import copy as copy_obj
18+
from copy import copy, deepcopy
1919
try:
2020
from typing import Any, Tuple, Union
2121
except ImportError:
@@ -116,13 +116,16 @@ def __init__(self, *keys, **kw):
116116
self.seq = kw.get("seq", tuple)
117117

118118

119-
class xml_element(object):
120-
"""A XML element."""
119+
class xml_element:
120+
"""
121+
Validate an XML element.
122+
"""
121123

122-
def __init__(self, tag=None, text=None, attrib=None):
124+
def __init__(self, tag=None, text=None, attrib=None, tail=None):
123125
self.tag = tag
124126
self.text = text
125127
self.attrib = attrib
128+
self.tail = tail
126129

127130

128131
# ----
@@ -466,6 +469,7 @@ def validate_xml_element(schema, value):
466469
_tag = value.tag
467470
_attrib = value.attrib
468471
_text = value.text
472+
_tail = value.tail
469473

470474
if schema.attrib is not None:
471475
try:
@@ -485,17 +489,24 @@ def validate_xml_element(schema, value):
485489
except ValueError as err:
486490
raise ValueError("Unable to validate XML text: {0}".format(err))
487491

492+
if schema.tail is not None:
493+
try:
494+
_tail = validate(schema.tail, value.tail)
495+
except ValueError as err:
496+
raise ValueError("Unable to validate XML text: {0}".format(err))
497+
488498
new = Element(_tag, _attrib)
489499
new.text = _text
500+
new.tail = _tail
490501
for child in value:
491-
new.append(child)
502+
new.append(deepcopy(child))
492503

493504
return new
494505

495506

496507
@validate.register(attr)
497508
def validate_attr(schema, value):
498-
new = copy_obj(value)
509+
new = copy(value)
499510

500511
for attr, schema in schema.schema.items():
501512
if not _hasattr(value, attr):

tests/test_api_validate.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,14 @@ def test_xml_element(self):
191191
upper = transform(str.upper)
192192
newelem = validate(xml_element(tag=upper, text=upper, attrib={upper: upper}), el)
193193

194+
assert newelem is not el
194195
assert newelem.tag == "TAG"
195196
assert newelem.text == "TEST"
196197
assert newelem.attrib == {"KEY": "VALUE"}
197-
assert list(newelem.iterchildren()) == [childA, childB]
198+
assert newelem[0].tag == "childA"
199+
assert newelem[1].tag == "childB"
200+
assert newelem[0] is not childA
201+
assert newelem[1] is not childB
198202

199203
with self.assertRaises(ValueError) as cm:
200204
validate(xml_element(tag="invalid"), el)

0 commit comments

Comments
 (0)