about summary refs log tree commit diff stats
path: root/src/html/htmlparser.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-09-13 20:44:55 +0200
committerbptato <nincsnevem662@gmail.com>2022-09-13 20:44:55 +0200
commit51d83224320b8bd4e81332802bb62158ae6deec5 (patch)
tree4bfb320d7960f78a68de857f26d43ff2f59bea57 /src/html/htmlparser.nim
parent51ea622d58bfca19212fac1800cfb033bb85ec39 (diff)
downloadchawan-51d83224320b8bd4e81332802bb62158ae6deec5.tar.gz
More JS bindings
Diffstat (limited to 'src/html/htmlparser.nim')
-rw-r--r--src/html/htmlparser.nim22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/html/htmlparser.nim b/src/html/htmlparser.nim
index a8ea5458..f28cd300 100644
--- a/src/html/htmlparser.nim
+++ b/src/html/htmlparser.nim
@@ -6,13 +6,16 @@ import strformat
 import tables
 import unicode
 
-import utils/twtstr
+import css/sheet
 import html/dom
 import html/tags
 import html/htmltokenizer
-import css/sheet
+import js/javascript
+import utils/twtstr
 
 type
+  DOMParser = ref object # JS interface
+
   HTML5Parser = object
     case fragment: bool
     of true: ctx: Element
@@ -2085,3 +2088,18 @@ proc parseHTML5*(inputStream: Stream): Document =
   parser.document = newDocument()
   parser.tokenizer = inputStream.newTokenizer()
   return parser.constructTree()
+
+proc newDOMParser*(): DOMParser {.jsctor.} =
+  new(result)
+
+proc parseFromString*(parser: DOMParser, str: string, t: string): Document {.jserr, jsfunc.} =
+  case t
+  of "text/html":
+    return parseHTML5(newStringStream(str))
+  of "text/xml", "application/xml", "application/xhtml+xml", "image/svg+xml":
+    JS_THROW JS_InternalError, "XML parsing is not supported yet"
+  else:
+    JS_THROW JS_TypeError, "Invalid mime type"
+
+proc addHTMLModule*(ctx: JSContext) =
+  ctx.registerType(DOMParser)