html: Rename Fragment to Html

This commit is contained in:
Kévin Commaille 2023-08-25 10:11:38 +02:00 committed by Kévin Commaille
parent 9d0f23c8a7
commit 80c71ef454
3 changed files with 25 additions and 25 deletions

View File

@ -14,13 +14,13 @@ use tracing::debug;
/// ///
/// To get the serialized HTML, use its `Display` implementation. /// To get the serialized HTML, use its `Display` implementation.
#[derive(Debug)] #[derive(Debug)]
pub struct Fragment { pub struct Html {
pub(crate) nodes: Vec<Node>, pub(crate) nodes: Vec<Node>,
} }
impl Fragment { impl Html {
/// Construct a new `Fragment` by parsing the given HTML. /// Construct a new `Html` by parsing the given string.
pub fn parse_html(html: &str) -> Self { pub fn parse(string: &str) -> Self {
let sink = Self::default(); let sink = Self::default();
let mut parser = parse_fragment( let mut parser = parse_fragment(
sink, sink,
@ -28,11 +28,11 @@ impl Fragment {
QualName::new(None, ns!(html), local_name!("div")), QualName::new(None, ns!(html), local_name!("div")),
Vec::new(), Vec::new(),
); );
parser.process(html.into()); parser.process(string.into());
parser.finish() parser.finish()
} }
/// Construct a new `Node` with the given data and add it to this `Fragment`. /// Construct a new `Node` with the given data and add it to this `Html`.
/// ///
/// Returns the index of the new node. /// Returns the index of the new node.
pub fn new_node(&mut self, data: NodeData) -> usize { pub fn new_node(&mut self, data: NodeData) -> usize {
@ -40,7 +40,7 @@ impl Fragment {
self.nodes.len() - 1 self.nodes.len() - 1
} }
/// Append the given node to the given parent in this `Fragment`. /// Append the given node to the given parent in this `Html`.
/// ///
/// The node is detached from its previous position. /// The node is detached from its previous position.
pub fn append_node(&mut self, parent_id: usize, node_id: usize) { pub fn append_node(&mut self, parent_id: usize, node_id: usize) {
@ -56,7 +56,7 @@ impl Fragment {
self.nodes[parent_id].last_child = Some(node_id); self.nodes[parent_id].last_child = Some(node_id);
} }
/// Insert the given node before the given sibling in this `Fragment`. /// Insert the given node before the given sibling in this `Html`.
/// ///
/// The node is detached from its previous position. /// The node is detached from its previous position.
pub fn insert_before(&mut self, sibling_id: usize, node_id: usize) { pub fn insert_before(&mut self, sibling_id: usize, node_id: usize) {
@ -73,7 +73,7 @@ impl Fragment {
self.nodes[sibling_id].prev_sibling = Some(node_id); self.nodes[sibling_id].prev_sibling = Some(node_id);
} }
/// Detach the given node from this `Fragment`. /// Detach the given node from this `Html`.
pub fn detach(&mut self, node_id: usize) { pub fn detach(&mut self, node_id: usize) {
let (parent, prev_sibling, next_sibling) = { let (parent, prev_sibling, next_sibling) = {
let node = &mut self.nodes[node_id]; let node = &mut self.nodes[node_id];
@ -94,13 +94,13 @@ impl Fragment {
} }
} }
impl Default for Fragment { impl Default for Html {
fn default() -> Self { fn default() -> Self {
Self { nodes: vec![Node::new(NodeData::Document)] } Self { nodes: vec![Node::new(NodeData::Document)] }
} }
} }
impl TreeSink for Fragment { impl TreeSink for Html {
type Handle = usize; type Handle = usize;
type Output = Self; type Output = Self;
@ -225,7 +225,7 @@ impl TreeSink for Fragment {
} }
} }
impl Serialize for Fragment { impl Serialize for Html {
fn serialize<S>(&self, serializer: &mut S, traversal_scope: TraversalScope) -> io::Result<()> fn serialize<S>(&self, serializer: &mut S, traversal_scope: TraversalScope) -> io::Result<()>
where where
S: Serializer, S: Serializer,
@ -248,7 +248,7 @@ impl Serialize for Fragment {
} }
} }
impl fmt::Display for Fragment { impl fmt::Display for Html {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut u8_vec = Vec::new(); let mut u8_vec = Vec::new();
serialize( serialize(
@ -306,7 +306,7 @@ impl Node {
} }
impl Node { impl Node {
pub(crate) fn serialize<S>(&self, fragment: &Fragment, serializer: &mut S) -> io::Result<()> pub(crate) fn serialize<S>(&self, fragment: &Html, serializer: &mut S) -> io::Result<()>
where where
S: Serializer, S: Serializer,
{ {
@ -348,7 +348,7 @@ impl Node {
#[derive(Debug)] #[derive(Debug)]
#[allow(clippy::exhaustive_enums)] #[allow(clippy::exhaustive_enums)]
pub enum NodeData { pub enum NodeData {
/// The root node of the `Fragment`. /// The root node of the `Html`.
Document, Document,
/// A text node. /// A text node.
@ -374,7 +374,7 @@ pub struct ElementData {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Fragment; use super::Html;
#[test] #[test]
fn sanity() { fn sanity() {
@ -384,8 +384,8 @@ mod tests {
<p>This is some <em>text</em></p>\ <p>This is some <em>text</em></p>\
</div>\ </div>\
"; ";
assert_eq!(Fragment::parse_html(html).to_string(), html); assert_eq!(Html::parse(html).to_string(), html);
assert_eq!(Fragment::parse_html("").to_string(), ""); assert_eq!(Html::parse("").to_string(), "");
} }
} }

View File

@ -10,10 +10,10 @@
#![warn(missing_docs)] #![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))] #![cfg_attr(docsrs, feature(doc_auto_cfg))]
mod html_fragment; mod html;
mod sanitize; mod sanitize;
pub use self::{ pub use self::{
html_fragment::{ElementData, Fragment, Node, NodeData}, html::{ElementData, Html, Node, NodeData},
sanitize::*, sanitize::*,
}; };

View File

@ -3,7 +3,7 @@ use phf::{phf_map, phf_set, Map, Set};
use wildmatch::WildMatch; use wildmatch::WildMatch;
use super::{HtmlSanitizerMode, RemoveReplyFallback}; use super::{HtmlSanitizerMode, RemoveReplyFallback};
use crate::{ElementData, Fragment, NodeData}; use crate::{ElementData, Html, NodeData};
/// A sanitizer to filter [HTML tags and attributes] according to the Matrix specification. /// A sanitizer to filter [HTML tags and attributes] according to the Matrix specification.
/// ///
@ -56,8 +56,8 @@ impl HtmlSanitizer {
} }
/// Clean the given HTML string with this sanitizer. /// Clean the given HTML string with this sanitizer.
pub fn clean(&self, html: &str) -> Fragment { pub fn clean(&self, html: &str) -> Html {
let mut fragment = Fragment::parse_html(html); let mut fragment = Html::parse(html);
let root = fragment.nodes[0].first_child.unwrap(); let root = fragment.nodes[0].first_child.unwrap();
let mut next_child = fragment.nodes[root].first_child; let mut next_child = fragment.nodes[root].first_child;
@ -69,7 +69,7 @@ impl HtmlSanitizer {
fragment fragment
} }
fn clean_node(&self, fragment: &mut Fragment, node_id: usize, depth: u32) { fn clean_node(&self, fragment: &mut Html, node_id: usize, depth: u32) {
let action = self.node_action(fragment, node_id, depth); let action = self.node_action(fragment, node_id, depth);
if action != NodeAction::Remove { if action != NodeAction::Remove {
@ -94,7 +94,7 @@ impl HtmlSanitizer {
} }
} }
fn node_action(&self, fragment: &Fragment, node_id: usize, depth: u32) -> NodeAction { fn node_action(&self, fragment: &Html, node_id: usize, depth: u32) -> NodeAction {
match &fragment.nodes[node_id].data { match &fragment.nodes[node_id].data {
NodeData::Element(ElementData { name, attrs, .. }) => { NodeData::Element(ElementData { name, attrs, .. }) => {
let tag: &str = &name.local; let tag: &str = &name.local;