html: Don't expose methods to manipulate HTML

Users should just use the provided way to sanitize HTML
This commit is contained in:
Kévin Commaille 2023-08-25 10:26:31 +02:00 committed by Kévin Commaille
parent 80c71ef454
commit 0120861951
3 changed files with 11 additions and 11 deletions

View File

@ -35,7 +35,7 @@ impl Html {
/// Construct a new `Node` with the given data and add it to this `Html`. /// 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(crate) fn new_node(&mut self, data: NodeData) -> usize {
self.nodes.push(Node::new(data)); self.nodes.push(Node::new(data));
self.nodes.len() - 1 self.nodes.len() - 1
} }
@ -43,7 +43,7 @@ impl Html {
/// Append the given node to the given parent in this `Html`. /// 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(crate) fn append_node(&mut self, parent_id: usize, node_id: usize) {
self.detach(node_id); self.detach(node_id);
self.nodes[node_id].parent = Some(parent_id); self.nodes[node_id].parent = Some(parent_id);
@ -59,7 +59,7 @@ impl Html {
/// Insert the given node before the given sibling in this `Html`. /// 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(crate) fn insert_before(&mut self, sibling_id: usize, node_id: usize) {
self.detach(node_id); self.detach(node_id);
self.nodes[node_id].parent = self.nodes[sibling_id].parent; self.nodes[node_id].parent = self.nodes[sibling_id].parent;
@ -74,7 +74,7 @@ impl Html {
} }
/// Detach the given node from this `Html`. /// Detach the given node from this `Html`.
pub fn detach(&mut self, node_id: usize) { pub(crate) 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];
(node.parent.take(), node.prev_sibling.take(), node.next_sibling.take()) (node.parent.take(), node.prev_sibling.take(), node.next_sibling.take())
@ -278,7 +278,7 @@ pub struct Node {
impl Node { impl Node {
/// Constructs a new `Node` with the given data. /// Constructs a new `Node` with the given data.
pub fn new(data: NodeData) -> Self { fn new(data: NodeData) -> Self {
Self { Self {
parent: None, parent: None,
prev_sibling: None, prev_sibling: None,
@ -289,18 +289,18 @@ impl Node {
} }
} }
/// Returns the `ElementData` of this `Node` if it is a `NodeData::Element`. /// Returns the data of this `Node` if it is an Element (aka an HTML tag).
pub fn as_element(&self) -> Option<&ElementData> { pub fn as_element(&self) -> Option<&ElementData> {
as_variant!(&self.data, NodeData::Element) as_variant!(&self.data, NodeData::Element)
} }
/// Returns the mutable `ElementData` of this `Node` if it is a `NodeData::Element`. /// Returns the mutable `ElementData` of this `Node` if it is a `NodeData::Element`.
pub fn as_element_mut(&mut self) -> Option<&mut ElementData> { pub(crate) fn as_element_mut(&mut self) -> Option<&mut ElementData> {
as_variant!(&mut self.data, NodeData::Element) as_variant!(&mut self.data, NodeData::Element)
} }
/// Returns the mutable text content of this `Node`, if it is a `NodeData::Text`. /// Returns the mutable text content of this `Node`, if it is a `NodeData::Text`.
pub fn as_text_mut(&mut self) -> Option<&mut StrTendril> { fn as_text_mut(&mut self) -> Option<&mut StrTendril> {
as_variant!(&mut self.data, NodeData::Text) as_variant!(&mut self.data, NodeData::Text)
} }
} }
@ -347,7 +347,7 @@ impl Node {
/// The data of a `Node`. /// The data of a `Node`.
#[derive(Debug)] #[derive(Debug)]
#[allow(clippy::exhaustive_enums)] #[allow(clippy::exhaustive_enums)]
pub enum NodeData { pub(crate) enum NodeData {
/// The root node of the `Html`. /// The root node of the `Html`.
Document, Document,

View File

@ -14,6 +14,6 @@ mod html;
mod sanitize; mod sanitize;
pub use self::{ pub use self::{
html::{ElementData, Html, Node, NodeData}, html::{ElementData, Html, Node},
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, Html, NodeData}; use crate::html::{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.
/// ///