@@ -25,7 +25,6 @@ import newxml.cursor;
25
25
26
26
import dom = newxml.dom;
27
27
import newxml.domimpl;
28
- import newxml.domstring;
29
28
30
29
/+ +
31
30
+ Built on top of Cursor, the DOM builder adds to it the ability to
@@ -36,8 +35,7 @@ import newxml.domstring;
36
35
+ This type should not be instantiated directly. Instead, the helper function
37
36
+ `domBuilder` should be used.
38
37
+/
39
- struct DOMBuilder (T)
40
- if (isCursor! T)
38
+ struct DOMBuilder (T) if (isCursor! T)
41
39
{
42
40
import std.traits : ReturnType;
43
41
@@ -75,22 +73,23 @@ struct DOMBuilder(T)
75
73
{
76
74
switch (attr.name)
77
75
{
78
- case " version" :
79
- document.xmlVersion = new DOMString(attr.value);
80
- switch (attr.value) {
81
- case " 1.1" :
82
- cursor.xmlVersion = XMLVersion.XML1_1 ;
83
- break ;
84
- default :
85
- cursor.xmlVersion = XMLVersion.XML1_0 ;
86
- break ;
87
- }
88
- break ;
89
- case " standalone" :
90
- document.xmlStandalone = attr.value == " yes" ;
76
+ case " version" :
77
+ document.xmlVersion = attr.value;
78
+ switch (attr.value)
79
+ {
80
+ case " 1.1" :
81
+ cursor.xmlVersion = XMLVersion.XML1_1 ;
91
82
break ;
92
83
default :
84
+ cursor.xmlVersion = XMLVersion.XML1_0 ;
93
85
break ;
86
+ }
87
+ break ;
88
+ case " standalone" :
89
+ document.xmlStandalone = attr.value == " yes" ;
90
+ break ;
91
+ default :
92
+ break ;
94
93
}
95
94
}
96
95
}
@@ -180,7 +179,9 @@ struct DOMBuilder(T)
180
179
181
180
auto cur = createCurrent;
182
181
if (cur)
182
+ {
183
183
currentNode.appendChild(createCurrent);
184
+ }
184
185
185
186
already_built = true ;
186
187
}
@@ -207,65 +208,68 @@ struct DOMBuilder(T)
207
208
return next ();
208
209
}
209
210
210
- private NodeType createCurrent ()
211
- // TODO: handling of system (external) entities
211
+ private NodeType createCurrent () // TODO: handling of system (external) entities
212
212
{
213
213
switch (cursor.kind)
214
214
{
215
215
216
216
// XMLKind.elementEnd is needed for empty tags: <tag></tag>
217
- case XMLKind.elementEnd:
218
- case XMLKind.elementStart:
219
- case XMLKind.elementEmpty:
220
- /* DOMImplementation.Element elem = cursor.prefix.length ?
221
- document.createElementNS(new DOMString( cursor.prefix), new DOMString( cursor.localName)) :
222
- document.createElement(new DOMString( cursor.name) ); */
223
- DOMImplementation.Element elem = document.createElement(new DOMString( cursor.name) );
224
- foreach (attr; cursor.attributes)
225
- {
226
- /* if (attr.prefix.length)
217
+ case XMLKind.elementEnd:
218
+ case XMLKind.elementStart:
219
+ case XMLKind.elementEmpty:
220
+ /* DOMImplementation.Element elem = cursor.prefix.length ?
221
+ document.createElementNS(cursor.prefix, cursor.localName) :
222
+ document.createElement(cursor.name); */
223
+ DOMImplementation.Element elem = document.createElement(cursor.name);
224
+ foreach (attr; cursor.attributes)
225
+ {
226
+ /* if (attr.prefix.length)
227
227
{
228
- elem.setAttributeNS(new DOMString( attr.prefix), new DOMString( attr.localName),
229
- new DOMString( attr.value) );
228
+ elem.setAttributeNS(attr.prefix, attr.localName,
229
+ attr.value);
230
230
}
231
231
else
232
232
{*/
233
- elem.setAttribute(new DOMString (attr.name), new DOMString(attr.value));
234
- // }
235
- }
236
- return elem;
237
- case XMLKind.text:
238
- return document.createTextNode(new DOMString(cursor.content));
239
- case XMLKind.cdata:
240
- return document.createCDATASection(new DOMString(cursor.content));
241
- case XMLKind.processingInstruction:
242
- return document.createProcessingInstruction(new DOMString(cursor.name), new DOMString(cursor.content));
243
- case XMLKind.comment:
244
- return document.createComment(new DOMString(cursor.content));
245
- case XMLKind.dtdStart, XMLKind.dtdEmpty:
246
- docType = domImpl.createDocumentType(new DOMString(cursor.name), new DOMString(), new DOMString());
247
- document.doctype = docType;
248
- return null ;
249
- case XMLKind.entityDecl:
250
- docType.createEntity(new DOMString (cursor.name), new DOMString(cursor.content));
251
- cursor.chrEntities[cursor.name] = cursor.content;
252
- return null ;
253
- default :
254
- return null ;
233
+ elem.setAttribute(attr.name, attr.value);
234
+ // }
235
+ }
236
+ return elem;
237
+ case XMLKind.text:
238
+ return document.createTextNode(cursor.content);
239
+ case XMLKind.cdata:
240
+ return document.createCDATASection(cursor.content);
241
+ case XMLKind.processingInstruction:
242
+ return document.createProcessingInstruction(cursor.name,
243
+ cursor.content);
244
+ case XMLKind.comment:
245
+ return document.createComment(cursor.content);
246
+ case XMLKind.dtdStart, XMLKind.dtdEmpty:
247
+ docType = domImpl.createDocumentType(cursor.name, " " , " " );
248
+ document.doctype = docType;
249
+ return null ;
250
+ case XMLKind.entityDecl:
251
+ docType.createEntity(cursor.name, cursor.content);
252
+ cursor.chrEntities[cursor.name] = cursor.content;
253
+ return null ;
254
+ default :
255
+ return null ;
255
256
}
256
257
}
257
258
258
259
/+ +
259
260
+ Returns the Document being built by this builder.
260
261
+/
261
- auto getDocument () { return document; }
262
+ auto getDocument ()
263
+ {
264
+ return document;
265
+ }
262
266
}
263
267
264
268
/+ +
265
269
+ Instantiates a suitable `DOMBuilder` on top of the given `cursor` and `DOMImplementation`.
266
270
+/
267
271
auto domBuilder (CursorType)(auto ref CursorType cursor, DOMImplementation domimpl)
268
- if (isCursor! CursorType)
272
+ if (isCursor! CursorType)
269
273
{
270
274
auto res = DOMBuilder! (CursorType)(domimpl);
271
275
res.cursor = cursor;
@@ -282,7 +286,6 @@ unittest
282
286
import newxml.parser;
283
287
import newxml.cursor;
284
288
import domimpl = newxml.domimpl;
285
-
286
289
287
290
alias DOMImpl = domimpl.DOMImplementation;
288
291
@@ -299,30 +302,25 @@ unittest
299
302
< / aaa>
300
303
};
301
304
302
- auto builder =
303
- xml
304
- .lexer
305
- .parser
306
- .cursor
307
- .domBuilder(new DOMImpl());
305
+ auto builder = xml.lexer.parser.cursor.domBuilder(new DOMImpl());
308
306
309
307
builder.setSource(xml);
310
308
builder.buildRecursive;
311
309
dom.Document doc = builder.getDocument;
312
310
313
- assert (doc.getElementsByTagName(new DOMString( " ccc" ) ).length == 1 );
314
- assert (doc.documentElement.getAttribute(new DOMString( " myattr" ) ));
315
- assert (doc.documentElement.getAttribute(new DOMString( " myattr" ) ) == " something" );
316
- assert (doc.documentElement.getAttribute(new DOMString( " xmlns:myns" ) ));
317
- assert (doc.documentElement.getAttribute(new DOMString( " xmlns:myns" ) ) == " something" );
318
- dom.Element e1 = cast (dom.Element)doc.firstChild;
311
+ assert (doc.getElementsByTagName(" ccc" ).length == 1 );
312
+ assert (doc.documentElement.getAttribute(" myattr" ));
313
+ assert (doc.documentElement.getAttribute(" myattr" ) == " something" );
314
+ assert (doc.documentElement.getAttribute(" xmlns:myns" ));
315
+ assert (doc.documentElement.getAttribute(" xmlns:myns" ) == " something" );
316
+ dom.Element e1 = cast (dom.Element) doc.firstChild;
319
317
assert (e1.nodeName == " aaa" );
320
- dom.Element e2 = cast (dom.Element)e1.firstChild();
318
+ dom.Element e2 = cast (dom.Element) e1.firstChild();
321
319
assert (e2.nodeName == " myns:bbb" );
322
- dom.Comment c1 = cast (dom.Comment)e2.firstChild;
320
+ dom.Comment c1 = cast (dom.Comment) e2.firstChild;
323
321
assert (c1.data == " lol " );
324
- dom.Text t1 = cast (dom.Text )e2.lastChild;
325
- // Issue: Extra whitespace isn't dropped between and after words when dropWhiteSpace is enabled in
322
+ dom.Text t1 = cast (dom.Text ) e2.lastChild;
323
+ // Issue: Extra whitespace isn't dropped between and after words when dropWhiteSpace is enabled in
326
324
// assert(t1.data == "Lots of Text! On multiple lines!", t1.data.transcodeToUTF8);
327
325
328
326
}
@@ -337,13 +335,7 @@ unittest
337
335
alias DOMImplType = domimpl.DOMImplementation;
338
336
339
337
auto xml = ` <?xml version="1.0" encoding="UTF-8"?><tag></tag>` ;
340
- auto builder =
341
- xml
342
- .lexer
343
- .parser
344
- .cursor
345
- .copyingCursor
346
- .domBuilder(new DOMImplType());
338
+ auto builder = xml.lexer.parser.cursor.copyingCursor.domBuilder(new DOMImplType());
347
339
348
340
builder.setSource(xml);
349
341
builder.buildRecursive;
0 commit comments