dom/edit
edit a Document in place, validated so edits cannot inject
Since: 0.2.0
edit-error-message
(edit-error-message [e : EditError] :)
a one-line description of an EditError.
| e | the error |
cstr -- static text.
(edit-error-message (BadName)) ; => "not a valid name here"
Since: 0.2.0
dom-new
(dom-new [flavor : DomFlavor] :)
an empty Document to build into.
| flavor | `(XmlFlavor)` or `(HtmlFlavor)` |
Document -- owned; release with `dom-free`.
(let [d (dom-new (XmlFlavor))] ... (dom-free d))
Since: 0.2.0
dom-flavor
(dom-flavor [d : Document] :)
which rules a Document follows.
| d | the document |
DomFlavor -- `(HtmlFlavor)` for a tur-html tree, `(XmlFlavor)` for a tur-xml one.
(dom-flavor (html-parse "<p>")) ; => (HtmlFlavor)
Since: 0.2.0
dom-garbage-bytes
(dom-garbage-bytes [d : Document] :)
arena bytes that edits have abandoned.
| d | the document |
int -- bytes.
(dom-garbage-bytes d) ; => 0 for a fresh dom-new
Since: 0.2.0
dom-compact
(dom-compact [d : Document] :)
a fresh Document holding a copy of the live tree.
| d | the document; still yours to `dom-free` |
(Option Document) -- the copy, owned (release with `dom-free`); none on allocation failure.
(match (dom-compact d) (Some c) (do (dom-free d) c) (None) d)
Since: 0.2.0
create-element
(create-element [d : Document name : cstr] :)
a new, detached element with no namespace.
| d | the document that will own it | |
| name | the element name |
(Result Node EditError) -- `BadName` for a name the serializer could not write back as this one element (`"x onload=y"`, `"1a"`).
(create-element d "li") ; => (Ok <li>)
Since: 0.2.0
create-element-ns
(create-element-ns [d : Document uri : cstr qname : cstr])
a new, detached element in a namespace.
| d | the document that will own it | |
| uri | the namespace URI ("" for none) | |
| qname | the qualified name, e.g. `"atom:entry"` or `"svg"` |
(Result Node EditError) -- `BadName` for an invalid name, or in XML a prefix without a namespace.
(create-element-ns d "http://www.w3.org/2005/Atom" "entry")
Since: 0.2.0
create-text
(create-text [d : Document s : cstr] :)
a new, detached text node.
| d | the document that will own it | |
| s | the text (escaped when serialized) |
(Result Node EditError) -- `BadContent` in XML for text that is not well-formed UTF-8 or holds a character XML forbids (most C0 controls).
(create-text d "1 < 2")
Since: 0.2.0
create-comment
(create-comment [d : Document s : cstr] :)
a new, detached comment.
| d | the document that will own it | |
| s | the comment body |
(Result Node EditError) -- `BadContent` for a body that would end the comment early or not parse back (`--` in XML; `-->`, or a leading `>`, in HTML).
(create-comment d " generated ")
Since: 0.2.0
create-cdata
(create-cdata [d : Document s : cstr] :)
a new, detached CDATA section (XML only).
| d | the document that will own it | |
| s | the content (a `]]>` inside is split on output) |
(Result Node EditError) -- `BadContent` for content holding `]]>` or `\r`, which could not come back as one section, as written.
(create-cdata d "a < b")
Since: 0.2.0
create-pi
(create-pi [d : Document target : cstr data : cstr] :)
a new, detached processing instruction (XML only).
| d | the document that will own it | |
| target | the PI target, e.g. `"xml-stylesheet"` | |
| data | the data after the target |
(Result Node EditError) -- `BadPlacement` in an HTML Document (HTML has no PIs); `BadName` for target `xml` or a non-NCName; `BadContent` for data holding `?>` or starting with whitespace.
(create-pi d "xml-stylesheet" "href=\"a.xsl\" type=\"text/xsl\"")
Since: 0.2.0
node-import
(node-import [d : Document n : Node] :)
a detached deep copy of a node, owned by another Document.
| d | the document that will own the copy | |
| n | any node but a document node |
(Result Node EditError) -- `WrongKind` for a document node; `BadName` / `BadContent` / `BadPlacement` for content `d`'s flavour cannot hold.
(node-import page (unwrap (dom-root fragment-doc)))
Since: 0.2.0
node-clone
(node-clone [n : Node] :)
a detached deep copy of a node in its own Document.
| n | any node but a document node |
(Result Node EditError) -- `WrongKind` for a document node.
(node-clone row) ; a copy to append elsewhere
Since: 0.2.0
node-append!
(node-append! [parent : Node child : Node] :)
make `child` the last child of `parent`.
| parent | an element or the document node | |
| child | a node of the same Document |
(Result nil EditError)
(node-append! ul li)
Since: 0.2.0
node-prepend!
(node-prepend! [parent : Node child : Node] :)
make `child` the first child of `parent`.
| parent | an element or the document node | |
| child | a node of the same Document |
(Result nil EditError)
(node-prepend! ul li)
Since: 0.2.0
node-insert-before!
(node-insert-before! [ref : Node child : Node] :)
put `child` immediately before `ref`.
| ref | an attached node | |
| child | a node of the same Document |
(Result nil EditError) -- `Detached` when `ref` has no parent.
(node-insert-before! second first)
Since: 0.2.0
node-insert-after!
(node-insert-after! [ref : Node child : Node] :)
put `child` immediately after `ref`.
| ref | an attached node | |
| child | a node of the same Document |
(Result nil EditError) -- `Detached` when `ref` has no parent.
(node-insert-after! first second)
Since: 0.2.0
node-replace!
(node-replace! [old : Node new : Node] :)
put `new` where `old` is; `old` becomes detached.
| old | an attached node | |
| new | a node of the same Document (it may be a descendant of `old`) |
(Result nil EditError) -- on an error `old` stays where it was.
(node-replace! placeholder widget)
Since: 0.2.0
node-remove!
(node-remove! [n : Node] :)
detach a node from its parent.
| n | the node |
(for n (query root ".ad") (node-remove! n))
Since: 0.2.0
node-unwrap!
(node-unwrap! [n : Node] :)
replace a node by its children.
| n | an attached element |
(Result nil EditError)
(node-unwrap! span)
Since: 0.2.0
set-attr!
(set-attr! [el : Node name : cstr value : cstr] :)
set an attribute, replacing its value if it exists.
| el | an element | |
| name | the attribute name (in XML, `xmlns` / `xmlns:p` declare | |
| namespaces) | ||
| value | the value (escaped when serialized) |
(Result nil EditError) -- `BadName` for a name containing whitespace, `/`, `>` or `=` (`"a onload"`); in XML, `BadContent` for a value with a forbidden character.
(set-attr! a "href" "/next")
Since: 0.2.0
set-attr-ns!
(set-attr-ns! [el : Node uri : cstr qname : cstr value : cstr])
set a namespaced attribute.
| el | an element | |
| uri | the namespace URI | |
| qname | the qualified name; in XML it needs a prefix | |
| value | the value |
(Result nil EditError) -- `BadName` for an unprefixed namespaced name in XML, or a prefix already bound to another URI on this element.
(set-attr-ns! use "http://www.w3.org/1999/xlink" "xlink:href" "#icon")
Since: 0.2.0
remove-attr!
(remove-attr! [el : Node name : cstr] :)
remove an attribute.
| el | an element | |
| name | the attribute name |
bool -- true when an attribute was removed.
(remove-attr! img "style") ; => true
Since: 0.2.0
set-text!
(set-text! [n : Node s : cstr] :)
replace an element's children with one text node.
| n | an element or a character-data node | |
| s | the text |
(Result nil EditError) -- `BadContent` for text that would end a `<script>` or `<style>` early; `BadPlacement` on an HTML void element.
(set-text! title "Hello")
Since: 0.2.0
set-value!
(set-value! [n : Node s : cstr] :)
set the data of a text, comment, CDATA or PI node.
| n | the node | |
| s | the new data |
(Result nil EditError) -- `WrongKind` on an element or doctype; `BadContent` as for the matching `create-*`.
(set-value! comment " updated ")
Since: 0.2.0
rename!
(rename! [el : Node name : cstr] :)
give an element a new name, keeping its namespace.
| el | an element | |
| name | the new qualified name |
(Result nil EditError)
(rename! b "strong")
Since: 0.2.0
rename-ns!
(rename-ns! [el : Node uri : cstr qname : cstr] :)
give an element a new name and namespace.
| el | an element | |
| uri | the new namespace URI ("" for none) | |
| qname | the new qualified name |
(Result nil EditError)
(rename-ns! el "urn:new" "n:item")
Since: 0.2.0
node-normalize!
(node-normalize! [n : Node] :)
merge adjacent text nodes and drop empty ones.
| n | the subtree root |
(node-normalize! (dom-node d))
Since: 0.2.0
node-attached?
(node-attached? [n : Node] :)
is the node in its Document's tree?
| n | the node |
bool
(node-attached? (unwrap (create-element d "p"))) ; => false
Since: 0.2.0
Internal definitions
__ed-error-- internal: a turdom error code as an EditError.__ed-done-- internal: a turdom error code as a Result.__ed-nullp-- internal: is this a null pointer?__ed-node-- internal: a checked-then-created node pointer as a Result.__ed-new-raw-- internal.__ed-flavor-raw-- internal.__ed-garbage-raw-- internal.__ed-compact-raw-- internal.__ed-check-element-- internal.__ed-element-raw-- internal.__ed-check-chars-- internal.__ed-chars-raw-- internal.__ed-create-chars-- internal.__ed-check-pi-- internal.__ed-pi-raw-- internal.__ed-import-check-- internal: a cross-flavour import must validate.__ed-import-raw-- internal.__ed-clone-raw-- internal.__ed-insert-- internal: turdom_edit_insert.__ed-unwrap-raw-- internal.__ed-set-attr-raw-- internal.__ed-set-text-raw-- internal.__ed-rename-raw-- internal.__ed-ns-raw-- internal: the element's namespace URI, "" for none.