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]: strip leading newline characters after 'pre' elements #7269

Closed
wants to merge 1 commit into from
Closed
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
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')) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure if carriage return is already stripped from the source before feed into the parser?

Couldn't find any references to CRLF or '\r' in the parser, but I don't know.

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"
}
]
}
]
}
}