diff options
author | bptato <nincsnevem662@gmail.com> | 2022-07-12 18:12:55 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-07-12 18:12:55 +0200 |
commit | 2f0ea08df1e6884da51ea510cc352395e853cfc0 (patch) | |
tree | 214aef273f772a8ad657039097cb7c9134ba9f7d /src/html | |
parent | 1abd3aadf0c999c6e26ba4e7910b5abe3510c6c2 (diff) | |
download | chawan-2f0ea08df1e6884da51ea510cc352395e853cfc0.tar.gz |
Fix a tokenizer bug, clean up some warnings
Diffstat (limited to 'src/html')
-rw-r--r-- | src/html/dom.nim | 25 | ||||
-rw-r--r-- | src/html/htmlparser.nim | 13 | ||||
-rw-r--r-- | src/html/htmltokenizer.nim | 2 |
3 files changed, 30 insertions, 10 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index 921c8702..e412a366 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -1,7 +1,6 @@ import tables import options import streams -import strformat import strutils import css/values @@ -317,7 +316,7 @@ func branch*(node: Node): seq[Node] = for node in node.branch: result.add(node) -func firstChild(node: Node): Node = +func firstChild*(node: Node): Node = if node.childNodes.len == 0: return nil return node.childNodes[0] @@ -727,9 +726,9 @@ proc remove*(node: Node) = #TODO not surpress observers => queue tree mutation record proc adopt(document: Document, node: Node) = - let oldDocument = node.document if node.parentNode != nil: remove(node) + #TODO shadow root proc applyChildInsert(parent, child: Node, index: int) = if parent.rootNode != nil: @@ -760,8 +759,8 @@ proc insert*(parent, node, before: Node) = if before != nil: #TODO live ranges discard - let previousSibling = if before == nil: parent.lastChild - else: before.previousSibling + #let previousSibling = if before == nil: parent.lastChild + #else: before.previousSibling for node in nodes: parent.document.adopt(node) if before == nil: @@ -835,7 +834,21 @@ proc appendAttribute*(element: Element, k, v: string) = for class in classes: if class != "" and class notin element.classList: element.classList.add(class) - else: discard + if element.tagType == TAG_INPUT: + case k + of "value": HTMLInputElement(element).value = v + of "type": HTMLInputElement(element).inputType = inputType(v) + of "size": + var i = 20 + var fail = v.len > 0 + for c in v: + if not c.isDigit: + fail = true + break + if not fail: + i = parseInt(v) + HTMLInputElement(element).size = i + of "checked": HTMLInputElement(element).checked = true element.attributes[k] = v proc setForm*(element: Element, form: HTMLFormElement) = diff --git a/src/html/htmlparser.nim b/src/html/htmlparser.nim index 0b2a99be..a9c9b1fd 100644 --- a/src/html/htmlparser.nim +++ b/src/html/htmlparser.nim @@ -54,7 +54,6 @@ proc resetInsertionMode(parser: var HTML5Parser) = node = parser.ctx if node.tagType == TAG_SELECT: if not last: - var ancestor = node for j in countdown(parser.openElements.high, 1): let ancestor = parser.openElements[j] case ancestor.tagType @@ -525,6 +524,7 @@ macro match(token: Token, body: typed): untyped = type OfBranchStore = object ofBranches: seq[(seq[NimNode], NimNode)] defaultBranch: NimNode + painted: bool # Stores 'of' branches var ofBranches: array[TokenType, OfBranchStore] @@ -552,12 +552,15 @@ macro match(token: Token, body: typed): untyped = case pattern.kind of nnkSym: # simple symbols; we assume these are the enums ofBranches[tokenTypes[pattern.strVal]].defaultBranch = action + ofBranches[tokenTypes[pattern.strVal]].painted = true of nnkCharLit: ofBranches[CHARACTER_ASCII].ofBranches.add((@[pattern], action)) + ofBranches[CHARACTER_ASCII].painted = true of nnkCurly: case pattern[0].kind of nnkCharLit: ofBranches[CHARACTER_ASCII].ofBranches.add((@[pattern], action)) + ofBranches[CHARACTER_ASCII].painted = true else: error fmt"Unsupported curly of kind {pattern[0].kind}" of nnkStrLit: var tempTokenizer = newTokenizer(newStringStream(pattern.strVal)) @@ -570,9 +573,11 @@ macro match(token: Token, body: typed): untyped = if ofBranches[token.t].ofBranches[i][1] == action: found = true ofBranches[token.t].ofBranches[i][0].add((quote do: TagType(`tt`))) + ofBranches[token.t].painted = true break if not found: ofBranches[token.t].ofBranches.add((@[(quote do: TagType(`tt`))], action)) + ofBranches[token.t].painted = true else: error fmt"{pattern.strVal}: Unsupported token {token} of kind {token.t}" break of nnkDiscardStmt: @@ -628,7 +633,10 @@ macro match(token: Token, body: typed): untyped = else: discard - mainCase.add(newNimNode(nnkElse).add(quote do: discard)) + for t in TokenType: + if not ofBranches[t].painted: + mainCase.add(newNimNode(nnkElse).add(quote do: discard)) + break var stmts = newStmtList().add(mainCase) for stmt in defaultBranch: @@ -1358,7 +1366,6 @@ proc processInHTMLContent(parser: var HTML5Parser, token: Token, insertionMode = ) "</script>" => (block: #TODO microtask - let script = parser.currentNode pop_current_node parser.insertionMode = parser.oldInsertionMode #TODO document.write() ? diff --git a/src/html/htmltokenizer.nim b/src/html/htmltokenizer.nim index 29680d19..8738323f 100644 --- a/src/html/htmltokenizer.nim +++ b/src/html/htmltokenizer.nim @@ -801,7 +801,7 @@ iterator tokenize*(tokenizer: var Tokenizer): Token = of '=': switch_state BEFORE_ATTRIBUTE_VALUE of '>': switch_state DATA - emit '>' + emit_tok of eof: parse_error eof_in_tag emit_eof |