about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-07-10 18:51:20 +0200
committerbptato <nincsnevem662@gmail.com>2023-07-10 18:51:20 +0200
commit882ad61e765529fca79ebf79dcaa079e6bc970fb (patch)
treeb7e86ac1336be324286dcaafc4a19ffb605b0379 /src/html
parent8d247fd79424f1e4c5ac6b3fb6642363fd186252 (diff)
downloadchawan-882ad61e765529fca79ebf79dcaa079e6bc970fb.tar.gz
htmlparser: fix form association regression
associateWithForm was checking whether the element was in the same
tree as the form, however associateWithForm is called when element
is has not been inserted into the tree yet. As a fix, pass the
intended parent and perform the check on that.
Diffstat (limited to 'src/html')
-rw-r--r--src/html/chadombuilder.nim5
-rw-r--r--src/html/htmlparser.nim16
2 files changed, 13 insertions, 8 deletions
diff --git a/src/html/chadombuilder.nim b/src/html/chadombuilder.nim
index acddb287..2ddd067b 100644
--- a/src/html/chadombuilder.nim
+++ b/src/html/chadombuilder.nim
@@ -116,8 +116,9 @@ proc addAttrsIfMissing(builder: DOMBuilder[Node], element: Node,
 proc setScriptAlreadyStarted(builder: DOMBuilder[Node], script: Node) =
   HTMLScriptElement(script).alreadyStarted = true
 
-proc associateWithForm(builder: DOMBuilder[Node], element, form: Node) =
-  if form.inSameTree(element):
+proc associateWithForm(builder: DOMBuilder[Node], element, form,
+    intendedParent: Node) =
+  if form.inSameTree(intendedParent):
     #TODO remove following test eventually
     if Element(element).tagType in SupportedFormAssociatedElements:
       let element = FormAssociatedElement(element)
diff --git a/src/html/htmlparser.nim b/src/html/htmlparser.nim
index 906daa76..202d3860 100644
--- a/src/html/htmlparser.nim
+++ b/src/html/htmlparser.nim
@@ -231,10 +231,13 @@ type
       ## to true.
 
   DOMBuilderAssociateWithForm*[Handle] =
-    proc(builder: DOMBuilder[Handle], element, form: Handle) {.nimcall.}
+    proc(builder: DOMBuilder[Handle], element, form, intendedParent: Handle)
+        {.nimcall.}
       ## Called after createElement. Attempts to set form for form-associated
-      ## elements. Note: the DOM builder is responsible for checking whether
-      ## the two nodes are in the same tree.
+      ## elements.
+      ##
+      ## Note: the DOM builder is responsible for checking whether the
+      ## intended parent and the form element are in the same tree.
 
   DOMBuilderIsSVGIntegrationPoint*[Handle] =
     proc(builder: DOMBuilder[Handle], element: Handle): bool {.nimcall.}
@@ -360,10 +363,11 @@ proc setScriptAlreadyStarted[Handle](parser: HTML5Parser, script: Handle) =
   if dombuilder.setScriptAlreadyStarted != nil:
     dombuilder.setScriptAlreadyStarted(dombuilder, script)
 
-proc associateWithForm[Handle](parser: HTML5Parser, element, form: Handle) =
+proc associateWithForm[Handle](parser: HTML5Parser, element, form,
+    intendedParent: Handle) =
   let dombuilder = parser.dombuilder
   if dombuilder.associateWithForm != nil:
-    dombuilder.associateWithForm(dombuilder, element, form)
+    dombuilder.associateWithForm(dombuilder, element, form, intendedParent)
 
 func isSVGIntegrationPoint[Handle](parser: HTML5Parser,
     element: Handle): bool =
@@ -575,7 +579,7 @@ func createElement[Handle](parser: HTML5Parser[Handle], token: Token,
   if token.tagtype in FormAssociatedElements and parser.form.isSome and
       not parser.hasElement(TAG_TEMPLATE) and
       (token.tagtype notin ListedElements or "form" notin token.attrs):
-    parser.associateWithForm(element, parser.form.get)
+    parser.associateWithForm(element, parser.form.get, intendedParent)
   return element
 
 proc pushElement[Handle](parser: var HTML5Parser[Handle], node: Handle) =