Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit d89630f

Browse files
committed
Refactor so all node types have outerHTML.
1 parent 1823a4f commit d89630f

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

polyplug.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,7 @@ def innerHTML(self):
368368
"""
369369
result = ""
370370
for child in self.childNodes:
371-
if isinstance(child, ElementNode):
372-
result += child.outerHTML
373-
elif isinstance(child, TextNode):
374-
result += child.nodeValue
375-
elif isinstance(child, CommentNode):
376-
result += "<!--" + child.nodeValue + "-->"
371+
result += child.outerHTML
377372
return result
378373

379374
@innerHTML.setter
@@ -433,6 +428,13 @@ def __init__(self, **kwargs):
433428
super().__init__(**kwargs)
434429
self.nodeValue = kwargs.get("nodeValue")
435430

431+
@property
432+
def outerHTML(self):
433+
"""
434+
Get a string representation of the element's outer HTML.
435+
"""
436+
return self.nodeValue
437+
436438
@property
437439
def as_dict(self):
438440
return {
@@ -452,6 +454,13 @@ def __init__(self, **kwargs):
452454
super().__init__(**kwargs)
453455
self.nodeValue = kwargs.get("nodeValue")
454456

457+
@property
458+
def outerHTML(self):
459+
"""
460+
Get a string representation of the element's outer HTML.
461+
"""
462+
return "<!--" + self.nodeValue + "-->"
463+
455464
@property
456465
def as_dict(self):
457466
return {
@@ -470,6 +479,13 @@ class FragmentNode(Node):
470479
def __init__(self, **kwargs):
471480
super().__init__(**kwargs)
472481

482+
@property
483+
def outerHTML(self):
484+
"""
485+
Get a string representation of the element's outer HTML.
486+
"""
487+
return ""
488+
473489
@property
474490
def as_dict(self):
475491
return {"nodeType": 11, "childNodes": []}

tests/test_polyplug.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ def test_text_node():
378378
)
379379
assert n.nodeValue == "Test text."
380380
assert n.parent == "fakeParent"
381+
assert n.outerHTML == "Test text."
381382

382383

383384
def test_comment_node():
@@ -392,6 +393,7 @@ def test_comment_node():
392393
)
393394
assert n.nodeValue == "Test comment."
394395
assert n.parent == "fakeParent"
396+
assert n.outerHTML == "<!--Test comment.-->"
395397

396398

397399
def test_fragment_node():
@@ -404,6 +406,7 @@ def test_fragment_node():
404406
parent="fakeParent",
405407
)
406408
assert n.parent == "fakeParent"
409+
assert n.outerHTML == ""
407410

408411

409412
def test_htmltokenizer_init():

0 commit comments

Comments
 (0)