Convenient class to represent the head node of an HTML tree.
Children of a "head" node are limited to the ones of HEAD_TAGS list.
Convenient class to represent the head node of an HTML tree.
Children of a "head" node are limited to the ones of HEAD_TAGS list.
Create the head node.
def __init__(self, parent):
"""Create the head node."""
super(HTMLHeadNode, self).__init__(parent, 'head', 'head')
Append a child node.
def append_child(self, node) -> None:
"""Append a child node.
:param node: (Node)
:raise: NodeChildTagError: if invalid child tag (not in HEAD_TAGS list)
"""
if node.tag not in HEAD_TAGS:
raise NodeChildTagError(node.tag)
HTMLNode.append_child(self, node)
Insert a child node at the given index.
def insert_child(self, pos, node) -> None:
"""Insert a child node at the given index.
:param pos: (int) Index position
:param node: (Node)
"""
if node.tag not in HEAD_TAGS:
raise NodeChildTagError(node.tag)
HTMLNode.insert_child(self, pos, node)
Set the title to the header.
def title(self, title) -> None:
"""Set the title to the header.
:param title: (str) The page title (expected short!)
"""
for child in self._children:
if child.identifier == 'title':
child.set_value(title)
break
Append a new meta tag to the header.
def meta(self, metadict) -> None:
"""Append a new meta tag to the header.
:param metadict: (dict)
"""
if isinstance(metadict, dict) is False:
raise TypeError('Expected a dict.')
child_node = EmptyNode(self.identifier, None, 'meta', attributes=metadict)
self._children.append(child_node)
Add a link tag to the header.
def link(self, rel: str, href: str, link_type: str=None) -> None:
"""Add a link tag to the header.
:param rel: (str)
:param href: (str) Path and/or name of the link reference
:param link_type: (str) Mimetype of the link file
"""
d = dict()
d['rel'] = rel
if len(href) > 0 and href[0].isalpha():
d['href'] = os.sep + href
else:
d['href'] = href
if link_type is not None:
d['type'] = link_type
child_node = EmptyNode(self.identifier, None, 'link', attributes=d)
self._children.append(child_node)
Add a meta tag to the header.
def script(self, src, script_type) -> None:
"""Add a meta tag to the header.
:param src: (str) Script source file or Script content
:param script_type: (str) Script type or None if script content
"""
if script_type is not None:
d = dict()
d['type'] = script_type
if len(src) > 0 and src[0].isalpha():
d['src'] = os.sep + src
else:
d['src'] = src
child_node = HTMLNode(self.identifier, None, 'script', attributes=d)
self._children.append(child_node)
else:
child_node = HTMLNode(self.identifier, None, 'script', value=str(src))
self._children.append(child_node)
Append css style content.
def css(self, css_content) -> None:
"""Append css style content.
:param script_content: (str) CSS content
"""
child_node = HTMLNode(self.identifier, None, 'style', value=str(css_content))
self._children.append(child_node)