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

import std.algorithm.comparison : equal;

assert(equal(encodeAttr(`foo & bar`), `foo &amp; bar`));
assert(equal(encodeAttr(`foo < bar`), `foo &lt; bar`));
assert(equal(encodeAttr(`foo > bar`), `foo > bar`));
assert(equal(encodeAttr(`foo ' bar`), `foo ' bar`));
assert(equal(encodeAttr(`foo " bar`), `foo &quot; bar`));

assert(equal(encodeAttr!'\''(`foo ' bar`), `foo &apos; bar`));
assert(equal(encodeAttr!'\''(`foo " bar`), `foo " bar`));

assert(equal(encodeAttr("hello world"), "hello world"));

See Also

Meta