Skip to content

Commit

Permalink
fix: fix nested <pre> (fix #70)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Oct 28, 2024
1 parent 6a9496a commit 47facf5
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 27 deletions.
67 changes: 40 additions & 27 deletions markup_fmt/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,23 +1028,22 @@ impl<'s> Parser<'s> {
children.push(self.parse_node()?);
}
Some(..) => {
children.push(
if tag_name.eq_ignore_ascii_case("script")
|| tag_name.eq_ignore_ascii_case("style")
|| tag_name.eq_ignore_ascii_case("pre")
|| tag_name.eq_ignore_ascii_case("textarea")
{
self.parse_raw_text_node(tag_name).map(|text_node| {
let raw = text_node.raw;
Node {
kind: NodeKind::Text(text_node),
raw,
}
})?
} else {
self.parse_node()?
},
);
if tag_name.eq_ignore_ascii_case("script")
|| tag_name.eq_ignore_ascii_case("style")
|| tag_name.eq_ignore_ascii_case("pre")
|| tag_name.eq_ignore_ascii_case("textarea")
{
let text_node = self.parse_raw_text_node(tag_name)?;
let raw = text_node.raw;
if !raw.is_empty() {
children.push(Node {
kind: NodeKind::Text(text_node),
raw,
});
}
} else {
children.push(self.parse_node()?);
}
}
None => return Err(self.emit_error(SyntaxErrorKind::ExpectCloseTag)),
}
Expand Down Expand Up @@ -1539,6 +1538,8 @@ impl<'s> Parser<'s> {
.map(|(i, _)| *i)
.unwrap_or(self.source.len());

let allow_nested = tag_name.eq_ignore_ascii_case("pre");
let mut nested = 0u16;
let mut line_breaks = 0;
let end;
loop {
Expand All @@ -1547,17 +1548,29 @@ impl<'s> Parser<'s> {
let i = *i;
let mut chars = self.chars.clone();
chars.next();
if chars
.next_if(|(_, c)| *c == '/')
.map(|_| {
chars
.zip(tag_name.chars())
.all(|((_, a), b)| a.eq_ignore_ascii_case(&b))
})
.unwrap_or_default()
if chars.next_if(|(_, c)| *c == '/').is_some()
&& chars
.by_ref()
.zip(tag_name.chars())
.all(|((_, a), b)| a.eq_ignore_ascii_case(&b))
{
end = i;
break;
if nested == 0 {
end = i;
break;
} else {
nested -= 1;
self.chars = chars;
continue;
}
} else if allow_nested
&& chars
.by_ref()
.zip(tag_name.chars())
.all(|((_, a), b)| a.eq_ignore_ascii_case(&b))
{
nested += 1;
self.chars = chars;
continue;
}
self.chars.next();
}
Expand Down
4 changes: 4 additions & 0 deletions markup_fmt/tests/fmt/html/tags/pre.default.snap
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@ ___________________________

<pre><a></pre>
<pre></b></pre>

<div>
<pre><pre></pre></pre>
</div>
4 changes: 4 additions & 0 deletions markup_fmt/tests/fmt/html/tags/pre.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@

<pre><a></pre>
<pre></b></pre>

<div>
<pre><pre></pre></pre>
</div>
4 changes: 4 additions & 0 deletions markup_fmt/tests/fmt/html/tags/pre.ignore-whitespace.snap
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@ ___________________________

<pre><a></pre>
<pre></b></pre>

<div>
<pre><pre></pre></pre>
</div>
4 changes: 4 additions & 0 deletions markup_fmt/tests/fmt/html/tags/pre.large-print-width.snap
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ ___________________________

<pre><a></pre>
<pre></b></pre>

<div>
<pre><pre></pre></pre>
</div>
8 changes: 8 additions & 0 deletions markup_fmt/tests/fmt/html/tags/pre.small-print-width.snap
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,11 @@ ___________________________
<pre
></b></pre
>

<div
>
<pre
><pre></pre></pre
>
</div
>
4 changes: 4 additions & 0 deletions markup_fmt/tests/fmt/html/tags/pre.strict-whitespace.snap
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,7 @@ ___________________________

<pre><a></pre>
<pre></b></pre>

<div>
<pre><pre></pre></pre>
</div>

0 comments on commit 47facf5

Please sign in to comment.