-
Notifications
You must be signed in to change notification settings - Fork 14
/
tree-help.lisp
357 lines (314 loc) · 13.7 KB
/
tree-help.lisp
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
;;;; HTML5 parser for Common Lisp
;;;;
;;;; Copyright (C) 2012 Thomas Bakketun <[email protected]>
;;;; Copyright (C) 2012 Asgeir Bjørlykke <[email protected]>
;;;; Copyright (C) 2012 Mathias Hellevang
;;;; Copyright (C) 2012 Stian Sletner <[email protected]>
;;;;
;;;; This library is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published
;;;; by the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this library. If not, see <http://www.gnu.org/licenses/>.
(in-package :html5-parser)
(defmacro pop-end (place)
"Pop from the end of list"
(let ((old-list (gensym)))
`(let ((,old-list ,place))
(prog1 (car (last ,old-list))
(setf ,place (butlast ,old-list))))))
(defmacro push-end (object place)
"Push to the end of list"
`(progn
;(format t "~&push ~S to ~S" ',object ',place)
(setf ,place (append ,place (list ,object)))))
(defvar *parser*)
(defun document* ()
(slot-value *parser* 'document))
(defun node-clone* (node)
(ecase (node-type node)
(:document
(make-document))
(:document-fragment
(make-fragment (document*)))
(:document-type
(make-doctype (document*)
(node-name node)
(node-public-id node)
(node-system-id node)))
(:comment
(make-comment (document*) (node-value node)))
(:text
(make-text-node (document*) (node-value node)))
(:element
(let ((clone (make-element (document*) (node-name node) (node-namespace node))))
(element-map-attributes*
(lambda (name namespace value)
(setf (element-attribute clone name namespace) value))
node)
clone))))
(defun node-name-tuple (node)
(cons (or (node-namespace node)
(find-namespace "html"))
(node-name node)))
(defun node-name-tuple-values (node)
(values (or (node-namespace node)
(find-namespace "html"))
(node-name node)))
(defun node-has-content (node)
(not (null (node-first-child node))))
(defun node-attributes= (node1 node2)
(labels ((has-all-attributes-of (node1 node2)
(element-map-attributes*
(lambda (name namespace value)
(unless (equal value
(element-attribute node2 name namespace))
(return-from has-all-attributes-of nil)))
node1)
t))
(and (has-all-attributes-of node1 node2)
(has-all-attributes-of node2 node1))))
(defun node-append-child* (node child)
(let ((last-child (node-last-child node)))
(if (and (eql :text (node-type child))
last-child
(eql :text (node-type last-child)))
(nconcatf (node-value last-child)
(node-value child))
(node-append-child node child))))
(defun node-insert-before* (node child insert-before)
(when (eql :text (node-type child))
(let ((prev-child (node-previous-sibling insert-before)))
(when (and prev-child
(eql :text (node-type prev-child)))
(node-remove-child node prev-child)
(setf child (make-text-node
(document*)
(concatenate 'string
(node-value prev-child)
(node-value child)))))))
(node-insert-before node child insert-before))
(defun node-reparent-children (node new-parent)
(element-map-children (lambda (child)
(node-append-child new-parent child))
node))
(defun node-insert-text (node data &optional insert-before)
(if insert-before
(node-insert-before* node (make-text-node (document*) data) insert-before)
(node-append-child* node (make-text-node (document*) data))))
(defun last-open-element ()
(with-slots (open-elements) *parser*
(car (last open-elements))))
(defun create-element (token)
"Create an element but don't insert it anywhere"
(with-slots (html-namespace) *parser*
(let ((element (make-element (document*)
(getf token :name)
(or (getf token :namespace)
html-namespace))))
(loop for (name . value) in (getf token :data)
do (if (consp name)
(setf (element-attribute element (second name) (third name)) value)
(setf (element-attribute element name) value)))
element)))
(defun insert-root (token)
(with-slots (open-elements) *parser*
(let ((element (create-element token)))
(assert element)
(push-end element open-elements)
(node-append-child (document*) element))))
(defun insert-doctype (token)
(node-append-child (document*)
(make-doctype (document*)
(getf token :name)
(getf token :public-id)
(getf token :system-id))))
(defun insert-comment (token &optional parent)
(with-slots (open-elements) *parser*
(unless parent
(setf parent (car (last open-elements))))
(node-append-child parent (make-comment (document*) (getf token :data)))))
(defun insert-element-normal (token)
(with-slots (open-elements) *parser*
(let ((element (create-element token)))
(node-append-child (last-open-element) element)
(push-end element open-elements)
element)))
(defun insert-element-table (token)
(with-slots (open-elements) *parser*
(if (not (member (node-name (last-open-element))
+table-insert-mode-elements+ :test #'string=))
(insert-element-normal token)
(let ((element (create-element token)))
;; We should be in the InTable mode. This means we want to do
;; special magic element rearranging
(multiple-value-bind (parent insert-before)
(get-table-misnested-nodeposition)
(if (not insert-before)
(node-append-child* parent element)
(node-insert-before* parent element insert-before))
(push-end element open-elements))
element))))
(defun insert-element (token)
(with-slots (insert-from-table) *parser*
(if insert-from-table
(insert-element-table token)
(insert-element-normal token))))
(defun parser-insert-text (data &optional parent)
"Insert text data."
(with-slots (open-elements insert-from-table) *parser*
(unless parent
(setf parent (car (last open-elements))))
(cond ((or (not insert-from-table)
(and insert-from-table
(not (member (node-name (last-open-element))
+table-insert-mode-elements+ :test #'string=))))
(node-insert-text parent data))
(t
;; We should be in the InTable mode. This means we want to do
;; special magic element rearranging
(multiple-value-bind (parent insert-before)
(get-table-misnested-nodeposition)
(node-insert-text parent data insert-before))))))
(defun get-table-misnested-nodeposition ()
"Get the foster parent element, and sibling to insert before
(or None) when inserting a misnested table node"
(with-slots (open-elements) *parser*
;; The foster parent element is the one which comes before the most
;; recently opened table element
(let ((last-table (find "table" open-elements :key #'node-name :test #'string= :from-end t))
(foster-parent nil)
(insert-before nil))
(cond (last-table
;; XXX - we should really check that this parent is actually a
;; node here
(if (node-parent last-table)
(setf foster-parent (node-parent last-table)
insert-before last-table)
(setf foster-parent (elt open-elements (1- (position last-table open-elements))))))
(t
(setf foster-parent (first open-elements))))
(values foster-parent insert-before))))
(defun generate-implied-end-tags (&optional exclude)
(with-slots (open-elements) *parser*
(let ((name (node-name (last-open-element))))
;; XXX td, th and tr are not actually needed
(when (and (member name '("dd" "dt" "li" "option" "optgroup" "p" "rp" "rt") :test #'string=)
(not (equal name exclude)))
(pop-end open-elements)
;; XXX This is not entirely what the specification says. We should
;; investigate it more closely.
(generate-implied-end-tags exclude)))))
(defun reconstruct-active-formatting-elements ()
;; Within this algorithm the order of steps described in the
;; specification is not quite the same as the order of steps in the
;; code. It should still do the same though.
(with-slots (active-formatting-elements open-elements) *parser*
;; Step 1: stop the algorithm when there's nothing to do.
(unless active-formatting-elements
(return-from reconstruct-active-formatting-elements))
;; Step 2 and step 3: we start with the last element. So i is -1.
(let* ((i (1- (length active-formatting-elements)))
(entry (elt active-formatting-elements i)))
(when (or (eql entry :marker)
(member entry open-elements))
(return-from reconstruct-active-formatting-elements))
;; Step 6
(loop while (and (not (eql entry :marker))
(not (member entry open-elements))) do
(when (zerop i)
;; This will be reset to 0 below
(setf i -1)
(return))
(decf i)
;; Step 5: let entry be one earlier in the list.
(setf entry (elt active-formatting-elements i)))
(loop
;; Step 7
(incf i)
;; Step 8
(setf entry (elt active-formatting-elements i))
;; Step 9
(let* ((element (insert-element (list :type :start-tag
:name (node-name entry)
:namespace (node-namespace entry)))))
(element-map-attributes* (lambda (name namespace value)
(setf (element-attribute element name namespace) value))
entry)
;; Step 10
(setf (elt active-formatting-elements i) element)
;; Step 11
(when (eql element (car (last active-formatting-elements)))
(return)))))))
(defun clear-active-formatting-elements ()
(with-slots (active-formatting-elements) *parser*
(loop for entry = (pop-end active-formatting-elements)
while (and active-formatting-elements
(not (eql entry :marker))))))
(defun element-in-active-formatting-elements (name)
"Check if an element exists between the end of the active
formatting elements and the last marker. If it does, return it, else
return false"
(with-slots (active-formatting-elements) *parser*
(loop for item in (reverse active-formatting-elements) do
;; Check for Marker first because if it's a Marker it doesn't have a
;; name attribute.
(when (eql item :marker)
(return nil))
(when (string= (node-name item) name)
(return item)))))
(defun scope-tree ()
(load-time-value
(flet ((unflatten (alist)
"Turn an alist into a tree."
(let ((alist2
(mapcar #'list
(remove-duplicates (mapcar #'car alist)
:test #'equal))))
(loop for (key . value) in alist
do (push value (cdr (assoc key alist2
:test #'equal))))
;; Put the XHTML ns first.
(sort alist2 #'<
:key (lambda (pair)
(position (car pair)
'("http://www.w3.org/1999/xhtml"
"http://www.w3.org/2000/svg"
"http://www.w3.org/1998/Math/MathML")
:test #'string=))))))
(let ((html (find-namespace "html")))
`((nil . ,(unflatten +scoping-elements+))
("button" . ,(unflatten
`(,@+scoping-elements+
(,html . "button"))))
("list" . ,(unflatten
`(,@+scoping-elements+
(,html . "ol")
(,html . "ul"))))
("table" . ((,html "html" "table")))
("select" . ((,html "optgroup" "option"))))))))
(defun element-in-scope (target &optional variant)
(let ((list-elements
(cdr (assoc variant (scope-tree) :test #'equal)))
(invert (equal "select" variant)))
(dolist (node (reverse (slot-value *parser* 'open-elements)))
(when (or (and (stringp target)
(string= (node-name node) target))
(eql node target))
(return-from element-in-scope t))
(multiple-value-bind (ns name)
(node-name-tuple-values node)
(let ((found (member name (cdr (assoc ns list-elements :test #'string=))
:test #'string=)))
(when invert
(setf found (not found)))
(when found
(return-from element-in-scope nil)))))
(error "We should never reach this point")))