Skip to content

Commit db7fe86

Browse files
committed
failing test for #381
1 parent a9f18b5 commit db7fe86

File tree

3 files changed

+165
-3
lines changed

3 files changed

+165
-3
lines changed

test/js/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ describe( 'js', () => {
1818
dir = path.resolve( 'test/js/samples', dir );
1919
const input = fs.readFileSync( `${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' );
2020

21-
const actual = svelte.compile( input, {
22-
shared: true
23-
}).code;
21+
let actual;
22+
23+
try {
24+
actual = svelte.compile( input, {
25+
shared: true
26+
}).code;
27+
} catch ( err ) {
28+
console.log( err.frame );
29+
throw err;
30+
}
2431

2532
fs.writeFileSync( `${dir}/_actual.js`, actual );
2633
const expected = fs.readFileSync( `${dir}/expected.js`, 'utf-8' );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { appendNode, assign, createComment, createElement, createText, destroyEach, detachBetween, detachNode, dispatchObservers, insertNode, proto } from "svelte/shared.js";
2+
3+
function create_main_fragment ( root, component ) {
4+
var each_block_anchor = createComment();
5+
var each_block_value = root.comments;
6+
var each_block_iterations = [];
7+
8+
for ( var i = 0; i < each_block_value.length; i += 1 ) {
9+
each_block_iterations[i] = create_each_block( root, each_block_value, each_block_value[i], i, component );
10+
}
11+
12+
return {
13+
mount: function ( target, anchor ) {
14+
insertNode( each_block_anchor, target, anchor );
15+
16+
for ( var i = 0; i < each_block_iterations.length; i += 1 ) {
17+
each_block_iterations[i].mount( each_block_anchor.parentNode, each_block_anchor );
18+
}
19+
},
20+
21+
update: function ( changed, root ) {
22+
if ( 'comments' in changed || 'time' in changed ) {
23+
var each_block_value = root.comments;
24+
25+
for ( var i = 0; i < each_block_value.length; i += 1 ) {
26+
if ( !each_block_iterations[i] ) {
27+
each_block_iterations[i] = create_each_block( root, each_block_value, each_block_value[i], i, component );
28+
each_block_iterations[i].mount( each_block_anchor.parentNode, each_block_anchor );
29+
} else {
30+
each_block_iterations[i].update( changed, root, each_block_value, each_block_value[i], i );
31+
}
32+
}
33+
34+
destroyEach( each_block_iterations, true, each_block_value.length );
35+
36+
each_block_iterations.length = each_block_value.length;
37+
}
38+
},
39+
40+
destroy: function ( detach ) {
41+
destroyEach( each_block_iterations, detach, 0 );
42+
43+
if ( detach ) {
44+
detachNode( each_block_anchor );
45+
}
46+
}
47+
};
48+
}
49+
50+
function create_each_block ( root, each_block_value, comment, comment_index, component ) {
51+
var div = createElement( 'div' );
52+
div.className = "comment";
53+
var span = createElement( 'span' );
54+
appendNode( span, div );
55+
span.className = "meta";
56+
var last_text = comment.author;
57+
var text = createText( last_text );
58+
appendNode( text, span );
59+
appendNode( createText( " wrote " ), span );
60+
var last_text_2 = root.elapsed(comment.time, root.time);
61+
var text_2 = createText( last_text_2 );
62+
appendNode( text_2, span );
63+
appendNode( createText( " ago:" ), span );
64+
appendNode( createText( "\n\n\t\t" ), div );
65+
var raw_before = createElement( 'noscript' );
66+
appendNode( raw_before, div );
67+
var raw_after = createElement( 'noscript' );
68+
appendNode( raw_after, div );
69+
var last_raw = comment.html;
70+
raw_before.insertAdjacentHTML( 'afterend', last_raw );
71+
72+
return {
73+
mount: function ( target, anchor ) {
74+
insertNode( div, target, anchor );
75+
},
76+
77+
update: function ( changed, root, each_block_value, comment, comment_index ) {
78+
var tmp;
79+
80+
if ( ( tmp = comment.author ) !== last_text ) {
81+
text.data = last_text = tmp;
82+
}
83+
84+
if ( ( tmp = root.elapsed(comment.time, root.time) ) !== last_text_2 ) {
85+
text_2.data = last_text_2 = tmp;
86+
}
87+
88+
if ( ( tmp = comment.html ) !== last_raw ) {
89+
last_raw = tmp;
90+
detachBetween( raw_before, raw_after );
91+
raw_before.insertAdjacentHTML( 'afterend', last_raw );
92+
}
93+
},
94+
95+
destroy: function ( detach ) {
96+
if ( detach ) {
97+
detachBetween( raw_before, raw_after );
98+
99+
detachNode( div );
100+
}
101+
}
102+
};
103+
}
104+
105+
function SvelteComponent ( options ) {
106+
options = options || {};
107+
this._state = options.data || {};
108+
109+
this._observers = {
110+
pre: Object.create( null ),
111+
post: Object.create( null )
112+
};
113+
114+
this._handlers = Object.create( null );
115+
116+
this._root = options._root;
117+
this._yield = options._yield;
118+
119+
this._torndown = false;
120+
121+
this._fragment = create_main_fragment( this._state, this );
122+
if ( options.target ) this._fragment.mount( options.target, null );
123+
}
124+
125+
assign( SvelteComponent.prototype, proto );
126+
127+
SvelteComponent.prototype._set = function _set ( newState ) {
128+
var oldState = this._state;
129+
this._state = assign( {}, oldState, newState );
130+
131+
dispatchObservers( this, this._observers.pre, newState, oldState );
132+
if ( this._fragment ) this._fragment.update( newState, this._state );
133+
dispatchObservers( this, this._observers.post, newState, oldState );
134+
};
135+
136+
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
137+
this.fire( 'destroy' );
138+
139+
this._fragment.destroy( detach !== false );
140+
this._fragment = null;
141+
142+
this._state = {};
143+
this._torndown = true;
144+
};
145+
146+
export default SvelteComponent;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{#each comments as comment}}
2+
<div class='comment'>
3+
<span class='meta'>
4+
{{comment.author}} wrote {{elapsed(comment.time, time)}} ago:
5+
</span>
6+
7+
{{{comment.html}}}
8+
</div>
9+
{{/each}}

0 commit comments

Comments
 (0)