encodeAttr

Returns a lazy range of code units which encodes any characters which cannot be put in an attribute value of an element tag in their literal form.

encodeAttr is intended primarily to be used with dxml.writer.XMLWriter.writeAttr to ensure that characters which cannot appear in their literal form do not appear in their literal form.

Specifically, what encodeAttr does is

convert & to &
convert < to &lt;
convert ' to &pos; if quote == $(D_STRING '\'')
convert " to &quot; if quote == $(D_STRING '"')
encodeAttr
(
char quote = '"'
R
)
()
if (
(
quote == '"' ||
quote == '\''
)
&&
isForwardRange!R
&&
isSomeChar!(ElementType!R)
)

Examples

1 import std.algorithm.comparison : equal;
2 
3 assert(equal(encodeAttr(`foo & bar`), `foo &amp; bar`));
4 assert(equal(encodeAttr(`foo < bar`), `foo &lt; bar`));
5 assert(equal(encodeAttr(`foo > bar`), `foo > bar`));
6 assert(equal(encodeAttr(`foo ' bar`), `foo ' bar`));
7 assert(equal(encodeAttr(`foo " bar`), `foo &quot; bar`));
8 
9 assert(equal(encodeAttr!'\''(`foo ' bar`), `foo &apos; bar`));
10 assert(equal(encodeAttr!'\''(`foo " bar`), `foo " bar`));
11 
12 assert(equal(encodeAttr("hello world"), "hello world"));

See Also

Meta