Represents a HTML form. This slightly specializes Element to add a few more convenience methods for adding and extracting form data.

Please note this object may not be used for all <form> tags.


this(Document _parentDocument)




Element addField(string label, string name, string type, FormFieldOptions fieldOptions)
Element addField(string label, string name, FormFieldOptions fieldOptions)
Element addField(string label, string name, string[string] options, FormFieldOptions fieldOptions)

Overrides of the base class implementations that more confirm to *my* conventions when writing form html.

Element addInput(string name, string value, string type)

Adds a new INPUT field to the end of the form with the given attributes.

void addValueArray(string key, string[] arrayOfValues)

This takes an array of strings and adds hidden <input> 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.

Element[] getField(string name)

Gets the actual elements with the given name

Element getLabel(string forId)

Grabs the <label> with the given for tag, if there is one.

string getPostableData()

Returns the form's contents in application/x-www-form-urlencoded format.

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

void removeField(string name)

Removes the given field from the form. It finds the element and knocks it right out.

void setValue(string field, string value)

Overrides of the base class implementations that more confirm to *my* conventions when writing form html.

void setValue(string field, string value, bool makeNew)

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.

Inherited Members

From Element

ElementCollection opIndex(string selector)

Returns a collection of elements by selector. See: Document.opIndex

Element opIndex(size_t index)

Returns the child node with the particular index.

SomeElementType requireElementById(string id, string file, size_t line)

Calls getElementById, but throws instead of returning null if the element is not found. You can also ask for a specific subclass of Element to dynamically cast to, which also throws if it cannot be done.

SomeElementType requireSelector(string selector, string file, size_t line)

ditto but with selectors instead of ids

MaybeNullElement!SomeElementType optionSelector(string selector, string file, size_t line)

If a matching selector is found, it returns that Element. Otherwise, the returned object returns null for all methods.

string[] classes [@property getter]

get all the classes on this element

struct ClassListHelper

The object classList returns.

inout(ClassListHelper) classList [@property getter]
alias classNames = classes

Returns a helper object to work with classes, just like javascript.

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.

Element addChild(string tagName, string childInfo, string childInfo2)
Element addChild(Element e)
Element addChild(string tagName, Element firstChild, string info2)
Element addChild(string tagName, Html innerHtml, string info2)

Family of convenience functions to quickly add a tag with some text or other relevant info (for example, it's a src for an <img> element instead of inner text). They forward to Element.make then calls appendChild.

Element addSibling(string tagName, string childInfo, string childInfo2)
Element addSibling(Element e)

Another convenience function. Adds a child directly after the current one, returning the new child.

void addChildren(T t)

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

void appendChildren(Element[] children)

Appends the list of children to this element.

void reparent(Element newParent)

Removes this element form its current parent and appends it to the given newParent.

void stripOut()

Strips this tag out of the document, putting its inner html as children of the parent.

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);

Element replaceWith(Element e)

Replaces this element with something else in the tree.

string firstInnerText()

Fetches the first consecutive text nodes concatenated together.

string directText [@property getter]

Returns the text directly under this element.

string directText [@property setter]

Sets the direct text, without modifying other child nodes.

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.

inout(Document) parentDocument [@property getter]

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

inout(Element) parentNode()

Returns the parent node in the tree this element is attached to.

Element make(string tagName, string childInfo, string childInfo2, string[] selfClosedElements)
Element make(string tagName, Html innerHtml, string childInfo2)
Element make(string tagName, Element child, string childInfo2)

Convenience function to try to do the right thing for HTML. This is the main way I create elements.

Element firstChild [@property getter]

Returns the first child of this element. If it has no children, returns null. Remember, text nodes are children too.

Element lastChild [@property getter]

Returns the last child of the element, or null if it has no children. Remember, text nodes are children too.

Element nextInSource()
Element previousInSource()

the next or previous element you would encounter if you were reading it in the source. May be a text node or other special non-tag object if you enabled them.

Element previousElementSibling [@property getter]
Element nextElementSibling [@property getter]

Returns the next or previous sibling that is not a text node. Please note: the behavior with comments is subject to change. Currently, it will return a comment or other nodes if it is in the tree (if you enabled it with Document.enableAddingSpecialTagsToDom or Document.parseSawComment) and not if you didn't, but the implementation will probably change at some point to skip them regardless.

string previousSibling [@property setter]
string nextSibling [@property setter]

Returns the next or previous sibling matching the tagName filter. The default filter of null will return the first sibling it sees, even if it is a comment or text node, or anything else. A filter of "*" will match any tag with a name. Otherwise, the string must match the tagName of the sibling you want to find.

T getParent(string tagName)

Gets the nearest node, going up the chain, with the given tagName May return null or throw. The type T will specify a subclass like Form, Table, or Link, which it will cast for you when found.

Element getElementById(string id)

Searches this element and the tree of elements under it for one matching the given id attribute.

Element querySelector(string selector)

Returns a child element that matches the given selector.

bool matches(string selector)

If the element matches the given selector. Previously known as matchesSelector.

Element closest(string selector)

Returns itself or the closest parent that matches the given selector, or null if none found

Element[] querySelectorAll(string selector)
alias getElementsBySelector = querySelectorAll

Returns elements that match the given CSS selector

Element[] getElementsByClassName(string cn)
Element[] getElementsByTagName(string tag)

Returns child elements that have the given class name or tag name.

string getAttribute(string name)

Gets the given attribute value, or null if the attribute is not set.

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.

string className [@property getter]
string className [@property setter]

Gets or sets the class attribute's contents. Returns an empty string if it has no class.

string opDispatch [@property setter]

Provides easy access to common HTML attributes, object style.

string opDispatch [@property setter]

Old access to attributes. Use attrs instead.

inout(Element[]) childNodes [@property getter]

Returns the element's children.

DataSet dataset [@property getter]

HTML5's dataset property. It is an alternate view into attributes with the data- prefix. Given <a data-my-property="cool" />, we get assert(a.dataset.myProperty == "cool");

AttributeSet attrs [@property getter]

Gives dot/opIndex access to attributes

ElementStyle style [@property getter]

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

string style [@property setter]

This sets the style attribute with a string.

CssStyle computedStyle [@property getter]

Don't use this. It can try to parse out the style element but it isn't complete and if I get back to it, it won't be for a while.

void* expansionHook;

These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good +////ditt

int offsetWidth;

These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good +////ditt

int offsetHeight;

These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good +////ditt

int offsetLeft;

These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good +////ditt

int offsetTop;

These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good +////ditt

Element offsetParent;

These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good +////ditt

bool hasLayout;

These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good +////ditt

int zIndex;
int absoluteLeft()
int absoluteTop()

These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good +////ditt

void removeAllChildren()

Removes all inner content from the tag; all child text and elements are gone.

Element appendSibling(Element e)
Element prependSibling(Element e)

Adds a sibling element before or after this one in the dom.

Element appendChild(Element e)

Appends the given element to this one. If it already has a parent, it is removed from that tree and moved to this one.

Element insertBefore(Element where, Element what)

Inserts the second element to this node, right before the first param

Element insertAfter(Element where, Element what)

Inserts the given element what as a sibling of the this element, after the element where in the parent node.

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)

Appends the given to the node.

string childElements [@property setter]

Returns child elements which are of a tag type (excludes text, comments, etc.).

Element[] appendHtml(string html)

Appends the given html to the element, returning the elements appended

void insertChildAfter(Element child, Element where)

Inserts a child under this element after the element where.

Element[] stealChildren(Element e, Element position)

Reparents all the child elements of e to this, leaving e childless.

Element prependChild(Element e)

Puts the current element first in our children list. The given element must not have a parent already.

Appender!string innerHTML [@property setter]

Returns a string containing all child elements, formatted such that it could be pasted into an XML file.

Element innerHTML(string html, bool strict)
Html innerHTML [@property setter]

Takes some html and replaces the element's children with the tree made from the string.

string outerHTML [@property setter]

Replaces this node with the given html string, which is parsed

string outerHTML [@property getter]

Returns all the html for this element, including the tag itself.

string innerRawSource [@property setter]

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.

Element replaceChild(Element find, Element replace)

Replaces the element find, which must be a child of this, with the element replace, which must have no parent.

void replaceChild(Element find, Element[] replace)

Replaces the given element with a whole group.

Element removeChild(Element c)

Removes the given child from this list.

Element[] removeChildren()

This removes all the children from this element, returning the old list.

string innerText [@property getter]
alias textContent = innerText

Fetch the inside text, with all tags stripped out.

string visibleText()

Gets the element's visible text, similar to how it would look assuming the document was HTML being displayed by a browser. This means it will attempt whitespace normalization (unless it is a <pre> tag), add \n characters for <br> tags, and I reserve the right to make it process additional css and tags in the future.

string innerText [@property setter]

Sets the inside text, replacing all children. You don't have to worry about entity encoding.

string outerText [@property setter]

Strips this node out of the document, replacing it with the given text

string outerText [@property getter]

Same result as innerText; the tag with all inner tags stripped out

Element cloned [@property getter]

This is a full clone of the element. Alias for cloneNode(true) now. Don't extend it.

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.

string nodeValue()

W3C DOM interface. Only really meaningful on TextNode instances, but the interface is present on the base class.

int nodeType [@property getter]


string toString()

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

bool isEmpty()

Returns if the node would be printed to string as <tag /> or <tag></tag>. In other words, if it has no non-empty text nodes and no element nodes. Please note that whitespace text nodes are NOT considered empty; Html("<tag> </tag>").isEmpty == false.

string toPrettyString(bool insertComments, int indentationLevel, string indentWith)

Writes out with formatting. Be warned: formatting changes the contents. Use ONLY for eyeball debugging.

string writeToAppender(Appender!string where)

This is the actual implementation used by toString. You can pass it a preallocated buffer to save some time. Note: the ordering of attributes in the string is undefined. Returns the string it creates.

ElementStream tree [@property getter]

Returns a lazy range of all its children, recursively.

Element addField(string label, string name, string type, FormFieldOptions fieldOptions)
Element addField(Element label, string name, string type, FormFieldOptions fieldOptions)
Element addField(string label, string name, FormFieldOptions fieldOptions)
Element addField(string label, string name, string[string] options, FormFieldOptions fieldOptions)
Element addSubmitButton(string label)

Adds a form field to this element, normally a <input> but type can also be "textarea".

See Also