Skip to content

Commit d32cb59

Browse files
authored
html_to_vdom only returned first element (#53)
If your html looked like this: ``` <div/> <p/> ``` The function only returned the model for the first div. Now it returns a list of models that are at the root level.
1 parent 0ae8b63 commit d32cb59

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

requirements/docs.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# copied from prod.txt
2-
sanic>=18.12, <19.0
3-
typing-extensions==3.7.2, <4.0
2+
sanic >=18.12, <19.0
3+
typing-extensions >=3.7.2, <4.0
44

55
# copied from dev.txt
66
pytest

requirements/prod.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
sanic>=18.12, <19.0
2-
typing-extensions==3.7.2, <4.0
1+
sanic >=18.12, <19.0
2+
typing-extensions >=3.7.2, <4.0

src/py/idom/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.4.0"
1+
__version__ = "0.4.1"
22

33
from .core import element, Element, Events, Layout
44
from .widgets import node, Image, hotswap, display, html, Input

src/py/idom/tools.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __repr__(self) -> str:
7070

7171
def html_to_vdom(
7272
source: str, transform: Optional[_ModelTransform] = None
73-
) -> Dict[str, Any]:
73+
) -> List[Dict[str, Any]]:
7474
"""Transform HTML into a DOM model
7575
7676
Parameters:
@@ -92,10 +92,10 @@ def __init__(self, transform: Optional[_ModelTransform] = None):
9292
if transform is not None:
9393
self._transform = transform
9494

95-
def model(self) -> Dict[str, Any]:
95+
def model(self) -> List[Dict[str, Any]]:
9696
root: Dict[str, Any] = self._node_stack[0]
97-
first_child: Dict[str, Any] = root["children"][0]
98-
return first_child
97+
root_children: List[Dict[str, Any]] = root["children"]
98+
return root_children
9999

100100
def feed(self, data: str) -> None:
101101
self._node_stack.append(self._make_node("div", {}))
@@ -110,6 +110,7 @@ def handle_starttag(self, tag: str, attrs: List[Tuple[str, str]]) -> None:
110110
current = self._node_stack[-1]
111111
current["children"].append(new)
112112
self._node_stack.append(new)
113+
print(self._node_stack[0])
113114

114115
def handle_endtag(self, tag: str) -> None:
115116
node = self._node_stack.pop(-1)

src/py/tests/test_tools.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_var_get():
5050
],
5151
)
5252
def test_html_to_vdom(case):
53-
assert html_to_vdom(case["source"]) == case["model"]
53+
assert html_to_vdom(case["source"]) == [case["model"]]
5454

5555

5656
def test_html_to_vdom_transform():
@@ -60,20 +60,22 @@ def make_links_blue(node):
6060
if node["tagName"] == "a":
6161
node["attributes"]["style"] = {"color": "blue"}
6262

63-
assert html_to_vdom(source, make_links_blue) == {
64-
"tagName": "p",
65-
"children": [
66-
"hello ",
67-
{
68-
"tagName": "a",
69-
"children": ["world"],
70-
"attributes": {"style": {"color": "blue"}},
71-
},
72-
" and ",
73-
{
74-
"tagName": "a",
75-
"children": ["universe"],
76-
"attributes": {"style": {"color": "blue"}},
77-
},
78-
],
79-
}
63+
assert html_to_vdom(source, make_links_blue) == [
64+
{
65+
"tagName": "p",
66+
"children": [
67+
"hello ",
68+
{
69+
"tagName": "a",
70+
"children": ["world"],
71+
"attributes": {"style": {"color": "blue"}},
72+
},
73+
" and ",
74+
{
75+
"tagName": "a",
76+
"children": ["universe"],
77+
"attributes": {"style": {"color": "blue"}},
78+
},
79+
],
80+
}
81+
]

0 commit comments

Comments
 (0)