This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathhooks.js
1079 lines (864 loc) · 30.3 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import render, { RenderOptions } from "./render";
import MorphList from "../morph-range/morph-list";
import { createChildMorph } from "./render";
import { keyLength, shallowCopy } from "../htmlbars-util/object-utils";
import { validateChildMorphs } from "../htmlbars-util/morph-utils";
import { RenderState, clearMorph, clearMorphList, renderAndCleanup } from "../htmlbars-util/template-utils";
import { linkParams } from "../htmlbars-util/morph-utils";
/**
HTMLBars delegates the runtime behavior of a template to
hooks provided by the host environment. These hooks explain
the lexical environment of a Handlebars template, the internal
representation of references, and the interaction between an
HTMLBars template and the DOM it is managing.
While HTMLBars host hooks have access to all of this internal
machinery, templates and helpers have access to the abstraction
provided by the host hooks.
## The Lexical Environment
The default lexical environment of an HTMLBars template includes:
* Any local variables, provided by *block arguments*
* The current value of `self`
## Simple Nesting
Let's look at a simple template with a nested block:
```hbs
<h1>{{title}}</h1>
{{#if author}}
<p class="byline">{{author}}</p>
{{/if}}
```
In this case, the lexical environment at the top-level of the
template does not change inside of the `if` block. This is
achieved via an implementation of `if` that looks like this:
```js
registerHelper('if', function(params) {
if (!!params[0]) {
return this.yield();
}
});
```
A call to `this.yield` invokes the child template using the
current lexical environment.
## Block Arguments
It is possible for nested blocks to introduce new local
variables:
```hbs
{{#count-calls as |i|}}
<h1>{{title}}</h1>
<p>Called {{i}} times</p>
{{/count}}
```
In this example, the child block inherits its surrounding
lexical environment, but augments it with a single new
variable binding.
The implementation of `count-calls` supplies the value of
`i`, but does not otherwise alter the environment:
```js
var count = 0;
registerHelper('count-calls', function() {
return this.yield([ ++count ]);
});
```
*/
export function wrap(template) {
if (template === null) { return null; }
return {
meta: template.meta,
arity: template.arity,
raw: template,
render: function(self, env, options, blockArguments) {
var scope = env.hooks.createFreshScope();
let contextualElement = options && options.contextualElement;
let renderOptions = new RenderOptions(null, self, blockArguments, contextualElement);
return render(template, env, scope, renderOptions);
}
};
}
export function wrapForHelper(template, env, scope, morph, renderState, visitor) {
if (!template) { return {}; }
var yieldArgs = yieldTemplate(template, env, scope, morph, renderState, visitor);
return {
meta: template.meta,
arity: template.arity,
'yield': yieldArgs, // quoted since it's a reserved word, see issue #420
yieldItem: yieldItem(template, env, scope, morph, renderState, visitor),
raw: template,
render: function(self, blockArguments) {
yieldArgs(blockArguments, self);
}
};
}
// Called by a user-land helper to render a template.
function yieldTemplate(template, env, parentScope, morph, renderState, visitor) {
return function(blockArguments, self) {
// Render state is used to track the progress of the helper (since it
// may call into us multiple times). As the user-land helper calls
// into library code, we track what needs to be cleaned up after the
// helper has returned.
//
// Here, we remember that a template has been yielded and so we do not
// need to remove the previous template. (If no template is yielded
// this render by the helper, we assume nothing should be shown and
// remove any previous rendered templates.)
renderState.morphToClear = null;
// In this conditional is true, it means that on the previous rendering pass
// the helper yielded multiple items via `yieldItem()`, but this time they
// are yielding a single template. In that case, we mark the morph list for
// cleanup so it is removed from the DOM.
if (morph.morphList) {
clearMorphList(morph.morphList, morph, env);
renderState.morphListToClear = null;
}
var scope = parentScope;
if (morph.lastYielded && isStableTemplate(template, morph.lastYielded)) {
return morph.lastResult.revalidateWith(env, undefined, self, blockArguments, visitor);
}
// Check to make sure that we actually **need** a new scope, and can't
// share the parent scope. Note that we need to move this check into
// a host hook, because the host's notion of scope may require a new
// scope in more cases than the ones we can determine statically.
if (self !== undefined || parentScope === null || template.arity) {
scope = env.hooks.createChildScope(parentScope);
}
morph.lastYielded = { self: self, template: template, shadowTemplate: null };
// Render the template that was selected by the helper
let renderOptions = new RenderOptions(morph, self, blockArguments);
render(template, env, scope, renderOptions);
};
}
function yieldItem(template, env, parentScope, morph, renderState, visitor) {
// Initialize state that tracks multiple items being
// yielded in.
var currentMorph = null;
// Candidate morphs for deletion.
var candidates = {};
// Reuse existing MorphList if this is not a first-time
// render.
var morphList = morph.morphList;
if (morphList) {
currentMorph = morphList.firstChildMorph;
}
// Advances the currentMorph pointer to the morph in the previously-rendered
// list that matches the yielded key. While doing so, it marks any morphs
// that it advances past as candidates for deletion. Assuming those morphs
// are not yielded in later, they will be removed in the prune step during
// cleanup.
// Note that this helper function assumes that the morph being seeked to is
// guaranteed to exist in the previous MorphList; if this is called and the
// morph does not exist, it will result in an infinite loop
function advanceToKey(key) {
let seek = currentMorph;
while (seek.key !== key) {
candidates[seek.key] = seek;
seek = seek.nextMorph;
}
currentMorph = seek.nextMorph;
return seek;
}
return function(_key, blockArguments, self) {
if (typeof _key !== 'string') {
throw new Error("You must provide a string key when calling `yieldItem`; you provided " + _key);
}
// At least one item has been yielded, so we do not wholesale
// clear the last MorphList but instead apply a prune operation.
renderState.morphListToClear = null;
morph.lastYielded = null;
var morphList, morphMap;
if (!morph.morphList) {
morph.morphList = new MorphList();
morph.morphMap = {};
morph.setMorphList(morph.morphList);
}
morphList = morph.morphList;
morphMap = morph.morphMap;
// A map of morphs that have been yielded in on this
// rendering pass. Any morphs that do not make it into
// this list will be pruned from the MorphList during the cleanup
// process.
let handledMorphs = renderState.handledMorphs;
let key;
if (_key in handledMorphs) {
// In this branch we are dealing with a duplicate key. The strategy
// is to take the original key and append a counter to it that is
// incremented every time the key is reused. In order to greatly
// reduce the chance of colliding with another valid key we also add
// an extra string "--z8mS2hvDW0A--" to the new key.
let collisions = renderState.collisions;
if (collisions === undefined) {
collisions = renderState.collisions = {};
}
let count = collisions[_key] | 0;
collisions[_key] = ++count;
key = _key + '--z8mS2hvDW0A--' + count;
} else {
key = _key;
}
if (currentMorph && currentMorph.key === key) {
yieldTemplate(template, env, parentScope, currentMorph, renderState, visitor)(blockArguments, self);
currentMorph = currentMorph.nextMorph;
handledMorphs[key] = currentMorph;
} else if (morphMap[key] !== undefined) {
let foundMorph = morphMap[key];
if (key in candidates) {
// If we already saw this morph, move it forward to this position
morphList.insertBeforeMorph(foundMorph, currentMorph);
} else {
// Otherwise, move the pointer forward to the existing morph for this key
advanceToKey(key);
}
handledMorphs[foundMorph.key] = foundMorph;
yieldTemplate(template, env, parentScope, foundMorph, renderState, visitor)(blockArguments, self);
} else {
var childMorph = createChildMorph(env.dom, morph);
childMorph.key = key;
morphMap[key] = handledMorphs[key] = childMorph;
morphList.insertBeforeMorph(childMorph, currentMorph);
yieldTemplate(template, env, parentScope, childMorph, renderState, visitor)(blockArguments, self);
}
renderState.morphListToPrune = morphList;
morph.childNodes = null;
};
}
function isStableTemplate(template, lastYielded) {
return !lastYielded.shadowTemplate && template === lastYielded.template;
}
function optionsFor(template, inverse, env, scope, morph, visitor) {
// If there was a template yielded last time, set morphToClear so it will be cleared
// if no template is yielded on this render.
var morphToClear = morph.lastResult ? morph : null;
var renderState = new RenderState(morphToClear, morph.morphList || null);
return {
templates: {
template: wrapForHelper(template, env, scope, morph, renderState, visitor),
inverse: wrapForHelper(inverse, env, scope, morph, renderState, visitor)
},
renderState: renderState
};
}
function thisFor(options) {
return {
arity: options.template.arity,
'yield': options.template.yield, // quoted since it's a reserved word, see issue #420
yieldItem: options.template.yieldItem,
yieldIn: options.template.yieldIn
};
}
/**
Host Hook: createScope
@param {Scope?} parentScope
@return Scope
Corresponds to entering a new HTMLBars block.
This hook is invoked when a block is entered with
a new `self` or additional local variables.
When invoked for a top-level template, the
`parentScope` is `null`, and this hook should return
a fresh Scope.
When invoked for a child template, the `parentScope`
is the scope for the parent environment.
Note that the `Scope` is an opaque value that is
passed to other host hooks. For example, the `get`
hook uses the scope to retrieve a value for a given
scope and variable name.
*/
export function createScope(env, parentScope) {
if (parentScope) {
return env.hooks.createChildScope(parentScope);
} else {
return env.hooks.createFreshScope();
}
}
export function createFreshScope() {
// because `in` checks have unpredictable performance, keep a
// separate dictionary to track whether a local was bound.
// See `bindLocal` for more information.
return { self: null, blocks: {}, locals: {}, localPresent: {} };
}
/**
Host Hook: bindShadowScope
@param {Scope?} parentScope
@return Scope
Corresponds to rendering a new template into an existing
render tree, but with a new top-level lexical scope. This
template is called the "shadow root".
If a shadow template invokes `{{yield}}`, it will render
the block provided to the shadow root in the original
lexical scope.
```hbs
{{!-- post template --}}
<p>{{props.title}}</p>
{{yield}}
{{!-- blog template --}}
{{#post title="Hello world"}}
<p>by {{byline}}</p>
<article>This is my first post</article>
{{/post}}
{{#post title="Goodbye world"}}
<p>by {{byline}}</p>
<article>This is my last post</article>
{{/post}}
```
```js
helpers.post = function(params, hash, options) {
options.template.yieldIn(postTemplate, { props: hash });
};
blog.render({ byline: "Yehuda Katz" });
```
Produces:
```html
<p>Hello world</p>
<p>by Yehuda Katz</p>
<article>This is my first post</article>
<p>Goodbye world</p>
<p>by Yehuda Katz</p>
<article>This is my last post</article>
```
In short, `yieldIn` creates a new top-level scope for the
provided template and renders it, making the original block
available to `{{yield}}` in that template.
*/
export function bindShadowScope(env /*, parentScope, shadowScope */) {
return env.hooks.createFreshScope();
}
export function createChildScope(parent) {
var scope = Object.create(parent);
scope.locals = Object.create(parent.locals);
scope.localPresent = Object.create(parent.localPresent);
scope.blocks = Object.create(parent.blocks);
return scope;
}
/**
Host Hook: bindSelf
@param {Scope} scope
@param {any} self
Corresponds to entering a template.
This hook is invoked when the `self` value for a scope is ready to be bound.
The host must ensure that child scopes reflect the change to the `self` in
future calls to the `get` hook.
*/
export function bindSelf(env, scope, self) {
scope.self = self;
}
export function updateSelf(env, scope, self) {
env.hooks.bindSelf(env, scope, self);
}
/**
Host Hook: bindLocal
@param {Environment} env
@param {Scope} scope
@param {String} name
@param {any} value
Corresponds to entering a template with block arguments.
This hook is invoked when a local variable for a scope has been provided.
The host must ensure that child scopes reflect the change in future calls
to the `get` hook.
*/
export function bindLocal(env, scope, name, value) {
scope.localPresent[name] = true;
scope.locals[name] = value;
}
export function updateLocal(env, scope, name, value) {
env.hooks.bindLocal(env, scope, name, value);
}
/**
Host Hook: bindBlock
@param {Environment} env
@param {Scope} scope
@param {Function} block
Corresponds to entering a shadow template that was invoked by a block helper with
`yieldIn`.
This hook is invoked with an opaque block that will be passed along
to the shadow template, and inserted into the shadow template when
`{{yield}}` is used. Optionally provide a non-default block name
that can be targeted by `{{yield to=blockName}}`.
*/
export function bindBlock(env, scope, block, name='default') {
scope.blocks[name] = block;
}
/**
Host Hook: block
@param {RenderNode} renderNode
@param {Environment} env
@param {Scope} scope
@param {String} path
@param {Array} params
@param {Object} hash
@param {Block} block
@param {Block} elseBlock
Corresponds to:
```hbs
{{#helper param1 param2 key1=val1 key2=val2}}
{{!-- child template --}}
{{/helper}}
```
This host hook is a workhorse of the system. It is invoked
whenever a block is encountered, and is responsible for
resolving the helper to call, and then invoke it.
The helper should be invoked with:
- `{Array} params`: the parameters passed to the helper
in the template.
- `{Object} hash`: an object containing the keys and values passed
in the hash position in the template.
The values in `params` and `hash` will already be resolved
through a previous call to the `get` host hook.
The helper should be invoked with a `this` value that is
an object with one field:
`{Function} yield`: when invoked, this function executes the
block with the current scope. It takes an optional array of
block parameters. If block parameters are supplied, HTMLBars
will invoke the `bindLocal` host hook to bind the supplied
values to the block arguments provided by the template.
In general, the default implementation of `block` should work
for most host environments. It delegates to other host hooks
where appropriate, and properly invokes the helper with the
appropriate arguments.
*/
export function block(morph, env, scope, path, params, hash, template, inverse, visitor) {
if (handleRedirect(morph, env, scope, path, params, hash, template, inverse, visitor)) {
return;
}
continueBlock(morph, env, scope, path, params, hash, template, inverse, visitor);
}
export function continueBlock(morph, env, scope, path, params, hash, template, inverse, visitor) {
hostBlock(morph, env, scope, template, inverse, null, visitor, function(options) {
var helper = env.hooks.lookupHelper(env, scope, path);
return env.hooks.invokeHelper(morph, env, scope, visitor, params, hash, helper, options.templates, thisFor(options.templates));
});
}
export function hostBlock(morph, env, scope, template, inverse, shadowOptions, visitor, callback) {
var options = optionsFor(template, inverse, env, scope, morph, visitor);
renderAndCleanup(morph, env, options, shadowOptions, callback);
}
export function handleRedirect(morph, env, scope, path, params, hash, template, inverse, visitor) {
if (!path) {
return false;
}
var redirect = env.hooks.classify(env, scope, path);
if (redirect) {
switch(redirect) {
case 'component': env.hooks.component(morph, env, scope, path, params, hash, {default: template, inverse}, visitor); break;
case 'inline': env.hooks.inline(morph, env, scope, path, params, hash, visitor); break;
case 'block': env.hooks.block(morph, env, scope, path, params, hash, template, inverse, visitor); break;
default: throw new Error("Internal HTMLBars redirection to " + redirect + " not supported");
}
return true;
}
if (handleKeyword(path, morph, env, scope, params, hash, template, inverse, visitor)) {
return true;
}
return false;
}
export function handleKeyword(path, morph, env, scope, params, hash, template, inverse, visitor) {
var keyword = env.hooks.keywords[path];
if (!keyword) { return false; }
if (typeof keyword === 'function') {
return keyword(morph, env, scope, params, hash, template, inverse, visitor);
}
if (keyword.willRender) {
keyword.willRender(morph, env);
}
var lastState, newState;
if (keyword.setupState) {
lastState = shallowCopy(morph.getState());
newState = morph.setState(keyword.setupState(lastState, env, scope, params, hash));
}
if (keyword.childEnv) {
// Build the child environment...
env = keyword.childEnv(morph.getState(), env);
// ..then save off the child env builder on the render node. If the render
// node tree is re-rendered and this node is not dirty, the child env
// builder will still be invoked so that child dirty render nodes still get
// the correct child env.
morph.buildChildEnv = keyword.childEnv;
}
var firstTime = !morph.rendered;
if (keyword.isEmpty) {
var isEmpty = keyword.isEmpty(morph.getState(), env, scope, params, hash);
if (isEmpty) {
if (!firstTime) { clearMorph(morph, env, false); }
return true;
}
}
if (firstTime) {
if (keyword.render) {
keyword.render(morph, env, scope, params, hash, template, inverse, visitor);
}
morph.rendered = true;
return true;
}
var isStable;
if (keyword.isStable) {
isStable = keyword.isStable(lastState, newState);
} else {
isStable = stableState(lastState, newState);
}
if (isStable) {
if (keyword.rerender) {
var newEnv = keyword.rerender(morph, env, scope, params, hash, template, inverse, visitor);
env = newEnv || env;
}
validateChildMorphs(env, morph, visitor);
return true;
} else {
clearMorph(morph, env, false);
}
// If the node is unstable, re-render from scratch
if (keyword.render) {
keyword.render(morph, env, scope, params, hash, template, inverse, visitor);
morph.rendered = true;
return true;
}
}
function stableState(oldState, newState) {
if (keyLength(oldState) !== keyLength(newState)) { return false; }
for (var prop in oldState) {
if (oldState[prop] !== newState[prop]) { return false; }
}
return true;
}
export function linkRenderNode(/* morph, env, scope, params, hash */) {
return;
}
/**
Host Hook: inline
@param {RenderNode} renderNode
@param {Environment} env
@param {Scope} scope
@param {String} path
@param {Array} params
@param {Hash} hash
Corresponds to:
```hbs
{{helper param1 param2 key1=val1 key2=val2}}
```
This host hook is similar to the `block` host hook, but it
invokes helpers that do not supply an attached block.
Like the `block` hook, the helper should be invoked with:
- `{Array} params`: the parameters passed to the helper
in the template.
- `{Object} hash`: an object containing the keys and values passed
in the hash position in the template.
The values in `params` and `hash` will already be resolved
through a previous call to the `get` host hook.
In general, the default implementation of `inline` should work
for most host environments. It delegates to other host hooks
where appropriate, and properly invokes the helper with the
appropriate arguments.
The default implementation of `inline` also makes `partial`
a keyword. Instead of invoking a helper named `partial`,
it invokes the `partial` host hook.
*/
export function inline(morph, env, scope, path, params, hash, visitor) {
if (handleRedirect(morph, env, scope, path, params, hash, null, null, visitor)) {
return;
}
let value, hasValue;
if (morph.linkedResult) {
value = env.hooks.getValue(morph.linkedResult);
hasValue = true;
} else {
var options = optionsFor(null, null, env, scope, morph);
var helper = env.hooks.lookupHelper(env, scope, path);
var result = env.hooks.invokeHelper(morph, env, scope, visitor, params, hash, helper, options.templates, thisFor(options.templates));
if (result && result.link) {
morph.linkedResult = result.value;
linkParams(env, scope, morph, '@content-helper', [morph.linkedResult], null);
}
if (result && 'value' in result) {
value = env.hooks.getValue(result.value);
hasValue = true;
}
}
if (hasValue) {
if (morph.lastValue !== value) {
morph.setContent(value);
}
morph.lastValue = value;
}
}
export function keyword(path, morph, env, scope, params, hash, template, inverse, visitor) {
handleKeyword(path, morph, env, scope, params, hash, template, inverse, visitor);
}
export function invokeHelper(morph, env, scope, visitor, _params, _hash, helper, templates, context) {
var params = normalizeArray(env, _params);
var hash = normalizeObject(env, _hash);
return { value: helper.call(context, params, hash, templates) };
}
function normalizeArray(env, array) {
var out = new Array(array.length);
for (var i=0, l=array.length; i<l; i++) {
out[i] = env.hooks.getCellOrValue(array[i]);
}
return out;
}
function normalizeObject(env, object) {
var out = {};
for (var prop in object) {
out[prop] = env.hooks.getCellOrValue(object[prop]);
}
return out;
}
export function classify(/* env, scope, path */) {
return null;
}
export var keywords = {
partial: function(morph, env, scope, params) {
var value = env.hooks.partial(morph, env, scope, params[0]);
morph.setContent(value);
return true;
},
// quoted since it's a reserved word, see issue #420
'yield': function(morph, env, scope, params, hash, template, inverse, visitor) {
// the current scope is provided purely for the creation of shadow
// scopes; it should not be provided to user code.
var to = env.hooks.getValue(hash.to) || 'default';
var block = env.hooks.getBlock(scope, to);
if (block) {
block.invoke(env, params, hash.self, morph, scope, visitor);
}
return true;
},
hasBlock: function(morph, env, scope, params) {
var name = env.hooks.getValue(params[0]) || 'default';
return !!env.hooks.getBlock(scope, name);
},
hasBlockParams: function(morph, env, scope, params) {
var name = env.hooks.getValue(params[0]) || 'default';
var block = env.hooks.getBlock(scope, name);
return !!(block && block.arity);
}
};
/**
Host Hook: partial
@param {RenderNode} renderNode
@param {Environment} env
@param {Scope} scope
@param {String} path
Corresponds to:
```hbs
{{partial "location"}}
```
This host hook is invoked by the default implementation of
the `inline` hook. This makes `partial` a keyword in an
HTMLBars environment using the default `inline` host hook.
It is implemented as a host hook so that it can retrieve
the named partial out of the `Environment`. Helpers, in
contrast, only have access to the values passed in to them,
and not to the ambient lexical environment.
The host hook should invoke the referenced partial with
the ambient `self`.
*/
export function partial(renderNode, env, scope, path) {
var template = env.partials[path];
return template.render(scope.self, env, {}).fragment;
}
/**
Host hook: range
@param {RenderNode} renderNode
@param {Environment} env
@param {Scope} scope
@param {any} value
Corresponds to:
```hbs
{{content}}
{{{unescaped}}}
```
This hook is responsible for updating a render node
that represents a range of content with a value.
*/
export function range(morph, env, scope, path, value, visitor) {
if (handleRedirect(morph, env, scope, path, [], {}, null, null, visitor)) {
return;
}
value = env.hooks.getValue(value);
if (morph.lastValue !== value) {
morph.setContent(value);
}
morph.lastValue = value;
}
/**
Host hook: element
@param {RenderNode} renderNode
@param {Environment} env
@param {Scope} scope
@param {String} path
@param {Array} params
@param {Hash} hash
Corresponds to:
```hbs
<div {{bind-attr foo=bar}}></div>
```
This hook is responsible for invoking a helper that
modifies an element.
Its purpose is largely legacy support for awkward
idioms that became common when using the string-based
Handlebars engine.
Most of the uses of the `element` hook are expected
to be superseded by component syntax and the
`attribute` hook.
*/
export function element(morph, env, scope, path, params, hash, visitor) {
if (handleRedirect(morph, env, scope, path, params, hash, null, null, visitor)) {
return;
}
var helper = env.hooks.lookupHelper(env, scope, path);
if (helper) {
env.hooks.invokeHelper(null, env, scope, null, params, hash, helper, { element: morph.element });
}
}
/**
Host hook: attribute
@param {RenderNode} renderNode
@param {Environment} env
@param {String} name
@param {any} value
Corresponds to:
```hbs
<div foo={{bar}}></div>
```
This hook is responsible for updating a render node
that represents an element's attribute with a value.
It receives the name of the attribute as well as an
already-resolved value, and should update the render
node with the value if appropriate.
*/
export function attribute(morph, env, scope, name, value) {
value = env.hooks.getValue(value);
if (morph.lastValue !== value) {
morph.setContent(value);
}
morph.lastValue = value;
}
export function subexpr(env, scope, helperName, params, hash) {
var helper = env.hooks.lookupHelper(env, scope, helperName);
var result = env.hooks.invokeHelper(null, env, scope, null, params, hash, helper, {});
if (result && 'value' in result) { return env.hooks.getValue(result.value); }
}
/**
Host Hook: get
@param {Environment} env
@param {Scope} scope
@param {String} path
Corresponds to:
```hbs
{{foo.bar}}
^
{{helper foo.bar key=value}}
^ ^
```
This hook is the "leaf" hook of the system. It is used to
resolve a path relative to the current scope.
*/
export function get(env, scope, path) {
if (path === '') {
return scope.self;
}
var keys = path.split('.');
var value = env.hooks.getRoot(scope, keys[0])[0];
for (var i = 1; i < keys.length; i++) {
if (value) {
value = env.hooks.getChild(value, keys[i]);
} else {
break;
}
}
return value;
}
export function getRoot(scope, key) {
if (scope.localPresent[key]) {
return [scope.locals[key]];
} else if (scope.self) {
return [scope.self[key]];
} else {
return [undefined];
}
}
export function getBlock(scope, key) {
return scope.blocks[key];
}
export function getChild(value, key) {
return value[key];
}
export function getValue(reference) {
return reference;
}
export function getCellOrValue(reference) {
return reference;
}
export function component(morph, env, scope, tagName, params, attrs, templates, visitor) {
if (env.hooks.hasHelper(env, scope, tagName)) {
return env.hooks.block(morph, env, scope, tagName, params, attrs, templates.default, templates.inverse, visitor);
}
componentFallback(morph, env, scope, tagName, attrs, templates.default);
}
export function concat(env, params) {
var value = "";