Skip to content

Commit 2c17e0e

Browse files
authored
Merge pull request #12 from burner/utf8
UTF8 for the win + lot of housekeeping
2 parents 9654461 + 32520b3 commit 2c17e0e

14 files changed

+2534
-2437
lines changed

source/newxml/cursor.d

+342-310
Large diffs are not rendered by default.

source/newxml/dom.d

+215-156
Large diffs are not rendered by default.

source/newxml/domimpl.d

+1,256-841
Large diffs are not rendered by default.

source/newxml/domparser.d

+70-78
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import newxml.cursor;
2525

2626
import dom = newxml.dom;
2727
import newxml.domimpl;
28-
import newxml.domstring;
2928

3029
/++
3130
+ Built on top of Cursor, the DOM builder adds to it the ability to
@@ -36,8 +35,7 @@ import newxml.domstring;
3635
+ This type should not be instantiated directly. Instead, the helper function
3736
+ `domBuilder` should be used.
3837
+/
39-
struct DOMBuilder(T)
40-
if (isCursor!T)
38+
struct DOMBuilder(T) if (isCursor!T)
4139
{
4240
import std.traits : ReturnType;
4341

@@ -75,22 +73,23 @@ struct DOMBuilder(T)
7573
{
7674
switch (attr.name)
7775
{
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;
9182
break;
9283
default:
84+
cursor.xmlVersion = XMLVersion.XML1_0;
9385
break;
86+
}
87+
break;
88+
case "standalone":
89+
document.xmlStandalone = attr.value == "yes";
90+
break;
91+
default:
92+
break;
9493
}
9594
}
9695
}
@@ -180,7 +179,9 @@ struct DOMBuilder(T)
180179

181180
auto cur = createCurrent;
182181
if (cur)
182+
{
183183
currentNode.appendChild(createCurrent);
184+
}
184185

185186
already_built = true;
186187
}
@@ -207,65 +208,68 @@ struct DOMBuilder(T)
207208
return next();
208209
}
209210

210-
private NodeType createCurrent()
211-
// TODO: handling of system (external) entities
211+
private NodeType createCurrent() // TODO: handling of system (external) entities
212212
{
213213
switch (cursor.kind)
214214
{
215215

216216
// 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)
227227
{
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);
230230
}
231231
else
232232
{*/
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;
255256
}
256257
}
257258

258259
/++
259260
+ Returns the Document being built by this builder.
260261
+/
261-
auto getDocument() { return document; }
262+
auto getDocument()
263+
{
264+
return document;
265+
}
262266
}
263267

264268
/++
265269
+ Instantiates a suitable `DOMBuilder` on top of the given `cursor` and `DOMImplementation`.
266270
+/
267271
auto domBuilder(CursorType)(auto ref CursorType cursor, DOMImplementation domimpl)
268-
if (isCursor!CursorType)
272+
if (isCursor!CursorType)
269273
{
270274
auto res = DOMBuilder!(CursorType)(domimpl);
271275
res.cursor = cursor;
@@ -282,7 +286,6 @@ unittest
282286
import newxml.parser;
283287
import newxml.cursor;
284288
import domimpl = newxml.domimpl;
285-
286289

287290
alias DOMImpl = domimpl.DOMImplementation;
288291

@@ -299,30 +302,25 @@ unittest
299302
</aaa>
300303
};
301304

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());
308306

309307
builder.setSource(xml);
310308
builder.buildRecursive;
311309
dom.Document doc = builder.getDocument;
312310

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;
319317
assert(e1.nodeName == "aaa");
320-
dom.Element e2 = cast(dom.Element)e1.firstChild();
318+
dom.Element e2 = cast(dom.Element) e1.firstChild();
321319
assert(e2.nodeName == "myns:bbb");
322-
dom.Comment c1 = cast(dom.Comment)e2.firstChild;
320+
dom.Comment c1 = cast(dom.Comment) e2.firstChild;
323321
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
326324
//assert(t1.data == "Lots of Text! On multiple lines!", t1.data.transcodeToUTF8);
327325

328326
}
@@ -337,13 +335,7 @@ unittest
337335
alias DOMImplType = domimpl.DOMImplementation;
338336

339337
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());
347339

348340
builder.setSource(xml);
349341
builder.buildRecursive;

0 commit comments

Comments
 (0)