aboutsummaryrefslogtreecommitdiffstats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-12-18 16:03:24 +0100
committerbptato <nincsnevem662@gmail.com>2021-12-18 16:03:47 +0100
commiteddf377277f85b172df453b5c3d5763d3a4467c6 (patch)
treeceaa7b7faef7670704a11868659b87bedfae8d4c /src/html
parenta125464839927fd04f761c530e90888e340758ef (diff)
downloadchawan-eddf377277f85b172df453b5c3d5763d3a4467c6.tar.gz
Refactor selector code, optimize style tags
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim11
-rw-r--r--src/html/parser.nim30
2 files changed, 31 insertions, 10 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 02258296..f9a4cfea 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -4,6 +4,7 @@ import options
import strutils
import css/values
+import css/selparser
import html/tags
type
@@ -112,6 +113,14 @@ type
value*: Option[int]
ordinalvalue*: int
+ HTMLStyleElement* = ref object of HTMLElement
+ stylesheet*: ParsedStylesheet
+
+iterator textNodes*(node: Node): Text {.inline.} =
+ for node in node.childNodes:
+ if node.nodeType == TEXT_NODE:
+ yield Text(node)
+
func firstChild(node: Node): Node =
if node.childNodes.len == 0:
return nil
@@ -275,6 +284,8 @@ func newHtmlElement*(tagType: TagType): HTMLElement =
HTMLMenuElement(result).ordinalcounter = 1
of TAG_LI:
result = new(HTMLLIElement)
+ of TAG_STYLE:
+ result = new(HTMLStyleElement)
else:
result = new(HTMLElement)
diff --git a/src/html/parser.nim b/src/html/parser.nim
index 153df1c5..c3e12300 100644
--- a/src/html/parser.nim
+++ b/src/html/parser.nim
@@ -10,6 +10,7 @@ import utils/radixtree
import html/dom
import html/entity
import html/tags
+import css/selparser
type
HTMLParseState = object
@@ -306,18 +307,27 @@ proc processDocumentStartElement(state: var HTMLParseState, element: Element, ta
else: discard
proc processDocumentEndElement(state: var HTMLParseState, tag: DOMParsedTag) =
- if tag.tagid in VoidTagTypes:
- return
- if tag.tagid == TAG_HEAD:
- processDocumentBody(state)
- return
- if tag.tagid == TAG_BODY:
- return
- if state.elementNode.nodeType == ELEMENT_NODE and tag.tagid != state.elementNode.tagType:
+ if tag.tagid != state.elementNode.tagType:
if state.elementNode.tagType in SelfClosingTagTypes:
processDocumentEndNode(state)
-
- processDocumentEndNode(state)
+ processDocumentEndNode(state)
+ else:
+ case tag.tagid
+ of VoidTagTypes:
+ return
+ of TAG_HEAD:
+ processDocumentBody(state)
+ return
+ of TAG_BODY:
+ return
+ of TAG_STYLE:
+ let style = HTMLStyleElement(state.elementNode)
+ var str = ""
+ for child in style.textNodes:
+ str &= child.data
+ style.stylesheet = newStringStream(str).parseStylesheet()
+ else: discard
+ processDocumentEndNode(state)
proc processDocumentTag(state: var HTMLParseState, tag: DOMParsedTag) =
if state.in_script: