markup5ever/
serialize.rs

1// Copyright 2014-2017 The html5ever Project Developers. See the
2// COPYRIGHT file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9//! Traits for serializing elements.
10//!
11//! The serializer expects the data to be xml-like (with a name,
12//! and optional children, attrs, text, comments, doctypes, and [processing instructions]). It uses
13//! the visitor pattern, where the serializer and the serializable objects are decoupled and
14//! implement their own traits.
15//!
16//! [processing instructions]: https://en.wikipedia.org/wiki/Processing_Instruction
17
18use crate::QualName;
19use std::io;
20
21//ยง serializing-html-fragments
22/// Used as a parameter to `serialize`, telling it if we want to skip the parent.
23#[derive(Clone, PartialEq)]
24pub enum TraversalScope {
25    /// Include the parent node when serializing.
26    IncludeNode,
27    /// Only serialize the children of the node, treating any provided qualified name as the
28    /// parent while serializing.
29    ///
30    /// This is used in the implementation of [`html5ever::serialize::serialize`]
31    ///
32    /// [`html5ever::serialize::serialize`]: ../../html5ever/serialize/fn.serialize.html
33    ChildrenOnly(Option<QualName>),
34}
35
36/// Types that can be serialized (according to the xml-like scheme in `Serializer`) implement this
37/// trait.
38pub trait Serialize {
39    /// Take the serializer and call its methods to serialize this type. The type will dictate
40    /// which methods are called and with what parameters.
41    fn serialize<S>(&self, serializer: &mut S, traversal_scope: TraversalScope) -> io::Result<()>
42    where
43        S: Serializer;
44}
45
46/// Types that are capable of serializing implement this trait
47pub trait Serializer {
48    /// Serialize the start of an element, for example `<div class="test">`.
49    fn start_elem<'a, AttrIter>(&mut self, name: QualName, attrs: AttrIter) -> io::Result<()>
50    where
51        AttrIter: Iterator<Item = AttrRef<'a>>;
52
53    /// Serialize the end of an element, for example `</div>`.
54    fn end_elem(&mut self, name: QualName) -> io::Result<()>;
55
56    /// Serialize a plain text node.
57    fn write_text(&mut self, text: &str) -> io::Result<()>;
58
59    /// Serialize a comment node, for example `<!-- comment -->`.
60    fn write_comment(&mut self, text: &str) -> io::Result<()>;
61
62    /// Serialize a doctype node, for example `<!doctype html>`.
63    fn write_doctype(&mut self, name: &str) -> io::Result<()>;
64
65    /// Serialize a processing instruction node, for example
66    /// `<?xml-stylesheet type="text/xsl" href="style.xsl"?>`.
67    fn write_processing_instruction(&mut self, target: &str, data: &str) -> io::Result<()>;
68}
69
70/// A type alias for an attribute name and value (e.g. the `class="test"` in `<div class="test">`
71/// is represented as `(<QualName of type class>, "test")`.
72///
73/// This is used in [`Serializer::start_elem`] where the value being serialized must supply an
74/// iterator over the attributes for the current element
75///
76/// [`Serializer::start_elem`]: trait.Serializer.html#tymethod.start_elem
77pub type AttrRef<'a> = (&'a QualName, &'a str);