Skip to content

Commit

Permalink
rename!: add Tag in tag node class name
Browse files Browse the repository at this point in the history
  • Loading branch information
almahdi404 committed Nov 27, 2024
1 parent d42d70b commit 2d90da8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
12 changes: 7 additions & 5 deletions src/htmst/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
AttrNode,
CommentNode,
DoctypeNode,
DoubleNode,
DoubleTagNode,
Pos,
SingleNode,
SingleTagNode,
TextNode,
)

Expand All @@ -28,7 +28,7 @@ class HtmlAst:

def __init__(self, html: str):
self.html: str = html
self.root = DoubleNode("", [], None, Pos(0, 0), Pos(0, 0))
self.root = DoubleTagNode("", [], None, Pos(0, 0), Pos(0, 0))
self.current_node = self.root
self.current_index = 0
self.current_pos = Pos(0, 0)
Expand Down Expand Up @@ -268,7 +268,9 @@ def handle_tag_start(self):
char = self.html[self.current_index]
if char == ">":
self.skip_char()
node = DoubleNode(tag, attrs, self.current_node, tag_start, Pos(0, 0))
node = DoubleTagNode(
tag, attrs, self.current_node, tag_start, Pos(0, 0)
)
self.current_node.children.append(node)
self.current_node = node
break
Expand All @@ -279,7 +281,7 @@ def handle_tag_start(self):
char = self.html[self.current_index]
if char == ">":
self.skip_char()
node = SingleNode(
node = SingleTagNode(
tag,
attrs,
self.current_node,
Expand Down
16 changes: 8 additions & 8 deletions src/htmst/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __repr__(self):
class TextNode:
__slots__ = ("text", "parent", "start", "end")

def __init__(self, text: str, parent: "DoubleNode", start: Pos, end: Pos):
def __init__(self, text: str, parent: "DoubleTagNode", start: Pos, end: Pos):
self.text = text
self.start = start
self.end = end
Expand Down Expand Up @@ -53,21 +53,21 @@ def __repr__(self):
return f"AttrNode(@{self.name}){self.start}-{self.end}"


class DoubleNode:
class DoubleTagNode:
__slots__ = ("tag", "attrs", "children", "parent", "start", "end")

def __init__(
self,
tag: str,
attrs: list[AttrNode],
parent: "DoubleNode | None",
parent: "DoubleTagNode | None",
start: Pos,
end: Pos,
):
self.tag = tag
self.attrs = attrs
self.children: list[
DoubleNode | SingleNode | TextNode | CommentNode | DoctypeNode
DoubleTagNode | SingleTagNode | TextNode | CommentNode | DoctypeNode
] = []
self.parent = parent
self.start = start
Expand All @@ -77,14 +77,14 @@ def __repr__(self):
return f"DoubleNode(<{self.tag}>){self.start}-{self.end}"


class SingleNode:
class SingleTagNode:
__slots__ = ("tag", "attrs", "parent", "start", "end")

def __init__(
self,
tag: str,
attrs: list[AttrNode],
parent: DoubleNode,
parent: DoubleTagNode,
start: Pos,
end: Pos,
):
Expand All @@ -102,7 +102,7 @@ class CommentNode:
def __init__(
self,
text: str,
parent: DoubleNode,
parent: DoubleTagNode,
start: Pos,
end: Pos,
):
Expand All @@ -119,7 +119,7 @@ class DoctypeNode:
def __init__(
self,
text: str,
parent: DoubleNode,
parent: DoubleTagNode,
start: Pos,
end: Pos,
):
Expand Down
28 changes: 14 additions & 14 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from htmst.structures import (
CommentNode,
DoctypeNode,
DoubleNode,
DoubleTagNode,
Pos,
SingleNode,
SingleTagNode,
TextNode,
)

Expand All @@ -21,7 +21,7 @@ def test_double():
html = """<div>hi</div>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "div"
assert hasattr(node, "children")
assert node.start == Pos(0, 0)
Expand All @@ -32,7 +32,7 @@ def test_single():
html = """<input type="text" />"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, SingleNode)
assert isinstance(node, SingleTagNode)
assert node.tag == "input"
assert not hasattr(node, "children")
assert node.start == Pos(0, 0)
Expand All @@ -43,7 +43,7 @@ def test_attrs():
html = """<div class="foo" id="bar" @click="alert()">hi</div>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "div"
assert node.attrs[0].name == "class"
assert node.attrs[0].value == "foo"
Expand All @@ -59,7 +59,7 @@ def test_double_quote():
html = """<div class="foo \\" bar">hi</div>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "div"
assert node.attrs[0].value == 'foo \\" bar'
assert node.start == Pos(0, 0)
Expand All @@ -70,7 +70,7 @@ def test_single_quote():
html = """<div class='foo \\' bar'>hi</div>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "div"
assert node.attrs[0].value == "foo \\' bar"
assert node.start == Pos(0, 0)
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_first_bracket(self):
html = """<script>foo(</script>)</script>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "script"
assert node.start == Pos(0, 0)
assert node.end == Pos(0, len(html))
Expand All @@ -111,7 +111,7 @@ def test_second_bracket(self):
html = """<script>function foo(){ </script>; }</script>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "script"
assert node.start == Pos(0, 0)
assert node.end == Pos(0, len(html))
Expand All @@ -120,7 +120,7 @@ def test_third_bracket(self):
html = """<script>function foo(){ bar[</script>]; }</script>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "script"
assert node.start == Pos(0, 0)
assert node.end == Pos(0, len(html))
Expand All @@ -129,7 +129,7 @@ def test_signle_quote(self):
html = """<script>function foo(){ bar['</script>']; }</script>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "script"
assert node.start == Pos(0, 0)
assert node.end == Pos(0, len(html))
Expand All @@ -138,7 +138,7 @@ def test_double_quote(self):
html = """<script>function foo(){ bar["</script>"]; }</script>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "script"
assert node.start == Pos(0, 0)
assert node.end == Pos(0, len(html))
Expand All @@ -148,7 +148,7 @@ def test_lf():
html = """<div>\n hi\n</div>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "div"
assert node.start == Pos(0, 0)
assert node.end == Pos(2, 6)
Expand All @@ -158,7 +158,7 @@ def test_crlf():
html = """<div>\r\n hi\r\n</div>"""
ast = HtmlAst(html)
node = ast.root.children[0]
assert isinstance(node, DoubleNode)
assert isinstance(node, DoubleTagNode)
assert node.tag == "div"
assert node.start == Pos(0, 0)
assert node.end == Pos(2, 6)

0 comments on commit 2d90da8

Please sign in to comment.