From 51d83224320b8bd4e81332802bb62158ae6deec5 Mon Sep 17 00:00:00 2001 From: bptato Date: Tue, 13 Sep 2022 20:44:55 +0200 Subject: More JS bindings --- src/html/htmlparser.nim | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/html/htmlparser.nim') 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) -- cgit 1.4.1-2-gfad0