about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-11-22 19:08:13 +0100
committerbptato <nincsnevem662@gmail.com>2024-11-22 20:06:25 +0100
commit7d549f10ba356486201645ae11fc4796ef5a4441 (patch)
tree4d5f0f12ee42597f7803907b519100e6793559f7
parent309852b46d93bc33e5ddaa1c5c49d071f71f0900 (diff)
downloadchawan-7d549f10ba356486201645ae11fc4796ef5a4441.tar.gz
dom: add document named property getter, update monoucha
m---------lib/monoucha10
-rw-r--r--src/html/dom.nim47
-rw-r--r--src/version.nim2
-rw-r--r--test/js/document.html33
4 files changed, 71 insertions, 21 deletions
diff --git a/lib/monoucha b/lib/monoucha
-Subproject bf8665e22f491c2e52390ef04f6af4b9464b0d4
+Subproject 1f3eb04e60132afb76e6197228853b1960fd09c
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 99cced5d..b7ecffe0 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -1647,18 +1647,19 @@ proc getter(ctx: JSContext; this: HTMLCollection; atom: JSAtom): JSValue
     return ctx.toJS(this.namedItem(s)).uninitIfNull()
   return JS_UNINITIALIZED
 
-func names(ctx: JSContext; collection: HTMLCollection): JSPropertyEnumList
+proc names(ctx: JSContext; collection: HTMLCollection): JSPropertyEnumList
     {.jspropnames.} =
   let L = collection.length
   var list = newJSPropertyEnumList(ctx, L)
   var ids = initOrderedSet[CAtom]()
+  let empty = ctx.toAtom("")
   for u in 0 ..< L:
     list.add(u)
-    let elem = collection.item(u)
-    if elem.id != CAtomNull:
-      ids.incl(elem.id)
-    if elem.namespace == Namespace.HTML:
-      ids.incl(elem.name)
+    let element = collection.item(u)
+    if element.id != CAtomNull and element.id != empty:
+      ids.incl(element.id)
+    if element.namespace == Namespace.HTML:
+      ids.incl(element.name)
   for id in ids:
     list.add(collection.root.document.toStr(id))
   return list
@@ -2353,6 +2354,36 @@ func nextElementSibling*(elem: Element): Element {.jsfget.} =
 func documentElement*(document: Document): Element {.jsfget.} =
   return document.firstElementChild()
 
+proc names(ctx: JSContext; document: Document): JSPropertyEnumList
+    {.jspropnames.} =
+  var list = newJSPropertyEnumList(ctx, 0)
+  #TODO I'm not quite sure why location isn't added, so I'll add it
+  # manually for now.
+  list.add("location")
+  let empty = ctx.toAtom("")
+  #TODO exposed embed, exposed object
+  for child in document.elements({TAG_FORM, TAG_IFRAME, TAG_IMG}):
+    if child.name != CAtomNull and child.name != empty:
+      if child.tagType == TAG_IMG and child.id != CAtomNull and
+          child.id != empty:
+        list.add(ctx.toStr(child.id))
+      list.add(ctx.toStr(child.name))
+  return list
+
+proc getter(ctx: JSContext; document: Document; s: string): JSValue
+    {.jsgetownprop.} =
+  if s.len != 0:
+    let id = ctx.toAtom(s)
+    let empty = ctx.toAtom("")
+    #TODO exposed embed, exposed object
+    for child in document.elements({TAG_FORM, TAG_IFRAME, TAG_IMG}):
+      if child.tagType == TAG_IMG and child.id == id and
+          child.name != CAtomNull and child.name != empty:
+        return ctx.toJS(child)
+      if child.name == id:
+        return ctx.toJS(child)
+  return JS_UNINITIALIZED
+
 func attr*(element: Element; s: CAtom): string =
   let i = element.findAttr(s)
   if i != -1:
@@ -3054,10 +3085,10 @@ proc newDocument*(factory: CAtomFactory): Document =
   let document = Document(
     url: newURL("about:blank").get,
     index: -1,
-    factory: factory
+    factory: factory,
+    contentType: "application/xml"
   )
   document.implementation = DOMImplementation(document: document)
-  document.contentType = "application/xml"
   return document
 
 proc newDocument(ctx: JSContext): Document {.jsctor.} =
diff --git a/src/version.nim b/src/version.nim
index 2a0a56e6..869974ae 100644
--- a/src/version.nim
+++ b/src/version.nim
@@ -29,4 +29,4 @@ tryImport monoucha/version, "monoucha"
 static:
   checkVersion("chagashi", 0, 6, 0)
   checkVersion("chame", 1, 0, 2)
-  checkVersion("monoucha", 0, 7, 0)
+  checkVersion("monoucha", 0, 7, 1)
diff --git a/test/js/document.html b/test/js/document.html
index 26dfa528..1020283c 100644
--- a/test/js/document.html
+++ b/test/js/document.html
@@ -1,11 +1,30 @@
 <!doctype html>
-<title>Window test</title>
+<title>Document test</title>
+<body>
 <div id="abc">Fail</div>
+<img id=img2 name=img3>
+<img id=toString name=a>
+<img id=img4 name=>
+<form id=myform name=asdf></form>
+<script src=asserts.js></script>
 <script>
-(function() {
-	if (document.toString() !== "[object Document]") return;
-	if (document !== window.document) return;
-	if (document.implementation !== document.implementation) return;
-	document.getElementById("abc").textContent = "Success";
-})()
+assertEquals(document.toString, document.getElementById("toString"));
+document.toString.remove();
+document.testtest = "hi";
+assertEquals(Object.getOwnPropertyNames(document).toString(), "testtest,location,img2,img3,asdf");
+// the spec doesn't have HTMLDocument, but browsers do.
+//TODO: we'll probably have to implement HTMLDocument ourselves too
+// nonetheless.
+if (!window.HTMLDocument)
+	assertEquals(document.toString(), "[object Document]");
+assertEquals(document, window.document);
+assertEquals(document.implementation, document.implementation);
+assertEquals(document.img2, document.getElementById("img2"));
+assertEquals(document.img3, document.getElementById("img2"));
+assertEquals(document.asdf, document.getElementById("myform"));
+assertEquals(document.img4, undefined);
+document.getElementById("img4").remove();
+document.asdf.remove();
+document.img2.remove();
+document.getElementById("abc").textContent = "Success";
 </script>