No matching definitions.

dom/edit

src/dom/edit.tur

edit a Document in place, validated so edits cannot inject

Since: 0.2.0

defn

edit-error-message

(edit-error-message [e : EditError] :)

a one-line description of an EditError.

ethe error

cstr -- static text.

(edit-error-message (BadName))   ; => "not a valid name here"

Since: 0.2.0

defn

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

defn

dom-flavor

(dom-flavor [d : Document] :)

which rules a Document follows.

dthe document

DomFlavor -- `(HtmlFlavor)` for a tur-html tree, `(XmlFlavor)` for a tur-xml one.

(dom-flavor (html-parse "<p>"))   ; => (HtmlFlavor)

Since: 0.2.0

defn

dom-garbage-bytes

(dom-garbage-bytes [d : Document] :)

arena bytes that edits have abandoned.

dthe document

int -- bytes.

(dom-garbage-bytes d)   ; => 0 for a fresh dom-new

Since: 0.2.0

defn

dom-compact

(dom-compact [d : Document] :)

a fresh Document holding a copy of the live tree.

dthe 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

defn

create-element

(create-element [d : Document name : cstr] :)

a new, detached element with no namespace.

dthe document that will own it
namethe 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

defn

create-element-ns

(create-element-ns [d : Document uri : cstr qname : cstr])

a new, detached element in a namespace.

dthe document that will own it
urithe namespace URI ("" for none)
qnamethe 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

defn

create-text

(create-text [d : Document s : cstr] :)

a new, detached text node.

dthe document that will own it
sthe 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

defn

create-comment

(create-comment [d : Document s : cstr] :)

a new, detached comment.

dthe document that will own it
sthe 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

defn

create-cdata

(create-cdata [d : Document s : cstr] :)

a new, detached CDATA section (XML only).

dthe document that will own it
sthe 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

defn

create-pi

(create-pi [d : Document target : cstr data : cstr] :)

a new, detached processing instruction (XML only).

dthe document that will own it
targetthe PI target, e.g. `"xml-stylesheet"`
datathe 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

defn

node-import

(node-import [d : Document n : Node] :)

a detached deep copy of a node, owned by another Document.

dthe document that will own the copy
nany 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

defn

node-clone

(node-clone [n : Node] :)

a detached deep copy of a node in its own Document.

nany 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

defn

node-append!

(node-append! [parent : Node child : Node] :)

make `child` the last child of `parent`.

parentan element or the document node
childa node of the same Document

(Result nil EditError)

(node-append! ul li)

Since: 0.2.0

defn

node-prepend!

(node-prepend! [parent : Node child : Node] :)

make `child` the first child of `parent`.

parentan element or the document node
childa node of the same Document

(Result nil EditError)

(node-prepend! ul li)

Since: 0.2.0

defn

node-insert-before!

(node-insert-before! [ref : Node child : Node] :)

put `child` immediately before `ref`.

refan attached node
childa node of the same Document

(Result nil EditError) -- `Detached` when `ref` has no parent.

(node-insert-before! second first)

Since: 0.2.0

defn

node-insert-after!

(node-insert-after! [ref : Node child : Node] :)

put `child` immediately after `ref`.

refan attached node
childa node of the same Document

(Result nil EditError) -- `Detached` when `ref` has no parent.

(node-insert-after! first second)

Since: 0.2.0

defn

node-replace!

(node-replace! [old : Node new : Node] :)

put `new` where `old` is; `old` becomes detached.

oldan attached node
newa 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

defn

node-remove!

(node-remove! [n : Node] :)

detach a node from its parent.

nthe node
(for n (query root ".ad") (node-remove! n))

Since: 0.2.0

defn

node-unwrap!

(node-unwrap! [n : Node] :)

replace a node by its children.

nan attached element

(Result nil EditError)

(node-unwrap! span)

Since: 0.2.0

defn

set-attr!

(set-attr! [el : Node name : cstr value : cstr] :)

set an attribute, replacing its value if it exists.

elan element
namethe attribute name (in XML, `xmlns` / `xmlns:p` declare
namespaces)
valuethe 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

defn

set-attr-ns!

(set-attr-ns! [el : Node uri : cstr qname : cstr value : cstr])

set a namespaced attribute.

elan element
urithe namespace URI
qnamethe qualified name; in XML it needs a prefix
valuethe 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

defn

remove-attr!

(remove-attr! [el : Node name : cstr] :)

remove an attribute.

elan element
namethe attribute name

bool -- true when an attribute was removed.

(remove-attr! img "style")   ; => true

Since: 0.2.0

defn

set-text!

(set-text! [n : Node s : cstr] :)

replace an element's children with one text node.

nan element or a character-data node
sthe 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

defn

set-value!

(set-value! [n : Node s : cstr] :)

set the data of a text, comment, CDATA or PI node.

nthe node
sthe 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

defn

rename!

(rename! [el : Node name : cstr] :)

give an element a new name, keeping its namespace.

elan element
namethe new qualified name

(Result nil EditError)

(rename! b "strong")

Since: 0.2.0

defn

rename-ns!

(rename-ns! [el : Node uri : cstr qname : cstr] :)

give an element a new name and namespace.

elan element
urithe new namespace URI ("" for none)
qnamethe new qualified name

(Result nil EditError)

(rename-ns! el "urn:new" "n:item")

Since: 0.2.0

defn

node-normalize!

(node-normalize! [n : Node] :)

merge adjacent text nodes and drop empty ones.

nthe subtree root
(node-normalize! (dom-node d))

Since: 0.2.0

defn

node-attached?

(node-attached? [n : Node] :)

is the node in its Document's tree?

nthe 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.