diff options
author | bptato <nincsnevem662@gmail.com> | 2021-11-12 16:55:53 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2021-11-12 16:55:53 +0100 |
commit | 7108754641cefb3f01af8e29aae11695d94e980c (patch) | |
tree | c191a33745da13d31131fa8186bcfbe58ff69488 /src/html | |
parent | fcd3a5b204e15fdfc739fd04975977d288e892e0 (diff) | |
download | chawan-7108754641cefb3f01af8e29aae11695d94e980c.tar.gz |
Colors, italic, bold, read from pipe
Diffstat (limited to 'src/html')
-rw-r--r-- | src/html/dom.nim | 30 | ||||
-rw-r--r-- | src/html/parser.nim | 11 |
2 files changed, 33 insertions, 8 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index 92d8cf1b..4e296b0c 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -443,16 +443,33 @@ proc applyRules*(document: Document, rules: CSSStylesheet): seq[tuple[e:Element, else: elem.applyProperty(decl, pseudo) - for child in elem.children: + var i = elem.children.len - 1 + while i >= 0: + let child = elem.children[i] stack.add(child) + dec i proc applyAuthorRules*(document: Document): seq[tuple[e:Element,d:CSSDeclaration]] = var stack: seq[Element] var embedded_rules: seq[ParsedStylesheet] - stack.add(document.root) + stack.add(document.head) + var rules_head = "" + + for child in document.head.children: + if child.tagType == TAG_STYLE: + for ct in child.childNodes: + if ct.nodeType == TEXT_NODE: + rules_head &= Text(ct).data + + + stack.setLen(0) + + stack.add(document.body) - #let parsed = rules.value.map((x) => (sels: parseSelectors(x.prelude), oblock: x.oblock)) + if rules_head.len > 0: + let parsed = parseCSS(newStringStream(rules_head)).value.map((x) => (sels: parseSelectors(x.prelude), oblock: x.oblock)) + embedded_rules.add(parsed) while stack.len > 0: let elem = stack.pop() @@ -481,8 +498,11 @@ proc applyAuthorRules*(document: Document): seq[tuple[e:Element,d:CSSDeclaration else: elem.applyProperty(decl, pseudo) - for child in elem.children: + var i = elem.children.len - 1 + while i >= 0: + let child = elem.children[i] stack.add(child) + dec i if rules_local.len > 0: discard embedded_rules.pop() @@ -490,6 +510,8 @@ proc applyAuthorRules*(document: Document): seq[tuple[e:Element,d:CSSDeclaration proc applyStylesheets*(document: Document) = let important_ua = document.applyRules(stylesheet) let important_author = document.applyAuthorRules() + for rule in important_author: + rule.e.applyProperty(rule.d) for rule in important_ua: rule.e.applyProperty(rule.d) diff --git a/src/html/parser.nim b/src/html/parser.nim index eed5baa7..68295dbe 100644 --- a/src/html/parser.nim +++ b/src/html/parser.nim @@ -225,8 +225,6 @@ proc processDocumentEndNode(state: var HTMLParseState) = state.elementNode = state.elementNode.parentElement proc processDocumentText(state: var HTMLParseState) = - if state.textNode != nil and state.textNode.data.len > 0: - processDocumentBody(state) if state.textNode == nil: state.textNode = newText() processDocumentAddNode(state, state.textNode) @@ -264,11 +262,16 @@ proc processDocumentStartElement(state: var HTMLParseState, element: Element, ta add = false of TAG_HEAD: add = false + state.in_body = false + if state.elementNode.ownerDocument != nil: + state.elementNode = state.elementNode.ownerDocument.head of TAG_BODY: add = false - processDocumentBody(state) else: discard + if not state.in_body and not (element.tagType in HeadTagTypes): + processDocumentBody(state) + if state.elementNode.nodeType == ELEMENT_NODE: case element.tagType of SelfClosingTagTypes: @@ -302,7 +305,7 @@ proc processDocumentEndElement(state: var HTMLParseState, tag: DOMParsedTag) = if tag.tagid in VoidTagTypes: return if tag.tagid == TAG_HEAD: - state.in_body = true + processDocumentBody(state) return if tag.tagid == TAG_BODY: return |