about summary refs log tree commit diff stats
path: root/src/html/chadombuilder.nim
blob: fa3b761d0a735113213327fcd4256f2c7e1eb4dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import std/deques
import std/options
import std/streams

import html/dom
import html/enums
import js/error
import js/fromjs
import js/javascript
import types/url

import chakasu/charset

import chame/htmlparser
import chame/htmltokenizer
import chame/tags

# DOMBuilder implementation for Chawan.

type
  ChaDOMBuilder = ref object of DOMBuilder[Node]
    isFragment: bool

type DOMParser = ref object # JS interface

jsDestructor(DOMParser)

template getDocument(dombuilder: ChaDOMBuilder): Document =
  cast[Document](dombuilder.document)

proc finish(builder: DOMBuilder[Node]) =
  let builder = cast[ChaDOMBuilder](builder)
  let document = builder.getDocument()
  while document.scriptsToExecOnLoad.len > 0:
    #TODO spin event loop
    let script = document.scriptsToExecOnLoad.popFirst()
    script.execute()
  #TODO events

proc restart(builder: DOMBuilder[Node]) =
  let document = newDocument()
  document.contentType = "text/html"
  let oldDocument = cast[Document](builder.document)
  document.url = oldDocument.url
  let window = oldDocument.window
  if window != nil:
    document.window = window
    window.document = document
  builder.document = document

proc parseError(builder: DOMBuilder[Node], message: string) =
  discard

proc setQuirksMode(builder: DOMBuilder[Node], quirksMode: QuirksMode) =
  let builder = cast[ChaDOMBuilder](builder)
  let document = builder.getDocument()
  if not document.parser_cannot_change_the_mode_flag:
    document.mode = quirksMode

proc setCharacterSet(builder: DOMBuilder[Node], charset: Charset) =
  let builder = cast[ChaDOMBuilder](builder)
  let document = builder.getDocument()
  document.charset = charset

proc getTemplateContent(builder: DOMBuilder[Node], handle: Node): Node =
  return HTMLTemplateElement(handle).content

proc getTagType(builder: DOMBuilder[Node], handle: Node): TagType =
  return Element(handle).tagType

proc getParentNode(builder: DOMBuilder[Node], handle: Node): Option[Node] =
  return option(handle.parentNode)

proc getLocalName(builder: DOMBuilder[Node], handle: Node): string =
  return Element(handle).localName

proc getNamespace(builder: DOMBuilder[Node], handle: Node): Namespace =
  return Element(handle).namespace

proc createElement(builder: DOMBuilder[Node], localName: string,
    namespace: Namespace, tagType: TagType,
    attrs: Table[string, string]): Node =
  let builder = cast[ChaDOMBuilder](builder)
  let document = builder.getDocument()
  let element = document.newHTMLElement(localName, namespace,
    tagType = tagType, attrs = attrs)
  if element.isResettable():
    element.resetElement()
  if tagType == TAG_SCRIPT:
    let script = HTMLScriptElement(element)
    script.parserDocument = document
    script.forceAsync = false
    if builder.isFragment:
      script.alreadyStarted = true
      #TODO document.write (?)
  return element

proc createComment(builder: DOMBuilder[Node], text: string): Node =
  let builder = cast[ChaDOMBuilder](builder)
  return builder.getDocument().createComment(text)

proc createDocumentType(builder: DOMBuilder[Node], name, publicId,
    systemId: string): Node =
  let builder = cast[ChaDOMBuilder](builder)
  return builder.getDocument().newDocumentType(name, publicId, systemId)

proc insertBefore(builder: DOMBuilder[Node], parent, child,
    before: Node) =
  discard parent.insertBefore(child, before)

proc insertText(builder: DOMBuilder[Node], parent: Node, text: string,
    before: Node) =
  let builder = cast[ChaDOMBuilder](builder)
  let prevSibling = if before != nil:
    before.previousSibling
  else:
    parent.lastChild
  if prevSibling != nil and prevSibling.nodeType == TEXT_NODE:
    Text(prevSibling).data &= text
  else:
    let text = builder.getDocument().createTextNode(text)
    discard parent.insertBefore(text, before)

proc remove(builder: DOMBuilder[Node], child: Node) =
  child.remove(suppressObservers = true)

proc moveChildren(builder: DOMBuilder[Node], fromNode, toNode: Node) =
  var tomove = fromNode.childList
  for node in tomove:
    node.remove(suppressObservers = true)
  for child in tomove:
    toNode.insert(child, nil)

proc addAttrsIfMissing(builder: DOMBuilder[Node], element: Node,
    attrs: Table[string, string]) =
  let element = Element(element)
  for k, v in attrs:
    if not element.attrb(k):
      element.attr(k, v)

proc setScriptAlreadyStarted(builder: DOMBuilder[Node], script: Node) =
  HTMLScriptElement(script).alreadyStarted = true

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)
      element.setForm(HTMLFormElement(form))
      element.parserInserted = true

proc elementPopped(builder: DOMBuilder[Node], element: Node) =
  let builder = cast[ChaDOMBuilder](builder)
  let document = builder.getDocument()
  let element = Element(element)
  if element.tagType == TAG_TEXTAREA:
    element.resetElement()
  elif element.tagType == TAG_SCRIPT:
    #TODO microtask (maybe it works here too?)
    let script = HTMLScriptElement(element)
    #TODO document.write() (?)
    script.prepare()
    while document.parserBlockingScript != nil:
      let script = document.parserBlockingScript
      document.parserBlockingScript = nil
      #TODO style sheet
      script.execute()

proc newChaDOMBuilder(url: URL, window: Window, isFragment = false):
    ChaDOMBuilder =
  let document = newDocument()
  document.contentType = "text/html"
  document.url = url
  if window != nil:
    document.window = window
    window.document = document
  return ChaDOMBuilder(
    document: document,
    finish: finish,
    restart: restart,
    setQuirksMode: setQuirksMode,
    setCharacterSet: setCharacterSet,
    elementPopped: elementPopped,
    getTemplateContent: getTemplateContent,
    getTagType: getTagType,
    getParentNode: getParentNode,
    getLocalName: getLocalName,
    getNamespace: getNamespace,
    createElement: createElement,
    createComment: createComment,
    createDocumentType: createDocumentType,
    insertBefore: insertBefore,
    insertText: insertText,
    remove: remove,
    moveChildren: moveChildren,
    addAttrsIfMissing: addAttrsIfMissing,
    setScriptAlreadyStarted: setScriptAlreadyStarted,
    associateWithForm: associateWithForm,
    #TODO isSVGIntegrationPoint (SVG support)
    isFragment: isFragment
  )

# https://html.spec.whatwg.org/multipage/parsing.html#parsing-html-fragments
proc parseHTMLFragment*(element: Element, s: string): seq[Node] =
  let url = parseURL("about:blank").get
  let builder = newChaDOMBuilder(url, nil)
  builder.isFragment = true
  let document = Document(builder.document)
  document.mode = element.document.mode
  let state = case element.tagType
  of TAG_TITLE, TAG_TEXTAREA: RCDATA
  of TAG_STYLE, TAG_XMP, TAG_IFRAME, TAG_NOEMBED, TAG_NOFRAMES: RAWTEXT
  of TAG_SCRIPT: SCRIPT_DATA
  of TAG_NOSCRIPT:
    if element.document != nil and element.document.scriptingEnabled:
      RAWTEXT
    else:
      DATA
  of TAG_PLAINTEXT:
    PLAINTEXT
  else: DATA
  let root = document.newHTMLElement(TAG_HTML)
  document.append(root)
  let opts = HTML5ParserOpts[Node](
    isIframeSrcdoc: false, #TODO?
    scripting: false,
    canReinterpret: false,
    charsets: @[CHARSET_UTF_8],
    ctx: some(Node(element)),
    initialTokenizerState: state,
    openElementsInit: @[Node(root)],
    pushInTemplate: element.tagType == TAG_TEMPLATE
  )
  let inputStream = newStringStream(s)
  parseHTML(inputStream, builder, opts)
  return root.childList

proc parseHTML*(inputStream: Stream, window: Window, url: URL,
    charsets: seq[Charset] = @[], canReinterpret = true): Document =
  let builder = newChaDOMBuilder(url, window)
  let opts = HTML5ParserOpts[Node](
    isIframeSrcdoc: false, #TODO?
    scripting: window != nil and window.settings.scripting,
    canReinterpret: canReinterpret,
    charsets: charsets
  )
  parseHTML(inputStream, builder, opts)
  return Document(builder.document)

proc newDOMParser(): DOMParser {.jsctor.} =
  return DOMParser()

proc parseFromString(ctx: JSContext, parser: DOMParser, str, t: string):
    JSResult[Document] {.jsfunc.} =
  case t
  of "text/html":
    let global = JS_GetGlobalObject(ctx)
    let window = if ctx.hasClass(Window):
      fromJS[Window](ctx, global).get(nil)
    else:
      Window(nil)
    JS_FreeValue(ctx, global)
    let url = if window != nil and window.document != nil:
      window.document.url
    else:
      newURL("about:blank").get
    let res = parseHTML(newStringStream(str), Window(nil), url)
    return ok(res)
  of "text/xml", "application/xml", "application/xhtml+xml", "image/svg+xml":
    return err(newInternalError("XML parsing is not supported yet"))
  else:
    return err(newTypeError("Invalid mime type"))

proc addHTMLModule*(ctx: JSContext) =
  ctx.registerType(DOMParser)