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

bug(parser): fix parsing of multiline class attribute value (#4242) #4243

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/compiler/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,13 @@ function read_attribute_value(parser: Parser) {
/(\/>|[\s"'=<>`])/
);

const value = read_sequence(parser, () => !!parser.match_regex(regex));
const value = read_sequence(parser, () => !!parser.match_regex(regex), true);

if (quote_mark) parser.index += 1;
return value;
}

function read_sequence(parser: Parser, done: () => boolean): TemplateNode[] {
function read_sequence(parser: Parser, done: () => boolean, is_attribute_value: boolean = false): TemplateNode[] {
let current_chunk: Text = {
start: parser.index,
end: null,
Expand All @@ -454,9 +454,13 @@ function read_sequence(parser: Parser, done: () => boolean): TemplateNode[] {
data: null
};

let is_text_attribute_value = false;

function flush() {
if (current_chunk.raw) {
current_chunk.data = decode_character_references(current_chunk.raw);
current_chunk.data = (is_text_attribute_value) ?
decode_character_references(current_chunk.raw.trim()) :
decode_character_references(current_chunk.raw);
current_chunk.end = parser.index;
chunks.push(current_chunk);
}
Expand Down Expand Up @@ -493,7 +497,20 @@ function read_sequence(parser: Parser, done: () => boolean): TemplateNode[] {
data: null
};
} else {
current_chunk.raw += parser.template[parser.index++];
let char: string = parser.template[parser.index++];

// remove newlines and tabs
if (is_attribute_value && ['\n', '\r', '\t'].includes(char)) {
is_text_attribute_value = true;
const previous_char_position = current_chunk.raw.length - 1;
const previous_char = current_chunk.raw[previous_char_position];

// prevent two spaces after each other
if (previous_char && previous_char !== ' ') char = ' ';
else char = '';
}

current_chunk.raw += char;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.foo.svelte-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p class="foo bar svelte-xyz">this is styled</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p
class='
foo
bar
'
>
this is styled
</p>

<style>
.foo {
color: red;
}
</style>