Struct svgdom::Document [−][src]
pub struct Document { /* fields omitted */ }
Container of Node
s.
Structure:
The Document
itself is just a container of Node
s.
You can create new Node
s 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]
impl Document
pub fn new() -> Document
[src]
pub fn new() -> Document
Constructs a new Document
.
pub fn from_str(text: &str) -> Result<Document, ParserError>
[src]
pub fn from_str(text: &str) -> Result<Document, ParserError>
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]
pub fn from_str_with_opt(
text: &str,
opt: &ParseOptions
) -> Result<Document, ParserError>
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]
pub fn create_element<'a, T>(&mut self, tag_name: T) -> Node where
TagNameRef<'a>: From<T>,
T: Copy,
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]
pub fn create_node<S: Into<String>>(
&mut self,
node_type: NodeType,
text: S
) -> Node
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]
pub fn root(&self) -> Node
Returns the root Node
.
pub fn svg_element(&self) -> Option<Node>
[src]
pub fn svg_element(&self) -> Option<Node>
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]
pub fn remove_node(&mut self, node: Node)
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'> <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]
pub fn drain<P>(&mut self, root: Node, f: P) -> usize where
P: Fn(&Node) -> bool,
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]
pub fn copy_node(&mut self, node: Node) -> Node
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]
pub fn copy_node_deep(&mut self, node: Node) -> Node
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]
impl WriteBuffer for Document
fn write_buf_opt(&self, opt: &WriteOptions, buf: &mut Vec<u8>)
[src]
fn write_buf_opt(&self, opt: &WriteOptions, buf: &mut Vec<u8>)
Writes data to the Vec<u8>
buffer using specified WriteOptions
.
fn write_buf(&self, buf: &mut Vec<u8>)
[src]
fn write_buf(&self, buf: &mut Vec<u8>)
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]
fn with_write_opt<'a>(&'a self, opt: &'a WriteOptions) -> DisplaySvg<'a, Self> where
Self: Sized,
Returns an object that implements fmt::Display
using provided write options.
impl Drop for Document
[src]
impl Drop for Document
impl Display for Document
[src]
impl Display for Document