[][src]Struct svgdom::Document

pub struct Document { /* fields omitted */ }

Container of Nodes.

Structure:

The Document itself is just a container of Nodes. You can create new Nodes only through the Document. Parsing and generating of the SVG data also done through it.

The Node represents any kind of an XML node. It can be an element, a comment, a text, etc. There are no different structs for each type.

The TagName represents a tag name of the element node. It's an enum of ElementId and String types. The ElementId contains all possible SVG element names and String used for non-SVG elements. Such separation used for performance reasons.

The Attributes container wraps a Vec of Attribute's.

At last, the id attribute is stored as a separate value and not as part of the Attributes.

Methods

impl Document[src]

pub fn new() -> Document[src]

Constructs a new Document.

pub fn from_str(text: &str) -> Result<Document, ParserError>[src]

Constructs a new Document from the text using a default ParseOptions.

Note: only SVG elements and attributes will be parsed.

pub fn from_str_with_opt(
    text: &str,
    opt: &ParseOptions
) -> Result<Document, ParserError>
[src]

Constructs a new Document from the text using a supplied ParseOptions.

Note: only SVG elements and attributes will be parsed.

pub fn create_element<'a, T>(&mut self, tag_name: T) -> Node where
    TagNameRef<'a>: From<T>,
    T: Copy
[src]

Constructs a new Node with NodeType::Element type.

Constructed node do belong to this document, but not added to it tree structure.

Panics

Panics if a string tag name is empty.

pub fn create_node<S: Into<String>>(
    &mut self,
    node_type: NodeType,
    text: S
) -> Node
[src]

Constructs a new Node using the supplied NodeType.

Constructed node do belong to this document, but not added to it tree structure.

This method should be used for any non-element nodes.

pub fn root(&self) -> Node[src]

Returns the root Node.

pub fn svg_element(&self) -> Option<Node>[src]

Returns the first child with svg tag name of the root Node.

In most of the cases result of this method and first_element_child() will be the same, but an additional check may be helpful.

Panics

Panics if the root node is currently mutability borrowed.

Examples

use svgdom::{Document, ElementId};

let doc = Document::from_str(
    "<!--comment--><svg xmlns='http://www.w3.org/2000/svg'/>").unwrap();

assert_eq!(doc.svg_element().unwrap().is_tag_name(ElementId::Svg), true);

pub fn remove_node(&mut self, node: Node)[src]

Removes this node and all it children from the tree.

Same as detach(), but also removes all linked attributes from the tree.

Panics

Panics if the node or one of its adjoining nodes or any children node is currently borrowed.

Examples

use svgdom::{Document, ElementId, AttributeId};

let mut doc = Document::from_str(
"<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>
    <rect id='rect1'/>
    <use xlink:href='#rect1'/>
</svg>").unwrap();

let mut rect_elem = doc.root().descendants().filter(|n| *n.id() == "rect1").next().unwrap();
let use_elem = doc.root().descendants().filter(|n| n.is_tag_name(ElementId::Use)).next().unwrap();

assert_eq!(use_elem.has_attribute(AttributeId::Href), true);

// The 'remove' method will remove 'rect' element and all it's children.
// Also it will remove all links to this element and it's children,
// so 'use' element will no longer have the 'xlink:href' attribute.
doc.remove_node(rect_elem);

assert_eq!(use_elem.has_attribute(AttributeId::Href), false);

pub fn drain<P>(&mut self, root: Node, f: P) -> usize where
    P: Fn(&Node) -> bool
[src]

Removes only the children nodes specified by the predicate.

Uses remove(), not detach() internally.

The root node will be ignored.

pub fn copy_node(&mut self, node: Node) -> Node[src]

Returns a copy of a current node without children.

All attributes except id will be copied, because id must be unique.

pub fn copy_node_deep(&mut self, node: Node) -> Node[src]

Returns a deep copy of a current node with all it's children.

All attributes except id will be copied, because id must be unique.

Trait Implementations

impl WriteBuffer for Document[src]

fn write_buf(&self, buf: &mut Vec<u8>)[src]

Writes data to the Vec<u8> buffer using default WriteOptions.

fn with_write_opt<'a>(&'a self, opt: &'a WriteOptions) -> DisplaySvg<'a, Self> where
    Self: Sized
[src]

Returns an object that implements fmt::Display using provided write options.

impl Drop for Document[src]

impl Display for Document[src]

Auto Trait Implementations

impl !Send for Document

impl !Sync for Document

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]