UNPKG

68.2 kBJavaScriptView Raw
1/*!
2 * Bulmil - MIT License
3 */
4const NAMESPACE = 'bulmil';
5
6let contentRef;
7let hostTagName;
8let useNativeShadowDom = false;
9let checkSlotFallbackVisibility = false;
10let checkSlotRelocate = false;
11let isSvgMode = false;
12let queuePending = false;
13const win = typeof window !== 'undefined' ? window : {};
14const doc = win.document || { head: {} };
15const plt = {
16 $flags$: 0,
17 $resourcesUrl$: '',
18 jmp: (h) => h(),
19 raf: (h) => requestAnimationFrame(h),
20 ael: (el, eventName, listener, opts) => el.addEventListener(eventName, listener, opts),
21 rel: (el, eventName, listener, opts) => el.removeEventListener(eventName, listener, opts),
22 ce: (eventName, opts) => new CustomEvent(eventName, opts),
23};
24const supportsShadow = true;
25const promiseResolve = (v) => Promise.resolve(v);
26const supportsConstructibleStylesheets = /*@__PURE__*/ (() => {
27 try {
28 new CSSStyleSheet();
29 return typeof new CSSStyleSheet().replace === 'function';
30 }
31 catch (e) { }
32 return false;
33 })()
34 ;
35const CONTENT_REF_ID = 'r';
36const ORG_LOCATION_ID = 'o';
37const SLOT_NODE_ID = 's';
38const TEXT_NODE_ID = 't';
39const HYDRATE_ID = 's-id';
40const HYDRATED_STYLE_ID = 'sty-id';
41const HYDRATE_CHILD_ID = 'c-id';
42const HYDRATED_CSS = '{visibility:hidden}.hydrated{visibility:inherit}';
43const createTime = (fnName, tagName = '') => {
44 {
45 return () => {
46 return;
47 };
48 }
49};
50const uniqueTime = (key, measureText) => {
51 {
52 return () => {
53 return;
54 };
55 }
56};
57const rootAppliedStyles = new WeakMap();
58const registerStyle = (scopeId, cssText, allowCS) => {
59 let style = styles.get(scopeId);
60 if (supportsConstructibleStylesheets && allowCS) {
61 style = (style || new CSSStyleSheet());
62 style.replace(cssText);
63 }
64 else {
65 style = cssText;
66 }
67 styles.set(scopeId, style);
68};
69const addStyle = (styleContainerNode, cmpMeta, mode, hostElm) => {
70 let scopeId = getScopeId(cmpMeta);
71 let style = styles.get(scopeId);
72 // if an element is NOT connected then getRootNode() will return the wrong root node
73 // so the fallback is to always use the document for the root node in those cases
74 styleContainerNode = styleContainerNode.nodeType === 11 /* DocumentFragment */ ? styleContainerNode : doc;
75 if (style) {
76 if (typeof style === 'string') {
77 styleContainerNode = styleContainerNode.head || styleContainerNode;
78 let appliedStyles = rootAppliedStyles.get(styleContainerNode);
79 let styleElm;
80 if (!appliedStyles) {
81 rootAppliedStyles.set(styleContainerNode, (appliedStyles = new Set()));
82 }
83 if (!appliedStyles.has(scopeId)) {
84 if (styleContainerNode.host &&
85 (styleElm = styleContainerNode.querySelector(`[${HYDRATED_STYLE_ID}="${scopeId}"]`))) {
86 // This is only happening on native shadow-dom, do not needs CSS var shim
87 styleElm.innerHTML = style;
88 }
89 else {
90 {
91 styleElm = doc.createElement('style');
92 styleElm.innerHTML = style;
93 }
94 styleContainerNode.insertBefore(styleElm, styleContainerNode.querySelector('link'));
95 }
96 if (appliedStyles) {
97 appliedStyles.add(scopeId);
98 }
99 }
100 }
101 else if (!styleContainerNode.adoptedStyleSheets.includes(style)) {
102 styleContainerNode.adoptedStyleSheets = [...styleContainerNode.adoptedStyleSheets, style];
103 }
104 }
105 return scopeId;
106};
107const attachStyles = (hostRef) => {
108 const cmpMeta = hostRef.$cmpMeta$;
109 const elm = hostRef.$hostElement$;
110 const endAttachStyles = createTime('attachStyles', cmpMeta.$tagName$);
111 addStyle(elm.getRootNode(), cmpMeta);
112 endAttachStyles();
113};
114const getScopeId = (cmp, mode) => 'sc-' + (cmp.$tagName$);
115/**
116 * Default style mode id
117 */
118/**
119 * Reusable empty obj/array
120 * Don't add values to these!!
121 */
122const EMPTY_OBJ = {};
123const isComplexType = (o) => {
124 // https://jsperf.com/typeof-fn-object/5
125 o = typeof o;
126 return o === 'object' || o === 'function';
127};
128/**
129 * Production h() function based on Preact by
130 * Jason Miller (@developit)
131 * Licensed under the MIT License
132 * https://github.com/developit/preact/blob/master/LICENSE
133 *
134 * Modified for Stencil's compiler and vdom
135 */
136// const stack: any[] = [];
137// export function h(nodeName: string | d.FunctionalComponent, vnodeData: d.PropsType, child?: d.ChildType): d.VNode;
138// export function h(nodeName: string | d.FunctionalComponent, vnodeData: d.PropsType, ...children: d.ChildType[]): d.VNode;
139const h = (nodeName, vnodeData, ...children) => {
140 let child = null;
141 let slotName = null;
142 let simple = false;
143 let lastSimple = false;
144 let vNodeChildren = [];
145 const walk = (c) => {
146 for (let i = 0; i < c.length; i++) {
147 child = c[i];
148 if (Array.isArray(child)) {
149 walk(child);
150 }
151 else if (child != null && typeof child !== 'boolean') {
152 if ((simple = typeof nodeName !== 'function' && !isComplexType(child))) {
153 child = String(child);
154 }
155 if (simple && lastSimple) {
156 // If the previous child was simple (string), we merge both
157 vNodeChildren[vNodeChildren.length - 1].$text$ += child;
158 }
159 else {
160 // Append a new vNode, if it's text, we create a text vNode
161 vNodeChildren.push(simple ? newVNode(null, child) : child);
162 }
163 lastSimple = simple;
164 }
165 }
166 };
167 walk(children);
168 if (vnodeData) {
169 if (vnodeData.name) {
170 slotName = vnodeData.name;
171 }
172 {
173 const classData = vnodeData.className || vnodeData.class;
174 if (classData) {
175 vnodeData.class =
176 typeof classData !== 'object'
177 ? classData
178 : Object.keys(classData)
179 .filter((k) => classData[k])
180 .join(' ');
181 }
182 }
183 }
184 if (typeof nodeName === 'function') {
185 // nodeName is a functional component
186 return nodeName(vnodeData === null ? {} : vnodeData, vNodeChildren, vdomFnUtils);
187 }
188 const vnode = newVNode(nodeName, null);
189 vnode.$attrs$ = vnodeData;
190 if (vNodeChildren.length > 0) {
191 vnode.$children$ = vNodeChildren;
192 }
193 {
194 vnode.$name$ = slotName;
195 }
196 return vnode;
197};
198const newVNode = (tag, text) => {
199 const vnode = {
200 $flags$: 0,
201 $tag$: tag,
202 $text$: text,
203 $elm$: null,
204 $children$: null,
205 };
206 {
207 vnode.$attrs$ = null;
208 }
209 {
210 vnode.$name$ = null;
211 }
212 return vnode;
213};
214const Host = {};
215const isHost = (node) => node && node.$tag$ === Host;
216const vdomFnUtils = {
217 forEach: (children, cb) => children.map(convertToPublic).forEach(cb),
218 map: (children, cb) => children.map(convertToPublic).map(cb).map(convertToPrivate),
219};
220const convertToPublic = (node) => ({
221 vattrs: node.$attrs$,
222 vchildren: node.$children$,
223 vkey: node.$key$,
224 vname: node.$name$,
225 vtag: node.$tag$,
226 vtext: node.$text$,
227});
228const convertToPrivate = (node) => {
229 if (typeof node.vtag === 'function') {
230 const vnodeData = Object.assign({}, node.vattrs);
231 if (node.vkey) {
232 vnodeData.key = node.vkey;
233 }
234 if (node.vname) {
235 vnodeData.name = node.vname;
236 }
237 return h(node.vtag, vnodeData, ...(node.vchildren || []));
238 }
239 const vnode = newVNode(node.vtag, node.vtext);
240 vnode.$attrs$ = node.vattrs;
241 vnode.$children$ = node.vchildren;
242 vnode.$key$ = node.vkey;
243 vnode.$name$ = node.vname;
244 return vnode;
245};
246/**
247 * Production setAccessor() function based on Preact by
248 * Jason Miller (@developit)
249 * Licensed under the MIT License
250 * https://github.com/developit/preact/blob/master/LICENSE
251 *
252 * Modified for Stencil's compiler and vdom
253 */
254const setAccessor = (elm, memberName, oldValue, newValue, isSvg, flags) => {
255 if (oldValue !== newValue) {
256 let isProp = isMemberInElement(elm, memberName);
257 let ln = memberName.toLowerCase();
258 if (memberName === 'class') {
259 const classList = elm.classList;
260 const oldClasses = parseClassList(oldValue);
261 const newClasses = parseClassList(newValue);
262 classList.remove(...oldClasses.filter((c) => c && !newClasses.includes(c)));
263 classList.add(...newClasses.filter((c) => c && !oldClasses.includes(c)));
264 }
265 else if ((!isProp ) &&
266 memberName[0] === 'o' &&
267 memberName[1] === 'n') {
268 // Event Handlers
269 // so if the member name starts with "on" and the 3rd characters is
270 // a capital letter, and it's not already a member on the element,
271 // then we're assuming it's an event listener
272 if (memberName[2] === '-') {
273 // on- prefixed events
274 // allows to be explicit about the dom event to listen without any magic
275 // under the hood:
276 // <my-cmp on-click> // listens for "click"
277 // <my-cmp on-Click> // listens for "Click"
278 // <my-cmp on-ionChange> // listens for "ionChange"
279 // <my-cmp on-EVENTS> // listens for "EVENTS"
280 memberName = memberName.slice(3);
281 }
282 else if (isMemberInElement(win, ln)) {
283 // standard event
284 // the JSX attribute could have been "onMouseOver" and the
285 // member name "onmouseover" is on the window's prototype
286 // so let's add the listener "mouseover", which is all lowercased
287 memberName = ln.slice(2);
288 }
289 else {
290 // custom event
291 // the JSX attribute could have been "onMyCustomEvent"
292 // so let's trim off the "on" prefix and lowercase the first character
293 // and add the listener "myCustomEvent"
294 // except for the first character, we keep the event name case
295 memberName = ln[2] + memberName.slice(3);
296 }
297 if (oldValue) {
298 plt.rel(elm, memberName, oldValue, false);
299 }
300 if (newValue) {
301 plt.ael(elm, memberName, newValue, false);
302 }
303 }
304 else {
305 // Set property if it exists and it's not a SVG
306 const isComplex = isComplexType(newValue);
307 if ((isProp || (isComplex && newValue !== null)) && !isSvg) {
308 try {
309 if (!elm.tagName.includes('-')) {
310 let n = newValue == null ? '' : newValue;
311 // Workaround for Safari, moving the <input> caret when re-assigning the same valued
312 if (memberName === 'list') {
313 isProp = false;
314 }
315 else if (oldValue == null || elm[memberName] != n) {
316 elm[memberName] = n;
317 }
318 }
319 else {
320 elm[memberName] = newValue;
321 }
322 }
323 catch (e) { }
324 }
325 if (newValue == null || newValue === false) {
326 if (newValue !== false || elm.getAttribute(memberName) === '') {
327 {
328 elm.removeAttribute(memberName);
329 }
330 }
331 }
332 else if ((!isProp || flags & 4 /* isHost */ || isSvg) && !isComplex) {
333 newValue = newValue === true ? '' : newValue;
334 {
335 elm.setAttribute(memberName, newValue);
336 }
337 }
338 }
339 }
340};
341const parseClassListRegex = /\s/;
342const parseClassList = (value) => (!value ? [] : value.split(parseClassListRegex));
343const updateElement = (oldVnode, newVnode, isSvgMode, memberName) => {
344 // if the element passed in is a shadow root, which is a document fragment
345 // then we want to be adding attrs/props to the shadow root's "host" element
346 // if it's not a shadow root, then we add attrs/props to the same element
347 const elm = newVnode.$elm$.nodeType === 11 /* DocumentFragment */ && newVnode.$elm$.host
348 ? newVnode.$elm$.host
349 : newVnode.$elm$;
350 const oldVnodeAttrs = (oldVnode && oldVnode.$attrs$) || EMPTY_OBJ;
351 const newVnodeAttrs = newVnode.$attrs$ || EMPTY_OBJ;
352 {
353 // remove attributes no longer present on the vnode by setting them to undefined
354 for (memberName in oldVnodeAttrs) {
355 if (!(memberName in newVnodeAttrs)) {
356 setAccessor(elm, memberName, oldVnodeAttrs[memberName], undefined, isSvgMode, newVnode.$flags$);
357 }
358 }
359 }
360 // add new & update changed attributes
361 for (memberName in newVnodeAttrs) {
362 setAccessor(elm, memberName, oldVnodeAttrs[memberName], newVnodeAttrs[memberName], isSvgMode, newVnode.$flags$);
363 }
364};
365const createElm = (oldParentVNode, newParentVNode, childIndex, parentElm) => {
366 // tslint:disable-next-line: prefer-const
367 let newVNode = newParentVNode.$children$[childIndex];
368 let i = 0;
369 let elm;
370 let childNode;
371 let oldVNode;
372 if (!useNativeShadowDom) {
373 // remember for later we need to check to relocate nodes
374 checkSlotRelocate = true;
375 if (newVNode.$tag$ === 'slot') {
376 newVNode.$flags$ |= newVNode.$children$
377 ? // slot element has fallback content
378 2 /* isSlotFallback */
379 : // slot element does not have fallback content
380 1 /* isSlotReference */;
381 }
382 }
383 if (newVNode.$text$ !== null) {
384 // create text node
385 elm = newVNode.$elm$ = doc.createTextNode(newVNode.$text$);
386 }
387 else if (newVNode.$flags$ & 1 /* isSlotReference */) {
388 // create a slot reference node
389 elm = newVNode.$elm$ =
390 doc.createTextNode('');
391 }
392 else {
393 // create element
394 elm = newVNode.$elm$ = (doc.createElement(newVNode.$flags$ & 2 /* isSlotFallback */
395 ? 'slot-fb'
396 : newVNode.$tag$));
397 // add css classes, attrs, props, listeners, etc.
398 {
399 updateElement(null, newVNode, isSvgMode);
400 }
401 if (newVNode.$children$) {
402 for (i = 0; i < newVNode.$children$.length; ++i) {
403 // create the node
404 childNode = createElm(oldParentVNode, newVNode, i);
405 // return node could have been null
406 if (childNode) {
407 // append our new node
408 elm.appendChild(childNode);
409 }
410 }
411 }
412 }
413 {
414 elm['s-hn'] = hostTagName;
415 if (newVNode.$flags$ & (2 /* isSlotFallback */ | 1 /* isSlotReference */)) {
416 // remember the content reference comment
417 elm['s-sr'] = true;
418 // remember the content reference comment
419 elm['s-cr'] = contentRef;
420 // remember the slot name, or empty string for default slot
421 elm['s-sn'] = newVNode.$name$ || '';
422 // check if we've got an old vnode for this slot
423 oldVNode = oldParentVNode && oldParentVNode.$children$ && oldParentVNode.$children$[childIndex];
424 if (oldVNode && oldVNode.$tag$ === newVNode.$tag$ && oldParentVNode.$elm$) {
425 // we've got an old slot vnode and the wrapper is being replaced
426 // so let's move the old slot content back to it's original location
427 putBackInOriginalLocation(oldParentVNode.$elm$, false);
428 }
429 }
430 }
431 return elm;
432};
433const putBackInOriginalLocation = (parentElm, recursive) => {
434 plt.$flags$ |= 1 /* isTmpDisconnected */;
435 const oldSlotChildNodes = parentElm.childNodes;
436 for (let i = oldSlotChildNodes.length - 1; i >= 0; i--) {
437 const childNode = oldSlotChildNodes[i];
438 if (childNode['s-hn'] !== hostTagName && childNode['s-ol']) {
439 // // this child node in the old element is from another component
440 // // remove this node from the old slot's parent
441 // childNode.remove();
442 // and relocate it back to it's original location
443 parentReferenceNode(childNode).insertBefore(childNode, referenceNode(childNode));
444 // remove the old original location comment entirely
445 // later on the patch function will know what to do
446 // and move this to the correct spot in need be
447 childNode['s-ol'].remove();
448 childNode['s-ol'] = undefined;
449 checkSlotRelocate = true;
450 }
451 if (recursive) {
452 putBackInOriginalLocation(childNode, recursive);
453 }
454 }
455 plt.$flags$ &= ~1 /* isTmpDisconnected */;
456};
457const addVnodes = (parentElm, before, parentVNode, vnodes, startIdx, endIdx) => {
458 let containerElm = ((parentElm['s-cr'] && parentElm['s-cr'].parentNode) || parentElm);
459 let childNode;
460 for (; startIdx <= endIdx; ++startIdx) {
461 if (vnodes[startIdx]) {
462 childNode = createElm(null, parentVNode, startIdx);
463 if (childNode) {
464 vnodes[startIdx].$elm$ = childNode;
465 containerElm.insertBefore(childNode, referenceNode(before) );
466 }
467 }
468 }
469};
470const removeVnodes = (vnodes, startIdx, endIdx, vnode, elm) => {
471 for (; startIdx <= endIdx; ++startIdx) {
472 if ((vnode = vnodes[startIdx])) {
473 elm = vnode.$elm$;
474 {
475 // we're removing this element
476 // so it's possible we need to show slot fallback content now
477 checkSlotFallbackVisibility = true;
478 if (elm['s-ol']) {
479 // remove the original location comment
480 elm['s-ol'].remove();
481 }
482 else {
483 // it's possible that child nodes of the node
484 // that's being removed are slot nodes
485 putBackInOriginalLocation(elm, true);
486 }
487 }
488 // remove the vnode's element from the dom
489 elm.remove();
490 }
491 }
492};
493const updateChildren = (parentElm, oldCh, newVNode, newCh) => {
494 let oldStartIdx = 0;
495 let newStartIdx = 0;
496 let oldEndIdx = oldCh.length - 1;
497 let oldStartVnode = oldCh[0];
498 let oldEndVnode = oldCh[oldEndIdx];
499 let newEndIdx = newCh.length - 1;
500 let newStartVnode = newCh[0];
501 let newEndVnode = newCh[newEndIdx];
502 let node;
503 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
504 if (oldStartVnode == null) {
505 // Vnode might have been moved left
506 oldStartVnode = oldCh[++oldStartIdx];
507 }
508 else if (oldEndVnode == null) {
509 oldEndVnode = oldCh[--oldEndIdx];
510 }
511 else if (newStartVnode == null) {
512 newStartVnode = newCh[++newStartIdx];
513 }
514 else if (newEndVnode == null) {
515 newEndVnode = newCh[--newEndIdx];
516 }
517 else if (isSameVnode(oldStartVnode, newStartVnode)) {
518 patch(oldStartVnode, newStartVnode);
519 oldStartVnode = oldCh[++oldStartIdx];
520 newStartVnode = newCh[++newStartIdx];
521 }
522 else if (isSameVnode(oldEndVnode, newEndVnode)) {
523 patch(oldEndVnode, newEndVnode);
524 oldEndVnode = oldCh[--oldEndIdx];
525 newEndVnode = newCh[--newEndIdx];
526 }
527 else if (isSameVnode(oldStartVnode, newEndVnode)) {
528 // Vnode moved right
529 if ((oldStartVnode.$tag$ === 'slot' || newEndVnode.$tag$ === 'slot')) {
530 putBackInOriginalLocation(oldStartVnode.$elm$.parentNode, false);
531 }
532 patch(oldStartVnode, newEndVnode);
533 parentElm.insertBefore(oldStartVnode.$elm$, oldEndVnode.$elm$.nextSibling);
534 oldStartVnode = oldCh[++oldStartIdx];
535 newEndVnode = newCh[--newEndIdx];
536 }
537 else if (isSameVnode(oldEndVnode, newStartVnode)) {
538 // Vnode moved left
539 if ((oldStartVnode.$tag$ === 'slot' || newEndVnode.$tag$ === 'slot')) {
540 putBackInOriginalLocation(oldEndVnode.$elm$.parentNode, false);
541 }
542 patch(oldEndVnode, newStartVnode);
543 parentElm.insertBefore(oldEndVnode.$elm$, oldStartVnode.$elm$);
544 oldEndVnode = oldCh[--oldEndIdx];
545 newStartVnode = newCh[++newStartIdx];
546 }
547 else {
548 {
549 // new element
550 node = createElm(oldCh && oldCh[newStartIdx], newVNode, newStartIdx);
551 newStartVnode = newCh[++newStartIdx];
552 }
553 if (node) {
554 {
555 parentReferenceNode(oldStartVnode.$elm$).insertBefore(node, referenceNode(oldStartVnode.$elm$));
556 }
557 }
558 }
559 }
560 if (oldStartIdx > oldEndIdx) {
561 addVnodes(parentElm, newCh[newEndIdx + 1] == null ? null : newCh[newEndIdx + 1].$elm$, newVNode, newCh, newStartIdx, newEndIdx);
562 }
563 else if (newStartIdx > newEndIdx) {
564 removeVnodes(oldCh, oldStartIdx, oldEndIdx);
565 }
566};
567const isSameVnode = (vnode1, vnode2) => {
568 // compare if two vnode to see if they're "technically" the same
569 // need to have the same element tag, and same key to be the same
570 if (vnode1.$tag$ === vnode2.$tag$) {
571 if (vnode1.$tag$ === 'slot') {
572 return vnode1.$name$ === vnode2.$name$;
573 }
574 return true;
575 }
576 return false;
577};
578const referenceNode = (node) => {
579 // this node was relocated to a new location in the dom
580 // because of some other component's slot
581 // but we still have an html comment in place of where
582 // it's original location was according to it's original vdom
583 return (node && node['s-ol']) || node;
584};
585const parentReferenceNode = (node) => (node['s-ol'] ? node['s-ol'] : node).parentNode;
586const patch = (oldVNode, newVNode) => {
587 const elm = (newVNode.$elm$ = oldVNode.$elm$);
588 const oldChildren = oldVNode.$children$;
589 const newChildren = newVNode.$children$;
590 const tag = newVNode.$tag$;
591 const text = newVNode.$text$;
592 let defaultHolder;
593 if (text === null) {
594 // element node
595 {
596 if (tag === 'slot')
597 ;
598 else {
599 // either this is the first render of an element OR it's an update
600 // AND we already know it's possible it could have changed
601 // this updates the element's css classes, attrs, props, listeners, etc.
602 updateElement(oldVNode, newVNode, isSvgMode);
603 }
604 }
605 if (oldChildren !== null && newChildren !== null) {
606 // looks like there's child vnodes for both the old and new vnodes
607 updateChildren(elm, oldChildren, newVNode, newChildren);
608 }
609 else if (newChildren !== null) {
610 // no old child vnodes, but there are new child vnodes to add
611 if (oldVNode.$text$ !== null) {
612 // the old vnode was text, so be sure to clear it out
613 elm.textContent = '';
614 }
615 // add the new vnode children
616 addVnodes(elm, null, newVNode, newChildren, 0, newChildren.length - 1);
617 }
618 else if (oldChildren !== null) {
619 // no new child vnodes, but there are old child vnodes to remove
620 removeVnodes(oldChildren, 0, oldChildren.length - 1);
621 }
622 }
623 else if ((defaultHolder = elm['s-cr'])) {
624 // this element has slotted content
625 defaultHolder.parentNode.textContent = text;
626 }
627 else if (oldVNode.$text$ !== text) {
628 // update the text content for the text only vnode
629 // and also only if the text is different than before
630 elm.data = text;
631 }
632};
633const updateFallbackSlotVisibility = (elm) => {
634 // tslint:disable-next-line: prefer-const
635 let childNodes = elm.childNodes;
636 let childNode;
637 let i;
638 let ilen;
639 let j;
640 let slotNameAttr;
641 let nodeType;
642 for (i = 0, ilen = childNodes.length; i < ilen; i++) {
643 childNode = childNodes[i];
644 if (childNode.nodeType === 1 /* ElementNode */) {
645 if (childNode['s-sr']) {
646 // this is a slot fallback node
647 // get the slot name for this slot reference node
648 slotNameAttr = childNode['s-sn'];
649 // by default always show a fallback slot node
650 // then hide it if there are other slots in the light dom
651 childNode.hidden = false;
652 for (j = 0; j < ilen; j++) {
653 nodeType = childNodes[j].nodeType;
654 if (childNodes[j]['s-hn'] !== childNode['s-hn'] || slotNameAttr !== '') {
655 // this sibling node is from a different component OR is a named fallback slot node
656 if (nodeType === 1 /* ElementNode */ && slotNameAttr === childNodes[j].getAttribute('slot')) {
657 childNode.hidden = true;
658 break;
659 }
660 }
661 else {
662 // this is a default fallback slot node
663 // any element or text node (with content)
664 // should hide the default fallback slot node
665 if (nodeType === 1 /* ElementNode */ ||
666 (nodeType === 3 /* TextNode */ && childNodes[j].textContent.trim() !== '')) {
667 childNode.hidden = true;
668 break;
669 }
670 }
671 }
672 }
673 // keep drilling down
674 updateFallbackSlotVisibility(childNode);
675 }
676 }
677};
678const relocateNodes = [];
679const relocateSlotContent = (elm) => {
680 // tslint:disable-next-line: prefer-const
681 let childNode;
682 let node;
683 let hostContentNodes;
684 let slotNameAttr;
685 let relocateNodeData;
686 let j;
687 let i = 0;
688 let childNodes = elm.childNodes;
689 let ilen = childNodes.length;
690 for (; i < ilen; i++) {
691 childNode = childNodes[i];
692 if (childNode['s-sr'] && (node = childNode['s-cr']) && node.parentNode) {
693 // first got the content reference comment node
694 // then we got it's parent, which is where all the host content is in now
695 hostContentNodes = node.parentNode.childNodes;
696 slotNameAttr = childNode['s-sn'];
697 for (j = hostContentNodes.length - 1; j >= 0; j--) {
698 node = hostContentNodes[j];
699 if (!node['s-cn'] && !node['s-nr'] && node['s-hn'] !== childNode['s-hn']) {
700 // let's do some relocating to its new home
701 // but never relocate a content reference node
702 // that is suppose to always represent the original content location
703 if (isNodeLocatedInSlot(node, slotNameAttr)) {
704 // it's possible we've already decided to relocate this node
705 relocateNodeData = relocateNodes.find((r) => r.$nodeToRelocate$ === node);
706 // made some changes to slots
707 // let's make sure we also double check
708 // fallbacks are correctly hidden or shown
709 checkSlotFallbackVisibility = true;
710 node['s-sn'] = node['s-sn'] || slotNameAttr;
711 if (relocateNodeData) {
712 // previously we never found a slot home for this node
713 // but turns out we did, so let's remember it now
714 relocateNodeData.$slotRefNode$ = childNode;
715 }
716 else {
717 // add to our list of nodes to relocate
718 relocateNodes.push({
719 $slotRefNode$: childNode,
720 $nodeToRelocate$: node,
721 });
722 }
723 if (node['s-sr']) {
724 relocateNodes.map((relocateNode) => {
725 if (isNodeLocatedInSlot(relocateNode.$nodeToRelocate$, node['s-sn'])) {
726 relocateNodeData = relocateNodes.find((r) => r.$nodeToRelocate$ === node);
727 if (relocateNodeData && !relocateNode.$slotRefNode$) {
728 relocateNode.$slotRefNode$ = relocateNodeData.$slotRefNode$;
729 }
730 }
731 });
732 }
733 }
734 else if (!relocateNodes.some((r) => r.$nodeToRelocate$ === node)) {
735 // so far this element does not have a slot home, not setting slotRefNode on purpose
736 // if we never find a home for this element then we'll need to hide it
737 relocateNodes.push({
738 $nodeToRelocate$: node,
739 });
740 }
741 }
742 }
743 }
744 if (childNode.nodeType === 1 /* ElementNode */) {
745 relocateSlotContent(childNode);
746 }
747 }
748};
749const isNodeLocatedInSlot = (nodeToRelocate, slotNameAttr) => {
750 if (nodeToRelocate.nodeType === 1 /* ElementNode */) {
751 if (nodeToRelocate.getAttribute('slot') === null && slotNameAttr === '') {
752 return true;
753 }
754 if (nodeToRelocate.getAttribute('slot') === slotNameAttr) {
755 return true;
756 }
757 return false;
758 }
759 if (nodeToRelocate['s-sn'] === slotNameAttr) {
760 return true;
761 }
762 return slotNameAttr === '';
763};
764const renderVdom = (hostRef, renderFnResults) => {
765 const hostElm = hostRef.$hostElement$;
766 const cmpMeta = hostRef.$cmpMeta$;
767 const oldVNode = hostRef.$vnode$ || newVNode(null, null);
768 const rootVnode = isHost(renderFnResults) ? renderFnResults : h(null, null, renderFnResults);
769 hostTagName = hostElm.tagName;
770 if (cmpMeta.$attrsToReflect$) {
771 rootVnode.$attrs$ = rootVnode.$attrs$ || {};
772 cmpMeta.$attrsToReflect$.map(([propName, attribute]) => (rootVnode.$attrs$[attribute] = hostElm[propName]));
773 }
774 rootVnode.$tag$ = null;
775 rootVnode.$flags$ |= 4 /* isHost */;
776 hostRef.$vnode$ = rootVnode;
777 rootVnode.$elm$ = oldVNode.$elm$ = (hostElm);
778 {
779 contentRef = hostElm['s-cr'];
780 useNativeShadowDom = (cmpMeta.$flags$ & 1 /* shadowDomEncapsulation */) !== 0;
781 // always reset
782 checkSlotFallbackVisibility = false;
783 }
784 // synchronous patch
785 patch(oldVNode, rootVnode);
786 {
787 // while we're moving nodes around existing nodes, temporarily disable
788 // the disconnectCallback from working
789 plt.$flags$ |= 1 /* isTmpDisconnected */;
790 if (checkSlotRelocate) {
791 relocateSlotContent(rootVnode.$elm$);
792 let relocateData;
793 let nodeToRelocate;
794 let orgLocationNode;
795 let parentNodeRef;
796 let insertBeforeNode;
797 let refNode;
798 let i = 0;
799 for (; i < relocateNodes.length; i++) {
800 relocateData = relocateNodes[i];
801 nodeToRelocate = relocateData.$nodeToRelocate$;
802 if (!nodeToRelocate['s-ol']) {
803 // add a reference node marking this node's original location
804 // keep a reference to this node for later lookups
805 orgLocationNode =
806 doc.createTextNode('');
807 orgLocationNode['s-nr'] = nodeToRelocate;
808 nodeToRelocate.parentNode.insertBefore((nodeToRelocate['s-ol'] = orgLocationNode), nodeToRelocate);
809 }
810 }
811 for (i = 0; i < relocateNodes.length; i++) {
812 relocateData = relocateNodes[i];
813 nodeToRelocate = relocateData.$nodeToRelocate$;
814 if (relocateData.$slotRefNode$) {
815 // by default we're just going to insert it directly
816 // after the slot reference node
817 parentNodeRef = relocateData.$slotRefNode$.parentNode;
818 insertBeforeNode = relocateData.$slotRefNode$.nextSibling;
819 orgLocationNode = nodeToRelocate['s-ol'];
820 while ((orgLocationNode = orgLocationNode.previousSibling)) {
821 refNode = orgLocationNode['s-nr'];
822 if (refNode && refNode['s-sn'] === nodeToRelocate['s-sn'] && parentNodeRef === refNode.parentNode) {
823 refNode = refNode.nextSibling;
824 if (!refNode || !refNode['s-nr']) {
825 insertBeforeNode = refNode;
826 break;
827 }
828 }
829 }
830 if ((!insertBeforeNode && parentNodeRef !== nodeToRelocate.parentNode) ||
831 nodeToRelocate.nextSibling !== insertBeforeNode) {
832 // we've checked that it's worth while to relocate
833 // since that the node to relocate
834 // has a different next sibling or parent relocated
835 if (nodeToRelocate !== insertBeforeNode) {
836 if (!nodeToRelocate['s-hn'] && nodeToRelocate['s-ol']) {
837 // probably a component in the index.html that doesn't have it's hostname set
838 nodeToRelocate['s-hn'] = nodeToRelocate['s-ol'].parentNode.nodeName;
839 }
840 // add it back to the dom but in its new home
841 parentNodeRef.insertBefore(nodeToRelocate, insertBeforeNode);
842 }
843 }
844 }
845 else {
846 // this node doesn't have a slot home to go to, so let's hide it
847 if (nodeToRelocate.nodeType === 1 /* ElementNode */) {
848 nodeToRelocate.hidden = true;
849 }
850 }
851 }
852 }
853 if (checkSlotFallbackVisibility) {
854 updateFallbackSlotVisibility(rootVnode.$elm$);
855 }
856 // done moving nodes around
857 // allow the disconnect callback to work again
858 plt.$flags$ &= ~1 /* isTmpDisconnected */;
859 // always reset
860 relocateNodes.length = 0;
861 }
862};
863/**
864 * Helper function to create & dispatch a custom Event on a provided target
865 * @param elm the target of the Event
866 * @param name the name to give the custom Event
867 * @param opts options for configuring a custom Event
868 * @returns the custom Event
869 */
870const emitEvent = (elm, name, opts) => {
871 const ev = plt.ce(name, opts);
872 elm.dispatchEvent(ev);
873 return ev;
874};
875const attachToAncestor = (hostRef, ancestorComponent) => {
876 if (ancestorComponent && !hostRef.$onRenderResolve$ && ancestorComponent['s-p']) {
877 ancestorComponent['s-p'].push(new Promise((r) => (hostRef.$onRenderResolve$ = r)));
878 }
879};
880const scheduleUpdate = (hostRef, isInitialLoad) => {
881 {
882 hostRef.$flags$ |= 16 /* isQueuedForUpdate */;
883 }
884 if (hostRef.$flags$ & 4 /* isWaitingForChildren */) {
885 hostRef.$flags$ |= 512 /* needsRerender */;
886 return;
887 }
888 attachToAncestor(hostRef, hostRef.$ancestorComponent$);
889 // there is no ancestor component or the ancestor component
890 // has already fired off its lifecycle update then
891 // fire off the initial update
892 const dispatch = () => dispatchHooks(hostRef, isInitialLoad);
893 return writeTask(dispatch) ;
894};
895const dispatchHooks = (hostRef, isInitialLoad) => {
896 const endSchedule = createTime('scheduleUpdate', hostRef.$cmpMeta$.$tagName$);
897 const instance = hostRef.$lazyInstance$ ;
898 let promise;
899 endSchedule();
900 return then(promise, () => updateComponent(hostRef, instance, isInitialLoad));
901};
902const updateComponent = async (hostRef, instance, isInitialLoad) => {
903 // updateComponent
904 const elm = hostRef.$hostElement$;
905 const endUpdate = createTime('update', hostRef.$cmpMeta$.$tagName$);
906 const rc = elm['s-rc'];
907 if (isInitialLoad) {
908 // DOM WRITE!
909 attachStyles(hostRef);
910 }
911 const endRender = createTime('render', hostRef.$cmpMeta$.$tagName$);
912 {
913 callRender(hostRef, instance);
914 }
915 if (rc) {
916 // ok, so turns out there are some child host elements
917 // waiting on this parent element to load
918 // let's fire off all update callbacks waiting
919 rc.map((cb) => cb());
920 elm['s-rc'] = undefined;
921 }
922 endRender();
923 endUpdate();
924 {
925 const childrenPromises = elm['s-p'];
926 const postUpdate = () => postUpdateComponent(hostRef);
927 if (childrenPromises.length === 0) {
928 postUpdate();
929 }
930 else {
931 Promise.all(childrenPromises).then(postUpdate);
932 hostRef.$flags$ |= 4 /* isWaitingForChildren */;
933 childrenPromises.length = 0;
934 }
935 }
936};
937const callRender = (hostRef, instance, elm) => {
938 try {
939 instance = instance.render() ;
940 {
941 hostRef.$flags$ &= ~16 /* isQueuedForUpdate */;
942 }
943 {
944 hostRef.$flags$ |= 2 /* hasRendered */;
945 }
946 {
947 {
948 // looks like we've got child nodes to render into this host element
949 // or we need to update the css class/attrs on the host element
950 // DOM WRITE!
951 {
952 renderVdom(hostRef, instance);
953 }
954 }
955 }
956 }
957 catch (e) {
958 consoleError(e, hostRef.$hostElement$);
959 }
960 return null;
961};
962const postUpdateComponent = (hostRef) => {
963 const tagName = hostRef.$cmpMeta$.$tagName$;
964 const elm = hostRef.$hostElement$;
965 const endPostUpdate = createTime('postUpdate', tagName);
966 const ancestorComponent = hostRef.$ancestorComponent$;
967 if (!(hostRef.$flags$ & 64 /* hasLoadedComponent */)) {
968 hostRef.$flags$ |= 64 /* hasLoadedComponent */;
969 {
970 // DOM WRITE!
971 addHydratedFlag(elm);
972 }
973 endPostUpdate();
974 {
975 hostRef.$onReadyResolve$(elm);
976 if (!ancestorComponent) {
977 appDidLoad();
978 }
979 }
980 }
981 else {
982 endPostUpdate();
983 }
984 // load events fire from bottom to top
985 // the deepest elements load first then bubbles up
986 {
987 if (hostRef.$onRenderResolve$) {
988 hostRef.$onRenderResolve$();
989 hostRef.$onRenderResolve$ = undefined;
990 }
991 if (hostRef.$flags$ & 512 /* needsRerender */) {
992 nextTick(() => scheduleUpdate(hostRef, false));
993 }
994 hostRef.$flags$ &= ~(4 /* isWaitingForChildren */ | 512 /* needsRerender */);
995 }
996 // ( •_•)
997 // ( •_•)>⌐■-■
998 // (⌐■_■)
999};
1000const appDidLoad = (who) => {
1001 // on appload
1002 // we have finish the first big initial render
1003 {
1004 addHydratedFlag(doc.documentElement);
1005 }
1006 nextTick(() => emitEvent(win, 'appload', { detail: { namespace: NAMESPACE } }));
1007};
1008const then = (promise, thenFn) => {
1009 return promise && promise.then ? promise.then(thenFn) : thenFn();
1010};
1011const addHydratedFlag = (elm) => elm.classList.add('hydrated')
1012 ;
1013const initializeClientHydrate = (hostElm, tagName, hostId, hostRef) => {
1014 const endHydrate = createTime('hydrateClient', tagName);
1015 const shadowRoot = hostElm.shadowRoot;
1016 const childRenderNodes = [];
1017 const slotNodes = [];
1018 const shadowRootNodes = null;
1019 const vnode = (hostRef.$vnode$ = newVNode(tagName, null));
1020 if (!plt.$orgLocNodes$) {
1021 initializeDocumentHydrate(doc.body, (plt.$orgLocNodes$ = new Map()));
1022 }
1023 hostElm[HYDRATE_ID] = hostId;
1024 hostElm.removeAttribute(HYDRATE_ID);
1025 clientHydrate(vnode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, hostElm, hostId);
1026 childRenderNodes.map((c) => {
1027 const orgLocationId = c.$hostId$ + '.' + c.$nodeId$;
1028 const orgLocationNode = plt.$orgLocNodes$.get(orgLocationId);
1029 const node = c.$elm$;
1030 if (orgLocationNode && supportsShadow && orgLocationNode['s-en'] === '') {
1031 orgLocationNode.parentNode.insertBefore(node, orgLocationNode.nextSibling);
1032 }
1033 if (!shadowRoot) {
1034 node['s-hn'] = tagName;
1035 if (orgLocationNode) {
1036 node['s-ol'] = orgLocationNode;
1037 node['s-ol']['s-nr'] = node;
1038 }
1039 }
1040 plt.$orgLocNodes$.delete(orgLocationId);
1041 });
1042 endHydrate();
1043};
1044const clientHydrate = (parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node, hostId) => {
1045 let childNodeType;
1046 let childIdSplt;
1047 let childVNode;
1048 let i;
1049 if (node.nodeType === 1 /* ElementNode */) {
1050 childNodeType = node.getAttribute(HYDRATE_CHILD_ID);
1051 if (childNodeType) {
1052 // got the node data from the element's attribute
1053 // `${hostId}.${nodeId}.${depth}.${index}`
1054 childIdSplt = childNodeType.split('.');
1055 if (childIdSplt[0] === hostId || childIdSplt[0] === '0') {
1056 childVNode = {
1057 $flags$: 0,
1058 $hostId$: childIdSplt[0],
1059 $nodeId$: childIdSplt[1],
1060 $depth$: childIdSplt[2],
1061 $index$: childIdSplt[3],
1062 $tag$: node.tagName.toLowerCase(),
1063 $elm$: node,
1064 $attrs$: null,
1065 $children$: null,
1066 $key$: null,
1067 $name$: null,
1068 $text$: null,
1069 };
1070 childRenderNodes.push(childVNode);
1071 node.removeAttribute(HYDRATE_CHILD_ID);
1072 // this is a new child vnode
1073 // so ensure its parent vnode has the vchildren array
1074 if (!parentVNode.$children$) {
1075 parentVNode.$children$ = [];
1076 }
1077 // add our child vnode to a specific index of the vnode's children
1078 parentVNode.$children$[childVNode.$index$] = childVNode;
1079 // this is now the new parent vnode for all the next child checks
1080 parentVNode = childVNode;
1081 if (shadowRootNodes && childVNode.$depth$ === '0') {
1082 shadowRootNodes[childVNode.$index$] = childVNode.$elm$;
1083 }
1084 }
1085 }
1086 // recursively drill down, end to start so we can remove nodes
1087 for (i = node.childNodes.length - 1; i >= 0; i--) {
1088 clientHydrate(parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node.childNodes[i], hostId);
1089 }
1090 if (node.shadowRoot) {
1091 // keep drilling down through the shadow root nodes
1092 for (i = node.shadowRoot.childNodes.length - 1; i >= 0; i--) {
1093 clientHydrate(parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node.shadowRoot.childNodes[i], hostId);
1094 }
1095 }
1096 }
1097 else if (node.nodeType === 8 /* CommentNode */) {
1098 // `${COMMENT_TYPE}.${hostId}.${nodeId}.${depth}.${index}`
1099 childIdSplt = node.nodeValue.split('.');
1100 if (childIdSplt[1] === hostId || childIdSplt[1] === '0') {
1101 // comment node for either the host id or a 0 host id
1102 childNodeType = childIdSplt[0];
1103 childVNode = {
1104 $flags$: 0,
1105 $hostId$: childIdSplt[1],
1106 $nodeId$: childIdSplt[2],
1107 $depth$: childIdSplt[3],
1108 $index$: childIdSplt[4],
1109 $elm$: node,
1110 $attrs$: null,
1111 $children$: null,
1112 $key$: null,
1113 $name$: null,
1114 $tag$: null,
1115 $text$: null,
1116 };
1117 if (childNodeType === TEXT_NODE_ID) {
1118 childVNode.$elm$ = node.nextSibling;
1119 if (childVNode.$elm$ && childVNode.$elm$.nodeType === 3 /* TextNode */) {
1120 childVNode.$text$ = childVNode.$elm$.textContent;
1121 childRenderNodes.push(childVNode);
1122 // remove the text comment since it's no longer needed
1123 node.remove();
1124 if (!parentVNode.$children$) {
1125 parentVNode.$children$ = [];
1126 }
1127 parentVNode.$children$[childVNode.$index$] = childVNode;
1128 if (shadowRootNodes && childVNode.$depth$ === '0') {
1129 shadowRootNodes[childVNode.$index$] = childVNode.$elm$;
1130 }
1131 }
1132 }
1133 else if (childVNode.$hostId$ === hostId) {
1134 // this comment node is specifcally for this host id
1135 if (childNodeType === SLOT_NODE_ID) {
1136 // `${SLOT_NODE_ID}.${hostId}.${nodeId}.${depth}.${index}.${slotName}`;
1137 childVNode.$tag$ = 'slot';
1138 if (childIdSplt[5]) {
1139 node['s-sn'] = childVNode.$name$ = childIdSplt[5];
1140 }
1141 else {
1142 node['s-sn'] = '';
1143 }
1144 node['s-sr'] = true;
1145 slotNodes.push(childVNode);
1146 if (!parentVNode.$children$) {
1147 parentVNode.$children$ = [];
1148 }
1149 parentVNode.$children$[childVNode.$index$] = childVNode;
1150 }
1151 else if (childNodeType === CONTENT_REF_ID) {
1152 // `${CONTENT_REF_ID}.${hostId}`;
1153 {
1154 hostElm['s-cr'] = node;
1155 node['s-cn'] = true;
1156 }
1157 }
1158 }
1159 }
1160 }
1161 else if (parentVNode && parentVNode.$tag$ === 'style') {
1162 const vnode = newVNode(null, node.textContent);
1163 vnode.$elm$ = node;
1164 vnode.$index$ = '0';
1165 parentVNode.$children$ = [vnode];
1166 }
1167};
1168const initializeDocumentHydrate = (node, orgLocNodes) => {
1169 if (node.nodeType === 1 /* ElementNode */) {
1170 let i = 0;
1171 for (; i < node.childNodes.length; i++) {
1172 initializeDocumentHydrate(node.childNodes[i], orgLocNodes);
1173 }
1174 if (node.shadowRoot) {
1175 for (i = 0; i < node.shadowRoot.childNodes.length; i++) {
1176 initializeDocumentHydrate(node.shadowRoot.childNodes[i], orgLocNodes);
1177 }
1178 }
1179 }
1180 else if (node.nodeType === 8 /* CommentNode */) {
1181 const childIdSplt = node.nodeValue.split('.');
1182 if (childIdSplt[0] === ORG_LOCATION_ID) {
1183 orgLocNodes.set(childIdSplt[1] + '.' + childIdSplt[2], node);
1184 node.nodeValue = '';
1185 // useful to know if the original location is
1186 // the root light-dom of a shadow dom component
1187 node['s-en'] = childIdSplt[3];
1188 }
1189 }
1190};
1191const parsePropertyValue = (propValue, propType) => {
1192 // ensure this value is of the correct prop type
1193 if (propValue != null && !isComplexType(propValue)) {
1194 if (propType & 4 /* Boolean */) {
1195 // per the HTML spec, any string value means it is a boolean true value
1196 // but we'll cheat here and say that the string "false" is the boolean false
1197 return propValue === 'false' ? false : propValue === '' || !!propValue;
1198 }
1199 if (propType & 2 /* Number */) {
1200 // force it to be a number
1201 return parseFloat(propValue);
1202 }
1203 if (propType & 1 /* String */) {
1204 // could have been passed as a number or boolean
1205 // but we still want it as a string
1206 return String(propValue);
1207 }
1208 // redundant return here for better minification
1209 return propValue;
1210 }
1211 // not sure exactly what type we want
1212 // so no need to change to a different type
1213 return propValue;
1214};
1215const getValue = (ref, propName) => getHostRef(ref).$instanceValues$.get(propName);
1216const setValue = (ref, propName, newVal, cmpMeta) => {
1217 // check our new property value against our internal value
1218 const hostRef = getHostRef(ref);
1219 const oldVal = hostRef.$instanceValues$.get(propName);
1220 const flags = hostRef.$flags$;
1221 const instance = hostRef.$lazyInstance$ ;
1222 newVal = parsePropertyValue(newVal, cmpMeta.$members$[propName][0]);
1223 if ((!(flags & 8 /* isConstructingInstance */) || oldVal === undefined) && newVal !== oldVal) {
1224 // gadzooks! the property's value has changed!!
1225 // set our new value!
1226 hostRef.$instanceValues$.set(propName, newVal);
1227 if (instance) {
1228 if ((flags & (2 /* hasRendered */ | 16 /* isQueuedForUpdate */)) === 2 /* hasRendered */) {
1229 // looks like this value actually changed, so we've got work to do!
1230 // but only if we've already rendered, otherwise just chill out
1231 // queue that we need to do an update, but don't worry about queuing
1232 // up millions cuz this function ensures it only runs once
1233 scheduleUpdate(hostRef, false);
1234 }
1235 }
1236 }
1237};
1238const proxyComponent = (Cstr, cmpMeta, flags) => {
1239 if (cmpMeta.$members$) {
1240 // It's better to have a const than two Object.entries()
1241 const members = Object.entries(cmpMeta.$members$);
1242 const prototype = Cstr.prototype;
1243 members.map(([memberName, [memberFlags]]) => {
1244 if ((memberFlags & 31 /* Prop */ ||
1245 ((flags & 2 /* proxyState */) && memberFlags & 32 /* State */))) {
1246 // proxyComponent - prop
1247 Object.defineProperty(prototype, memberName, {
1248 get() {
1249 // proxyComponent, get value
1250 return getValue(this, memberName);
1251 },
1252 set(newValue) {
1253 // proxyComponent, set value
1254 setValue(this, memberName, newValue, cmpMeta);
1255 },
1256 configurable: true,
1257 enumerable: true,
1258 });
1259 }
1260 });
1261 if ((flags & 1 /* isElementConstructor */)) {
1262 const attrNameToPropName = new Map();
1263 prototype.attributeChangedCallback = function (attrName, _oldValue, newValue) {
1264 plt.jmp(() => {
1265 const propName = attrNameToPropName.get(attrName);
1266 // In a web component lifecycle the attributeChangedCallback runs prior to connectedCallback
1267 // in the case where an attribute was set inline.
1268 // ```html
1269 // <my-component some-attribute="some-value"></my-component>
1270 // ```
1271 //
1272 // There is an edge case where a developer sets the attribute inline on a custom element and then
1273 // programmatically changes it before it has been upgraded as shown below:
1274 //
1275 // ```html
1276 // <!-- this component has _not_ been upgraded yet -->
1277 // <my-component id="test" some-attribute="some-value"></my-component>
1278 // <script>
1279 // // grab non-upgraded component
1280 // el = document.querySelector("#test");
1281 // el.someAttribute = "another-value";
1282 // // upgrade component
1283 // customElements.define('my-component', MyComponent);
1284 // </script>
1285 // ```
1286 // In this case if we do not unshadow here and use the value of the shadowing property, attributeChangedCallback
1287 // will be called with `newValue = "some-value"` and will set the shadowed property (this.someAttribute = "another-value")
1288 // to the value that was set inline i.e. "some-value" from above example. When
1289 // the connectedCallback attempts to unshadow it will use "some-value" as the initial value rather than "another-value"
1290 //
1291 // The case where the attribute was NOT set inline but was not set programmatically shall be handled/unshadowed
1292 // by connectedCallback as this attributeChangedCallback will not fire.
1293 //
1294 // https://developers.google.com/web/fundamentals/web-components/best-practices#lazy-properties
1295 //
1296 // TODO(STENCIL-16) we should think about whether or not we actually want to be reflecting the attributes to
1297 // properties here given that this goes against best practices outlined here
1298 // https://developers.google.com/web/fundamentals/web-components/best-practices#avoid-reentrancy
1299 if (this.hasOwnProperty(propName)) {
1300 newValue = this[propName];
1301 delete this[propName];
1302 }
1303 else if (prototype.hasOwnProperty(propName) &&
1304 typeof this[propName] === 'number' &&
1305 this[propName] == newValue) {
1306 // if the propName exists on the prototype of `Cstr`, this update may be a result of Stencil using native
1307 // APIs to reflect props as attributes. Calls to `setAttribute(someElement, propName)` will result in
1308 // `propName` to be converted to a `DOMString`, which may not be what we want for other primitive props.
1309 return;
1310 }
1311 this[propName] = newValue === null && typeof this[propName] === 'boolean' ? false : newValue;
1312 });
1313 };
1314 // create an array of attributes to observe
1315 // and also create a map of html attribute name to js property name
1316 Cstr.observedAttributes = members
1317 .filter(([_, m]) => m[0] & 15 /* HasAttribute */) // filter to only keep props that should match attributes
1318 .map(([propName, m]) => {
1319 const attrName = m[1] || propName;
1320 attrNameToPropName.set(attrName, propName);
1321 if (m[0] & 512 /* ReflectAttr */) {
1322 cmpMeta.$attrsToReflect$.push([propName, attrName]);
1323 }
1324 return attrName;
1325 });
1326 }
1327 }
1328 return Cstr;
1329};
1330const initializeComponent = async (elm, hostRef, cmpMeta, hmrVersionId, Cstr) => {
1331 // initializeComponent
1332 if ((hostRef.$flags$ & 32 /* hasInitializedComponent */) === 0) {
1333 {
1334 // we haven't initialized this element yet
1335 hostRef.$flags$ |= 32 /* hasInitializedComponent */;
1336 // lazy loaded components
1337 // request the component's implementation to be
1338 // wired up with the host element
1339 Cstr = loadModule(cmpMeta);
1340 if (Cstr.then) {
1341 // Await creates a micro-task avoid if possible
1342 const endLoad = uniqueTime();
1343 Cstr = await Cstr;
1344 endLoad();
1345 }
1346 if (!Cstr.isProxied) {
1347 proxyComponent(Cstr, cmpMeta, 2 /* proxyState */);
1348 Cstr.isProxied = true;
1349 }
1350 const endNewInstance = createTime('createInstance', cmpMeta.$tagName$);
1351 // ok, time to construct the instance
1352 // but let's keep track of when we start and stop
1353 // so that the getters/setters don't incorrectly step on data
1354 {
1355 hostRef.$flags$ |= 8 /* isConstructingInstance */;
1356 }
1357 // construct the lazy-loaded component implementation
1358 // passing the hostRef is very important during
1359 // construction in order to directly wire together the
1360 // host element and the lazy-loaded instance
1361 try {
1362 new Cstr(hostRef);
1363 }
1364 catch (e) {
1365 consoleError(e);
1366 }
1367 {
1368 hostRef.$flags$ &= ~8 /* isConstructingInstance */;
1369 }
1370 endNewInstance();
1371 }
1372 if (Cstr.style) {
1373 // this component has styles but we haven't registered them yet
1374 let style = Cstr.style;
1375 const scopeId = getScopeId(cmpMeta);
1376 if (!styles.has(scopeId)) {
1377 const endRegisterStyles = createTime('registerStyles', cmpMeta.$tagName$);
1378 registerStyle(scopeId, style, !!(cmpMeta.$flags$ & 1 /* shadowDomEncapsulation */));
1379 endRegisterStyles();
1380 }
1381 }
1382 }
1383 // we've successfully created a lazy instance
1384 const ancestorComponent = hostRef.$ancestorComponent$;
1385 const schedule = () => scheduleUpdate(hostRef, true);
1386 if (ancestorComponent && ancestorComponent['s-rc']) {
1387 // this is the initial load and this component it has an ancestor component
1388 // but the ancestor component has NOT fired its will update lifecycle yet
1389 // so let's just cool our jets and wait for the ancestor to continue first
1390 // this will get fired off when the ancestor component
1391 // finally gets around to rendering its lazy self
1392 // fire off the initial update
1393 ancestorComponent['s-rc'].push(schedule);
1394 }
1395 else {
1396 schedule();
1397 }
1398};
1399const connectedCallback = (elm) => {
1400 if ((plt.$flags$ & 1 /* isTmpDisconnected */) === 0) {
1401 const hostRef = getHostRef(elm);
1402 const cmpMeta = hostRef.$cmpMeta$;
1403 const endConnected = createTime('connectedCallback', cmpMeta.$tagName$);
1404 if (!(hostRef.$flags$ & 1 /* hasConnected */)) {
1405 // first time this component has connected
1406 hostRef.$flags$ |= 1 /* hasConnected */;
1407 let hostId;
1408 {
1409 hostId = elm.getAttribute(HYDRATE_ID);
1410 if (hostId) {
1411 initializeClientHydrate(elm, cmpMeta.$tagName$, hostId, hostRef);
1412 }
1413 }
1414 if (!hostId) {
1415 // initUpdate
1416 // if the slot polyfill is required we'll need to put some nodes
1417 // in here to act as original content anchors as we move nodes around
1418 // host element has been connected to the DOM
1419 if ((cmpMeta.$flags$ & (4 /* hasSlotRelocation */ | 8 /* needsShadowDomShim */))) {
1420 setContentReference(elm);
1421 }
1422 }
1423 {
1424 // find the first ancestor component (if there is one) and register
1425 // this component as one of the actively loading child components for its ancestor
1426 let ancestorComponent = elm;
1427 while ((ancestorComponent = ancestorComponent.parentNode || ancestorComponent.host)) {
1428 // climb up the ancestors looking for the first
1429 // component that hasn't finished its lifecycle update yet
1430 if ((ancestorComponent.nodeType === 1 /* ElementNode */ &&
1431 ancestorComponent.hasAttribute('s-id') &&
1432 ancestorComponent['s-p']) ||
1433 ancestorComponent['s-p']) {
1434 // we found this components first ancestor component
1435 // keep a reference to this component's ancestor component
1436 attachToAncestor(hostRef, (hostRef.$ancestorComponent$ = ancestorComponent));
1437 break;
1438 }
1439 }
1440 }
1441 // Lazy properties
1442 // https://developers.google.com/web/fundamentals/web-components/best-practices#lazy-properties
1443 if (cmpMeta.$members$) {
1444 Object.entries(cmpMeta.$members$).map(([memberName, [memberFlags]]) => {
1445 if (memberFlags & 31 /* Prop */ && elm.hasOwnProperty(memberName)) {
1446 const value = elm[memberName];
1447 delete elm[memberName];
1448 elm[memberName] = value;
1449 }
1450 });
1451 }
1452 {
1453 initializeComponent(elm, hostRef, cmpMeta);
1454 }
1455 }
1456 endConnected();
1457 }
1458};
1459const setContentReference = (elm) => {
1460 // only required when we're NOT using native shadow dom (slot)
1461 // or this browser doesn't support native shadow dom
1462 // and this host element was NOT created with SSR
1463 // let's pick out the inner content for slot projection
1464 // create a node to represent where the original
1465 // content was first placed, which is useful later on
1466 const contentRefElm = (elm['s-cr'] = doc.createComment(''));
1467 contentRefElm['s-cn'] = true;
1468 elm.insertBefore(contentRefElm, elm.firstChild);
1469};
1470const disconnectedCallback = (elm) => {
1471 if ((plt.$flags$ & 1 /* isTmpDisconnected */) === 0) {
1472 getHostRef(elm);
1473 }
1474};
1475const bootstrapLazy = (lazyBundles, options = {}) => {
1476 const endBootstrap = createTime();
1477 const cmpTags = [];
1478 const exclude = options.exclude || [];
1479 const customElements = win.customElements;
1480 const head = doc.head;
1481 const metaCharset = /*@__PURE__*/ head.querySelector('meta[charset]');
1482 const visibilityStyle = /*@__PURE__*/ doc.createElement('style');
1483 const deferredConnectedCallbacks = [];
1484 let appLoadFallback;
1485 let isBootstrapping = true;
1486 Object.assign(plt, options);
1487 plt.$resourcesUrl$ = new URL(options.resourcesUrl || './', doc.baseURI).href;
1488 {
1489 // If the app is already hydrated there is not point to disable the
1490 // async queue. This will improve the first input delay
1491 plt.$flags$ |= 2 /* appLoaded */;
1492 }
1493 lazyBundles.map((lazyBundle) => {
1494 lazyBundle[1].map((compactMeta) => {
1495 const cmpMeta = {
1496 $flags$: compactMeta[0],
1497 $tagName$: compactMeta[1],
1498 $members$: compactMeta[2],
1499 $listeners$: compactMeta[3],
1500 };
1501 {
1502 cmpMeta.$members$ = compactMeta[2];
1503 }
1504 {
1505 cmpMeta.$attrsToReflect$ = [];
1506 }
1507 const tagName = cmpMeta.$tagName$;
1508 const HostElement = class extends HTMLElement {
1509 // StencilLazyHost
1510 constructor(self) {
1511 // @ts-ignore
1512 super(self);
1513 self = this;
1514 registerHost(self, cmpMeta);
1515 }
1516 connectedCallback() {
1517 if (appLoadFallback) {
1518 clearTimeout(appLoadFallback);
1519 appLoadFallback = null;
1520 }
1521 if (isBootstrapping) {
1522 // connectedCallback will be processed once all components have been registered
1523 deferredConnectedCallbacks.push(this);
1524 }
1525 else {
1526 plt.jmp(() => connectedCallback(this));
1527 }
1528 }
1529 disconnectedCallback() {
1530 plt.jmp(() => disconnectedCallback(this));
1531 }
1532 componentOnReady() {
1533 return getHostRef(this).$onReadyPromise$;
1534 }
1535 };
1536 cmpMeta.$lazyBundleId$ = lazyBundle[0];
1537 if (!exclude.includes(tagName) && !customElements.get(tagName)) {
1538 cmpTags.push(tagName);
1539 customElements.define(tagName, proxyComponent(HostElement, cmpMeta, 1 /* isElementConstructor */));
1540 }
1541 });
1542 });
1543 {
1544 visibilityStyle.innerHTML = cmpTags + HYDRATED_CSS;
1545 visibilityStyle.setAttribute('data-styles', '');
1546 head.insertBefore(visibilityStyle, metaCharset ? metaCharset.nextSibling : head.firstChild);
1547 }
1548 // Process deferred connectedCallbacks now all components have been registered
1549 isBootstrapping = false;
1550 if (deferredConnectedCallbacks.length) {
1551 deferredConnectedCallbacks.map((host) => host.connectedCallback());
1552 }
1553 else {
1554 {
1555 plt.jmp(() => (appLoadFallback = setTimeout(appDidLoad, 30)));
1556 }
1557 }
1558 // Fallback appLoad event
1559 endBootstrap();
1560};
1561const hostRefs = new WeakMap();
1562const getHostRef = (ref) => hostRefs.get(ref);
1563const registerInstance = (lazyInstance, hostRef) => hostRefs.set((hostRef.$lazyInstance$ = lazyInstance), hostRef);
1564const registerHost = (elm, cmpMeta) => {
1565 const hostRef = {
1566 $flags$: 0,
1567 $hostElement$: elm,
1568 $cmpMeta$: cmpMeta,
1569 $instanceValues$: new Map(),
1570 };
1571 {
1572 hostRef.$onReadyPromise$ = new Promise((r) => (hostRef.$onReadyResolve$ = r));
1573 elm['s-p'] = [];
1574 elm['s-rc'] = [];
1575 }
1576 return hostRefs.set(elm, hostRef);
1577};
1578const isMemberInElement = (elm, memberName) => memberName in elm;
1579const consoleError = (e, el) => (0, console.error)(e, el);
1580const cmpModules = /*@__PURE__*/ new Map();
1581const loadModule = (cmpMeta, hostRef, hmrVersionId) => {
1582 // loadModuleImport
1583 const exportName = cmpMeta.$tagName$.replace(/-/g, '_');
1584 const bundleId = cmpMeta.$lazyBundleId$;
1585 const module = cmpModules.get(bundleId) ;
1586 if (module) {
1587 return module[exportName];
1588 }
1589 return import(
1590 /* webpackInclude: /\.entry\.js$/ */
1591 /* webpackExclude: /\.system\.entry\.js$/ */
1592 /* webpackMode: "lazy" */
1593 `./${bundleId}.entry.js${''}`).then((importedModule) => {
1594 {
1595 cmpModules.set(bundleId, importedModule);
1596 }
1597 return importedModule[exportName];
1598 }, consoleError);
1599};
1600const styles = new Map();
1601const queueDomReads = [];
1602const queueDomWrites = [];
1603const queueTask = (queue, write) => (cb) => {
1604 queue.push(cb);
1605 if (!queuePending) {
1606 queuePending = true;
1607 if (write && plt.$flags$ & 4 /* queueSync */) {
1608 nextTick(flush);
1609 }
1610 else {
1611 plt.raf(flush);
1612 }
1613 }
1614};
1615const consume = (queue) => {
1616 for (let i = 0; i < queue.length; i++) {
1617 try {
1618 queue[i](performance.now());
1619 }
1620 catch (e) {
1621 consoleError(e);
1622 }
1623 }
1624 queue.length = 0;
1625};
1626const flush = () => {
1627 // always force a bunch of medium callbacks to run, but still have
1628 // a throttle on how many can run in a certain time
1629 // DOM READS!!!
1630 consume(queueDomReads);
1631 // DOM WRITES!!!
1632 {
1633 consume(queueDomWrites);
1634 if ((queuePending = queueDomReads.length > 0)) {
1635 // still more to do yet, but we've run out of time
1636 // let's let this thing cool off and try again in the next tick
1637 plt.raf(flush);
1638 }
1639 }
1640};
1641const nextTick = /*@__PURE__*/ (cb) => promiseResolve().then(cb);
1642const writeTask = /*@__PURE__*/ queueTask(queueDomWrites, true);
1643
1644export { Host as H, bootstrapLazy as b, h, promiseResolve as p, registerInstance as r };