mirror of
https://github.com/django-components/django-components.git
synced 2025-09-25 23:19:11 +00:00
refactor: use component IDs as keys for slot fill llokup
This commit is contained in:
parent
c1369ab2c7
commit
ce5b5c40d8
5 changed files with 210 additions and 115 deletions
38
src/django_components/node.py
Normal file
38
src/django_components/node.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from typing import Callable
|
||||
|
||||
from django.template.base import Node, NodeList, TextNode
|
||||
from django.template.defaulttags import CommentNode
|
||||
|
||||
|
||||
def nodelist_has_content(nodelist: NodeList) -> bool:
|
||||
for node in nodelist:
|
||||
if isinstance(node, TextNode) and node.s.isspace():
|
||||
pass
|
||||
elif isinstance(node, CommentNode):
|
||||
pass
|
||||
else:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def walk_nodelist(nodes: NodeList, callback: Callable[[Node], None]) -> None:
|
||||
"""Recursively walk a NodeList, calling `callback` for each Node."""
|
||||
node_queue = [*nodes]
|
||||
while len(node_queue):
|
||||
node: Node = node_queue.pop()
|
||||
callback(node)
|
||||
node_queue.extend(get_node_children(node))
|
||||
|
||||
|
||||
def get_node_children(node: Node) -> NodeList:
|
||||
"""
|
||||
Get child Nodes from Node's nodelist atribute.
|
||||
|
||||
This function is taken from `get_nodes_by_type` method of `django.template.base.Node`.
|
||||
"""
|
||||
nodes = NodeList()
|
||||
for attr in node.child_nodelists:
|
||||
nodelist = getattr(node, attr, [])
|
||||
if nodelist:
|
||||
nodes.extend(nodelist)
|
||||
return nodes
|
Loading…
Add table
Add a link
Reference in a new issue