From f18ad5e6f6e6e0681c29c968fa0ca5372625558f Mon Sep 17 00:00:00 2001 From: qintarp Date: Fri, 14 Jun 2019 14:47:11 +0300 Subject: [PATCH 1/7] Fix div nbsp --- src/compiler/compile/render-dom/wrappers/Text.ts | 2 +- src/runtime/internal/dom.ts | 4 ++++ test/runtime/samples/nbsp-div/_config.js | 8 ++++++++ test/runtime/samples/nbsp-div/main.svelte | 6 ++++++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/nbsp-div/_config.js create mode 100644 test/runtime/samples/nbsp-div/main.svelte diff --git a/src/compiler/compile/render-dom/wrappers/Text.ts b/src/compiler/compile/render-dom/wrappers/Text.ts index ceacae29baf6..1781eb2e5863 100644 --- a/src/compiler/compile/render-dom/wrappers/Text.ts +++ b/src/compiler/compile/render-dom/wrappers/Text.ts @@ -54,7 +54,7 @@ export default class TextWrapper extends Wrapper { block.add_element( this.var, - this.node.use_space ? `@space()` : `@text(${stringify(this.data)})`, + this.node.use_space ? (this.node.data && this.node.data.charCodeAt(0) === 160) ? `@nbsp()` : `@space()` : `@text(${stringify(this.data)})`, parent_nodes && `@claim_text(${parent_nodes}, ${stringify(this.data)})`, parent_node ); diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index f65d07117ce9..30962e74c730 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -66,6 +66,10 @@ export function space() { return text(' '); } +export function nbsp() { + return text(String.fromCharCode(160)); +} + export function empty() { return text(''); } diff --git a/test/runtime/samples/nbsp-div/_config.js b/test/runtime/samples/nbsp-div/_config.js new file mode 100644 index 000000000000..e292da9fbb2c --- /dev/null +++ b/test/runtime/samples/nbsp-div/_config.js @@ -0,0 +1,8 @@ +export default { + html: `
hello 
`, + + test({ assert, component, target }) { + const text = target.querySelector( 'div' ).textContent; + assert.equal( text.charCodeAt( 5 ), 160 ); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/nbsp-div/main.svelte b/test/runtime/samples/nbsp-div/main.svelte new file mode 100644 index 000000000000..dac4b60514e8 --- /dev/null +++ b/test/runtime/samples/nbsp-div/main.svelte @@ -0,0 +1,6 @@ + + + +
{name} 
From 4f61dfc338aab41ed669482b51591dc40ea73113 Mon Sep 17 00:00:00 2001 From: qintarp Date: Fri, 14 Jun 2019 16:08:48 +0300 Subject: [PATCH 2/7] Is this test correct? --- test/runtime/samples/nbsp-div/_config.js | 14 +++++++++++--- test/runtime/samples/nbsp-div/main.svelte | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/runtime/samples/nbsp-div/_config.js b/test/runtime/samples/nbsp-div/_config.js index e292da9fbb2c..3114328b9dc2 100644 --- a/test/runtime/samples/nbsp-div/_config.js +++ b/test/runtime/samples/nbsp-div/_config.js @@ -1,8 +1,16 @@ export default { - html: `
hello 
`, + html: `
 hello
+
 hello 
+
 hello hello
`, test({ assert, component, target }) { - const text = target.querySelector( 'div' ).textContent; - assert.equal( text.charCodeAt( 5 ), 160 ); + var divList = target.querySelectorAll('div') + assert.equal( divList[0].textContent.charCodeAt( 0 ), 160 ); + assert.equal( divList[1].textContent.charCodeAt( 0 ), 160 ); + assert.equal( divList[1].textContent.charCodeAt( 6 ), 160 ); + assert.equal( divList[2].textContent.charCodeAt( 0 ), 160 ); + assert.equal( divList[2].textContent.charCodeAt( 6 ), 160 ); + + } }; \ No newline at end of file diff --git a/test/runtime/samples/nbsp-div/main.svelte b/test/runtime/samples/nbsp-div/main.svelte index dac4b60514e8..aede3b7ae12f 100644 --- a/test/runtime/samples/nbsp-div/main.svelte +++ b/test/runtime/samples/nbsp-div/main.svelte @@ -2,5 +2,6 @@ let name = 'hello'; - -
{name} 
+
 {name}
+
 {name}  
+
 {name}  {name}
\ No newline at end of file From 0a119d3e271fd17f08e8053e8608c31e8baf88f2 Mon Sep 17 00:00:00 2001 From: qintarp Date: Sat, 15 Jun 2019 12:27:19 +0300 Subject: [PATCH 3/7] Why filter space? --- src/compiler/compile/nodes/Text.ts | 5 ++--- src/compiler/compile/render-dom/wrappers/Text.ts | 2 +- src/runtime/internal/dom.ts | 7 ------- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/compiler/compile/nodes/Text.ts b/src/compiler/compile/nodes/Text.ts index 7500f5ff30d9..bb2082e0730c 100644 --- a/src/compiler/compile/nodes/Text.ts +++ b/src/compiler/compile/nodes/Text.ts @@ -12,7 +12,7 @@ export default class Text extends Node { super(component, parent, scope, info); this.data = info.data; - if (!component.component_options.preserveWhitespace && !/\S/.test(info.data)) { + let node = parent; while (node) { if (node.type === 'Element' && node.name === 'pre') { @@ -21,7 +21,6 @@ export default class Text extends Node { node = node.parent; } - this.use_space = true; - } + } } diff --git a/src/compiler/compile/render-dom/wrappers/Text.ts b/src/compiler/compile/render-dom/wrappers/Text.ts index 1781eb2e5863..c83fd38f2c44 100644 --- a/src/compiler/compile/render-dom/wrappers/Text.ts +++ b/src/compiler/compile/render-dom/wrappers/Text.ts @@ -54,7 +54,7 @@ export default class TextWrapper extends Wrapper { block.add_element( this.var, - this.node.use_space ? (this.node.data && this.node.data.charCodeAt(0) === 160) ? `@nbsp()` : `@space()` : `@text(${stringify(this.data)})`, + `@text(${stringify(this.data)})`, parent_nodes && `@claim_text(${parent_nodes}, ${stringify(this.data)})`, parent_node ); diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index 30962e74c730..05e18e2bcda0 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -62,13 +62,6 @@ export function text(data: string) { return document.createTextNode(data); } -export function space() { - return text(' '); -} - -export function nbsp() { - return text(String.fromCharCode(160)); -} export function empty() { return text(''); From 4b9aaabec101831f5a4af457cf2b348d8059f0cb Mon Sep 17 00:00:00 2001 From: qintarp Date: Sat, 15 Jun 2019 12:31:06 +0300 Subject: [PATCH 4/7] If or not if? --- src/compiler/compile/nodes/Text.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/compiler/compile/nodes/Text.ts b/src/compiler/compile/nodes/Text.ts index bb2082e0730c..a3ba4b9e85c8 100644 --- a/src/compiler/compile/nodes/Text.ts +++ b/src/compiler/compile/nodes/Text.ts @@ -12,7 +12,7 @@ export default class Text extends Node { super(component, parent, scope, info); this.data = info.data; - + if (!component.component_options.preserveWhitespace) { let node = parent; while (node) { if (node.type === 'Element' && node.name === 'pre') { @@ -21,6 +21,7 @@ export default class Text extends Node { node = node.parent; } - + + } } } From 8b66fa23c8bc1bd58fb4b7c696d39b8f7e2c39b9 Mon Sep 17 00:00:00 2001 From: qintarp Date: Sat, 15 Jun 2019 12:40:42 +0300 Subject: [PATCH 5/7] adjacent nbsps are now present --- test/runtime/samples/nbsp-div/_config.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/runtime/samples/nbsp-div/_config.js b/test/runtime/samples/nbsp-div/_config.js index 3114328b9dc2..32e4d237036f 100644 --- a/test/runtime/samples/nbsp-div/_config.js +++ b/test/runtime/samples/nbsp-div/_config.js @@ -1,15 +1,17 @@ export default { html: `
 hello
-
 hello 
-
 hello hello
`, +
 hello  
+
 hello  hello
`, test({ assert, component, target }) { var divList = target.querySelectorAll('div') assert.equal( divList[0].textContent.charCodeAt( 0 ), 160 ); assert.equal( divList[1].textContent.charCodeAt( 0 ), 160 ); assert.equal( divList[1].textContent.charCodeAt( 6 ), 160 ); + assert.equal( divList[1].textContent.charCodeAt( 7 ), 160 ); assert.equal( divList[2].textContent.charCodeAt( 0 ), 160 ); assert.equal( divList[2].textContent.charCodeAt( 6 ), 160 ); + assert.equal( divList[2].textContent.charCodeAt( 7 ), 160 ); } From dea4149c0e062c1d02208706bf13917996021454 Mon Sep 17 00:00:00 2001 From: qintarp Date: Sat, 15 Jun 2019 12:49:05 +0300 Subject: [PATCH 6/7] small fix to main test --- test/runtime/samples/nbsp-div/_config.js | 5 +++-- test/runtime/samples/nbsp-div/main.svelte | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/runtime/samples/nbsp-div/_config.js b/test/runtime/samples/nbsp-div/_config.js index 32e4d237036f..6026af2c9090 100644 --- a/test/runtime/samples/nbsp-div/_config.js +++ b/test/runtime/samples/nbsp-div/_config.js @@ -1,7 +1,7 @@ export default { html: `
 hello
 hello  
-
 hello  hello
`, +
 hello   hello
`, test({ assert, component, target }) { var divList = target.querySelectorAll('div') @@ -11,7 +11,8 @@ export default { assert.equal( divList[1].textContent.charCodeAt( 7 ), 160 ); assert.equal( divList[2].textContent.charCodeAt( 0 ), 160 ); assert.equal( divList[2].textContent.charCodeAt( 6 ), 160 ); - assert.equal( divList[2].textContent.charCodeAt( 7 ), 160 ); + assert.equal( divList[2].textContent.charCodeAt( 7 ), 32 );//normal space + assert.equal( divList[2].textContent.charCodeAt( 8 ), 160 ); } diff --git a/test/runtime/samples/nbsp-div/main.svelte b/test/runtime/samples/nbsp-div/main.svelte index aede3b7ae12f..64557bfeb142 100644 --- a/test/runtime/samples/nbsp-div/main.svelte +++ b/test/runtime/samples/nbsp-div/main.svelte @@ -4,4 +4,4 @@
 {name}
 {name}  
-
 {name}  {name}
\ No newline at end of file +
 {name}   {name}
\ No newline at end of file From e61ecbeb0a692e9bfe97e7a78bdc2f96c83fff82 Mon Sep 17 00:00:00 2001 From: qintarp Date: Sat, 15 Jun 2019 14:45:18 +0300 Subject: [PATCH 7/7] Changed to Conduitry's suggestion --- src/compiler/compile/nodes/Text.ts | 4 ++-- src/compiler/compile/render-dom/wrappers/Text.ts | 2 +- src/runtime/internal/dom.ts | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/compiler/compile/nodes/Text.ts b/src/compiler/compile/nodes/Text.ts index a3ba4b9e85c8..a4514f56f26e 100644 --- a/src/compiler/compile/nodes/Text.ts +++ b/src/compiler/compile/nodes/Text.ts @@ -12,7 +12,7 @@ export default class Text extends Node { super(component, parent, scope, info); this.data = info.data; - if (!component.component_options.preserveWhitespace) { + if (!component.component_options.preserveWhitespace && !/[\S\u00A0]/.test(info.data)) { let node = parent; while (node) { if (node.type === 'Element' && node.name === 'pre') { @@ -21,7 +21,7 @@ export default class Text extends Node { node = node.parent; } - + this.use_space = true; } } } diff --git a/src/compiler/compile/render-dom/wrappers/Text.ts b/src/compiler/compile/render-dom/wrappers/Text.ts index c83fd38f2c44..ceacae29baf6 100644 --- a/src/compiler/compile/render-dom/wrappers/Text.ts +++ b/src/compiler/compile/render-dom/wrappers/Text.ts @@ -54,7 +54,7 @@ export default class TextWrapper extends Wrapper { block.add_element( this.var, - `@text(${stringify(this.data)})`, + this.node.use_space ? `@space()` : `@text(${stringify(this.data)})`, parent_nodes && `@claim_text(${parent_nodes}, ${stringify(this.data)})`, parent_node ); diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index 05e18e2bcda0..f65d07117ce9 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -62,6 +62,9 @@ export function text(data: string) { return document.createTextNode(data); } +export function space() { + return text(' '); +} export function empty() { return text('');