Skip to content

Commit

Permalink
[fix]: strip leading newline characters after 'pre' elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapped committed Feb 15, 2022
1 parent 39f0d8c commit 1dccec7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/compiler/parse/state/tag.ts
Expand Up @@ -84,7 +84,7 @@ export default function tag(parser: Parser) {
parser.current().children.length
) {
parser.error(
parser_errors.invalid_element_content(slug, name),
parser_errors.invalid_element_content(slug, name),
parser.current().children[0].start
);
}
Expand Down Expand Up @@ -197,6 +197,14 @@ export default function tag(parser: Parser) {

parser.eat('>', true);

// A leading newline character immediately following the pre element start tag is stripped
// See spec: https://www.w3.org/TR/2011/WD-html5-20110113/grouping-content.html#the-pre-element
if (!is_closing_tag && name === 'pre') {
if (!parser.eat('\r\n')) {
parser.eat('\n');
}
}

if (self_closing) {
// don't push self-closing elements onto the stack
element.end = parser.index;
Expand Down Expand Up @@ -258,7 +266,7 @@ function read_tag_name(parser: Parser) {
const match = fuzzymatch(name.slice(7), valid_meta_tags);

parser.error(
parser_errors.invalid_tag_name_svelte_element(valid_meta_tags, match),
parser_errors.invalid_tag_name_svelte_element(valid_meta_tags, match),
start
);
}
Expand Down
@@ -0,0 +1,3 @@
<pre>
test
</pre>
25 changes: 25 additions & 0 deletions test/parser/samples/leading-newline-after-pre-tag/output.json
@@ -0,0 +1,25 @@
{
"html": {
"start": 0,
"end": 17,
"type": "Fragment",
"children": [
{
"start": 0,
"end": 17,
"type": "Element",
"name": "pre",
"attributes": [],
"children": [
{
"start": 6,
"end": 11,
"type": "Text",
"raw": "test\n",
"data": "test\n"
}
]
}
]
}
}

0 comments on commit 1dccec7

Please sign in to comment.