Don't escape &{
[closure-html.git] / src / parse / unparse.lisp
blob622eb43a20ae18afe5904c1dac2cf9ac528d53b2
1 ;;; -*- show-trailing-whitespace: t; indent-tabs: nil -*-
2 ;;; ---------------------------------------------------------------------------
3 ;;; Title: Unparse HTML
4 ;;; Created: 2007-10-14
5 ;;; Author: David Lichteblau <david@lichteblau.com>
6 ;;; License: BSD-style
7 ;;; ---------------------------------------------------------------------------
8 ;;; (c) copyright 2005-2007 David Lichteblau
11 (in-package :closure-html)
14 ;;; SINK: an HTML output sink
16 (defclass sink (hax:abstract-handler)
17 ((ystream :initarg :ystream :accessor sink-ystream)
18 (stack :initform nil :accessor stack)))
20 #-rune-is-character
21 (defmethod hax:%want-strings-p ((handler sink))
22 nil)
24 ;; bisschen unschoen hier SCHON WIEDER die ganze api zu duplizieren, aber die
25 ;; ystreams sind noch undokumentiert
26 (macrolet ((define-maker (make-sink make-ystream &rest args)
27 `(defun ,make-sink (,@args &rest initargs)
28 (apply #'make-instance
29 'sink
30 :ystream (,make-ystream ,@args)
31 initargs))))
32 (define-maker make-octet-vector-sink make-octet-vector-ystream)
33 (define-maker make-octet-stream-sink make-octet-stream-ystream stream)
34 (define-maker make-rod-sink make-rod-ystream)
36 #+rune-is-character
37 (define-maker make-character-stream-sink make-character-stream-ystream stream)
39 #-rune-is-character
40 (define-maker make-string-sink/utf8 make-string-ystream/utf8)
42 #-rune-is-character
43 (define-maker make-character-stream-sink/utf8
44 make-character-stream-ystream/utf8
45 stream))
47 #+rune-is-character
48 (defun make-string-sink (&rest args) (apply #'make-rod-sink args))
51 ;;;; Events
53 (defmethod hax:start-document ((sink sink) name public-id system-id)
54 (when (plusp (length system-id))
55 (%write-rod #"<!DOCTYPE " sink)
56 (%write-rod name sink)
57 (cond
58 ((plusp (length public-id))
59 (%write-rod #" PUBLIC \"" sink)
60 (unparse-string public-id sink)
61 (%write-rod #"\" \"" sink)
62 (unparse-string system-id sink)
63 (%write-rod #"\"" sink))
65 (%write-rod #" SYSTEM \"" sink)
66 (unparse-string system-id sink)
67 (%write-rod #"\"" sink)))
68 (%write-rod #">" sink)
69 (%write-rune #/U+000A sink)))
71 (defmethod hax:end-document ((sink sink))
72 (close-ystream (sink-ystream sink)))
74 (defmethod hax:start-element ((sink sink) name attributes)
75 (let* ((key (find-symbol (string-upcase (rod-string name)) :keyword))
76 (elt
77 (and key (sgml::find-element closure-html::*html-dtd* key nil nil)))
78 (attlist (and elt (sgml::element-attlist elt))))
79 (push (cons name elt) (stack sink))
80 (%write-rune #/< sink)
81 (%write-rod name sink)
82 (dolist (a attributes)
83 (let* ((aname (hax:attribute-name a))
84 (akey (find-symbol (string-upcase (string-rod aname)) :keyword))
85 (att (and akey (assoc akey attlist)))
86 (values (second att)))
87 (%write-rune #/space sink)
88 (%write-rod aname sink)
89 (unless (and att (listp values) (eq (car att) (car values)))
90 (%write-rune #/= sink)
91 (%write-rune #/\" sink)
92 (let ((value (hax:attribute-value a)))
93 (when (uri-attribute-p aname)
94 (setf value (escape-uri-attribute value)))
95 (unparse-attribute-string value sink))
96 (%write-rune #/\" sink))))
97 (%write-rune #/> sink)))
99 ;;; everything written as %URI in the DTD. Complete list per element,
100 ;;; as found in the HTML 4.01 Strict DTD
102 ;;; a href
103 ;;; area href
104 ;;; link href
105 ;;; img src longdesc usemap
106 ;;; object classid codebase data usemap
107 ;;; q cite
108 ;;; blockquote cite
109 ;;; inl cite
110 ;;; del cite
111 ;;; form action
112 ;;; input src usemap
113 ;;; head profile
114 ;;; base href
115 ;;; script src for
117 ;;; plus the reserved attribute datasrc.
118 (defun uri-attribute-p (name)
119 (find (rod-downcase name)
120 '(#"action" #"cite" #"classid" #"codebase" #"data" #"for" #"href"
121 #"longdesc" #"profile" #"src" #"usemap")
122 :test 'rod=))
124 (defun escape-uri-attribute (x)
125 ;; implementme
128 (defmethod hax:end-element
129 ((sink sink) name)
130 (let* ((prev (pop (stack sink)))
131 (prev-name (car prev))
132 (elt (cdr prev)))
133 (unless (rod= prev-name name)
134 (error "output does not nest: expected ~A but got ~A"
135 name prev-name))
136 (unless (and elt (null (sgml::element-include elt)))
137 (%write-rod '#.(string-rod "</") sink)
138 (%write-rod name sink)
139 (%write-rod '#.(string-rod ">") sink))))
141 (defmethod hax:characters ((sink sink) data)
142 (let ((y (sink-ystream sink)))
143 (if (find (caar (stack sink)) '("script" "style") :test 'equalp)
144 (write-rod data (sink-ystream sink))
145 (loop for c across data do (unparse-datachar-readable c y)))))
147 (defmethod hax:unescaped ((sink sink) data)
148 (%write-rod data sink))
150 (defmethod hax:comment ((sink sink) data)
151 ;; XXX signal error if body is unprintable?
152 (%write-rod #"<!--" sink)
153 (map nil (lambda (c) (%write-rune c sink)) data)
154 (%write-rod #"-->" sink))
156 (defun unparse-string (str sink)
157 (let ((y (sink-ystream sink)))
158 (loop for rune across str do (unparse-datachar rune y))))
160 (defun unparse-attribute-string (str sink)
161 (let ((y (sink-ystream sink)))
162 (loop
163 for i from 1
164 for c across str
166 (cond ((rune= c #/&)
167 (if (and (< i (length str)) (rune= (rune str i) #/{))
168 (write-rune c y)
169 (write-rod '#.(string-rod "&amp;") y)))
170 ((rune= c #/\") (write-rod '#.(string-rod "&quot;") y))
171 ((rune= c #/U+000A) (write-rod '#.(string-rod "&#10;") y))
172 ((rune= c #/U+000D) (write-rod '#.(string-rod "&#13;") y))
174 (write-rune c y))))))
176 (defun unparse-datachar (c ystream)
177 (cond ((rune= c #/&) (write-rod '#.(string-rod "&amp;") ystream))
178 ((rune= c #/<) (write-rod '#.(string-rod "&lt;") ystream))
179 ((rune= c #/>) (write-rod '#.(string-rod "&gt;") ystream))
180 ((rune= c #/\") (write-rod '#.(string-rod "&quot;") ystream))
181 ((rune= c #/U+0009) (write-rod '#.(string-rod "&#9;") ystream))
182 ((rune= c #/U+000A) (write-rod '#.(string-rod "&#10;") ystream))
183 ((rune= c #/U+000D) (write-rod '#.(string-rod "&#13;") ystream))
185 (write-rune c ystream))))
187 (defun unparse-datachar-readable (c ystream)
188 (cond ((rune= c #/&) (write-rod '#.(string-rod "&amp;") ystream))
189 ((rune= c #/<) (write-rod '#.(string-rod "&lt;") ystream))
190 ((rune= c #/>) (write-rod '#.(string-rod "&gt;") ystream))
191 ((rune= c #/\") (write-rod '#.(string-rod "&quot;") ystream))
192 ((rune= c #/U+000D) (write-rod '#.(string-rod "&#13;") ystream))
194 (write-rune c ystream))))
196 (defun unparse-dtd-string (str sink)
197 (let ((y (sink-ystream sink)))
198 (loop for rune across str do (unparse-dtd-char rune y))))
200 (defun unparse-dtd-char (c ystream)
201 (cond ((rune= c #/%) (write-rod '#.(string-rod "&#37;") ystream))
202 ((rune= c #/&) (write-rod '#.(string-rod "&amp;") ystream))
203 ((rune= c #/<) (write-rod '#.(string-rod "&lt;") ystream))
204 ((rune= c #/>) (write-rod '#.(string-rod "&gt;") ystream))
205 ((rune= c #/\") (write-rod '#.(string-rod "&quot;") ystream))
206 ((rune= c #/U+0009) (write-rod '#.(string-rod "&#9;") ystream))
207 ((rune= c #/U+000A) (write-rod '#.(string-rod "&#10;") ystream))
208 ((rune= c #/U+000D) (write-rod '#.(string-rod "&#13;") ystream))
210 (write-rune c ystream))))
212 (defun %write-rune (c sink)
213 (write-rune c (sink-ystream sink)))
215 (defun %write-rod (r sink)
216 (write-rod r (sink-ystream sink)))
219 ;;;; convenience functions for PTless HTML serialization
221 (defvar *current-element*)
222 (defvar *sink*)
224 (defmacro with-html-output
225 ((sink &key (name "HTML") public-id system-id) &body body)
226 `(invoke-with-html-output (lambda () ,@body)
227 ,sink
228 ,name
229 ,public-id
230 ,system-id))
232 (defun invoke-with-html-output (fn sink name pubid sysid)
233 (let ((*sink* sink)
234 (*current-element* nil))
235 (hax:start-document *sink* name pubid sysid)
236 (funcall fn)
237 (hax:end-document *sink*)))
239 ;; fuer XML ist hier mehr zu tun, also gehen wir vorsichtshalber fuer HTML
240 ;; erstmal auch diesen Weg
241 (defmacro with-output-sink ((var) &body body)
242 `(invoke-with-output-sink (lambda (,var) ,@body)))
243 (defun invoke-with-output-sink (fn)
244 (funcall fn *sink*))
246 (defmacro with-element (name &body body)
247 `(invoke-with-element (lambda () ,@body) ,name))
249 (defun maybe-emit-start-tag ()
250 (when *current-element*
251 ;; starting child node, need to emit opening tag of parent first:
252 (destructuring-bind (name &rest attributes) *current-element*
253 (hax:start-element *sink* name (reverse attributes)))
254 (setf *current-element* nil)))
256 (defun invoke-with-element (fn name)
257 (setf name (rod name))
258 (maybe-emit-start-tag)
259 (let ((*current-element* (list name)))
260 (multiple-value-prog1
261 (funcall fn)
262 (maybe-emit-start-tag)
263 (hax:end-element *sink* name))))
265 (defgeneric unparse-attribute (value))
266 (defmethod unparse-attribute ((value string)) value)
267 (defmethod unparse-attribute ((value null)) nil)
268 (defmethod unparse-attribute ((value integer)) (write-to-string value))
270 (defun attribute (name value)
271 (setf name (rod name))
272 (setf value (unparse-attribute value))
273 (push (hax:make-attribute name value t)
274 (cdr *current-element*)))
276 (defun text (data)
277 (maybe-emit-start-tag)
278 (hax:characters *sink* (rod data))
279 data)
281 (defun comment (data)
282 (maybe-emit-start-tag)
283 (hax:comment *sink* (rod data))
284 data)