/usr/include/d/diet/diet/dom.d is in libdiet-dev 1.4.4-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | /** Types to represent the DOM tree.
The DOM tree is used as an intermediate representation between the parser
and the generator. Filters and other kinds of transformations can be
executed on the DOM tree. The generator itself will apply filters and
other traits using `diet.traits.applyTraits`.
*/
module diet.dom;
import diet.internal.string;
string expectText(const(Attribute) att)
{
import diet.defs : enforcep;
if (att.contents.length == 0) return null;
enforcep(att.isText, "'"~att.name~"' expected to be a pure text attribute.", att.loc);
return att.contents[0].value;
}
string expectText(const(Node) n)
{
import diet.defs : enforcep;
if (n.contents.length == 0) return null;
enforcep(n.contents.length > 0 && n.contents[0].kind == NodeContent.Kind.text &&
(n.contents.length == 1 || n.contents[1].kind != NodeContent.Kind.node),
"Expected pure text node.", n.loc);
return n.contents[0].value;
}
string expectExpression(const(Attribute) att)
{
import diet.defs : enforcep;
enforcep(att.isExpression, "'"~att.name~"' expected to be an expression attribute.", att.loc);
return att.contents[0].value;
}
bool isExpression(const(Attribute) att) { return att.contents.length == 1 && att.contents[0].kind == AttributeContent.Kind.interpolation; }
bool isText(const(Attribute) att) { return att.contents.length == 0 || att.contents.length == 1 && att.contents[0].kind == AttributeContent.Kind.text; }
/** Converts an array of attribute contents to node contents.
*/
NodeContent[] toNodeContent(in AttributeContent[] contents, Location loc)
{
auto ret = new NodeContent[](contents.length);
foreach (i, ref c; contents) {
final switch (c.kind) {
case AttributeContent.Kind.text: ret[i] = NodeContent.text(c.value, loc); break;
case AttributeContent.Kind.interpolation: ret[i] = NodeContent.interpolation(c.value, loc); break;
case AttributeContent.Kind.rawInterpolation: ret[i] = NodeContent.rawInterpolation(c.value, loc); break;
}
}
return ret;
}
/** Encapsulates a full Diet template document.
*/
/*final*/ class Document { // non-final because of https://issues.dlang.org/show_bug.cgi?id=17146
Node[] nodes;
this(Node[] nodes) { this.nodes = nodes; }
}
/** Represents a single node in the DOM tree.
*/
/*final*/ class Node { // non-final because of https://issues.dlang.org/show_bug.cgi?id=17146
@safe nothrow:
/// A set of names that identify special-purpose nodes
enum SpecialName {
/** Normal comment. The content will appear in the output if the output
format supports comments.
*/
comment = "//",
/** Hidden comment. The content will never appear in the output.
*/
hidden = "//-",
/** D statement. A node that has pure text as its first content,
optionally followed by any number of child nodes. The text content
is either a complete D statement, or an open block statement
(without a block statement appended). In the latter case, all nested
nodes are considered to be part of the block statement's body by
the generator.
*/
code = "-",
/** A dummy node that contains only text and string interpolations.
These nodes behave the same as if their node content would be
inserted in their place, except that they will cause whitespace
(usually a space or a newline) to be prepended in the output, if
they are not the first child of their parent.
*/
text = "|",
/** Filter node. These nodes contain only text and string interpolations
and have a "filterChain" attribute that contains a space separated
list of filter names that are applied in reverse order when the
traits (see `diet.traits.applyTraits`) are applied by the generator.
*/
filter = ":"
}
/// Start location of the node in the source file.
Location loc;
/// Name of the node
string name;
/// A key-value set of attributes.
Attribute[] attributes;
/// The main contents of the node.
NodeContent[] contents;
/// Flags that control the parser and generator behavior.
NodeAttribs attribs;
/// Constructs a new node.
this(Location loc = Location.init, string name = null, Attribute[] attributes = null, NodeContent[] contents = null, NodeAttribs attribs = NodeAttribs.none)
{
this.loc = loc;
this.name = name;
this.attributes = attributes;
this.contents = contents;
this.attribs = attribs;
}
/// Returns the "id" attribute.
@property inout(Attribute) id() inout { return getAttribute("id"); }
/// Returns "class" attribute - a white space separated list of style class identifiers.
@property inout(Attribute) class_() inout { return getAttribute("class"); }
/** Adds a piece of text to the node's contents.
If the node already has some content and the last piece of content is
also text, with a matching location, the text will be appended to that
`NodeContent`'s value. Otherwise, a new `NodeContent` will be appended.
Params:
text = The text to append to the node
loc = Location in the source file
*/
void addText(string text, in ref Location loc)
{
if (contents.length && contents[$-1].kind == NodeContent.Kind.text && contents[$-1].loc == loc)
contents[$-1].value ~= text;
else contents ~= NodeContent.text(text, loc);
}
/** Removes all content if it conists of only white space. */
void stripIfOnlyWhitespace()
{
if (!this.hasNonWhitespaceContent)
contents = null;
}
/** Determines if this node has any non-whitespace contents. */
bool hasNonWhitespaceContent()
const {
import std.algorithm.searching : any;
return contents.any!(c => c.kind != NodeContent.Kind.text || c.value.ctstrip.length > 0);
}
/** Strips any leading whitespace from the contents. */
void stripLeadingWhitespace()
{
while (contents.length >= 1 && contents[0].kind == NodeContent.Kind.text) {
contents[0].value = ctstripLeft(contents[0].value);
if (contents[0].value.length == 0)
contents = contents[1 .. $];
else break;
}
}
/** Strips any trailign whitespace from the contents. */
void stripTrailingWhitespace()
{
while (contents.length >= 1 && contents[$-1].kind == NodeContent.Kind.text) {
contents[$-1].value = ctstripRight(contents[$-1].value);
if (contents[$-1].value.length == 0)
contents = contents[0 .. $-1];
else break;
}
}
/// Tests if the node consists of only a single, static string.
bool isTextNode() const { return contents.length == 1 && contents[0].kind == NodeContent.Kind.text; }
/// Tests if the node consists only of text and interpolations, but doesn't contain child nodes.
bool isProceduralTextNode() const { import std.algorithm.searching : all; return contents.all!(c => c.kind != NodeContent.Kind.node); }
bool hasAttribute(string name)
const {
foreach (ref a; this.attributes)
if (a.name == name)
return true;
return false;
}
/** Returns a given named attribute.
If the attribute doesn't exist, an empty value will be returned.
*/
inout(Attribute) getAttribute(string name)
inout @trusted {
foreach (ref a; this.attributes)
if (a.name == name)
return a;
return cast(inout)Attribute(this.loc, name, null);
}
void setAttribute(Attribute att)
{
foreach (ref da; attributes)
if (da.name == att.name) {
da = att;
return;
}
attributes ~= att;
}
/// Outputs a simple string representation of the node.
override string toString() const {
scope (failure) assert(false);
import std.string : format;
return format("Node(%s, %s, %s, %s, %s)", this.tupleof);
}
/// Compares all properties of two nodes for equality.
override bool opEquals(Object other_) {
auto other = cast(Node)other_;
if (!other) return false;
return this.opEquals(other);
}
bool opEquals(in Node other) const { return this.tupleof == other.tupleof; }
}
/** Flags that control parser or generator behavior.
*/
enum NodeAttribs {
none = 0,
translated = 1<<0, /// Translate node contents
textNode = 1<<1, /// All nested lines are treated as text
rawTextNode = 1<<2, /// All nested lines are treated as raw text (no interpolations or inline tags)
fitOutside = 1<<3, /// Don't insert white space outside of the node when generating output (currently ignored by the HTML generator)
fitInside = 1<<4, /// Don't insert white space around the node contents when generating output (currently ignored by the HTML generator)
}
/** A single node attribute.
Attributes are key-value pairs, where the value can either be empty
(considered as a Boolean value of `true`), a string with optional
string interpolations, or a D expression (stored as a single
`interpolation` `AttributeContent`).
*/
struct Attribute {
@safe nothrow:
/// Location in source file
Location loc;
/// Name of the attribute
string name;
/// Value of the attribute
AttributeContent[] contents;
/// Creates a new attribute with a static text value.
static Attribute text(string name, string value, Location loc) { return Attribute(loc, name, [AttributeContent.text(value)]); }
/// Creates a new attribute with an expression based value.
static Attribute expr(string name, string value, Location loc) { return Attribute(loc, name, [AttributeContent.interpolation(value)]); }
this(Location loc, string name, AttributeContent[] contents)
{
this.name = name;
this.contents = contents;
this.loc = loc;
}
/// Creates a copy of the attribute.
@property Attribute dup() const { return Attribute(loc, name, contents.dup); }
/** Appends raw text to the attribute.
If the attribute already has contents and the last piece of content is
also text, then the text will be appended to the value of that
`AttributeContent`. Otherwise, a new `AttributeContent` will be
appended to `contents`.
*/
void addText(string str)
{
if (contents.length && contents[$-1].kind == AttributeContent.Kind.text)
contents[$-1].value ~= str;
else
contents ~= AttributeContent.text(str);
}
/** Appends a list of contents.
If the list of contents starts with a text `AttributeContent`, then this
first part will be appended using the same rules as for `addText`. The
remaining parts will be appended normally.
*/
void addContents(const(AttributeContent)[] contents)
{
if (contents.length > 0 && contents[0].kind == AttributeContent.Kind.text) {
addText(contents[0].value);
contents = contents[1 .. $];
}
this.contents ~= contents;
}
}
/** A single piece of an attribute value.
*/
struct AttributeContent {
@safe nothrow:
///
enum Kind {
text, /// Raw text (will be escaped by the generator as necessary)
interpolation, /// A D expression that will be converted to text at runtime (escaped as necessary)
rawInterpolation /// A D expression that will be converted to text at runtime (not escaped)
}
/// Kind of this attribute content
Kind kind;
/// The value - either text or a D expression
string value;
/// Creates a new text attribute content value.
static AttributeContent text(string text) { return AttributeContent(Kind.text, text); }
/// Creates a new string interpolation attribute content value.
static AttributeContent interpolation(string expression) { return AttributeContent(Kind.interpolation, expression); }
/// Creates a new raw string interpolation attribute content value.
static AttributeContent rawInterpolation(string expression) { return AttributeContent(Kind.rawInterpolation, expression); }
}
/** A single piece of node content.
*/
struct NodeContent {
@safe nothrow:
///
enum Kind {
node, /// A child node
text, /// Raw text (not escaped in the output)
interpolation, /// A D expression that will be converted to text at runtime (escaped as necessary)
rawInterpolation /// A D expression that will be converted to text at runtime (not escaped)
}
/// Kind of this node content
Kind kind;
/// Location of the content in the source file
Location loc;
/// The node - only used for `Kind.node`
Node node;
/// The string value - either text or a D expression
string value;
/// Creates a new child node content value.
static NodeContent tag(Node node) { return NodeContent(Kind.node, node.loc, node); }
/// Creates a new text node content value.
static NodeContent text(string text, Location loc) { return NodeContent(Kind.text, loc, Node.init, text); }
/// Creates a new string interpolation node content value.
static NodeContent interpolation(string text, Location loc) { return NodeContent(Kind.interpolation, loc, Node.init, text); }
/// Creates a new raw string interpolation node content value.
static NodeContent rawInterpolation(string text, Location loc) { return NodeContent(Kind.rawInterpolation, loc, Node.init, text); }
/// Compares node content for equality.
bool opEquals(in ref NodeContent other)
const {
if (this.kind != other.kind) return false;
if (this.loc != other.loc) return false;
if (this.value != other.value) return false;
if (this.node is other.node) return true;
if (this.node is null || other.node is null) return false;
return this.node.opEquals(other.node);
}
}
/// Represents the location of an entity within the source file.
struct Location {
/// Name of the source file
string file;
/// Zero based line index within the file
int line;
}
|