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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
use std::path::PathBuf;
use swc_common::{errors::Handler, input::SourceFileInput, Spanned};
use swc_html_ast::*;
use swc_html_parser::{
lexer::Lexer,
parser::{PResult, Parser, ParserConfig},
};
use swc_html_visit::{Visit, VisitMut, VisitMutWith, VisitWith};
use testing::NormalizedOutput;
#[allow(dead_code)]
pub fn document_test(input: PathBuf, config: ParserConfig) {
testing::run_test2(false, |cm, handler| {
let json_path = input.parent().unwrap().join("output.json");
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm));
let mut parser = Parser::new(lexer, config);
let document: PResult<Document> = parser.parse_document();
let errors = parser.take_errors();
for err in &errors {
err.to_diagnostics(&handler).emit();
}
if !errors.is_empty() {
return Err(());
}
match document {
Ok(document) => {
let actual_json = serde_json::to_string_pretty(&document)
.map(NormalizedOutput::from)
.expect("failed to serialize document");
actual_json.compare_to_file(json_path).unwrap();
Ok(())
}
Err(err) => {
let mut d = err.to_diagnostics(&handler);
d.note(&format!("current token = {}", parser.dump_cur()));
d.emit();
Err(())
}
}
})
.unwrap();
}
pub struct DomVisualizer<'a> {
pub dom_buf: &'a mut String,
pub indent: usize,
}
impl DomVisualizer<'_> {
fn get_ident(&self) -> String {
let mut indent = String::new();
indent.push_str("| ");
indent.push_str(&" ".repeat(self.indent));
indent
}
}
impl VisitMut for DomVisualizer<'_> {
fn visit_mut_document_type(&mut self, n: &mut DocumentType) {
let mut document_type = String::new();
document_type.push_str(&self.get_ident());
document_type.push_str("<!DOCTYPE ");
if let Some(name) = &n.name {
document_type.push_str(name);
}
if let Some(public_id) = &n.public_id {
document_type.push(' ');
document_type.push('"');
document_type.push_str(public_id);
document_type.push('"');
if let Some(system_id) = &n.system_id {
document_type.push(' ');
document_type.push('"');
document_type.push_str(system_id);
document_type.push('"');
} else {
document_type.push(' ');
document_type.push('"');
document_type.push('"');
}
} else if let Some(system_id) = &n.system_id {
document_type.push(' ');
document_type.push('"');
document_type.push('"');
document_type.push(' ');
document_type.push('"');
document_type.push_str(system_id);
document_type.push('"');
}
document_type.push('>');
document_type.push('\n');
self.dom_buf.push_str(&document_type);
n.visit_mut_children_with(self);
}
fn visit_mut_element(&mut self, n: &mut Element) {
let mut element = String::new();
element.push_str(&self.get_ident());
element.push('<');
match n.namespace {
Namespace::SVG => {
element.push_str("svg ");
}
Namespace::MATHML => {
element.push_str("math ");
}
_ => {}
}
element.push_str(&n.tag_name);
element.push('>');
element.push('\n');
let is_template = n.namespace == Namespace::HTML && &*n.tag_name == "template";
if is_template {
self.indent += 1;
element.push_str(&self.get_ident());
element.push_str("content");
element.push('\n');
}
n.attributes
.sort_by(|a, b| a.name.partial_cmp(&b.name).unwrap());
self.dom_buf.push_str(&element);
let old_indent = self.indent;
self.indent += 1;
n.visit_mut_children_with(self);
self.indent = old_indent;
if is_template {
self.indent -= 1;
}
}
fn visit_mut_attribute(&mut self, n: &mut Attribute) {
let mut attribute = String::new();
attribute.push_str(&self.get_ident());
if let Some(prefix) = &n.prefix {
attribute.push_str(prefix);
attribute.push(' ');
}
attribute.push_str(&n.name);
attribute.push('=');
attribute.push('"');
if let Some(value) = &n.value {
attribute.push_str(value);
}
attribute.push('"');
attribute.push('\n');
self.dom_buf.push_str(&attribute);
n.visit_mut_children_with(self);
}
fn visit_mut_text(&mut self, n: &mut Text) {
let mut text = String::new();
text.push_str(&self.get_ident());
text.push('"');
text.push_str(&n.data);
text.push('"');
text.push('\n');
self.dom_buf.push_str(&text);
n.visit_mut_children_with(self);
}
fn visit_mut_comment(&mut self, n: &mut Comment) {
let mut comment = String::new();
comment.push_str(&self.get_ident());
comment.push_str("<!-- ");
comment.push_str(&n.data);
comment.push_str(" -->");
comment.push('\n');
self.dom_buf.push_str(&comment);
n.visit_mut_children_with(self);
}
}
#[allow(dead_code)]
pub fn document_dom_visualizer(input: PathBuf, config: ParserConfig) {
let dir = input.parent().unwrap().to_path_buf();
testing::run_test2(false, |cm, handler| {
// Type annotation
if false {
return Ok(());
}
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm));
let mut parser = Parser::new(lexer, config);
let document: PResult<Document> = parser.parse_document();
match document {
Ok(mut document) => {
let mut dom_buf = String::new();
document.visit_mut_with(&mut DomVisualizer {
dom_buf: &mut dom_buf,
indent: 0,
});
NormalizedOutput::from(dom_buf)
.compare_to_file(dir.join("dom.rust-debug"))
.unwrap();
Ok(())
}
Err(err) => {
let mut d = err.to_diagnostics(&handler);
d.note(&format!("current token = {}", parser.dump_cur()));
d.emit();
panic!();
}
}
})
.unwrap();
}
struct SpanVisualizer<'a> {
handler: &'a Handler,
}
macro_rules! mtd {
($T:ty,$name:ident) => {
fn $name(&mut self, n: &$T) {
let span = n.span();
self.handler.struct_span_err(span, stringify!($T)).emit();
n.visit_children_with(self);
}
};
}
impl Visit for SpanVisualizer<'_> {
mtd!(Document, visit_document);
mtd!(DocumentFragment, visit_document_fragment);
mtd!(Child, visit_child);
mtd!(DocumentType, visit_document_type);
mtd!(Element, visit_element);
mtd!(Attribute, visit_attribute);
mtd!(Text, visit_text);
mtd!(Comment, visit_comment);
}
pub fn document_span_visualizer(input: PathBuf, config: ParserConfig, relative_to_file: bool) {
let dir = input.parent().unwrap().to_path_buf();
let output = testing::run_test2(false, |cm, handler| {
// Type annotation
if false {
return Ok(());
}
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm));
let mut parser = Parser::new(lexer, config);
let document: PResult<Document> = parser.parse_document();
match document {
Ok(document) => {
document.visit_with(&mut SpanVisualizer { handler: &handler });
Err(())
}
Err(err) => {
let mut d = err.to_diagnostics(&handler);
d.note(&format!("current token = {}", parser.dump_cur()));
d.emit();
panic!();
}
}
})
.unwrap_err();
let output_path = if relative_to_file {
input.with_extension("span.rust-debug")
} else {
dir.join("span.rust-debug")
};
output.compare_to_file(output_path).unwrap();
}
|