Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions src/html/dom.d
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,9 @@ class Node {
app.put(attr);

if (value.length) {
if (value.requiresQuotes) {
app.put("=\"");
app.writeQuotesEscaped(value);
app.put("\"");
} else {
app.put('=');
app.put(value);
}
app.put("=\"");
app.writeHTMLEscaped!(Yes.escapeQuotes)(value);
app.put("\"");
}
}

Expand All @@ -633,7 +628,7 @@ class Node {
}
break;
case Text:
app.put(tag_);
app.writeHTMLEscaped!(No.escapeQuotes)(tag_);
break;
case Comment:
app.put("<!--");
Expand Down Expand Up @@ -669,14 +664,9 @@ class Node {
app.put(attr);

if (value.length) {
if (value.requiresQuotes) {
app.put("=\"");
app.writeQuotesEscaped(value);
app.put("\"");
} else {
app.put('=');
app.put(value);
}
app.put("=\"");
app.writeHTMLEscaped!(Yes.escapeQuotes)(value);
app.put("\"");
}
}

Expand Down Expand Up @@ -768,7 +758,7 @@ class Node {
} else {
space = false;
}
app.put(ch);
app.writeHTMLEscaped!(No.escapeQuotes)(ch);
}
}
break;
Expand Down Expand Up @@ -1105,13 +1095,24 @@ unittest {
auto doc = createDocument(`<html><body>&nbsp;</body></html>`);
assert(doc.root.outerHTML == "<root><html><body>\&nbsp;</body></html></root>");
doc = createDocument!(DOMCreateOptions.None)(`<html><body>&nbsp;</body></html>`);
assert(doc.root.outerHTML == `<root><html><body>&nbsp;</body></html></root>`);
assert(doc.root.outerHTML == `<root><html><body>&amp;nbsp;</body></html></root>`);
doc = createDocument(`<script>&nbsp;</script>`);
assert(doc.root.outerHTML == `<root><script>&nbsp;</script></root>`, doc.root.outerHTML);
doc = createDocument(`<style>&nbsp;</style>`);
assert(doc.root.outerHTML == `<root><style>&nbsp;</style></root>`, doc.root.outerHTML);
}

unittest {
string src = `&lt;script&gt;alert("test&amp;");&lt;/script&gt;`;
auto doc = createDocument(src);
assert(doc.root.html == src, doc.root.html);
assert(doc.root.compactHTML == src, doc.root.compactHTML);
src = `<div title="&gt;&lt;script&gt;alert('test&amp;');&lt;/script&gt;"></div>`;
doc = createDocument(src);
assert(doc.root.html == src, doc.root.html);
assert(doc.root.compactHTML == src, doc.root.compactHTML);
}

unittest {
const doc = createDocument(`<html><body><div title='"Тест&apos;"'>"К"ириллица</div></body></html>`);
assert(doc.root.html == `<html><body><div title="&#34;Тест'&#34;">"К"ириллица</div></body></html>`);
Expand All @@ -1127,6 +1128,12 @@ unittest {
assert(doc.root.outerHTML == `<root><br><div></div></root>`, doc.root.outerHTML);
}

// quotes should be preserved as they are mandatory in SVG
unittest {
auto doc = createDocument(`<svg><text x="7" /></svg>`);
assert(doc.root.outerHTML == `<root><svg><text x="7"></text></svg></root>`, doc.root.outerHTML);
}

// toString prints elements with content as <tag attr="value"/>
unittest {
// self-closed element w/o content
Expand Down Expand Up @@ -1346,8 +1353,8 @@ private:


unittest {
const(char)[] src = `<parent attr=value><child></child>text</parent>`;
auto doc = createDocument(src);
const(char)[] src = `<parent attr="value"><child></child>text</parent>`;
auto doc = createDocument(`<parent attr=value><child></child>text</parent>`);
assert(doc.root.html == src, doc.root.html);

const(char)[] srcq = `<parent attr="v a l u e"><child></child>text</parent>`;
Expand All @@ -1366,11 +1373,11 @@ unittest {
auto child = cloned.find("child").front.clone;
child.attr("attr", "test");
cloned.find("parent").front.appendChild(child);
assert(cloned.html == `<parent attr=value><child></child>text<child attr=test></child></parent>`, cloned.html);
assert(cloned.html == `<parent attr="value"><child></child>text<child attr="test"></child></parent>`, cloned.html);
assert(doc.root.html == src, doc.root.html);

child.text = "text";
assert(cloned.html == `<parent attr=value><child></child>text<child attr=test>text</child></parent>`, cloned.html);
assert(cloned.html == `<parent attr="value"><child></child>text<child attr="test">text</child></parent>`, cloned.html);
assert(doc.root.html == src, doc.root.html);

// document cloning
Expand Down
64 changes: 16 additions & 48 deletions src/html/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ bool isAllWhite(Char)(Char[] value) {
}


bool requiresQuotes(Char)(Char[] value) {
auto ptr = value.ptr;
const end = ptr + value.length;

while (ptr != end) {
switch (*ptr++) {
case 'a': .. case 'z':
case 'A': .. case 'Z':
case '0': .. case '9':
case '-':
case '_':
case '.':
case ':':
continue;
default:
return true;
}
}
return false;
}


bool equalsCI(CharA, CharB)(const(CharA)[] a, const(CharB)[] b) {
if (a.length == b.length) {
for (size_t i; i < a.length; ++i) {
Expand Down Expand Up @@ -79,42 +57,32 @@ hash_t tagHashOf(const(char)[] x) {
}


void writeQuotesEscaped(Appender)(ref Appender app, const(char)[] x) {
foreach (dchar ch; x) {
switch (ch) {
void writeHTMLEscaped(Flag!q{escapeQuotes} escapeQuotes, Appender)(ref Appender app, dchar ch) {
switch (ch) {
static if (escapeQuotes) {
case '"':
app.put("&#34;"); // shorter than &quot;
static assert('"' == 34);
break;
default:
app.put(ch);
break;
}
case '<':
app.put("&lt;");
break;
case '>':
app.put("&gt;");
break;
case '&':
app.put("&amp;");
break;
default:
app.put(ch);
break;
}
}


void writeHTMLEscaped(Flag!q{escapeQuotes} escapeQuotes, Appender)(ref Appender app, const(char)[] x) {
foreach (dchar ch; x) {
switch (ch) {
static if (escapeQuotes) {
case '"':
app.put("&#34;"); // shorter than &quot;
static assert('"' == 34);
break;
}
case '<':
app.put("&lt;");
break;
case '>':
app.put("&gt;");
break;
case '&':
app.put("&amp;");
break;
default:
app.put(ch);
break;
}
writeHTMLEscaped!escapeQuotes(app, ch);
}
}