pub struct Document { /* private fields */ }
Expand description
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
.
Implementations
sourceimpl Document
impl Document
sourcepub fn from_str(text: &str) -> Result<Document, ParserError>
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.
sourcepub fn from_str_with_opt(
text: &str,
opt: &ParseOptions
) -> Result<Document, ParserError>
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.
sourcepub fn create_element<'a, T>(&mut self, tag_name: T) -> Nodewhere
TagNameRef<'a>: From<T>,
T: Copy,
pub fn create_element<'a, T>(&mut self, tag_name: T) -> Nodewhere
TagNameRef<'a>: From<T>,
T: Copy,
sourcepub fn svg_element(&self) -> Option<Node>
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);
sourcepub fn remove_node(&mut self, node: Node)
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);
sourcepub fn copy_node(&mut self, node: Node) -> Node
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.
sourcepub fn copy_node_deep(&mut self, node: Node) -> Node
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
sourceimpl WriteBuffer for Document
impl WriteBuffer for Document
sourcefn write_buf_opt(&self, opt: &WriteOptions, buf: &mut Vec<u8>)
fn write_buf_opt(&self, opt: &WriteOptions, buf: &mut Vec<u8>)
Vec<u8>
buffer using specified WriteOptions
.sourcefn write_buf(&self, buf: &mut Vec<u8>)
fn write_buf(&self, buf: &mut Vec<u8>)
Vec<u8>
buffer using default WriteOptions
.sourcefn with_write_opt<'a>(&'a self, opt: &'a WriteOptions) -> DisplaySvg<'a, Self>where
Self: Sized,
fn with_write_opt<'a>(&'a self, opt: &'a WriteOptions) -> DisplaySvg<'a, Self>where
Self: Sized,
fmt::Display
using provided write options.