Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html/parser): cdata parsing + tests #6534

Merged
merged 7 commits into from Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/swc_html_codegen/tests/fixture/cdata/input.html
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<svg viewBox="0 0 100 100">
<text><![CDATA[content]]></text>
</svg>
</body>
</html>
11 changes: 11 additions & 0 deletions crates/swc_html_codegen/tests/fixture/cdata/output.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<svg viewBox="0 0 100 100">
<text>content</text>
</svg>

</body></html>
8 changes: 8 additions & 0 deletions crates/swc_html_codegen/tests/fixture/cdata/output.min.html
@@ -0,0 +1,8 @@
<!doctype html><html lang=en><head>
<title>Document</title>
</head>
<body>
<svg viewBox="0 0 100 100">
<text>content</text>
</svg>

64 changes: 30 additions & 34 deletions crates/swc_html_parser/src/lexer/mod.rs
Expand Up @@ -134,8 +134,6 @@ struct Comment {

pub(crate) type LexResult<T> = Result<T, ErrorKind>;

// TODO improve `raw` for all tokens (linting + better codegen)

pub struct Lexer<I>
where
I: Input,
Expand Down Expand Up @@ -2682,37 +2680,35 @@ where
// error. Create a comment token whose data is the "[CDATA[" string.
// Switch to the bogus comment state.
Some('[') => match self.consume_next_char() {
Some(c @ 'c' | c @ 'C') => match self.consume_next_char() {
Some(d @ 'd' | d @ 'D') => match self.consume_next_char() {
Some(a1 @ 'a' | a1 @ 'A') => match self.consume_next_char() {
Some(t @ 't' | t @ 'T') => match self.consume_next_char() {
Some(a2 @ 'a' | a2 @ 'A') => {
match self.consume_next_char() {
Some('[') => {
if let Some(false) = self.is_adjusted_current_node_is_element_in_html_namespace {
self.state = State::CdataSection;
} else {
self.emit_error(
ErrorKind::CdataInHtmlContent,
);
let mut data = String::with_capacity(7);

data.push('[');
data.push(c);
data.push(d);
data.push(a1);
data.push(t);
data.push(a2);
data.push('[');

self.create_comment_token(Some(data), "<!");
self.state = State::BogusComment;
}
}
_ => {
anything_else(self);
Some(c @ 'C') => match self.consume_next_char() {
Some(d @ 'D') => match self.consume_next_char() {
Some(a1 @ 'A') => match self.consume_next_char() {
Some(t @ 'T') => match self.consume_next_char() {
Some(a2 @ 'A') => match self.consume_next_char() {
Some('[') => {
if let Some(false) = self.is_adjusted_current_node_is_element_in_html_namespace {
self.state = State::CdataSection;
} else {
self.emit_error(
ErrorKind::CdataInHtmlContent,
);
let mut data = String::with_capacity(7);

data.push('[');
data.push(c);
data.push(d);
data.push(a1);
data.push(t);
data.push(a2);
data.push('[');

self.create_comment_token(Some(data), "<!");
self.state = State::BogusComment;
}
}
_ => {
anything_else(self);
}
}
_ => {
anything_else(self);
Expand All @@ -2725,15 +2721,15 @@ where
_ => {
anything_else(self);
}
},
}
_ => {
anything_else(self);
}
},
}
_ => {
anything_else(self);
}
},
}
// Anything else
// This is an incorrectly-opened-comment parse error. Create a comment token
// whose data is the empty string. Switch to the bogus comment state (don't
Expand Down
12 changes: 7 additions & 5 deletions crates/swc_html_parser/src/parser/mod.rs
Expand Up @@ -508,7 +508,7 @@ where
}
Data::Text { data, raw } => {
let span = if let Some(end_span) = node.end_span.take() {
swc_common::Span::new(start_span.lo(), end_span.hi(), Default::default())
Span::new(start_span.lo(), end_span.hi(), Default::default())
} else {
start_span
};
Expand All @@ -533,7 +533,12 @@ where
fn run(&mut self) -> PResult<()> {
while !self.stopped {
let adjusted_current_node = self.get_adjusted_current_node();
let is_element_in_html_namespace = is_element_in_html_namespace(adjusted_current_node);
let is_element_in_html_namespace =
if is_element_in_html_namespace(adjusted_current_node) {
true
} else {
is_html_integration_point(adjusted_current_node)
};

self.input
.set_adjusted_current_node_to_html_namespace(is_element_in_html_namespace);
Expand Down Expand Up @@ -625,9 +630,6 @@ where
let is_mathml_annotation_xml = is_mathml_annotation_xml(adjusted_current_node);
let is_html_integration_point = is_html_integration_point(adjusted_current_node);

self.input
.set_adjusted_current_node_to_html_namespace(is_element_in_html_namespace);

if self.open_elements_stack.items.is_empty()
|| is_element_in_html_namespace
|| (is_mathml_text_integration_point
Expand Down
112 changes: 112 additions & 0 deletions crates/swc_html_parser/tests/fixture/text/cdata-svg/dom.rust-debug
@@ -0,0 +1,112 @@
| <!DOCTYPE html>
| <html>
| lang="en-US"
| <head>
| "
"
| <meta>
| charset="utf-8"
| "
"
| <title>
| "SVG Demo"
| "
"
| <meta>
| content="width=device-width"
| name="viewport"
| "
"
| "
"
| <body>
| "
"
| <svg svg>
| viewBox="0 0 100 100"
| "
"
| <svg title>
| "A gradient"
| "
"
| <svg linearGradient>
| id="gradient"
| "
"
| <svg stop>
| class="begin"
| offset="0%"
| "
"
| <svg stop>
| class="end"
| offset="100%"
| "
"
| "
"
| <svg rect>
| height="100"
| style="fill:url(#gradient)"
| width="100"
| x="0"
| y="0"
| "
"
| <svg circle>
| cx="50"
| cy="50"
| r="30"
| style="fill:url(#gradient)"
| "
"
| <svg text>
| class="empty"
| "
"
| <svg text>
| "content"
| "
"
| <svg text>
| "&amping"
| "
"
| <svg text>
| "&amping ]"
| "
"
| <svg text>
| "&amping]] "
| "
"
| <svg text>
| "<message>text</message>"
| "
"
| <svg text>
| "</this is malformed!</malformed</malformed & worse>"
| "
"
| <svg text>
| "12"
| "
"
| <svg text>
| "
data
"
| "
"
| <svg text>
| "bracket ]after"
| "
"
| <svg text>
| "abracket ]afterb"
| "
"
| "

"
32 changes: 32 additions & 0 deletions crates/swc_html_parser/tests/fixture/text/cdata-svg/input.html
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>SVG Demo</title>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<svg viewBox="0 0 100 100">
<title>A gradient</title>
<linearGradient id="gradient">
<stop class="begin" offset="0%" />
<stop class="end" offset="100%" />
</linearGradient>
<rect x="0" y="0" width="100" height="100" style="fill:url(#gradient)" />
<circle cx="50" cy="50" r="30" style="fill:url(#gradient)" />
<text class="empty"><![CDATA[]]></text>
<text><![CDATA[content]]></text>
<text><![CDATA[&amping]]></text>
<text><![CDATA[&amping ]]]></text>
<text><![CDATA[&amping]] ]]></text>
<text><![CDATA[<message>text</message>]]></text>
<text><![CDATA[</this is malformed!</malformed</malformed & worse>]]></text>
<text><![CDATA[1]]><![CDATA[2]]></text>
<text>
<![CDATA[data]]>
</text>
<text><![CDATA[bracket ]after]]></text>
<text>a<![CDATA[bracket ]after]]>b</text>
</svg>
</body>
</html>