diff options
author | bptato <nincsnevem662@gmail.com> | 2022-09-13 20:44:55 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-09-13 20:44:55 +0200 |
commit | 51d83224320b8bd4e81332802bb62158ae6deec5 (patch) | |
tree | 4bfb320d7960f78a68de857f26d43ff2f59bea57 /src/html/htmlparser.nim | |
parent | 51ea622d58bfca19212fac1800cfb033bb85ec39 (diff) | |
download | chawan-51d83224320b8bd4e81332802bb62158ae6deec5.tar.gz |
More JS bindings
Diffstat (limited to 'src/html/htmlparser.nim')
-rw-r--r-- | src/html/htmlparser.nim | 22 |
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) |