-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathscript.js
More file actions
1137 lines (849 loc) · 36.9 KB
/
script.js
File metadata and controls
1137 lines (849 loc) · 36.9 KB
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
/*-
Custom JavaScript
=================
The `up.script` package lets you pair HTML elements with JavaScript behavior.
Unpoly encourages you to migrate all your custom JavaScript from `DOMContentLoaded`
callbacks to [compilers](/enhancing-elements). This will ensure they run both at page load and
when a new fragment is inserted later. See [Migrating legacy JavaScript](/legacy-scripts)
for details.
@see enhancing-elements
@see data
@see legacy-scripts
@see handling-asset-changes
@see script-security
@see up.compiler
@see [up-data]
@see up.macro
@see up.hello
@module up.script
*/
up.script = (function() {
const u = up.util
const e = up.element
/*-
Configures defaults for script handling and asset tracking.
@section Assets
@param {Array<string} [config.assetSelectors]
An array of CSS selectors matching default [assets](/up-asset).
By default, all remote scripts and stylesheets in the `<head>` are considered assets.
[Inline scripts](https://simpledev.io/lesson/inline-script-javascript-1/) and
[internal styles](https://www.tutorialspoint.com/How-to-use-internal-CSS-Style-Sheet-in-HTML)
are not tracked by default, but you can include them with an `[up-asset]` attribute.
Unpoly only tracks assets in the `<head>`. Elements in the `<body>` are never tracked,
even if they match one of the configured selectors.
See [Tracking assets](/handling-asset-changes#tracking-assets) for examples.
@param {Array<string} [config.noAssetSelectors]
Exceptions to `up.script.config.assetSelectors`.
Matching elements will *not* be considered [assets](/up-asset),
even if they match `up.script.config.assetSelectors`.
@section Scripts
@param {Array<string} [config.scriptSelectors]
An array of CSS selectors matching elements that run JavaScript.
By default, this matches all `<script>` elements with a JavaScript type.
This configuration does not affect what Unpoly considers [assets](/up-asset).
For this, configure `up.script.config.assetSelectors`.
@param {Array<string} [config.noScriptSelectors]
Exceptions to `up.script.config.scriptSelectors`.
@param {string} [config.evalCallbackPolicy='auto']
Whether Unpoly will [run callbacks in HTML attributes](/script-security#callbacks) like (`[up-on-loaded]`)(/up-follow#up-on-loaded).
@param {string} [config.scriptElementPolicy='auto']
Whether Unpoly will [run `<script>` elements in new fragments](/script-security#script-elements).
@param {string|Function(): string} [config.cspNonce]
A [CSP script nonce](https://content-security-policy.com/nonce/)
for the initial page that [booted](/up.boot) Unpoly.
The nonce lets Unpoly run JavaScript in HTML attributes like
[`[up-on-loaded]`](/up-follow#up-on-loaded) or [`[up-on-accepted]`](/up-layer-new#up-on-accepted).
See [Restricting callbacks with nonces](/script-security#callback-nonces).
The nonce can either be configured as a string or as a function that returns the nonce.
Defaults to the `content` attribute of a `<meta>` tag named `csp-nonce`:
```
<meta name='csp-nonce' content='secret4367243'>
```
@param {boolean} [config.cspWarnings=true]
Whether Unpoly will print a warning when it observes a potentially unsafe `Content-Security-Policy` header.
@property up.script.config
@stable
*/
const config = new up.Config(() => ({
cspNonce() { return e.metaContent('csp-nonce') },
cspWarnings: true,
scriptElementPolicy: 'auto',
evalCallbackPolicy: 'auto',
assetSelectors: [
'link[rel=stylesheet]',
'script[src]',
'[up-asset]'
],
noAssetSelectors: [
'[up-asset=false]',
],
nonceableAttributes: [
'up-watch',
'up-on-keep',
'up-on-hungry',
'up-on-opened',
'up-on-accepted',
'up-on-dismissed',
'up-on-loaded',
'up-on-rendered',
'up-on-finished',
'up-on-error',
'up-on-offline',
],
scriptSelectors: [
'script:not([type])',
'script[type="text/javascript"]',
'script[type="module"]',
'script[type="importmap"]',
],
noScriptSelectors: [
'script[type="application/ld+json"]'
]
}))
const SYSTEM_MACRO_PRIORITIES = {
'[up-back]': -100, // sets [up-href] to previous URL
'[up-clickable]': -200, // A11y for link-like elements
'[up-drawer]': -200, //
'[up-modal]': -200, //
'[up-cover]': -200, //
'[up-popup]': -200, //
'[up-tooltip]': -200, //
'[up-dash]': -200, // sets [up-href] unless already set, also other [up-*] attributes
'[up-flashes]': -200, //
'[up-expand]': -300, // distributes [up-*] attributes to parents
'[data-method]': -400, // converts [data-method] to [up-method] only if link has followable [up-*] attributes
'[data-confirm]': -400, // converts [data-confirm] to [up-confirm] only if link has followable [up-*] attributes
'[up-keep]': 9999999999, // cache the identity of e.g. [up-keep="same-html"] before any client-side changes
}
let registeredCompilers = []
let registeredMacros = []
/*-
Registers a function to be called when a matching element is inserted into the DOM.
[Enhancing elements](/enhancing-elements){:.article-ref}
## Anatomy of a compiler
This code registers a function that is called when an element with a `.foo` class
enters the DOM:
```js
up.compiler('.foo', function(element, data, meta) {
// do something with `element`
})
```
The compiler function is passed three arguments:
1. The newly inserted `element` matching the selector
2. Any [data attached to the element](/data)
3. [Meta information](/enhancing-elements#meta) about the current render pass
## Destructor functions {#destructor}
When a compiler registers effects *outside* the compiling element's subtree,
it must return a destructor function that reverts the effect. The destructor
function is called automatically when the element is swapped or destroyed later.
Examples of non-local effects are global event handlers bound to
the `document`, or `setInterval()`:
```js
up.compiler('.current-time', function(element) {
let update = () => element.textContent = new Date().toString()
let updateInterval = setInterval(update, 1000)
return () => clearInterval(updateInterval)
})
```
See [cleaning up after yourself](/enhancing-elements#destructor)
for more details and examples.
## Batching multiple elements
When multiple elements in an update match the selector, the compiler function
is called once for each element.
Let's say a fragment with three `.card` elements is inserted and compiled:
```html
<div class="fragment">
<div class="card">...</div>
<div class="card">...</div>
<div class="card">...</div>
</div>
```
A compiler function for `.card` would be called three separate times:
```js
up.compiler('.card', function(card, data) {
// chip: called three times
})
```
If you would like the compiler to run *once* for all matches within
a render pass, use the `{ batch: true }` option:
```js
up.compiler('.card', { batch: true }, function(cards, datas) {
console.debug(`We have ${cards.length} new cards`)
})
```
## Error handling {#errors}
It is safe to throw exceptions from a compiler or its [destructor](#destructor).
A crashing compiler will *not* interrupt a render pass or prevent other compilers on the same element.
Exceptions thrown by compiler functions are logged to the browser's [error console](https://developer.mozilla.org/en-US/docs/Web/API/console/error).
Unpoly also emits an [`error` event on `window`](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event).
See [errors in user code](/render-lifecycle#errors-in-user-code) for details.
## Compilation order {#order}
Compilers are called in the order they were registered.
You can control the order by passing a `{ priority }` option.
The default priority is `0`. Compilers with a higher priority are run first.
```js
up.compiler('.foo', { priority: 999 }, function() {
// called before compilers with a lower priority
})
```
When a compiler sets an Unpoly attribute, this usually has no effect since attributes have already been evaluated.
Use a [macro](/up.macro) instead.
See [Rendering lifecycle](/render-lifecycle) for a full overview of
Unpoly's rendering steps.
## Asynchronous compilers {#async}
Compiler functions can be [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function).
This is useful when a compiler needs to fetch network resources
or when calling a library with an asynchronous API:
```js
up.compiler('textarea.wysiwyg', async function(textarea) { // mark: async
let editor = await import('wysiwyg-editor') // mark: await
editor.init(textarea) // mark: await
})
```
You can also use this to split up expensive tasks, giving the browser a chance
to render and process user input:
```js
up.compiler('.element', async function(element) {
doRenderBlockingWork(element)
await scheduler.yield() // mark-line
doUserVisibleWork(element)
})
```
### Cleaning up async work {#async-destructors}
Like synchronous compilers, async compiler functions can return a [destructor function](#destructor):
```js
up.compiler('textarea.wysiwyg', async function(textarea) {
let editor = await import('wysiwyg-editor')
editor.init(textarea) // mark: await
return () => editor.destroy(textarea) // mark-line
})
```
Unpoly guarantees that the destructor is called even if the element is destroyed
before the compiler function terminates.
### Timing render-blocking mutations {#async-timing}
Unpoly will run the first [task](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/) of every compiler
function before allowing the browser to render DOM changes. If an async compiler function runs for multiple tasks,
the browser will render between tasks. If you have render-blocking mutations that should be hidden from the user,
they must happen in the first task.
{:width='670'}
Async compilers will not delay the promise returned by rendering functions `up.render()` or `up.layer.open()`.\
Async compilers *will* delay the promise returned by [`up.render().finished`](/render-lifecycle#postprocessing)
and `up.hello()`.
## Registering compilers after booting {#late}
When you [deliver your JavaScript in multiple files](https://makandracards.com/makandra/498036-webpacker-loading-code-on-demand),
you may register compilers after Unpoly has booted.
When compilers are registered after Unpoly has booted, they are run
on current elements, but **only** if the compiler has the default priority.
If the compiler has a non-default [priority](#order), it is run on future
fragments only. In this case, either remove the `{ priority }` option
or manually call `up.hello()` on an element that should be
[recompiled](/up.hello#recompiling-elements).
@function up.compiler
@param {string} selector
The selector to match.
@param {number} [options.priority=0]
The priority of this compiler.
Compilers with a higher priority are run first.
Two compilers with the same priority are run in the order they were registered.
@param {boolean} [options.batch=false]
If set to `true` and a fragment insertion contains multiple
elements matching `selector`, the `compiler` function is only called once
with all these elements.
@param {Function(element, data, meta): (Function|Array<Function>|Promise<Function>|undefined)} compiler
The function to call when an element matching `selector` is inserted.
Up to three arguments can be accepted:
1. The newly inserted `element` matching the selector
2. Any [data attached to the element](/data)
3. [Meta information](/enhancing-elements#meta) about the current render pass
The function may return a [destructor function](/enhancing-elements#destructor) that is called
before it is removed from the DOM. The destructor function is called with the compiled element.
The compiler function can be [`async`](/up.compiler#async).
@stable
*/
function registerCompiler(...args) {
registerProcessor(args)
}
/*-
Registers a [compiler](/enhancing-elements) that is run before all other compilers.
A macro lets you set attributes that will be compiled afterward.
If you want default attributes for *every* link and form, consider customizing your
[navigation options](/navigation) or configuring Unpoly to [handle everything](/handling-everything).
## Example
You will sometimes find yourself setting the same combination of UJS attributes again and again:
```html
<a href="/page1" up-layer="new modal" up-class="warning" up-animation="shake">Page 1</a>
<a href="/page1" up-layer="new modal" up-class="warning" up-animation="shake">Page 2</a>
<a href="/page1" up-layer="new modal" up-class="warning" up-animation="shake">Page 3</a>
```
We would much rather define a new `[shake-modal]` attribute that lets us
write the same links like this:
```html
<a href="/page1" shake-modal>Page 1</a>
<a href="/page2" shake-modal>Page 2</a>
<a href="/page3" shake-modal>Page 3</a>
```
We can define the `[shake-modal]` attribute by registering a macro that
sets the `[up-layer]`, `[up-class]`, and `[up-animation]` attributes for us:
```js
up.macro('[shake-modal]', function(link) {
link.setAttribute('up-layer', 'new modal')
link.setAttribute('up-class', 'warning')
link.setAttribute('up-animation', 'shake')
})
```
@function up.macro
@param selector
@like up.compiler
@param {Object} options
See options for [`up.compiler()`](/up.compiler).
@param macro
@like up.compiler/compiler
@stable
*/
function registerMacro(...args) {
registerProcessor(args, { macro: true })
}
function registerAttrCompiler(...args) {
let [attr, options, valueCallback] = parseProcessorArgs(args)
let selector = `[${attr}]`
let { defaultValue } = options
let callback = (element) => {
let value = e.booleanOrStringAttr(element, attr)
// Do nothing in a case like [up-defer=false]
if (value === false) return
// Some attributes have a default value in case no value is given.
// E.g. [up-defer=""] or [up-defer="true"] defaults to [up-defer="insert"].
if (u.isDefined(defaultValue) && value === true) value = defaultValue
return valueCallback(element, value)
}
registerProcessor([selector, options, callback])
}
function detectSystemMacroPriority(macroSelector) {
macroSelector = u.evalOption(macroSelector)
for (let substr in SYSTEM_MACRO_PRIORITIES) {
if (macroSelector.indexOf(substr) >= 0) {
return SYSTEM_MACRO_PRIORITIES[substr]
}
}
up.fail('Unregistered priority for system macro %o', macroSelector)
}
function registerProcessor(args, overrides = {}) {
let processor = buildProcessor(args, overrides)
if (processor.macro) {
if (up.framework.evaling) {
// Don't allow the default priority (0) for Unpoly's own macros.
processor.priority ||= detectSystemMacroPriority(processor.selector)
}
insertProcessor(registeredMacros, processor)
} else {
insertProcessor(registeredCompilers, processor)
}
}
const parseProcessorArgs = function(args) {
return u.args(args, 'val', 'options', 'callback')
}
function buildProcessor(args, overrides) {
let [selector, options, callback] = parseProcessorArgs(args)
options = u.options(options, {
selector,
isDefault: up.framework.evaling,
priority: 0,
batch: false,
rerun: false,
})
return Object.assign(callback, options, overrides)
}
function insertProcessor(queue, newCompiler) {
let existingCompiler
let index = 0
while ((existingCompiler = queue[index]) && (existingCompiler.priority >= newCompiler.priority)) {
index += 1
}
queue.splice(index, 0, newCompiler)
if (up.framework.booted) {
if (newCompiler.priority === 0) {
for (let layer of up.layer.stack) {
hello(layer.element, { layer, compilers: [newCompiler], macros: [], insertedEvent: false })
}
} else {
up.puts('up.compiler()', 'Compiler with priority (%s) was registered after booting Unpoly. Compiler will run for future fragments only.', newCompiler.selector)
}
}
return newCompiler
}
/*-
Registers a function to be called when the given element
is destroyed.
Elements are destroyed when they are swapped during a render pass, when their [layer](/up.layer)
closes, or when `up.destroy()` is called on the element or its container.
An alternative way to register a destructor function is to
[return it from your compiler function](/enhancing-elements#destructor).
## Example
The code below will log a message when `element` exits the DOM:
```js
let element = document.querySelector('.element')
up.destructor(element, () => console.log('Element was destroyed!'))
```
## Registration time
The element should be attached when the destructor is registered. This is commonly done during
[compilation](/enhancing-elements).
If called on a detached element, Unpoly assumes
an [async compiler](/up.compiler#async) has registered the destructor after the element has been destroyed.
The destructor is then run immediately.
## Reusing destructor functions
You may reuse the same destructor function for multiple elements.
The destructor function is called with the element being destroyed:
```js
let fn = (element) => console.log('Element %o was destroyed', element)
for (let element of document.querySelector('div')) {
up.destructor(fn)
}
```
@function up.destructor
@param {Element} element
The element to observe.
@param {Function(Element)|Array<Function(Element)>} destructor
One or more destructor functions.
@stable
*/
function registerDestructor(element, value) {
let fns = u.scanFunctions(value)
if (!fns.length) return
// (A) If the element has already been destroyed, registering a destructor function
// has no effect. No one will ever look at the destructor registry again.
// In this case we immediately call the destructor function.
// (B) Because destructors are called *after* an destroy animation, we don't need
// to check .up-destroying here. We can use the cheaper Element#isConnected instead.
if (element.isConnected) {
let registry = (element.upDestructors ||= buildDestructorRegistry(element))
registry.guard(fns)
} else {
// The element was destroyed before an async compiler function resolved.
up.puts('up.destructor()', 'Immediately calling destructor for detached element (%o)', element)
for (let fn of fns) up.error.guard(fn, element)
}
}
function buildDestructorRegistry(element) {
let registry = u.cleaner()
registry(e.addClassTemp(element, 'up-can-clean'))
return registry
}
/*-
Manually compiles a page fragment that has been inserted into the DOM
by external code.
All registered [compilers](/enhancing-elements) and [macros](/up.macro) will be called
with matches in the given `element`.
The [`up:fragment:inserted`](/up:fragment:inserted) event is emitted on the compiled element.
## Unpoly automatically calls `up.hello()`
When the page is manipulated using Unpoly functions or HTML selectors,
Unpoly will automatically call `up.hello()` on new fragments:
```js
let link = document.querySelector('a[href]')
let { fragment } = await up.follow(link)
// The fragment is already compiled. No need to call up.hello().
```
You only need to use `up.hello()` after creating DOM elements without Unpoly's involvement, for example:
- Creating elements with `document.createElement()`
- Setting the `innerHTML` property on an existing element
- Parsing HTML into elements using browser APIs like `DOMParser()`
- Elements created by other libraries
In this case, compilers will *not* run automatically and some of Unpoly's own HTML selectors will not be active.
We can address this by manually calling `up.hello()` on new elements:
```js
let element = document.createElement('div')
element.innerHTML = '<a href="/path" up-follow>click me</a>'
// The element is not compiled yet. We must compile it manually:
up.hello(element)
```
## Recompiling elements
It is safe to call `up.hello()` multiple times with the same elements.
In particular, every compiler function is guaranteed to run only once for each matching element.
If a new compiler is registered after initial compilation,
that new compiler is [run automatically on current elements](/up.compiler#late).
## Detecting compiler errors
If a compiler function throws an error, `up.hello()` will still finish the compilation and *not* throw an error.
Instead, compiler errors will print to the [error console](https://developer.mozilla.org/en-US/docs/Web/API/console/error)
and emit an [`error` event on `window`](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event):
```js
window.addEventListener('error', function(error) {
alert("Got an error " + error.name)
})
up.hello(element)
```
See [errors in user code](/render-lifecycle#errors-in-user-code) for details.
## Awaiting asynchronous compilation {#async}
Unpoly supports [`async` compiler functions](/up.compiler#async):
```js
up.compiler('textarea.wysiwyg', async function(textarea) { // mark: async
let editor = await import('wysiwyg-editor') // mark: await
await editor.init(textarea) // mark: await
return () => editor.destroy(textarea)
})
```
The `up.hello()` function returns a promise that fulfills when all synchronous and asynchronous
compilers have terminated:
```js
let textarea = up.element.createFromHTML('<textarea class="wysiwyg"></textarea>')
await up.hello(textarea) // mark: await
// chip: WYSIWYG editor is now initialized
```
The fulfillment value is the same element that was passed as an argument:
```js
let html = '<textarea class="wysiwyg"></textarea>'
let textarea = await up.hello(up.element.createFromHTML(html))
```
@function up.hello
@param {Element|jQuery|string} element
The root element of the new page fragment.
@param {Object} [options.layer]
An existing `up.Layer` object can be passed to prevent re-lookup.
@internal
@param {Object} [options.data]
Overrides properties from the new fragment's `[up-data]`
with the given [data object](/data).
@experimental
@param {Object} [options.dataMap]
An object mapping selectors to `options.data`.
@internal
@param {Object} [options.meta={}]
An object containing [information about this compiler pass](/enhancing-elements#meta).
This typically contains `{ layer, revalidating }` properties.
It will be passed as a third [compiler](/enhancing-elements) argument.
@experimental
@param {Object} [options.compilers]
A list of compilers to use.
Defaults to all registered compilers.
@internal
@param {Object} [options.compilers]
A list of macros to use.
Defaults to all registered macros.
@internal
@param {boolean} [insertedEvent]
Whether to emit `up:fragment:inserted` after compilation:
@internal
@return {Promise<Element>}
A promise that fulfills when the element has finished compilation.
The fulfillment value is the `element` argument.
@stable
*/
function hello(element, options = {}) {
// If passed a selector, up.fragment.get() will prefer a match on the current layer.
element = up.fragment.get(element, options)
// We default to running all registered processor fns, but callers
// can opt to only run given processor arguments.
let macros = options.macros ?? registeredMacros
let compilers = options.compilers ?? registeredCompilers
const pass = new up.HelloFragment(element, macros, compilers, options)
return pass.run()
}
/*-
Runs any destructors on the given fragment and its descendants in the same layer.
Unlike [`up.destroy()`](/up.destroy), this does not emit any events
and does not remove the element from the DOM.
@function up.script.clean
@param {Element} fragment
@param {up.Layer} options.layer
@internal
*/
function clean(fragment, options = {}) {
new up.DestructorPass(fragment, options).run()
}
/*-
Returns the [data](/data) attached to the given element.
If the element has no attached data, an empty object is returned.
Multiple `up.data()` calls for the same element always return the same object reference.
[Attaching data to elements](/data){:.article-ref}
## Use with `[up-data]`
You have an element with JSON data serialized into an `[up-data]` attribute:
```html
<span class="person" up-data="{ age: 18, name: 'Bob' }">Bob</span>
```
Calling `up.data()` will deserialize the JSON string into a JavaScript object:
```js
up.data('.person') // returns { age: 18, name: 'Bob' }
```
## Use with data attributes
You may also use standard [`[data-*]` attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes)
to attach data to an element.
```html
<span class='person' data-first-name='Alice' data-last-name='Anderson'>Alice</span>
```
The object returned by `up.data()` will contain all data attributes with camelCased keys:
```js
up.data('.person') // returns { firstName: 'Alice', lastName: 'Anderson' }
```
You may use both `[up-data]` and `[data-*]` attributes on the same element.
The object returned by `up.data()` will contain values from both.
@function up.data
@param {string|Element|jQuery} element
The element for which to return data.
@return {Object}
The [data](/data) attached to the element.
Returns an empty object if the element has no attached data.
Multiple `up.data()` calls for the same element always return the same object reference.
@stable
*/
/*-
Attaches structured data to an element to be consumed by a compiler or event handler.
If an element with an `[up-data]` attribute enters the DOM,
Unpoly will parse the JSON and pass the resulting object to any matching
`up.compiler()` functions and `up.on()` callbacks.
To programmatically parse an `[up-data]` attribute into an object, use `up.data(element)`.
[Attaching data to elements](/data){:.article-ref}
## Example
A container for a [Google Map](https://developers.google.com/maps/documentation/javascript/tutorial)
might attach the location and names of its marker pins:
```html
<div class="google-map" up-data="{ pins: [
{ lat: 48.36, lng: 10.99, title: 'Friedberg' },
{ lat: 48.75, lng: 11.45, title: 'Ingolstadt' }
] }"></div>
```
The attribute value will be parsed as [relaxed JSON](/relaxed-json).
The parsed object is passed to your compiler as a second argument:
```js
up.compiler('.google-map', function(element, data) {
var map = new google.maps.Map(element)
for (let pin of data.pins) {
var position = new google.maps.LatLng(pin.lat, pin.lng)
new google.maps.Marker({ position, map, title: pin.title })
}
})
```
Similarly, when an event is triggered on an element annotated with
`[up-data]`, the parsed object will be passed to any matching
[`up.on()`](/up.on) handlers:
```js
up.on('click', '.google-map', function(event, element, data) {
console.log("There are %d pins on the clicked map", data.pins.length)
})
```
You may also parse the data object programmatically using the `up.data()` function:
```js
let data = up.data('.google-map')
data[0].lat // result: 48.36
data[0].lng // result: 10.99
data[0].title // result: 'Friedberg'
```
@selector [up-data]
@param up-data
A data object serialized as [relaxed JSON](/relaxed-json).
@stable
*/
function readData(element) {
// If passed a selector, up.fragment.get() will prefer a match on the current layer.
element = up.fragment.get(element)
return element.upData ||= buildData(element)
}
function buildData(element) {
let parsedJSON = e.jsonAttr(element, 'up-data') ?? {}
// If the [up-data] JSON isn't an object then we cannot offer live merging
// of [up-data] and [data-...] attributes.
//
// It would be better to parse this lazily, but I don't want to pay
// the bytes for a second Proxy handler when dealing with this edge case.
if (!u.isOptions(parsedJSON)) {
return parsedJSON
}
return {
...element.upTemplateData,
...element.dataset,
...parsedJSON,
...element.upCompileData,
}
}
/*-
@function up.script.assets
@param {Element} [head=document.head]
@return {NodeList}
@internal
*/
function findAssets(head = document.head) {
return head.querySelectorAll(config.selector('assetSelectors'))
}
/*-
Tracks an element as a [frontend asset](/handling-asset-changes), usually JavaScript and stylesheets.
When [rendering](/up.render), Unpoly compares the assets on the current page with the new assets
from the server response. If the assets don't match, an `up:assets:changed` event is emitted.
[Handling changes in frontend code](/handling-asset-changes){:.article-ref}
## Default assets
By default, all remote scripts and stylesheets in the `<head>` are considered assets:
```html
<html>
<head>
<link rel="stylesheet" href="/assets/frontend-5f3aa101.css"> <!-- mark-line -->
<script src="/assets/frontend-81ba23a9.js"></script> <!-- mark-line -->
</head>
<body>
...
</body>
</html>
```
Unpoly only tracks assets in the `<head>`. Elements in the `<body>` are never tracked.
[Inline scripts](https://simpledev.io/lesson/inline-script-javascript-1/) and
[internal styles](https://www.tutorialspoint.com/How-to-use-internal-CSS-Style-Sheet-in-HTML)
are not tracked by default, but you can [include them explicitly](#including-assets).
## Excluding assets from tracking {#excluding-assets}
To *exclude* an element in the `<head>` from tracking, mark it with an `[up-asset="false"]` attribute:
```html
<script src="/assets/analytics.js" up-asset="false"></script>
```
To exclude assets by default, configure `up.script.config.noAssetSelectors`.
## Tracking additional assets {#including-assets}
To track additional assets in the `<head>`, mark them with an `[up-asset]` attribute.
For example, [inline scripts](https://simpledev.io/lesson/inline-script-javascript-1/) are not tracked by default,
but you can include them explicitly:
```html
<script up-asset>
window.SALE_START = new Date('2024-05-01')
</script>
```
Only elements in the `<head>` can be matched this way.
To track additional assets by default, configure `up.script.config.assetSelectors`.
## Tracking the backend version {#tracking-backend-versions}
To detect a new deployment of your *backend* code, consider including the deployed commit hash in a `<meta>` tag.
By marking the `<meta>` tag with `[up-asset]`, it will also emit an `up:assets:changed` event when the commit hash changes:
```html
<meta name="backend-version" value="d50c6dd629e9bbc80304e14a6ba99a18c32ba738" up-asset>
```
@selector [up-asset]
@stable
*/
function assertAssetsOK(newAssets, renderOptions) {
let { response } = renderOptions
let oldAssets = findAssets()
if (assetsIdentity(oldAssets) !== assetsIdentity(newAssets)) {
up.event.assertEmitted('up:assets:changed', { oldAssets, newAssets, renderOptions, response })
}
}
function assetsIdentity(assets) {
return u.map(assets, assetIdentity).join()
}
function assetIdentity(assetElement) {
// Remove the [nonce] attribute which the browser usually masks from the outerHTML of <script> elements.
// However it isn't masked for inert scripts (e.g. when they fail a CSP check).
return e.removeAttrsFromHTML(assetElement.outerHTML, ['nonce'])
}
/*-
This event is emitted when [frontend code](/up-asset) changes while the application is running.
When a server response has no `<head>`, this event is never emitted.
The event is emitted on the `document`.
[Handling changes in frontend code](/handling-asset-changes){:.article-ref}
## Example
There is no default behavior when assets have changed.
In particular, no asset elements from the response are updated in the current page.
Event listeners may [handle changed frontend code](/handling-asset-changes#handling-changed-assets),
e.g., by [notifying the user](/handling-asset-changes#notifying-the-user) or [loading new assets](/handling-asset-changes#loading-new-assets).
The code below inserts a clickable `<div id="new-version">` banner when assets change.
The user can then choose to reload at their convenience by clicking on the notification.
@include new-asset-notification-example
For more examples, see [Handling asset changes](/handling-asset-changes).
## Emission time