@@ -43,6 +43,158 @@ where
43
43
self . emit_list ( & n. children , ListFormat :: NotDelimited ) ?;
44
44
}
45
45
46
+ #[ emitter]
47
+ fn emit_child ( & mut self , n : & Child ) -> Result {
48
+ match n {
49
+ Child :: DocumentType ( n) => emit ! ( self , n) ,
50
+ Child :: Element ( n) => emit ! ( self , n) ,
51
+ Child :: Text ( n) => emit ! ( self , n) ,
52
+ Child :: Comment ( n) => emit ! ( self , n) ,
53
+ }
54
+ }
55
+
56
+ #[ emitter]
57
+ fn emit_document_doctype ( & mut self , n : & DocumentType ) -> Result {
58
+ let mut doctype = String :: new ( ) ;
59
+
60
+ doctype. push ( '<' ) ;
61
+ doctype. push ( '!' ) ;
62
+ doctype. push_str ( "DOCTYPE" ) ;
63
+
64
+ if let Some ( name) = & n. name {
65
+ doctype. push ( ' ' ) ;
66
+ doctype. push_str ( name) ;
67
+ }
68
+
69
+ if let Some ( public_id) = & n. public_id {
70
+ doctype. push ( ' ' ) ;
71
+ doctype. push_str ( "PUBLIC" ) ;
72
+ doctype. push ( ' ' ) ;
73
+ doctype. push ( '"' ) ;
74
+ doctype. push_str ( public_id) ;
75
+ doctype. push ( '"' ) ;
76
+
77
+ if let Some ( system_id) = & n. system_id {
78
+ doctype. push ( ' ' ) ;
79
+ doctype. push ( '"' ) ;
80
+ doctype. push_str ( system_id) ;
81
+ doctype. push ( '"' ) ;
82
+ }
83
+ } else if let Some ( system_id) = & n. system_id {
84
+ doctype. push ( ' ' ) ;
85
+ doctype. push_str ( "SYSTEM" ) ;
86
+ doctype. push ( ' ' ) ;
87
+ doctype. push ( '"' ) ;
88
+ doctype. push_str ( system_id) ;
89
+ doctype. push ( '"' ) ;
90
+ }
91
+
92
+ doctype. push ( '>' ) ;
93
+
94
+ write_raw ! ( self , n. span, & doctype) ;
95
+ }
96
+
97
+ #[ emitter]
98
+ fn emit_element ( & mut self , n : & Element ) -> Result {
99
+ let mut start_tag = String :: new ( ) ;
100
+
101
+ start_tag. push ( '<' ) ;
102
+ start_tag. push_str ( & n. tag_name ) ;
103
+
104
+ for attribute in & n. attributes {
105
+ start_tag. push ( ' ' ) ;
106
+ start_tag. push_str ( & attribute. name ) ;
107
+
108
+ if let Some ( value) = & attribute. value {
109
+ start_tag. push ( '=' ) ;
110
+
111
+ let quote = if value. contains ( '"' ) { '\'' } else { '"' } ;
112
+
113
+ start_tag. push ( quote) ;
114
+ start_tag. push_str ( value) ;
115
+ start_tag. push ( quote) ;
116
+ }
117
+ }
118
+
119
+ start_tag. push ( '>' ) ;
120
+
121
+ write_str ! ( self , n. span, & start_tag) ;
122
+
123
+ let no_children = n. namespace == Namespace :: HTML
124
+ && matches ! (
125
+ & * n. tag_name,
126
+ "area"
127
+ | "base"
128
+ | "basefont"
129
+ | "bgsound"
130
+ | "br"
131
+ | "col"
132
+ | "embed"
133
+ | "frame"
134
+ | "hr"
135
+ | "img"
136
+ | "input"
137
+ | "keygen"
138
+ | "link"
139
+ | "meta"
140
+ | "param"
141
+ | "source"
142
+ | "track"
143
+ | "wbr"
144
+ ) ;
145
+
146
+ if no_children {
147
+ return Ok ( ( ) ) ;
148
+ }
149
+
150
+ if !n. children . is_empty ( ) {
151
+ self . emit_list ( & n. children , ListFormat :: NotDelimited ) ?;
152
+ }
153
+
154
+ let mut end_tag = String :: new ( ) ;
155
+
156
+ end_tag. push ( '<' ) ;
157
+ end_tag. push ( '/' ) ;
158
+ end_tag. push_str ( & n. tag_name ) ;
159
+ end_tag. push ( '>' ) ;
160
+
161
+ write_str ! ( self , n. span, & end_tag) ;
162
+ }
163
+
164
+ #[ emitter]
165
+ fn emit_text ( & mut self , n : & Text ) -> Result {
166
+ let mut text = String :: new ( ) ;
167
+
168
+ for c in n. value . chars ( ) {
169
+ match c {
170
+ '&' => {
171
+ text. push_str ( & String :: from ( "&" ) ) ;
172
+ }
173
+ '<' => {
174
+ text. push_str ( & String :: from ( "<" ) ) ;
175
+ }
176
+ '>' => {
177
+ text. push_str ( & String :: from ( ">" ) ) ;
178
+ }
179
+ '\u{00A0}' => text. push_str ( & String :: from ( " " ) ) ,
180
+ _ => text. push ( c) ,
181
+ }
182
+ }
183
+
184
+ write_str ! ( self , n. span, & text) ;
185
+ }
186
+
187
+ #[ emitter]
188
+ fn emit_comment ( & mut self , n : & Comment ) -> Result {
189
+ let mut comment = String :: new ( ) ;
190
+
191
+ comment. push_str ( "<!--" ) ;
192
+ comment. push_str ( & n. data ) ;
193
+ comment. push_str ( "-->" ) ;
194
+
195
+ write_str ! ( self , n. span, & comment) ;
196
+ }
197
+
46
198
#[ emitter]
47
199
fn emit_token_and_span ( & mut self , n : & TokenAndSpan ) -> Result {
48
200
let span = n. span ;
@@ -226,7 +378,7 @@ where
226
378
227
379
start_tag. push ( '>' ) ;
228
380
229
- write_str ! ( self , span, & start_tag) ;
381
+ write_raw ! ( self , span, & start_tag) ;
230
382
}
231
383
Token :: EndTag {
232
384
tag_name,
@@ -325,19 +477,9 @@ where
325
477
fn write_delim ( & mut self , f : ListFormat ) -> Result {
326
478
match f & ListFormat :: DelimitersMask {
327
479
ListFormat :: None => { }
328
- ListFormat :: CommaDelimited => {
329
- write_raw ! ( self , "," ) ;
330
- formatting_space ! ( self ) ;
331
- }
332
480
ListFormat :: SpaceDelimited => {
333
481
space ! ( self )
334
482
}
335
- ListFormat :: SemiDelimited => {
336
- write_raw ! ( self , ";" )
337
- }
338
- ListFormat :: DotDelimited => {
339
- write_raw ! ( self , "." ) ;
340
- }
341
483
_ => unreachable ! ( ) ,
342
484
}
343
485
0 commit comments