www.digitalmars.com

D Programming Language 2.0

Last update Sat Apr 7 20:59:05 2012

arsd.dom

This is an html DOM implementation, started with cloning what the browser offers in Javascript, but going well beyond it in convenience.

If you can do it in Javascript, you can probably do it with this module.

And much more.

Note:
some of the documentation here writes html with added spaces. That's because ddoc doesn't bother encoding html output, and adding spaces is easier than using LT macros everywhere.

BTW:
this file depends on arsd.characterencodings, so help it correctly read files from the internet. You should be able to get characterencodings.d from the same place you got this file.

this(Document _parentDocument);
.

Class / struct Quick Reference
Class / struct NameDescription
CssStyleThis is probably not useful to you unless you're writing a browser or something like that. It represents a *computed* style, like what the browser gives you after applying stylesheets, inline styles, and html attributes. From here, you can start to make a layout engine for the box model and have a css aware browser.
DataSetA proxy object to do the Element class' dataset property. See Element.dataset for more info.
DocumentThe main document interface, including a html parser.
DocumentFragment.
ElementThis represents almost everything in the DOM.
ElementNotFoundExceptionThis is used when you are using one of the require variants of navigation, and no matching element can be found in the tree.
ElementStreamThis is the lazy range that walks the tree for you. It tries to go in the lexical order of the source: node, then children from first to last, each recursively.
ElementStylefor style, i want to be able to set it with a string like a plain attribute, but also be able to do properties Javascript style.
EventThis is a DOM event, like in javascript. Note that this library never fires events - it is only here for you to use if you want it.
Form.
HtmlThe html struct is used to differentiate between regular text nodes and html in certain functions
LinkThere are subclasses of Element offering improved helper functions for the element in HTML. .
MarkupError.
RawSource.
Selector.
SelectorPart.
StackThis is kinda private; just a little utility container for use by the ElementStream class.
StyleSheetThis probably isn't useful, unless you're writing a browser or something like that. You might want to look at arsd.html for css macro, nesting, etc., or just use standard css as text.
Table.
TableCellRepresents anything that can be a table cell - or html.

this(Document _parentDocument, string _tagName);
.

TableRowRepresents a table row element - a
TextNode.
XmlDocumentSpecializes Document for handling generic XML. (always uses strict mode, uses xml mime type and file header)
Enum Quick Reference
Enum NameDescription
NodeType.
Function Quick Reference
Function NameDescription
EventHandlerused for DOM events
FileResourceThis might belong in another module, but it represents a file with a mime type and some data. Document implements this interface with type = text/html (see Document.contentType for more info) and data = document.toString, so you can return Documents anywhere web.d expects FileResources.
camelCaseTranslates a css style property-name to a camel cased propertyName
getElementsBySelectorParts.
htmlEntitiesDecodeThis takes a string of raw HTML and decodes the entities into a nice D utf-8 string. By default, it uses loose mode - it will try to return a useful string from garbage input too. Set the second parameter to true if you'd prefer it to strictly throw exceptions on garbage input.
htmlEntitiesEncodeGiven text, encode all html entities on it - &, <, >, and ". This function also encodes all 8 bit characters as entities, thus ensuring the resultant text will work even if your charset isn't set right.
idToken.
intFromHex.
lexSelector.
parseEntityThis helper function is used for decoding html entities. It has a hard-coded list of entities and characters.
parseSelector.
parseSelectorString.
removeDuplicates.
requireYou can use this to do an easy null check or a dynamic cast+null check on any element.
selectorTokens.
unCamelCaseConverts a camel cased propertyName to a css style dashed property-name
xmlEntitiesEncodeAn alias for htmlEntitiesEncode; it works for xml too
abstract interface FileResource;
This might belong in another module, but it represents a file with a mime type and some data. Document implements this interface with type = text/html (see Document.contentType for more info) and data = document.toString, so you can return Documents anywhere web.d expects FileResources.

Member Quick Reference
Member NameDescription
contentTypethe content-type of the file. e.g. "text/html; charset=utf-8" or "image/png"
getDatathe data
abstract const string contentType();
the content-type of the file. e.g. "text/html; charset=utf-8" or "image/png"

abstract const immutable(ubyte)[] getData();
the data

struct DataSet;
A proxy object to do the Element class' dataset property. See Element.dataset for more info.

Do not create this object directly.

struct ElementStyle;
for style, i want to be able to set it with a string like a plain attribute, but also be able to do properties Javascript style.

enum NodeType;
.

T require(T = Element, string file = __FILE__, int line = __LINE__)(Element e);
You can use this to do an easy null check or a dynamic cast+null check on any element.

class Element;
This represents almost everything in the DOM.

Member Quick Reference
Member NameDescription
addChild.
addChildrenConvenience function to append text intermixed with other children. For example: div.addChildren("You can visit my website by ", new Link("mysite.com", "clicking here"), "."); or div.addChildren("Hello, ", user.name, "!"); See also: appendHtml. This might be a bit simpler though because you don't have to think about escaping.
addClassAdds a string to the class attribute. The class attribute is used a lot in CSS.
addSiblingAnother convenience function. Adds a child directly after the current one, returning the new child.
appendChildAppends the given element to this one. The given element must not have a parent already.
appendChildren.
appendHtmlAppends the given html to the element, returning the elements appended
appendText.
attributesThis is where the attributes are actually stored. You should use getAttribute, setAttribute, and hasAttribute instead.
childElements.
childNodesMutable version of the same
className.
classNamesSplits the className into an array of each class given
classesget all the classes on this element
cloneNodeClones the node. If deepClone is true, clone all inner tags too. If false, only do this tag (and its attributes), but it will have no contents.
clonedThis is a full clone of the element
computedStyle.
datasetHTML5's dataset property. It is an alternate view into attributes with the data- prefix.
directTextSets the direct text, keeping the same place.
firstChildReturns the first child of this element. If it has no children, returns null. Remember, text nodes are children too.
firstInnerTextFetches the first consecutive nodes, if text nodes, concatenated together
getAttributeGets the given attribute value, or null if the attribute is not set.
getElementById.
getElementsByClassName.
getElementsBySelectorDoes a CSS selector
getElementsByTagName.
getParentGets the nearest node, going up the chain, with the given tagName May return null or throw.
hasAttributeReturns if the attribute exists.
hasClassReturns whether the given class appears in this element.
innerHTMLTakes some html and replaces the element's children with the tree made from the string.
innerRawSourceThis sets the inner content of the element *without* trying to parse it. You can inject any code in there; this serves as an escape hatch from the dom.
innerTextFetch the inside text, with all tags stripped out.
insertAfter.
insertBeforeInserts the second element to this node, right before the first param
insertChildAfter.
lastChild
makeConvenience function to try to do the right thing for HTML. This is the main way I create elements.
nextSibling.
nodeType.
nodeValue.
opDispatchProvides easy access to attributes, object style.
outerHTMLReplaces this node with the given html string, which is parsed
outerTextStrips this node out of the document, replacing it with the given text
parentDocumentGet the parent Document object that contains this element. It may be null, so remember to check for that.
parentNode.
prependChildPuts the current element first in our children list. The given element must not have a parent already.
previousSibling.
querySelectorNote:
you can give multiple selectors, separated by commas. It will return the first match it finds.
querySelectorAlla more standards-compliant alias for getElementsBySelector
removeAllChildrenRemoves all inner content from the tag; all child text and elements are gone.
removeAttributeRemoves the given attribute from the element.
removeChildRemoves the given child from this list.
removeChildrenThis removes all the children from this element, returning the old list.
removeClassRemoves a particular class name.
removeFromTreeshorthand for this.parentNode.removeChild(this) with parentNode null check if the element already isn't in a tree, it does nothing.
reparent.
replaceChildReplaces the given element with a whole group.
replaceWithReplaces this element with something else in the tree.
requireElementById.
requireSelector.
setAttributeSets an attribute. Returns this for easy chaining
stealChildren.
stripOutStrips this tag out of the document, putting its inner html as children of the parent.
styleThis sets the style attribute with a string.
swapNodeswaps one child for a new thing. Returns the old child which is now parentless.
tagNameThe name of the tag. Remember, changing this doesn't change the dynamic type of the object.
toStringTurns the whole element, including tag, attributes, and children, into a string which could be pasted into an XML file.
treeReturns a lazy range of all its children, recursively.
wrapInWraps this element inside the given element. It's like this.replaceWith(what); what.appendchild(this);
writeToAppenderThis is the actual implementation used by toString. You can pass it a preallocated buffer to save some time. Returns the string it creates.
string tagName;
The name of the tag. Remember, changing this doesn't change the dynamic type of the object.

string[string] attributes;
This is where the attributes are actually stored. You should use getAttribute, setAttribute, and hasAttribute instead.

Document parentDocument;
Get the parent Document object that contains this element. It may be null, so remember to check for that.

Element parentNode;
.

static Element make(string tagName, string childInfo = null, string childInfo2 = null);
Convenience function to try to do the right thing for HTML. This is the main way I create elements.

this(Document _parentDocument, string _tagName, string[string] _attributes = null, bool _selfClosed = false);
Generally, you don't want to call this yourself - use Element.make or document.createElement instead.

this(string _tagName, string[string] _attributes = null);
Convenience constructor when you don't care about the parentDocument. Note this might break things on the document. Note also that without a parent document, elements are always in strict, case-sensitive mode.

@property Element firstChild();
Returns the first child of this element. If it has no children, returns null. Remember, text nodes are children too.

@property Element lastChild();

@property Element previousSibling(string tagName = null);
.

@property Element nextSibling(string tagName = null);
.

T getParent(T = Element)(string tagName = null);
Gets the nearest node, going up the chain, with the given tagName May return null or throw.

Element getElementById(string id);
.

SomeElementType requireElementById(SomeElementType = Element, string file = __FILE__, int line = __LINE__)(string id);
.

SomeElementType requireSelector(SomeElementType = Element, string file = __FILE__, int line = __LINE__)(string selector);
.

Element querySelector(string selector);
Note:
you can give multiple selectors, separated by commas. It will return the first match it finds.

Element[] querySelectorAll(string selector);
a more standards-compliant alias for getElementsBySelector

Element[] getElementsBySelector(string selector);
Does a CSS selector

-- all, default if nothing else is there

tag#id.class.class.class:pseudo[attrib=what][attrib=what] OP selector

It is all additive

OP

space = descendant > = direct descendant + = sibling (E+F Matches any F element immediately preceded by a sibling element E)

[foo] Foo is present as an attribute [foo="warning"] Matches any E element whose "foo" attribute value is exactly equal to "warning". E[foo~="warning"] Matches any E element whose "foo" attribute value is a list of space-separated values, one of which is exactly equal to "warning" E[lang|="en"] Matches any E element whose "lang" attribute has a hyphen-separated list of values beginning (from the left) with "en".

[item$=sdas] ends with [item^-sdsad] begins with

Quotes are optional here.

Pseudos:
:first-child :last-child :link (same as a[href] for our purposes here)

There can be commas separating the selector. A comma separated list result is OR'd onto the main.

This ONLY cares about elements. text, etc, are ignored

There should be two functions: given element, does it match the selector? and given a selector, give me all the elements

Element[] getElementsByClassName(string cn);
.

Element[] getElementsByTagName(string tag);
.

const string getAttribute(string name);
Gets the given attribute value, or null if the attribute is not set.

Note that the returned string is decoded, so it no longer contains any xml entities.

Element setAttribute(string name, string value);
Sets an attribute. Returns this for easy chaining

bool hasAttribute(string name);
Returns if the attribute exists.

Element removeAttribute(string name);
Removes the given attribute from the element.

const string className();
Gets the class attribute's contents. Returns an empty string if it has no class.

Element className(string c);
.

string opDispatch(string name)(string v = null);
Provides easy access to attributes, object style.

auto element = Element.make("a"); a.href = "cool.html"; // this is the same as a.setAttribute("href", "cool.html"); string where = a.href; // same as a.getAttribute("href");

const @property const(Element[]) childNodes();
Returns the element's children.

@property Element[] childNodes();
Mutable version of the same

@property string[] classes();
get all the classes on this element

Element addClass(string c);
Adds a string to the class attribute. The class attribute is used a lot in CSS.

Element removeClass(string c);
Removes a particular class name.

bool hasClass(string c);
Returns whether the given class appears in this element.

DataSet dataset();
HTML5's dataset property. It is an alternate view into attributes with the data- prefix.

Given:

We get: assert(a.
dataset.myProperty == "cool");

@property ElementStyle style();
Provides both string and object style (like in Javascript) access to the style attribute.

@property ElementStyle style(string s);
This sets the style attribute with a string.

@property CssStyle computedStyle();
.

void removeAllChildren();
Removes all inner content from the tag; all child text and elements are gone.

Element addChild(string tagName, string childInfo = null, string childInfo2 = null);
convenience function to quickly add a tag with some text or other relevant info (for example, it's a src for an element instead of inner text)

Element addSibling(string tagName, string childInfo = null, string childInfo2 = null);
Another convenience function. Adds a child directly after the current one, returning the new child.

Between this, addChild, and parentNode, you can build a tree as a single expression.

void addChildren(T...)(T t);
Convenience function to append text intermixed with other children. For example: div.addChildren("You can visit my website by ", new Link("mysite.com", "clicking here"), "."); or div.addChildren("Hello, ", user.name, "!"); See also: appendHtml. This might be a bit simpler though because you don't have to think about escaping.

Element addChild(string tagName, Element firstChild);
.

Element appendChild(Element e);
Appends the given element to this one. The given element must not have a parent already.

void appendChildren(Element[] children);
.

Element insertBefore(in Element where, Element what);
Inserts the second element to this node, right before the first param

Element insertAfter(in Element where, Element what);
.

Element swapNode(Element child, Element replacement);
swaps one child for a new thing. Returns the old child which is now parentless.

Element appendText(string text);
.

@property Element[] childElements();
.

Element[] appendHtml(string html);
Appends the given html to the element, returning the elements appended

void reparent(Element newParent);
.

void insertChildAfter(Element child, Element where);
.

Element[] stealChildren(Element e, Element position = null);
.

Element prependChild(Element e);
Puts the current element first in our children list. The given element must not have a parent already.

const @property string innerHTML(Appender!(string) where = appender(null));
Returns a string containing all child elements, formatted such that it could be pasted into an XML file.

@property void innerHTML(string html);
@property void innerHTML(Html html);
Takes some html and replaces the element's children with the tree made from the string.

@property Element[] outerHTML(string html);
Replaces this node with the given html string, which is parsed

Note:
this invalidates the this reference, since it is removed from the tree.

Returns the new children that replace this.

@property string outerHTML();
Returns all the html for this element, including the tag itself. This is equivalent to calling toString().

@property void innerRawSource(string rawSource);
This sets the inner content of the element *without* trying to parse it. You can inject any code in there; this serves as an escape hatch from the dom.

The only times you might actually need it are for < style > and < script > tags in html. Other than that, innerHTML and/or innerText should do the job.

Element replaceChild(Element find, Element replace);
.

Element removeChild(Element c);
Removes the given child from this list.

Returns the removed element.

Element[] removeChildren();
This removes all the children from this element, returning the old list.

void replaceChild(Element find, Element[] replace);
Replaces the given element with a whole group.

void stripOut();
Strips this tag out of the document, putting its inner html as children of the parent.

For example, given:

hello there

, if you call stripOut() on the b element, you'll be left with

hello there

. The idea here is to make it easy to get rid of garbage markup you aren't interested in.

Element removeFromTree();
shorthand for this.parentNode.removeChild(this) with parentNode null check if the element already isn't in a tree, it does nothing.

Element wrapIn(Element what);
Wraps this element inside the given element. It's like this.replaceWith(what); what.appendchild(this);

Given:
< b >cool</ b >, if you call b.wrapIn(new Link("site.com", "my site is ")); you'll end up with: < a href="site.com">my site is < b >cool< /b ></ a >.

Element replaceWith(Element e);
Replaces this element with something else in the tree.

const string[] classNames();
Splits the className into an array of each class given

const string firstInnerText();
Fetches the first consecutive nodes, if text nodes, concatenated together

If the first node is not text, returns null.

See also: directText, innerText

const @property string innerText();
Fetch the inside text, with all tags stripped out.

cool api & code dude

innerText of that is "cool api & code dude".

@property void innerText(string text);
Sets the inside text, replacing all children. You don't have to worry about entity encoding.

@property string directText();
Returns the text directly under this element, not recursively like innerText.

See also: firstInnerText

@property void directText(string text);
Sets the direct text, keeping the same place.

Unlike innerText, this does *not* remove existing elements in the element.

It only replaces the first text node it sees.

If there are no text nodes, it calls appendText

So, given (ignore the spaces in the tags): < div > < img > text here < /div >

it will keep the img, and replace the "text here".

@property void outerText(string text);
Strips this node out of the document, replacing it with the given text

const @property string outerText();
Same result as innerText; the tag with all inner tags stripped out

@property Element cloned();
This is a full clone of the element

Element cloneNode(bool deepClone);
Clones the node. If deepClone is true, clone all inner tags too. If false, only do this tag (and its attributes), but it will have no contents.

const string nodeValue();
.

const @property int nodeType();
.

const string toString();
Turns the whole element, including tag, attributes, and children, into a string which could be pasted into an XML file.

const string writeToAppender(Appender!(string) where = appender(null));
This is the actual implementation used by toString. You can pass it a preallocated buffer to save some time. Returns the string it creates.

@property ElementStream tree();
Returns a lazy range of all its children, recursively.

class DocumentFragment: arsd.dom.Element;
.

Member Quick Reference
Member NameDescription
writeToAppender.
this(Document _parentDocument);
.

const string writeToAppender(Appender!(string) where = appender(null));
.

string htmlEntitiesEncode(string data, Appender!(string) output = appender(null));
Given text, encode all html entities on it - &, <, >, and ". This function also encodes all 8 bit characters as entities, thus ensuring the resultant text will work even if your charset isn't set right.

The output parameter can be given to append to an existing buffer. You don't have to pass one; regardless, the return value will be usable for you, with just the data encoded.

string xmlEntitiesEncode(string data);
An alias for htmlEntitiesEncode; it works for xml too

dchar parseEntity(in dchar[] entity);
This helper function is used for decoding html entities. It has a hard-coded list of entities and characters.

string htmlEntitiesDecode(string data, bool strict = false);
This takes a string of raw HTML and decodes the entities into a nice D utf-8 string. By default, it uses loose mode - it will try to return a useful string from garbage input too. Set the second parameter to true if you'd prefer it to strictly throw exceptions on garbage input.

class RawSource: arsd.dom.Element;
.

Member Quick Reference
Member NameDescription
appendChild.
nodeType.
nodeValue.
source.
writeToAppender.
this(Document _parentDocument, string s);
.

const string nodeValue();
.

const int nodeType();
.

const string writeToAppender(Appender!(string) where = appender(null));
.

Element appendChild(Element e);
.

string source;
.

class TextNode: arsd.dom.Element;
.

Member Quick Reference
Member NameDescription
appendChild.
cloned.
contents.
fromUndecodedString.
nodeType.
nodeValue.
writeToAppender.
this(Document _parentDocument, string e);
.

static TextNode fromUndecodedString(Document _parentDocument, string html);
.

@property Element cloned();
.

const string nodeValue();
.

const int nodeType();
.

const string writeToAppender(Appender!(string) where = appender(null));
.

Element appendChild(Element e);
.

string contents;
.

class Link: arsd.dom.Element;
There are subclasses of Element offering improved helper functions for the element in HTML. .

Member Quick Reference
Member NameDescription
getValueThis gets a variable from the URL's query string.
removeValueRemoves the given variable from the query string
setValueSets or adds the variable with the given name to the given value It automatically URI encodes the values and takes care of the ? and &.
updateQueryString.
this(Document _parentDocument);
.

this(string href, string text);
.

string getValue(string name);
This gets a variable from the URL's query string.

void updateQueryString(string[string] vars);
.

void setValue(string name, string variable);
Sets or adds the variable with the given name to the given value It automatically URI encodes the values and takes care of the ? and &.

void removeValue(string name);
Removes the given variable from the query string

class Form: arsd.dom.Element;
.

Member Quick Reference
Member NameDescription
addFieldAdds a new INPUT field to the end of the form with the given attributes.
addValueArrayThis takes an array of strings and adds hidden elements for each one of them. Unlike setValue, it makes no attempt to find and modify existing elements in the form to the new values.
getFieldGets the actual elements with the given name
getLabelGrabs the
getPostableData.
getValueGets the value of the field; what would be given if it submitted right now. (so it handles select boxes and radio buttons too). For checkboxes, if a value isn't given, but it is checked, it returns "checked", since null and "" are indistinguishable
removeFieldRemoves the given field from the form. It finds the element and knocks it right out.
setValueSet's the form field's value. For input boxes, this sets the value attribute. For textareas, it sets the innerText. For radio boxes and select boxes, it removes the checked/selected attribute from all, and adds it to the one matching the value. For checkboxes, if the value is non-null and not empty, it checks the box. If you set a value that doesn't exist, it throws an exception if makeNew is false. Otherwise, it makes a new input with type=hidden to keep the value.
this(Document _parentDocument);
.

void setValue(string field, string value, bool makeNew = true);
Set's the form field's value. For input boxes, this sets the value attribute. For textareas, it sets the innerText. For radio boxes and select boxes, it removes the checked/selected attribute from all, and adds it to the one matching the value. For checkboxes, if the value is non-null and not empty, it checks the box. If you set a value that doesn't exist, it throws an exception if makeNew is false. Otherwise, it makes a new input with type=hidden to keep the value.

void addValueArray(string key, string[] arrayOfValues);
This takes an array of strings and adds hidden elements for each one of them. Unlike setValue, it makes no attempt to find and modify existing elements in the form to the new values.

string getValue(string field);
Gets the value of the field; what would be given if it submitted right now. (so it handles select boxes and radio buttons too). For checkboxes, if a value isn't given, but it is checked, it returns "checked", since null and "" are indistinguishable

string getPostableData();
.

Element[] getField(string name);
Gets the actual elements with the given name

Element getLabel(string forId);
Grabs the
Element addField(string name, string value, string type = "hidden");
Adds a new INPUT field to the end of the form with the given attributes.

void removeField(string name);
Removes the given field from the form. It finds the element and knocks it right out.

class Table: arsd.dom.Element;
.

, , or portion if you pass one.

Note:
the rectangular grid might include null cells.

This is kinda expensive so you should call once when you want the grid, then do lookups on the returned array.

Member Quick Reference
Member NameDescription
appendRow.
caption.
captionElement.
getGridGets the logical layout of the table as a rectangular grid of cells. It considers rowspan and colspan. A cell with a large span is represented in the grid by being referenced several times. The tablePortition parameter can get just a
td.
th.
this(Document _parentDocument);
.

Element th(T)(T t);
.

Element td(T)(T t);
.

Element appendRow(T...)(T t);
.

Element captionElement();
.

@property string caption();
.

@property void caption(string text);
.

TableCell[][] getGrid(Element tablePortition = null);
Gets the logical layout of the table as a rectangular grid of cells. It considers rowspan and colspan. A cell with a large span is represented in the grid by being referenced several times. The tablePortition parameter can get just a , , or portion if you pass one.

Note:
the rectangular grid might include null cells.

This is kinda expensive so you should call once when you want the grid, then do lookups on the returned array.

class TableRow: arsd.dom.Element;
Represents a table row element - a

this(Document _parentDocument);
.

class TableCell: arsd.dom.Element;
Represents anything that can be a table cell - or html.

this(Document _parentDocument, string _tagName);
.

class MarkupError: object.Exception;
.

this(string message);
.

class ElementNotFoundException: object.Exception;
This is used when you are using one of the require variants of navigation, and no matching element can be found in the tree.

this(string type, string search, string file = __FILE__, int line = __LINE__);
type == kind of element you were looking for and search == a selector describing the search.

struct Html;
The html struct is used to differentiate between regular text nodes and html in certain functions

Easiest way to construct it is like this: auto html = Html("

hello

");

Member Quick Reference
Member NameDescription
sourceThis string holds the actual html. Use it to retrieve the contents.
string source;
This string holds the actual html. Use it to retrieve the contents.

class Document: arsd.dom.FileResource;
The main document interface, including a html parser.

Member Quick Reference
Member NameDescription
clear.
contentTypeIf you're using this for some other kind of XML, you can set the content type here.
createElement.
createForm.
createFragment.
createTextNode.
findFirst.
forms.
getDataimplementing the FileResource interface; it calls toString.
getElementByIdThese functions all forward to the root element. See the documentation in the Element class.
getElementsBySelectorThese functions all forward to the root element. See the documentation in the Element class.
getElementsByTagNameThese functions all forward to the root element. See the documentation in the Element class.
getFirstElementByTagNameFIXME:
btw, this could just be a lazy range......
getMetathis uses a weird thing... it's [name=] if no colon and [property=] if colon
loose.
mainBodyThis returns the element, if there is one. (It different than Javascript, where it is called 'body', because body is a keyword in D.)

opIndexThis is just something I'm toying with. Right now, you use opIndex to put in css selectors. It returns a struct that forwards calls to all elements it holds, and returns itself so you can chain it.
parseTake XMLish data and try to make the DOM tree out of it.
parseGarbageConcatenates any consecutive text nodes Given the kind of garbage you find on the Internet, try to make sense of it. Equivalent to document.parse(data, false, false, null); (Case-insensitive, non-strict, determine character encoding from the data.)
prolog.
querySelectorThese functions all forward to the root element. See the documentation in the Element class.
querySelectorAllThese functions all forward to the root element. See the documentation in the Element class.
requireElementByIdThese functions all forward to the root element. See the documentation in the Element class.
requireSelectorThese functions all forward to the root element. See the documentation in the Element class.
root.
setMetaSets a meta tag in the document header. It is kinda hacky to work easily for both Facebook open graph and traditional html meta tags/
setProlog.
titleSets the title of the page, creating a element if needed.<p></p>
toString.
this(string data, bool caseSensitive = false, bool strict = false);
.

this();
Creates an empty document. It has *nothing* in it at all.

ElementCollection opIndex(string selector);
This is just something I'm toying with. Right now, you use opIndex to put in css selectors. It returns a struct that forwards calls to all elements it holds, and returns itself so you can chain it.

Example:
document["p"].innerText("hello").addClass("modified");

Equivalent to: foreach(e; document.getElementsBySelector("p")) { e.innerText("hello"); e.addClas("modified"); }

Note:
always use function calls (not property syntax) and don't use toString in there for best results.

You can also do things like: document["p"]["b"] though tbh I'm not sure why since the selector string can do all that anyway. Maybe you could put in some kind of custom filter function tho.

string contentType(string mimeType);
If you're using this for some other kind of XML, you can set the content type here.

Note:
this has no impact on the function of this class. It is only used if the document is sent via a protocol like HTTP.

This may be called by parse() if it recognizes the data. Otherwise, if you don't set it, it assumes text/html; charset=utf-8.

const string contentType();
implementing the FileResource interface, useful for sending via http automatically.

const immutable(ubyte)[] getData();
implementing the FileResource interface; it calls toString.

void parseGarbage(string data);
Concatenates any consecutive text nodes Given the kind of garbage you find on the Internet, try to make sense of it. Equivalent to document.parse(data, false, false, null); (Case-insensitive, non-strict, determine character encoding from the data.)

NOTE:
this makes no attempt at added security.

void parse(in string rawdata, bool caseSensitive = false, bool strict = false, string dataEncoding = "UTF-8");
Take XMLish data and try to make the DOM tree out of it.

The goal isn't to be perfect, but to just be good enough to approximate Javascript's behavior.

If strict, it throws on something that doesn't make sense. (Examples: mismatched tags. It doesn't validate!) If not strict, it tries to recover anyway, and only throws when something is REALLY unworkable.

If strict is false, it uses a magic list of tags that needn't be closed. If you are writing a document specifically for this, try to avoid such - use self closed tags at least. Easier to parse.

The dataEncoding argument can be used to pass a specific charset encoding for automatic conversion. If null (which is NOT the default!), it tries to determine from the data itself, using the xml prolog or meta tags, and assumes UTF-8 if unsure.

If this assumption is wrong, it can throw on non-ascii characters!

Note that it previously assumed the data was encoded as UTF-8, which is why the dataEncoding argument defaults to that.

So it shouldn't break backward compatibility.

But, if you want the best behavior on wild data - figuring it out from the document instead of assuming - you'll probably want to change that argument to null.

@property string title();
Gets the element's innerText, if one exists<p></p>
@property void title(string t);
Sets the title of the page, creating a element if needed.<p></p>
Element getElementById(string id);
SomeElementType requireElementById(SomeElementType = Element)(string id);
SomeElementType requireSelector(SomeElementType = Element, string file = __FILE__, int line = __LINE__)(string selector);
Element querySelector(string selector);
Element[] querySelectorAll(string selector);
Element[] getElementsBySelector(string selector);
Element[] getElementsByTagName(string tag);
These functions all forward to the root element. See the documentation in the Element class.

Element getFirstElementByTagName(string tag);
FIXME:
btw, this could just be a lazy range......

Element mainBody();
This returns the element, if there is one. (It different than Javascript, where it is called 'body', because body is a keyword in D.)

string getMeta(string name);
this uses a weird thing... it's [name=] if no colon and [property=] if colon

void setMeta(string name, string value);
Sets a meta tag in the document header. It is kinda hacky to work easily for both Facebook open graph and traditional html meta tags/

Form[] forms();
.

Form createForm();
.

Element createElement(string name);
.

Element createFragment();
.

Element createTextNode(string content);
.

Element findFirst(bool delegate(Element) doesItMatch);
.

void clear();
.

void setProlog(string d);
.

string prolog;
.

const string toString();
.

Element root;
.

bool loose;
.

class XmlDocument: arsd.dom.Document;
Specializes Document for handling generic XML. (always uses strict mode, uses xml mime type and file header)

int intFromHex(string hex);
.

static immutable string[] selectorTokens;
.

sizediff_t idToken(string str, sizediff_t position);
.

string[] lexSelector(string selector);
.

struct SelectorPart;
.

Member Quick Reference
Member NameDescription
attributesEndsWith[attr$=value]
attributesEqual[attr=value]
attributesInclude[attr*=value]
attributesIncludesSeparatedByDashes[attr|=value]
attributesIncludesSeparatedBySpaces[attr~=value]
attributesNotEqual[attr!=value] -- extension by me
attributesPresent[attr]
attributesStartsWith[attr^=value]
emptyElement.
evenChild.
firstChild.
lastChild.
matchElement.
oddChild.
rootElement.
separation-1 == only itself; the null selector, 0 == tree, 1 == childNodes, 2 == childAfter, 3 == youngerSibling, 4 == parentOf
tagNameFilter.
toString.
string tagNameFilter;
.

string[] attributesPresent;
[attr]

string[2u][] attributesEqual;
[attr=value]

string[2u][] attributesStartsWith;
[attr^=value]

string[2u][] attributesEndsWith;
[attr$=value]

string[2u][] attributesIncludesSeparatedBySpaces;
[attr~=value]

string[2u][] attributesIncludesSeparatedByDashes;
[attr|=value]

string[2u][] attributesInclude;
[attr*=value]

string[2u][] attributesNotEqual;
[attr!=value] -- extension by me

bool firstChild;
.

bool lastChild;
.

bool emptyElement;
.

bool oddChild;
.

bool evenChild;
.

bool rootElement;
.

int separation;
-1 == only itself; the null selector, 0 == tree, 1 == childNodes, 2 == childAfter, 3 == youngerSibling, 4 == parentOf

string toString();
.

bool matchElement(Element e);
.

Element[] getElementsBySelectorParts(Element start, SelectorPart[] parts);
.

struct Selector;
.

Member Quick Reference
Member NameDescription
fromString.
getElements.
matchElementIf relativeTo == null, it assumes the root of the parent document.
parts.
toString.
SelectorPart[] parts;
.

string toString();
.

Element[] getElements(Element start);
.

bool matchElement(Element e, Element relativeTo = null);
If relativeTo == null, it assumes the root of the parent document.

static Selector fromString(string selector);
.

Selector[] parseSelectorString(string selector, bool caseSensitiveTags = true);
.

Selector parseSelector(string[] tokens, bool caseSensitiveTags = true);
.

Element[] removeDuplicates(Element[] input);
.

string unCamelCase(string a);
Converts a camel cased propertyName to a css style dashed property-name

string camelCase(string a);
Translates a css style property-name to a camel cased propertyName

class CssStyle;
This is probably not useful to you unless you're writing a browser or something like that. It represents a *computed* style, like what the browser gives you after applying stylesheets, inline styles, and html attributes. From here, you can start to make a layout engine for the box model and have a css aware browser.

Member Quick Reference
Member NameDescription
Property.
Specificity.
classes.
expandShortForm.
getSpecificityOfRule.
getValuetakes dash style name
givenExplicitlythis is false if for example the user said "padding" and this is "padding-left"
ids.
important0 = none, 1 = stylesheet author, 2 = inline style, 3 = user important .
name.
opDispatch.
originatingRule.
originatingSpecificity.
properties.
score.
setValuetakes dash style name
specificity.
tags.
toString.
value.
this(string rule, string content);
.

Specificity getSpecificityOfRule(string rule);
.

string originatingRule;
.

Specificity originatingSpecificity;
.

union Specificity;
.

uint score;
.

ubyte tags;
.

ubyte classes;
.

ubyte ids;
.

ubyte important;
0 = none, 1 = stylesheet author, 2 = inline style, 3 = user important .

struct Property;
.

bool givenExplicitly;
this is false if for example the user said "padding" and this is "padding-left"

string name;
.

string value;
.

Specificity specificity;
.

Property[] properties;
.

string opDispatch(string nameGiven)(string value = null);
.

string getValue(string name);
takes dash style name

string setValue(string name, string value, Specificity newSpecificity, bool explicit = true);
takes dash style name

void expandShortForm(Property p, Specificity specificity);
.

string toString();
.

class StyleSheet;
This probably isn't useful, unless you're writing a browser or something like that. You might want to look at arsd.html for css macro, nesting, etc., or just use standard css as text.

The idea, however, is to represent a kind of CSS object model, complete with specificity, that you can apply to your documents to build the complete computedStyle object.

Member Quick Reference
Member NameDescription
applyRun through the document and apply this stylesheet to it. The computedStyle member will be accurate after this call
rules.
CssStyle[] rules;
.

this(string source);
.

void apply(Document document);
Run through the document and apply this stylesheet to it. The computedStyle member will be accurate after this call

class Stack(T);
This is kinda private; just a little utility container for use by the ElementStream class.

Member Quick Reference
Member NameDescription
arr.
empty.
peek.
pop.
push.
void push(T t);
.

T pop();
.

T peek();
.

bool empty();
.

T[] arr;
.

class ElementStream;
This is the lazy range that walks the tree for you. It tries to go in the lexical order of the source: node, then children from first to last, each recursively.

Member Quick Reference
Member NameDescription
currentKilledYou should call this when you remove an element from the tree. It then doesn't recurse into that node and adjusts the current position, keeping the range stable.
empty.
front.
popFront.
@property Element front();
.

this(Element start);
Use Element.tree instead.

void popFront();
.

void currentKilled();
You should call this when you remove an element from the tree. It then doesn't recurse into that node and adjusts the current position, keeping the range stable.

@property bool empty();
.

alias EventHandler;
used for DOM events

class Event;
This is a DOM event, like in javascript. Note that this library never fires events - it is only here for you to use if you want it.

Member Quick Reference
Member NameDescription
dispatchthis dispatches the element using the capture -> target -> bubble process
preventDefaultPrevents the default event handler (if there is one) from being called
sendthis sends it only to the target. If you want propagation, use dispatch() instead.
stopPropagationStops the event propagation immediately.
void preventDefault();
Prevents the default event handler (if there is one) from being called

void stopPropagation();
Stops the event propagation immediately.

void send();
this sends it only to the target. If you want propagation, use dispatch() instead.

void dispatch();
this dispatches the element using the capture -> target -> bubble process