A Rust crate providing a reasonably faithful implementation of the W3C Document Object Model Core, Level 2.
This crate provides a trait-based implementation of the DOM with minimal changes to the style and semantics defined in the Level 2 specification. The specific mapping from the IDL in the specification is described in the documentation, however from a purely style point of view the implementation has the following characteristics:
RefNode
reference type._data
, _node
) have been reduced for brevity/clarity.xml_dom::convert
module.```rust use xmldom::*; use xmldom::convert::*;
// Bootstrap; get an instance of DOMImplementation
. The mechanism for this is
// intentionally undefined by the specification.
let implementation = get_implementation();
// Create a DocumentType
instance.
let documenttype = implementation
.createdocument_type(
"html",
Some("-//W3C//DTD XHTML 1.0 Transitional//EN"),
Some("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"),
)
.unwrap();
// Create a new Document
using the document type defined above. Note that this
// also has the side-effect of creating the document's root element named "html".
let mut documentnode = implementation
.createdocument("http://www.w3.org/1999/xhtml", "html", Some(document_type))
.unwrap();
// Cast the returned document RefNode
into a RefDocument
trait reference
let document = asdocumentmut(&mut document_node).unwrap();
// Fetch the document's root element as a node, then cast to RefElement
.
let mut rootnode = document.documentelement().unwrap();
let root = aselementmut(&mut root_node).unwrap();
// Create an Attribute
instance on the root element.
root.set_attribute("lang", "en");
// Create two child Element
s of "html".
let head = root.appendchild(document.createelement("head").unwrap());
let _body = root.appendchild(document.create_element("body").unwrap());
// Display as XML. let xml = documentnode.tostring(); println!("document 2: {}", xml); ```
This should result in the following XML; note that formatting was added for this document, the provided
implementation of Display
for RefNode
does not format the output.
xml
<!DOCTYPE
html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head></head>
<body></body>
</html>
Version 0.1.3 (in progress)
XmlDecl
, XmlVersion
), not reusing processing instructionEntity
, EntityReference
, and Notation
).
create_notation
, create_entity
, and create_internal_entity
to dom_impl
.ProcessingOptions
and DOMImplementation::create_document_with_options
) capability to turn
on extended processing behaviors.Version 0.1.2
NodeImpl
for extended traitsVersion 0.1.1
Display
formattingappend_child
rule supportVersion 0.1.0
unimplemented!()
; Node::clone_node
, Node::normalize
, and
Namespaced::normalize_mappings
.