|
1 | 1 | import type * as d from '../declarations';
|
2 | 2 | import { BUILD } from '@app-data';
|
| 3 | +import { NODE_TYPES } from '@stencil/core/mock-doc'; |
3 | 4 | import { CMP_FLAGS, HOST_FLAGS } from '@utils';
|
4 | 5 | import { PLATFORM_FLAGS } from './runtime-constants';
|
5 | 6 | import { plt, supportsShadow, getHostRef } from '@platform';
|
@@ -63,6 +64,60 @@ export const patchSlotAppendChild = (HostElementPrototype: any) => {
|
63 | 64 | };
|
64 | 65 | };
|
65 | 66 |
|
| 67 | +/** |
| 68 | + * Patches the text content of an unnamed slotted node inside a scoped component |
| 69 | + * @param hostElementPrototype the `Element` to be patched |
| 70 | + * @param cmpMeta component runtime metadata used to determine if the component should be patched or not |
| 71 | + */ |
| 72 | +export const patchTextContent = (hostElementPrototype: HTMLElement, cmpMeta: d.ComponentRuntimeMeta): void => { |
| 73 | + if (BUILD.scoped && cmpMeta.$flags$ & CMP_FLAGS.scopedCssEncapsulation) { |
| 74 | + const descriptor = Object.getOwnPropertyDescriptor(Node.prototype, 'textContent'); |
| 75 | + |
| 76 | + Object.defineProperty(hostElementPrototype, '__textContent', descriptor); |
| 77 | + |
| 78 | + Object.defineProperty(hostElementPrototype, 'textContent', { |
| 79 | + get(): string | null { |
| 80 | + // get the 'default slot', which would be the first slot in a shadow tree (if we were using one), whose name is |
| 81 | + // the empty string |
| 82 | + const slotNode = getHostSlotNode(this.childNodes, ''); |
| 83 | + // when a slot node is found, the textContent _may_ be found in the next sibling (text) node, depending on how |
| 84 | + // nodes were reordered during the vdom render. first try to get the text content from the sibling. |
| 85 | + if (slotNode?.nextSibling?.nodeType === NODE_TYPES.TEXT_NODE) { |
| 86 | + return slotNode.nextSibling.textContent; |
| 87 | + } else if (slotNode) { |
| 88 | + return slotNode.textContent; |
| 89 | + } else { |
| 90 | + // fallback to the original implementation |
| 91 | + return this.__textContent; |
| 92 | + } |
| 93 | + }, |
| 94 | + |
| 95 | + set(value: string | null) { |
| 96 | + // get the 'default slot', which would be the first slot in a shadow tree (if we were using one), whose name is |
| 97 | + // the empty string |
| 98 | + const slotNode = getHostSlotNode(this.childNodes, ''); |
| 99 | + // when a slot node is found, the textContent _may_ need to be placed in the next sibling (text) node, |
| 100 | + // depending on how nodes were reordered during the vdom render. first try to set the text content on the |
| 101 | + // sibling. |
| 102 | + if (slotNode?.nextSibling?.nodeType === NODE_TYPES.TEXT_NODE) { |
| 103 | + slotNode.nextSibling.textContent = value; |
| 104 | + } else if (slotNode) { |
| 105 | + slotNode.textContent = value; |
| 106 | + } else { |
| 107 | + // we couldn't find a slot, but that doesn't mean that there isn't one. if this check ran before the DOM |
| 108 | + // loaded, we could have missed it. check for a content reference element on the scoped component and insert |
| 109 | + // it there |
| 110 | + this.__textContent = value; |
| 111 | + const contentRefElm = this['s-cr']; |
| 112 | + if (contentRefElm) { |
| 113 | + this.insertBefore(contentRefElm, this.firstChild); |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + }); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
66 | 121 | export const patchChildSlotNodes = (elm: any, cmpMeta: d.ComponentRuntimeMeta) => {
|
67 | 122 | class FakeNodeList extends Array {
|
68 | 123 | item(n: number) {
|
@@ -109,6 +164,12 @@ export const patchChildSlotNodes = (elm: any, cmpMeta: d.ComponentRuntimeMeta) =
|
109 | 164 | const getSlotName = (node: d.RenderNode) =>
|
110 | 165 | node['s-sn'] || (node.nodeType === 1 && (node as Element).getAttribute('slot')) || '';
|
111 | 166 |
|
| 167 | +/** |
| 168 | + * Recursively searches a series of child nodes for a slot with the provided name. |
| 169 | + * @param childNodes the nodes to search for a slot with a specific name. |
| 170 | + * @param slotName the name of the slot to match on. |
| 171 | + * @returns a reference to the slot node that matches the provided name, `null` otherwise |
| 172 | + */ |
112 | 173 | const getHostSlotNode = (childNodes: NodeListOf<ChildNode>, slotName: string) => {
|
113 | 174 | let i = 0;
|
114 | 175 | let childNode: d.RenderNode;
|
|
0 commit comments