[][src]Crate xml_dom

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 below, however from a purely style point of view the implementation has the following characteristics:

  1. It maintains a reasonable separation between the node type traits and the tree implementation using opaque Node reference types.
  2. Where possible the names from IDL are used with minimal conversion, however some redundant suffixes (_data, _node) have been reduced for brevity/clarity.
  3. This leads to a replication of the typical programmer experience where casting between the node traits is required. This is supported by the xml_dom::convert module.

Example

use xml_dom::*;
use xml_dom::convert::*;

let implementation = get_implementation();
let mut document_node = implementation
    .create_document("http://www.w3.org/1999/xhtml", "html", None)
    .unwrap();
println!("document 1: {:#?}", document_node);

let document = as_document_mut(&mut document_node).unwrap();
let mut root_node = document.document_element().unwrap();

let root = as_element_mut(&mut root_node).unwrap();
root.set_attribute("lang", "en");
let _head = root.append_child(document.create_element("head").unwrap());
let _body = root.append_child(document.create_element("body").unwrap());

let xml = document_node.to_string();
println!("document 2: {}", xml);

Specification

Conformance

TBD

The has_feature method DOMImplementation and is_supported on Node will return true when the request is for support of the Core or XML feature and supports both version 1.0 and version 2.0 of Core and version 1.0 of XML.

use xml_dom::{DOMImplementation, get_implementation};

let implementation = get_implementation();
assert!(implementation.has_feature("Core", "1.0"));
assert!(implementation.has_feature("Core", "2.0"));
assert!(implementation.has_feature("XML", "1.0"));
assert!(!implementation.has_feature("XML", "2.0"));

IDL to Rust Mapping

From the core documentation:

The Node interface is the primary datatype for the entire Document Object Model. It represents a single node in the document tree. While all objects implementing the Node interface expose methods for dealing with children, not all objects implementing the Node interface may have children. For example, Text nodes may not have children, and adding children to such nodes results in a DOMException being raised.

The attributes nodeName, nodeValue and attributes are included as a mechanism to get at node information without casting down to the specific derived interface. In cases where there is no obvious mapping of these attributes for a specific nodeType (e.g., nodeValue for an Element or attributes for a Comment), this returns null. Note that the specialized interfaces may contain additional and more convenient mechanisms to get and set the relevant information.

Wherever possible the documentation included in sections headed Specification is taken from the specification documents listed above.

Interface Mapping

The actual concrete types used in the DOM tree are RefNode and WeakRefNode which in turn are references an opaque NodeImpl struct. Only RefNodeimplements all of the DOM interfaces and client programmers should never need to interact withWeakRefNode`.

IDL InterfaceRust Mapping
AttrAttribute
CharacterDataCharacterData
CDATASectionCDataSection
CommentComment
DocumentDocument
DocumentFragmentDocumentFragment
DocumentTypeDocumentType
DOMImplementationDOMImplementation
ElementElement
EntityEntity
EntityReferenceEntityReference
NamedNodeMapHashMap<Name, RefNode>
NodeNode
NodeListVec<Rc<RefNode>>
NotationNotation
ProcessingInstructionProcessingInstruction
TextText
  • The exception type DOMException and associated constants are represented by the enumeration Error.
  • IDL Interface attributes are represented by functions;
    • readonly attributes simply have an attribute_name getter,
    • writeable attributes also have a set_attribute_name setter,
    • some attributes allow null in which case they have an unset_attribute_name setter.
  • IDL function names are altered from lowerCamelCase to snake_case.
  • IDL functions that are marked raises(DOMException) return Result with Error as the error type.
  • IDL attributes of type T that are described as "may be null", or IDL functions that "may return T or null" instead return Option<T>.

Primitive Type Mapping

IDL TypeRust TypeUsage
booleanboolall
DOMStringStringall
unsigned shortError, u16as representation of exception code
unsigned longusizelist/string indexes and lengths

Extensions

The following extensions are provided beyond the DOM Level 2 specification.

  1. The get_implementation function returns an instance of DOMImplementation to allow bootstrapping the creation of documents. This satisfies the requirement from the specification: "The DOM Level 2 API does not define a standard way to create DOMImplementation objects; DOM implementations must provide some proprietary way of bootstrapping these DOM interfaces, and then all other objects can be built from there.".
  2. The get_implementation_version function in the dom_impl module returns a vendor-specific version identifier for the DOMImplementation.
  3. The standard DOMImplementation trait also has an additional member create_document_with_options, and associated ProcessingOptions structure, that can set optional behavior for a given Document instance.
  4. The trait DocumentDecl extends Document with the ability to set and retrieve the XML declaration from the document's prolog.
  5. The trait Namespaced extends Element with the ability to look-up namespace mappings (using the standard xmlns attribute).
  6. The functiions create_entity, create_internal_entity, and create_notation in the dom_impl module provide the ability to create instances of these Level 2 extended interfaces. In general most clients using the DOM do not need to create these however parsers constructing the DOM may.

Logging

As of this time the only dependency xml_dom has is the log crate and only warn! and error! macros are used to provide more information than the set of error conditions defined by the DOM.

Re-exports

pub use dom_impl::get_implementation;
pub use error::Error;
pub use error::Result;

Modules

convert

Provides safe RefNode conversion functions.

dom_impl

This module implements certain capabilities required by, but not specified by, the DOM Core.

error

Provides a common Error and Result type and a set of common error messages.

Structs

Name

Corresponds to attributes localName, namespaceURI, and prefix on the DOM Node interface.

ProcessingOptions

This type encapsulates a set of options that a client can set that affect the processing of nodes as they are added/removed from the DOM.

XmlDecl

The following productions are taken from XML 1.1 §2.8 Prolog and Document Type Declaration.

Enums

NodeType

This corresponds to the DOM NodeType set of constants.

XmlVersion

Captures the supported version of the XML specification itself, as used in XmlDecl.

Traits

Attribute

This corresponds to the DOM Attr interface.

CDataSection

This corresponds to the DOM CDataSection interface.

CharacterData

This corresponds to the DOM CharacterData interface.

Comment

This corresponds to the DOM Comment interface.

DOMImplementation

This corresponds to the DOM DOMImplementation interface.

Document

This corresponds to the DOM Document interface.

DocumentDecl

This interface extends the DOM standard Document and allows the setting, and retrieval, of the XML declaration from the document prolog.

DocumentFragment

This corresponds to the DOM DocumentFragment interface (current unsupported).

DocumentType

This corresponds to the DOM DocumentType interface.

Element

This corresponds to the DOM Element interface.

Entity

This corresponds to the DOM Entity interface (currently unsupported).

EntityReference

This corresponds to the DOM EntityReference interface (currently unsupported).

Namespaced

An extended interface that provides access to namespace information for elements, including the resolving of prefixes and namespaces in the hierarchy of the document.

Node

This corresponds to the DOM Node interface.

Notation

This corresponds to the DOM Notation interface (currently unsupported).

ProcessingInstruction

This corresponds to the DOM ProcessingInstruction interface.

Text

This corresponds to the DOM Text interface.

Type Definitions

RefNode

Opaque DOM tree node reference. This is the type used by this implementation as the concrete type for the NodeRef associated type in the Node trait.