fix(html/codegen): Emit newline in textarea/pre (#4919)

This commit is contained in:
Alexander Akait 2022-06-10 07:19:30 +03:00 committed by GitHub
parent c58f6eb661
commit 4f1e04671d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 763 additions and 10 deletions

View File

@ -23,7 +23,6 @@ where
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct Ctx {
pub need_escape_text: bool,
pub need_extra_newline_in_text: bool,
}
pub(super) struct WithCtx<'w, I: 'w + HtmlWriter> {

View File

@ -341,6 +341,19 @@ where
} else if !n.children.is_empty() {
let ctx = self.create_context_for_element(n);
let need_extra_newline =
n.namespace == Namespace::HTML && matches!(&*n.tag_name, "textarea" | "pre");
if need_extra_newline {
if let Some(Child::Text(Text { data, .. })) = &n.children.first() {
if data.contains('\n') {
newline!(self);
} else {
formatting_newline!(self);
}
}
}
if self.config.minify {
self.with_ctx(ctx)
.emit_list_for_tag_omission(TagOmissionParent::Element(n))?;
@ -706,10 +719,6 @@ where
if self.ctx.need_escape_text {
let mut data = String::with_capacity(n.data.len());
if self.ctx.need_extra_newline_in_text && n.data.contains('\n') {
data.push('\n');
}
if self.config.minify {
data.push_str(&minify_text(&n.data));
} else {
@ -740,12 +749,9 @@ where
_ if self.is_plaintext => false,
_ => true,
};
let need_extra_newline_in_text =
n.namespace == Namespace::HTML && matches!(&*n.tag_name, "textarea" | "pre");
Ctx {
need_escape_text,
need_extra_newline_in_text,
..self.ctx
}
}

View File

@ -7,7 +7,9 @@
<textarea>
foo</textarea>
<textarea>foo</textarea>
<textarea>foo</textarea>
<textarea>
foo</textarea>
<textarea>
foo</textarea>
</body></html>

View File

@ -14,6 +14,92 @@
Oh, bring back my Bonnie to me.
</pre>
<pre>
<code>
Text
</code>
</pre>
<pre>Text</pre>
<pre><code>
Text
</code></pre>
<pre>
<code>
Text
</code>
</pre>
<pre>Text
</pre>
<pre>
Text</pre>
<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>
<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>
<pre>
<pre>Text
</pre>
</pre>
<pre><pre>Text</pre></pre>
<pre>
<pre>
Text
</pre>
</pre>
<pre>
<pre>
Text
</pre>
</pre>
</body>
</html>

View File

@ -14,6 +14,94 @@
Oh, bring back my Bonnie to me.
</pre>
<pre>
<code>
Text
</code>
</pre>
<pre>
Text</pre>
<pre><code>
Text
</code></pre>
<pre>
<code>
Text
</code>
</pre>
<pre>
Text
</pre>
<pre>
Text</pre>
<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>
<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>
<pre><pre>
Text
</pre>
</pre>
<pre><pre>
Text</pre></pre>
<pre><pre>
Text
</pre>
</pre>
<pre>
<pre>
Text
</pre>
</pre>

View File

@ -12,6 +12,90 @@
Oh, bring back my Bonnie to me.
</pre>
<pre>
<code>
Text
</code>
</pre>
<pre>Text</pre>
<pre><code>
Text
</code></pre>
<pre> <code>
Text
</code>
</pre>
<pre>
Text
</pre>
<pre>Text</pre>
<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>
<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>
<pre><pre>
Text
</pre>
</pre>
<pre><pre>Text</pre></pre>
<pre><pre>
Text
</pre>
</pre>
<pre>
<pre>
Text
</pre>
</pre>

View File

@ -0,0 +1,30 @@
| <!DOCTYPE html>
| <html>
| <head>
| <body>
| "
"
| <pre>
| "
"
| <code>
| "
Text
"
| "
"
| "
"

View File

@ -0,0 +1,23 @@
<!doctype html>
<html>
<body>
<pre>
<code>
Text
</code>
</pre>
</body>
</html>

View File

@ -0,0 +1,136 @@
{
"type": "Document",
"span": {
"start": 1,
"end": 105,
"ctxt": 0
},
"mode": "no-quirks",
"children": [
{
"type": "DocumentType",
"span": {
"start": 1,
"end": 16,
"ctxt": 0
},
"name": "html",
"publicId": null,
"systemId": null
},
{
"type": "Element",
"span": {
"start": 17,
"end": 105,
"ctxt": 0
},
"tagName": "html",
"namespace": "http://www.w3.org/1999/xhtml",
"attributes": [],
"children": [
{
"type": "Element",
"span": {
"start": 0,
"end": 0,
"ctxt": 0
},
"tagName": "head",
"namespace": "http://www.w3.org/1999/xhtml",
"attributes": [],
"children": [],
"content": null
},
{
"type": "Element",
"span": {
"start": 24,
"end": 98,
"ctxt": 0
},
"tagName": "body",
"namespace": "http://www.w3.org/1999/xhtml",
"attributes": [],
"children": [
{
"type": "Text",
"span": {
"start": 30,
"end": 32,
"ctxt": 0
},
"data": "\n\n"
},
{
"type": "Element",
"span": {
"start": 32,
"end": 88,
"ctxt": 0
},
"tagName": "pre",
"namespace": "http://www.w3.org/1999/xhtml",
"attributes": [],
"children": [
{
"type": "Text",
"span": {
"start": 38,
"end": 44,
"ctxt": 0
},
"data": "\n\n\n "
},
{
"type": "Element",
"span": {
"start": 44,
"end": 79,
"ctxt": 0
},
"tagName": "code",
"namespace": "http://www.w3.org/1999/xhtml",
"attributes": [],
"children": [
{
"type": "Text",
"span": {
"start": 50,
"end": 72,
"ctxt": 0
},
"data": "\n\n\n\n Text\n\n\n\n "
}
],
"content": null
},
{
"type": "Text",
"span": {
"start": 79,
"end": 82,
"ctxt": 0
},
"data": "\n\n\n"
}
],
"content": null
},
{
"type": "Text",
"span": {
"start": 88,
"end": 98,
"ctxt": 0
},
"data": "\n\n\n"
}
],
"content": null
}
],
"content": null
}
]
}

View File

@ -0,0 +1,299 @@
x Document
,-[$DIR/tests/fixture/element/pre-3/input.html:1:1]
1 | ,-> <!doctype html>
2 | | <html>
3 | | <body>
4 | |
5 | | <pre>
6 | |
7 | |
8 | |
9 | | <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | | </code>
18 | |
19 | |
20 | | </pre>
21 | |
22 | | </body>
23 | `-> </html>
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:1:1]
1 | <!doctype html>
: ^^^^^^^^^^^^^^^
`----
x DocumentType
,-[$DIR/tests/fixture/element/pre-3/input.html:1:1]
1 | <!doctype html>
: ^^^^^^^^^^^^^^^
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:2:1]
2 | ,-> <html>
3 | | <body>
4 | |
5 | | <pre>
6 | |
7 | |
8 | |
9 | | <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | | </code>
18 | |
19 | |
20 | | </pre>
21 | |
22 | | </body>
23 | `-> </html>
`----
x Element
,-[$DIR/tests/fixture/element/pre-3/input.html:2:1]
2 | ,-> <html>
3 | | <body>
4 | |
5 | | <pre>
6 | |
7 | |
8 | |
9 | | <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | | </code>
18 | |
19 | |
20 | | </pre>
21 | |
22 | | </body>
23 | `-> </html>
`----
x Child
x Element
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:3:1]
3 | ,-> <body>
4 | |
5 | | <pre>
6 | |
7 | |
8 | |
9 | | <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | | </code>
18 | |
19 | |
20 | | </pre>
21 | |
22 | `-> </body>
23 | </html>
`----
x Element
,-[$DIR/tests/fixture/element/pre-3/input.html:3:1]
3 | ,-> <body>
4 | |
5 | | <pre>
6 | |
7 | |
8 | |
9 | | <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | | </code>
18 | |
19 | |
20 | | </pre>
21 | |
22 | `-> </body>
23 | </html>
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:3:1]
3 | ,-> <body>
4 | `->
5 | <pre>
`----
x Text
,-[$DIR/tests/fixture/element/pre-3/input.html:3:1]
3 | ,-> <body>
4 | `->
5 | <pre>
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:5:1]
5 | ,-> <pre>
6 | |
7 | |
8 | |
9 | | <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | | </code>
18 | |
19 | |
20 | `-> </pre>
`----
x Element
,-[$DIR/tests/fixture/element/pre-3/input.html:5:1]
5 | ,-> <pre>
6 | |
7 | |
8 | |
9 | | <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | | </code>
18 | |
19 | |
20 | `-> </pre>
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:6:1]
6 | ,->
7 | |
8 | |
9 | `-> <code>
`----
x Text
,-[$DIR/tests/fixture/element/pre-3/input.html:6:1]
6 | ,->
7 | |
8 | |
9 | `-> <code>
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:9:4]
9 | ,-> <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | `-> </code>
`----
x Element
,-[$DIR/tests/fixture/element/pre-3/input.html:9:4]
9 | ,-> <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | `-> </code>
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:9:4]
9 | ,-> <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | `-> </code>
`----
x Text
,-[$DIR/tests/fixture/element/pre-3/input.html:9:4]
9 | ,-> <code>
10 | |
11 | |
12 | |
13 | | Text
14 | |
15 | |
16 | |
17 | `-> </code>
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:17:4]
17 | ,-> </code>
18 | |
19 | `->
20 | </pre>
`----
x Text
,-[$DIR/tests/fixture/element/pre-3/input.html:17:4]
17 | ,-> </code>
18 | |
19 | `->
20 | </pre>
`----
x Child
,-[$DIR/tests/fixture/element/pre-3/input.html:20:1]
20 | ,-> </pre>
21 | |
22 | `-> </body>
23 | </html>
`----
x Text
,-[$DIR/tests/fixture/element/pre-3/input.html:20:1]
20 | ,-> </pre>
21 | |
22 | `-> </body>
23 | </html>
`----