Skip to content

Commit

Permalink
Merge pull request #206 from kornelski/html5up
Browse files Browse the repository at this point in the history
Upgrade html5ever
  • Loading branch information
notriddle authored Sep 20, 2024
2 parents 25017a4 + f2da069 commit d11ff76
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Set toolchain
run: |
rustup set profile minimal
rustup override set 1.61.0
rustup override set 1.65.0
- name: Check
run: cargo check --lib --all-features
env:
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Unreleased

* Bump MSRV to 1.61.0
* Bump MSRV to 1.65.0
* Improve panic message when `clean_content_tags` conflicts with other options

# 4.0.0
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ documentation = "https://docs.rs/ammonia/"
repository = "https://github.com/rust-ammonia/ammonia"
categories = [ "web-programming", "text-processing" ]
edition = "2021"
rust-version = "1.61"
rust-version = "1.65"

[dependencies]
html5ever = "0.27"
html5ever = "0.29"
maplit = "1.0"
tendril = "0.4"
url = "2"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ HTML Sanitization
=================

[![Crates.IO](https://img.shields.io/crates/v/ammonia.svg)](https://crates.io/crates/ammonia)
![Requires rustc 1.61.0](https://img.shields.io/badge/rustc-1.61.0+-green.svg)
![Requires rustc 1.65.0](https://img.shields.io/badge/rustc-1.65.0+-green.svg)

Ammonia is a whitelist-based HTML sanitization library. It is designed to
prevent cross-site scripting, layout breaking, and clickjacking caused
Expand Down
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use maplit::{hashmap, hashset};
use once_cell::sync::Lazy;
use rcdom::{Handle, NodeData, RcDom, SerializableHandle};
use std::borrow::{Borrow, Cow};
use std::cell::Cell;
use std::cmp::max;
use std::collections::{HashMap, HashSet};
use std::fmt::{self, Display};
Expand Down Expand Up @@ -179,37 +180,37 @@ pub fn is_html(input: &str) -> bool {
let mut input = BufferQueue::default();
input.push_back(chunk.try_reinterpret().unwrap());

let mut tok = Tokenizer::new(santok, Default::default());
let tok = Tokenizer::new(santok, Default::default());
let _ = tok.feed(&mut input);
tok.end();
tok.sink.was_sanitized
tok.sink.was_sanitized.get()
}

#[derive(Copy, Clone)]
#[derive(Clone)]
struct SanitizationTokenizer {
was_sanitized: bool,
was_sanitized: Cell<bool>,
}

impl SanitizationTokenizer {
pub fn new() -> SanitizationTokenizer {
SanitizationTokenizer {
was_sanitized: false,
was_sanitized: false.into(),
}
}
}

impl TokenSink for SanitizationTokenizer {
type Handle = ();
fn process_token(&mut self, token: Token, _line_number: u64) -> TokenSinkResult<()> {
fn process_token(&self, token: Token, _line_number: u64) -> TokenSinkResult<()> {
match token {
Token::CharacterTokens(_) | Token::EOFToken | Token::ParseError(_) => {}
_ => {
self.was_sanitized = true;
self.was_sanitized.set(true);
}
}
TokenSinkResult::Continue
}
fn end(&mut self) {}
fn end(&self) {}
}

/// An HTML sanitizer.
Expand Down
42 changes: 22 additions & 20 deletions src/rcdom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ pub struct RcDom {
pub document: Handle,

/// Errors that occurred during parsing.
pub errors: Vec<Cow<'static, str>>,
pub errors: RefCell<Vec<Cow<'static, str>>>,

/// The document's quirks mode.
pub quirks_mode: QuirksMode,
pub quirks_mode: Cell<QuirksMode>,
}

impl TreeSink for RcDom {
Expand All @@ -224,15 +224,17 @@ impl TreeSink for RcDom {

type Handle = Handle;

fn parse_error(&mut self, msg: Cow<'static, str>) {
self.errors.push(msg);
type ElemName<'a> = ExpandedName<'a>;

fn parse_error(&self, msg: Cow<'static, str>) {
self.errors.borrow_mut().push(msg);
}

fn get_document(&mut self) -> Handle {
fn get_document(&self) -> Handle {
self.document.clone()
}

fn get_template_contents(&mut self, target: &Handle) -> Handle {
fn get_template_contents(&self, target: &Handle) -> Handle {
if let NodeData::Element {
ref template_contents,
..
Expand All @@ -248,8 +250,8 @@ impl TreeSink for RcDom {
}
}

fn set_quirks_mode(&mut self, mode: QuirksMode) {
self.quirks_mode = mode;
fn set_quirks_mode(&self, mode: QuirksMode) {
self.quirks_mode.set(mode);
}

fn same_node(&self, x: &Handle, y: &Handle) -> bool {
Expand All @@ -264,7 +266,7 @@ impl TreeSink for RcDom {
}

fn create_element(
&mut self,
&self,
name: QualName,
attrs: Vec<Attribute>,
flags: ElementFlags,
Expand All @@ -281,18 +283,18 @@ impl TreeSink for RcDom {
})
}

fn create_comment(&mut self, text: StrTendril) -> Handle {
fn create_comment(&self, text: StrTendril) -> Handle {
Node::new(NodeData::Comment { contents: text })
}

fn create_pi(&mut self, target: StrTendril, data: StrTendril) -> Handle {
fn create_pi(&self, target: StrTendril, data: StrTendril) -> Handle {
Node::new(NodeData::ProcessingInstruction {
target,
contents: data,
})
}

fn append(&mut self, parent: &Handle, child: NodeOrText<Handle>) {
fn append(&self, parent: &Handle, child: NodeOrText<Handle>) {
// Append to an existing Text node if we have one.
if let NodeOrText::AppendText(ref text) = child {
if let Some(h) = parent.children.borrow().last() {
Expand All @@ -313,7 +315,7 @@ impl TreeSink for RcDom {
);
}

fn append_before_sibling(&mut self, sibling: &Handle, child: NodeOrText<Handle>) {
fn append_before_sibling(&self, sibling: &Handle, child: NodeOrText<Handle>) {
let (parent, i) = get_parent_and_index(sibling)
.expect("append_before_sibling called on node without parent");

Expand Down Expand Up @@ -349,7 +351,7 @@ impl TreeSink for RcDom {
}

fn append_based_on_parent_node(
&mut self,
&self,
element: &Self::Handle,
prev_element: &Self::Handle,
child: NodeOrText<Self::Handle>,
Expand All @@ -366,7 +368,7 @@ impl TreeSink for RcDom {
}

fn append_doctype_to_document(
&mut self,
&self,
name: StrTendril,
public_id: StrTendril,
system_id: StrTendril,
Expand All @@ -381,7 +383,7 @@ impl TreeSink for RcDom {
);
}

fn add_attrs_if_missing(&mut self, target: &Handle, attrs: Vec<Attribute>) {
fn add_attrs_if_missing(&self, target: &Handle, attrs: Vec<Attribute>) {
let mut existing = if let NodeData::Element { ref attrs, .. } = target.data {
attrs.borrow_mut()
} else {
Expand All @@ -399,11 +401,11 @@ impl TreeSink for RcDom {
);
}

fn remove_from_parent(&mut self, target: &Handle) {
fn remove_from_parent(&self, target: &Handle) {
remove_from_parent(target);
}

fn reparent_children(&mut self, node: &Handle, new_parent: &Handle) {
fn reparent_children(&self, node: &Handle, new_parent: &Handle) {
let mut children = node.children.borrow_mut();
let mut new_children = new_parent.children.borrow_mut();
for child in children.iter() {
Expand Down Expand Up @@ -433,8 +435,8 @@ impl Default for RcDom {
fn default() -> RcDom {
RcDom {
document: Node::new(NodeData::Document),
errors: vec![],
quirks_mode: tree_builder::NoQuirks,
errors: vec![].into(),
quirks_mode: tree_builder::NoQuirks.into(),
}
}
}
Expand Down

0 comments on commit d11ff76

Please sign in to comment.