Skip to content

Commit b95ae0e

Browse files
xxkl1dummdidumm
authored andcommitted
fix: html space entities lost in component slot (#8464)
fixes #8359
1 parent 6e1674e commit b95ae0e

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

src/compiler/utils/patterns.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const regex_starts_with_whitespace = /^\s/;
44
export const regex_starts_with_whitespaces = /^[ \t\r\n]*/;
55
export const regex_ends_with_whitespace = /\s$/;
66
export const regex_ends_with_whitespaces = /[ \t\r\n]*$/;
7-
export const regex_only_whitespaces = /^\s+$/;
7+
export const regex_only_whitespaces = /^[ \t\n\r\f]+$/;
88

99
export const regex_whitespace_characters = /\s/g;
1010
export const regex_non_whitespace_character = /\S/;

test/helpers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ function cleanChildren(node) {
109109
node.removeChild(child);
110110
}
111111

112-
child.data = child.data.replace(/\s+/g, '\n');
112+
child.data = child.data.replace(/[ \t\n\r\f]+/g, '\n');
113113

114114
if (previous && previous.nodeType === 3) {
115115
previous.data += child.data;
116-
previous.data = previous.data.replace(/\s+/g, '\n');
116+
previous.data = previous.data.replace(/[ \t\n\r\f]+/g, '\n');
117117

118118
node.removeChild(child);
119119
child = previous;
@@ -130,13 +130,13 @@ function cleanChildren(node) {
130130

131131
// collapse whitespace
132132
if (node.firstChild && node.firstChild.nodeType === 3) {
133-
node.firstChild.data = node.firstChild.data.replace(/^\s+/, '');
134-
if (!node.firstChild.data) node.removeChild(node.firstChild);
133+
node.firstChild.data = node.firstChild.data.replace(/^[ \t\n\r\f]+/, '');
134+
if (!node.firstChild.data.length) node.removeChild(node.firstChild);
135135
}
136136

137137
if (node.lastChild && node.lastChild.nodeType === 3) {
138-
node.lastChild.data = node.lastChild.data.replace(/\s+$/, '');
139-
if (!node.lastChild.data) node.removeChild(node.lastChild);
138+
node.lastChild.data = node.lastChild.data.replace(/[ \t\n\r\f]+$/, '');
139+
if (!node.lastChild.data.length) node.removeChild(node.lastChild);
140140
}
141141
}
142142

@@ -145,7 +145,7 @@ export function normalizeHtml(window, html, preserveComments = false) {
145145
const node = window.document.createElement('div');
146146
node.innerHTML = html
147147
.replace(/(<!--.*?-->)/g, preserveComments ? '$1' : '')
148-
.replace(/>[\s\r\n]+</g, '><')
148+
.replace(/>[ \t\n\r\f]+</g, '><')
149149
.trim();
150150
cleanChildren(node);
151151
return node.innerHTML.replace(/<\/?noscript\/?>/g, '');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<slot />
3+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
html: `
3+
<div>&nbsp;</div>
4+
5+
<div>
6+
<span>&nbsp;</span>
7+
</div>
8+
9+
<div>&nbsp;</div>
10+
`
11+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import Component from './Component.svelte'
3+
</script>
4+
5+
<Component>&nbsp;</Component>
6+
7+
<Component>
8+
<span>&nbsp;</span>
9+
</Component>
10+
11+
<Component>
12+
{@html "&nbsp;"}
13+
</Component>

0 commit comments

Comments
 (0)