aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-12-31 00:18:01 +0100
committerbptato <nincsnevem662@gmail.com>2024-12-31 00:18:01 +0100
commite45ceaf646fed961d58a8aef98c125ed50366155 (patch)
treedc13f309099932efac0db8a0d737ac47729f981c /src
parentc39dd8a5c52ae21ce8ccd3ca9674af5fd85d611a (diff)
downloadchawan-e45ceaf646fed961d58a8aef98c125ed50366155.tar.gz
dom: small refactoring + optimization
Diffstat (limited to 'src')
-rw-r--r--src/html/catom.nim8
-rw-r--r--src/html/dom.nim36
2 files changed, 22 insertions, 22 deletions
diff --git a/src/html/catom.nim b/src/html/catom.nim
index 8d1696be..dfceb965 100644
--- a/src/html/catom.nim
+++ b/src/html/catom.nim
@@ -209,6 +209,14 @@ func toLowerAscii*(factory: CAtomFactory; a: CAtom): CAtom =
func equalsIgnoreCase*(factory: CAtomFactory; a, b: CAtom): bool =
return factory.lowerMap[int32(a)] == factory.lowerMap[int32(b)]
+func containsIgnoreCase*(factory: CAtomFactory; aa: openArray[CAtom];
+ a: CAtom): bool =
+ let a = factory.toLowerAscii(a)
+ for it in aa:
+ if a == factory.toLowerAscii(it):
+ return true
+ return false
+
func toAtom*(factory: CAtomFactory; s: string): CAtom =
return factory[].toAtom(s)
diff --git a/src/html/dom.nim b/src/html/dom.nim
index e97292a7..5e0b6534 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -184,7 +184,7 @@ type
factory*: CAtomFactory
charset*: Charset
window* {.jsget: "defaultView".}: Window
- url* {.jsget: "URL".}: URL
+ url* {.jsget: "URL".}: URL # not nil
mode*: QuirksMode
currentScript {.jsget.}: HTMLScriptElement
isxml*: bool
@@ -2311,7 +2311,7 @@ func getElementsByName(document: Document; name: CAtom): NodeList {.jsfunc.} =
childonly = false
)
-func getElementsByTagName0(root: Node; tagName: string): HTMLCollection =
+func getElementsByTagNameImpl(root: Node; tagName: string): HTMLCollection =
if tagName == "*":
return newCollection[HTMLCollection](
root,
@@ -2336,32 +2336,25 @@ func getElementsByTagName0(root: Node; tagName: string): HTMLCollection =
func getElementsByTagName(document: Document; tagName: string): HTMLCollection
{.jsfunc.} =
- return document.getElementsByTagName0(tagName)
+ return document.getElementsByTagNameImpl(tagName)
func getElementsByTagName(element: Element; tagName: string): HTMLCollection
{.jsfunc.} =
- return element.getElementsByTagName0(tagName)
+ return element.getElementsByTagNameImpl(tagName)
-func getElementsByClassName0(node: Node; classNames: string): HTMLCollection =
+func getElementsByClassNameImpl(node: Node; classNames: string):
+ HTMLCollection =
var classAtoms = newSeq[CAtom]()
- let document = node.document
- let isquirks = document.mode == QUIRKS
- if isquirks:
- for class in classNames.split(AsciiWhitespace):
- classAtoms.add(document.toAtomLower(class))
- else:
- for class in classNames.split(AsciiWhitespace):
- classAtoms.add(document.toAtom(class))
+ for class in classNames.split(AsciiWhitespace):
+ classAtoms.add(node.document.toAtom(class))
return newCollection[HTMLCollection](node,
func(node: Node): bool =
if node of Element:
let element = Element(node)
- if isquirks:
- var cl = newSeq[CAtom]()
- for tok in element.classList.toks:
- cl.add(document.factory.toLowerAscii(tok))
+ if element.document.mode == QUIRKS:
+ let factory = element.document.factory
for class in classAtoms:
- if class notin cl:
+ if not factory.containsIgnoreCase(element.classList.toks, class):
return false
else:
for class in classAtoms:
@@ -2374,11 +2367,11 @@ func getElementsByClassName0(node: Node; classNames: string): HTMLCollection =
func getElementsByClassName(document: Document; classNames: string):
HTMLCollection {.jsfunc.} =
- return document.getElementsByClassName0(classNames)
+ return document.getElementsByClassNameImpl(classNames)
func getElementsByClassName(element: Element; classNames: string):
HTMLCollection {.jsfunc.} =
- return element.getElementsByClassName0(classNames)
+ return element.getElementsByClassNameImpl(classNames)
func previousElementSibling*(elem: Element): Element {.jsfget.} =
let p = elem.parentNode
@@ -2606,6 +2599,7 @@ func inputString*(input: HTMLInputElement): string =
else: input.value
func textAreaString*(textarea: HTMLTextAreaElement): string =
+ result = ""
let split = textarea.value.split('\n')
let rows = int(textarea.attrul(satRows).get(1))
for i in 0 ..< rows:
@@ -3194,8 +3188,6 @@ proc baseURL*(document: Document): URL =
href = base.attr(satHref)
if href == "":
return document.url
- if document.url == nil:
- return newURL("about:blank").get #TODO ???
let url = parseURL(href, some(document.url))
if url.isNone:
return document.url