| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#![allow(dead_code)] |
| 54 |
|
| 55 |
use std::cell::RefCell; |
| 56 |
use std::collections::BTreeMap; |
| 57 |
use std::fmt::Write as _; |
| 58 |
|
| 59 |
use html5ever::tokenizer::{ |
| 60 |
BufferQueue, Token, TokenSink, TokenSinkResult, Tokenizer, TokenizerOpts, |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 65 |
pub(crate) enum Piece { |
| 66 |
|
| 67 |
|
| 68 |
Open(String, BTreeMap<String, String>), |
| 69 |
|
| 70 |
Close(String), |
| 71 |
|
| 72 |
Text(String), |
| 73 |
|
| 74 |
|
| 75 |
Doctype(String), |
| 76 |
} |
| 77 |
|
| 78 |
impl Piece { |
| 79 |
|
| 80 |
fn show(&self) -> String { |
| 81 |
match self { |
| 82 |
Self::Open(name, attrs) => { |
| 83 |
let attrs: Vec<String> = attrs |
| 84 |
.iter() |
| 85 |
.map(|(k, v)| { |
| 86 |
if v.is_empty() { |
| 87 |
k.clone() |
| 88 |
} else { |
| 89 |
format!("{k}=\"{v}\"") |
| 90 |
} |
| 91 |
}) |
| 92 |
.collect(); |
| 93 |
if attrs.is_empty() { |
| 94 |
format!("<{name}>") |
| 95 |
} else { |
| 96 |
format!("<{name} {}>", attrs.join(" ")) |
| 97 |
} |
| 98 |
} |
| 99 |
Self::Close(name) => format!("</{name}>"), |
| 100 |
Self::Text(t) => format!("{t:?}"), |
| 101 |
Self::Doctype(d) => format!("<!doctype {d}>"), |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
#[derive(Debug, Clone, Default)] |
| 108 |
pub(crate) struct Parity { |
| 109 |
ignored_attrs: Vec<String>, |
| 110 |
ignored_classes: Vec<String>, |
| 111 |
} |
| 112 |
|
| 113 |
impl Parity { |
| 114 |
|
| 115 |
pub(crate) fn strict() -> Self { |
| 116 |
Self::default() |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
#[must_use] |
| 124 |
pub(crate) fn ignoring_attr(mut self, name: &str) -> Self { |
| 125 |
self.ignored_attrs.push(name.to_ascii_lowercase()); |
| 126 |
self |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
#[must_use] |
| 134 |
pub(crate) fn ignoring_class(mut self, name: &str) -> Self { |
| 135 |
self.ignored_classes.push(name.to_owned()); |
| 136 |
self |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
pub(crate) fn normalize(&self, html: &str) -> Vec<Piece> { |
| 141 |
let raw = tokenize(html); |
| 142 |
let mut out = Vec::with_capacity(raw.len()); |
| 143 |
for piece in raw { |
| 144 |
match piece { |
| 145 |
Piece::Open(name, attrs) => { |
| 146 |
let attrs = attrs |
| 147 |
.into_iter() |
| 148 |
.filter(|(k, _)| !self.ignored_attrs.contains(k)) |
| 149 |
.map(|(k, v)| { |
| 150 |
if k == "class" { |
| 151 |
let mut classes: Vec<&str> = v |
| 152 |
.split_ascii_whitespace() |
| 153 |
.filter(|c| !self.ignored_classes.iter().any(|i| i == c)) |
| 154 |
.collect(); |
| 155 |
classes.sort_unstable(); |
| 156 |
(k, classes.join(" ")) |
| 157 |
} else { |
| 158 |
(k, v) |
| 159 |
} |
| 160 |
}) |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
.filter(|(k, v)| !(k == "class" && v.is_empty())) |
| 165 |
.collect(); |
| 166 |
out.push(Piece::Open(name, attrs)); |
| 167 |
} |
| 168 |
other => out.push(other), |
| 169 |
} |
| 170 |
} |
| 171 |
out |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
#[track_caller] |
| 180 |
pub(crate) fn assert(&self, screen: &str, askama: &str, quasi: &str) { |
| 181 |
let left = self.normalize(askama); |
| 182 |
let right = self.normalize(quasi); |
| 183 |
if left == right { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
let at = left |
| 188 |
.iter() |
| 189 |
.zip(right.iter()) |
| 190 |
.position(|(a, b)| a != b) |
| 191 |
.unwrap_or_else(|| left.len().min(right.len())); |
| 192 |
|
| 193 |
let mut msg = format!( |
| 194 |
"screen `{screen}` does not render the same page through quasi as through Askama\n\ |
| 195 |
first difference at piece {at} of {} (Askama) / {} (quasi)\n", |
| 196 |
left.len(), |
| 197 |
right.len() |
| 198 |
); |
| 199 |
let from = at.saturating_sub(3); |
| 200 |
msg.push_str("\n context, Askama:\n"); |
| 201 |
for (i, p) in left.iter().enumerate().skip(from).take(7) { |
| 202 |
let marker = if i == at { ">>" } else { " " }; |
| 203 |
let _ = writeln!(msg, " {marker} {i:4} {}", p.show()); |
| 204 |
} |
| 205 |
msg.push_str("\n context, quasi:\n"); |
| 206 |
for (i, p) in right.iter().enumerate().skip(from).take(7) { |
| 207 |
let marker = if i == at { ">>" } else { " " }; |
| 208 |
let _ = writeln!(msg, " {marker} {i:4} {}", p.show()); |
| 209 |
} |
| 210 |
msg.push_str( |
| 211 |
"\nIf the difference is the point of the conversion, name it with \ |
| 212 |
.ignoring_attr()/.ignoring_class() rather than widening the test.\n", |
| 213 |
); |
| 214 |
panic!("{msg}"); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
struct Sink { |
| 220 |
pieces: RefCell<Vec<Piece>>, |
| 221 |
|
| 222 |
literal: RefCell<u32>, |
| 223 |
} |
| 224 |
|
| 225 |
impl TokenSink for Sink { |
| 226 |
type Handle = (); |
| 227 |
|
| 228 |
fn process_token(&self, token: Token, _line: u64) -> TokenSinkResult<()> { |
| 229 |
match token { |
| 230 |
Token::DoctypeToken(d) => { |
| 231 |
let name = d.name.unwrap_or_default().to_ascii_lowercase(); |
| 232 |
self.pieces.borrow_mut().push(Piece::Doctype(name)); |
| 233 |
} |
| 234 |
Token::TagToken(tag) => { |
| 235 |
let name = tag.name.to_string(); |
| 236 |
let literal = matches!(name.as_str(), "pre" | "textarea"); |
| 237 |
match tag.kind { |
| 238 |
html5ever::tokenizer::TagKind::StartTag => { |
| 239 |
if literal { |
| 240 |
*self.literal.borrow_mut() += 1; |
| 241 |
} |
| 242 |
let mut attrs = BTreeMap::new(); |
| 243 |
for attr in tag.attrs { |
| 244 |
attrs.insert( |
| 245 |
attr.name.local.to_string().to_ascii_lowercase(), |
| 246 |
attr.value.to_string(), |
| 247 |
); |
| 248 |
} |
| 249 |
self.pieces.borrow_mut().push(Piece::Open(name, attrs)); |
| 250 |
} |
| 251 |
html5ever::tokenizer::TagKind::EndTag => { |
| 252 |
if literal { |
| 253 |
let mut depth = self.literal.borrow_mut(); |
| 254 |
*depth = depth.saturating_sub(1); |
| 255 |
} |
| 256 |
self.pieces.borrow_mut().push(Piece::Close(name)); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
Token::CharacterTokens(text) => { |
| 261 |
let text = text.to_string(); |
| 262 |
if *self.literal.borrow() > 0 { |
| 263 |
self.pieces.borrow_mut().push(Piece::Text(text)); |
| 264 |
} else { |
| 265 |
let collapsed = text.split_whitespace().collect::<Vec<_>>().join(" "); |
| 266 |
if !collapsed.is_empty() { |
| 267 |
self.pieces.borrow_mut().push(Piece::Text(collapsed)); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
Token::CommentToken(_) | Token::ParseError(_) => {} |
| 275 |
Token::NullCharacterToken | Token::EOFToken => {} |
| 276 |
} |
| 277 |
TokenSinkResult::Continue |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
fn tokenize(html: &str) -> Vec<Piece> { |
| 282 |
let sink = Sink { |
| 283 |
pieces: RefCell::new(Vec::new()), |
| 284 |
literal: RefCell::new(0), |
| 285 |
}; |
| 286 |
let tok = Tokenizer::new(sink, TokenizerOpts::default()); |
| 287 |
let input = BufferQueue::default(); |
| 288 |
input.push_back(html5ever::tendril::StrTendril::from(html)); |
| 289 |
let _ = tok.feed(&input); |
| 290 |
tok.end(); |
| 291 |
tok.sink.pieces.take() |
| 292 |
} |
| 293 |
|