frosty 0.0.137 → 0.0.138
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dom.js +1 -1
- package/dist/dom.mjs +1 -1
- package/dist/internals/renderer-Cb5O2Zol.d.ts.map +1 -1
- package/dist/internals/{renderer-BAgpMuVn.js → renderer-Cz3yZBzr.js} +15 -12
- package/dist/internals/{renderer-BAgpMuVn.js.map → renderer-Cz3yZBzr.js.map} +1 -1
- package/dist/internals/{renderer-OcZGvm3-.mjs → renderer-DEgVaqmG.mjs} +15 -12
- package/dist/internals/{renderer-OcZGvm3-.mjs.map → renderer-DEgVaqmG.mjs.map} +1 -1
- package/dist/internals/renderer-KDTvueeU.d.mts.map +1 -1
- package/dist/server-dom.js +1 -1
- package/dist/server-dom.mjs +1 -1
- package/dist/web.js +1 -1
- package/dist/web.mjs +2 -2
- package/package.json +1 -1
package/dist/dom.js
CHANGED
package/dist/dom.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderer-Cb5O2Zol.d.ts","sources":["../../src/renderer/common/node.ts","../../src/renderer/common/renderer.ts"],"sourcesContent":["//\n// common.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport type { _DOMRenderer } from './renderer';\nimport { myersSync } from 'myers.js';\nimport { globalEvents } from '../../core/web/event';\nimport { NativeElementType } from '../../core/types/component';\nimport { svgProps, htmlProps } from '../../../generated/elements';\nimport { _propValue } from '../../core/web/props';\nimport { _Renderer } from '../../core/renderer';\n\nconst findPrototypeProperty = (object: any, propertyName: string) => {\n while (object && object.constructor && object.constructor.name !== 'Object') {\n let desc = Object.getOwnPropertyDescriptor(object, propertyName);\n if (desc) return desc;\n object = Object.getPrototypeOf(object);\n }\n return null;\n};\n\nconst isWriteable = (object: any, propertyName: string) => {\n let desc = findPrototypeProperty(object, propertyName);\n if (!desc) {\n return false;\n }\n if (desc.writable && typeof desc.value !== 'function') {\n return true;\n }\n return !!desc.set;\n};\n\nconst tracked_props = new WeakMap<Element, string[]>();\nconst tracked_listeners = new WeakMap<Element, Record<string, EventListener | undefined>>();\nconst _updateEventListener = (\n element: Element,\n key: string,\n listener: EventListener | undefined,\n) => {\n const event = key.endsWith('Capture') ? key.slice(2, -7).toLowerCase() : key.slice(2).toLowerCase();\n const listeners = tracked_listeners.get(element) ?? {};\n if (!tracked_listeners.has(element)) tracked_listeners.set(element, listeners);\n if (listeners[key] !== listener) {\n const options = { capture: key.endsWith('Capture') };\n if (_.isFunction(listeners[key])) element.removeEventListener(event, listeners[key], options);\n if (_.isFunction(listener)) element.addEventListener(event, listener, options);\n }\n listeners[key] = listener;\n}\n\nconst DOMUtils = new class {\n\n update(\n element: Element,\n { className, style, ...props }: Record<string, any> & {\n className?: string;\n style?: string;\n },\n ) {\n if (className) {\n if (element.className !== className)\n element.className = className;\n } else if (!_.isNil(element.getAttribute('class'))) {\n element.removeAttribute('class');\n }\n if (style) {\n const oldValue = element.getAttribute('style');\n if (oldValue !== style)\n element.setAttribute('style', style);\n } else if (!_.isNil(element.getAttribute('style'))) {\n element.removeAttribute('style');\n }\n for (const [key, value] of _.entries(props)) {\n if (_.includes(globalEvents, key)) {\n _updateEventListener(element, key, value);\n } else if (key.endsWith('Capture') && _.includes(globalEvents, key.slice(0, -7))) {\n _updateEventListener(element, key, value);\n } else if (key.startsWith('data-')) {\n const oldValue = element.getAttribute(key);\n if (value === false || _.isNil(value)) {\n if (!_.isNil(oldValue))\n element.removeAttribute(key);\n } else {\n const newValue = value === true ? '' : `${value}`;\n if (oldValue !== newValue)\n element.setAttribute(key, newValue);\n }\n } else {\n const tagName = _.toLower(element.tagName);\n const { type: _type, attr } = (htmlProps as any)['*'][key]\n ?? (htmlProps as any)[tagName]?.[key]\n ?? (svgProps as any)['*'][key]\n ?? (svgProps as any)[tagName]?.[key]\n ?? {};\n const tracked = tracked_props.get(element) ?? [];\n const writeable = isWriteable(element, key);\n if (!tracked_props.has(element)) tracked_props.set(element, tracked);\n const assigned = _.includes(tracked, key);\n if (writeable && !_.isNil(value)) {\n if (!assigned || (element as any)[key] !== value) (element as any)[key] = value;\n if (!assigned) tracked.push(key);\n } else if (_type && attr && (_propValue as any)[_type]) {\n const oldValue = element.getAttribute(attr);\n if (value === false || _.isNil(value)) {\n if (!_.isNil(oldValue))\n element.removeAttribute(attr);\n } else {\n const newValue = value === true ? '' : `${value}`;\n if (oldValue !== newValue)\n element.setAttribute(attr, newValue);\n }\n } else if (writeable) {\n if (!assigned || (element as any)[key] !== value) (element as any)[key] = value;\n if (!assigned) tracked.push(key);\n }\n }\n }\n }\n\n replaceChildren(\n element: Element,\n children: (string | ChildNode | DOMNativeNode)[],\n shouldRemove: (child: ChildNode) => boolean = () => true,\n ) {\n const document = element.ownerDocument;\n const diff = myersSync(\n _.map(element.childNodes, x => x.nodeType === document.TEXT_NODE ? x.textContent ?? '' : x),\n _.flatMap(children, x => x instanceof DOMNativeNode ? x.target : x),\n { compare: (a, b) => a === b },\n );\n let i = 0;\n for (const { remove, insert, equivalent } of diff) {\n if (equivalent) {\n i += equivalent.length;\n } else if (remove) {\n for (const child of remove) {\n if (_.isString(child) || shouldRemove(child)) {\n element.removeChild(element.childNodes[i]);\n } else {\n i++;\n }\n }\n }\n if (insert) {\n for (const child of insert) {\n const node = _.isString(child) ? document.createTextNode(child) : child;\n element.insertBefore(node, element.childNodes[i++]);\n }\n }\n }\n }\n\n destroy(element: Element) {\n const listeners = tracked_listeners.get(element);\n for (const [key, listener] of _.entries(listeners)) {\n const event = key.endsWith('Capture') ? key.slice(2, -7).toLowerCase() : key.slice(2).toLowerCase();\n if (_.isFunction(listener)) {\n element.removeEventListener(event, listener, { capture: key.endsWith('Capture') });\n }\n }\n tracked_listeners.delete(element);\n }\n}\n\nexport abstract class DOMNativeNode extends NativeElementType {\n\n static get Utils() { return DOMUtils; }\n\n static createElement: (doc: Document, renderer: _DOMRenderer) => DOMNativeNode;\n\n abstract get target(): Element | Element[];\n\n abstract update(\n props: Record<string, any> & {\n className?: string;\n style?: string;\n },\n children: (string | Element | DOMNativeNode)[]\n ): void;\n\n abstract destroy(): void;\n}\n","//\n// renderer.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { VNode } from '../../core/reconciler';\nimport { ComponentNode } from '../../core/types/component';\nimport { tags } from '../../../generated/elements';\nimport { _propValue } from '../../core/web/props';\nimport { ClassName, StyleProp } from '../../core/types/style';\nimport { ExtendedCSSProperties } from '../../core/web/styles/css';\nimport { _Renderer } from '../../core/renderer';\nimport { processCss } from '../../core/web/styles/process';\nimport { StyleBuilder } from '../style';\nimport { mergeRefs } from '../../core/utils';\nimport type { DOMWindow } from 'jsdom';\nimport { compress } from '../minify/compress';\nimport { DOMNativeNode } from './node';\n\nconst SVG_NS = 'http://www.w3.org/2000/svg';\nconst HTML_NS = 'http://www.w3.org/1999/xhtml';\nconst MATHML_NS = 'http://www.w3.org/1998/Math/MathML';\n\nexport abstract class _DOMRenderer extends _Renderer<Element | DOMNativeNode> {\n\n #window: Window | DOMWindow;\n #namespace_map = new WeakMap<VNode, string | undefined>();\n\n #tracked_head_children = new Map<VNode, (string | Element | DOMNativeNode)[]>();\n #tracked_body_head_children = new Map<VNode, (string | Element | DOMNativeNode)[]>();\n #tracked_style = new StyleBuilder();\n /** @internal */\n _tracked_server_resource = new Map<string, string>();\n #tracked_elements = new Map<Element | DOMNativeNode, { props: string[]; className: string[]; }>();\n\n #server_head_elements: ChildNode[] = [];\n\n constructor(window: Window | DOMWindow) {\n super();\n this.#window = window;\n }\n\n get document() {\n return this.window.document;\n }\n\n get window() {\n return this.#window;\n }\n\n _beforeUpdate() {\n const head = this.document.head\n if (this._server) {\n this._tracked_server_resource = new Map();\n } else if (head) {\n const found_marker = _.findIndex(head.childNodes, x => x.nodeType === head.COMMENT_NODE && x.textContent === 'frosty-server-head-marker');\n if (found_marker > -1) {\n this.#server_head_elements = _.slice(head.childNodes, 0, found_marker);\n } else {\n const styleElem = this.document.querySelector('style[data-frosty-style]');\n const ssrDataElem = this.document.querySelector('script[data-frosty-ssr-data]');\n this.#server_head_elements = _.filter([...head.childNodes], x => !_.includes([styleElem, ssrDataElem], x));\n }\n }\n }\n\n _afterUpdate() {\n this.#tracked_style.select([...this.#tracked_elements.values().flatMap(({ className }) => className)]);\n const head = this.document.head ?? this.document.createElementNS(HTML_NS, 'head');\n const styleElem = this.document.querySelector('style[data-frosty-style]') ?? this.document.createElementNS(HTML_NS, 'style');\n styleElem.setAttribute('data-frosty-style', '');\n if (styleElem.textContent !== this.#tracked_style.css)\n styleElem.textContent = this.#tracked_style.css;\n if (this._server) {\n const ssrData = this._tracked_server_resource.size ? this.document.createElementNS(HTML_NS, 'script') : undefined;\n if (ssrData) {\n ssrData.setAttribute('data-frosty-ssr-data', '');\n ssrData.setAttribute('type', 'text/plain');\n ssrData.innerHTML = compress(JSON.stringify(Object.fromEntries(this._tracked_server_resource)));\n }\n const tracked_head_children = _.flattenDeep([...this.#tracked_body_head_children.values()]);\n const maker = this.document.createComment('frosty-server-head-marker');\n DOMNativeNode.Utils.replaceChildren(head, _.compact([\n ..._.flattenDeep([...this.#tracked_head_children.values()]),\n !_.isEmpty(tracked_head_children) && maker,\n ...tracked_head_children,\n styleElem.textContent && styleElem,\n ssrData,\n ]), (x) => this.#tracked_elements.has(x as any));\n } else {\n DOMNativeNode.Utils.replaceChildren(head, _.compact([\n ...this.#server_head_elements.filter(x => x !== styleElem),\n ..._.flattenDeep([...this.#tracked_body_head_children.values()]),\n styleElem.textContent && styleElem,\n ]));\n }\n if (!this.document.head) {\n this.document.documentElement.insertBefore(head, this.document.body);\n }\n }\n\n _createElement(node: VNode) {\n const { type } = node;\n if (!_.isString(type) && type.prototype instanceof DOMNativeNode) {\n const ElementType = type as typeof DOMNativeNode;\n const elem = ElementType.createElement(this.document, this);\n this.#tracked_elements.set(elem, {\n props: [],\n className: [],\n });\n return elem;\n }\n if (!_.isString(type)) throw Error(`Invalid type ${type}`);\n switch (type) {\n case 'html': return this.document.documentElement;\n case 'head': return this.document.head ?? this.document.createElementNS(HTML_NS, 'head');\n case 'body': return this.document.body ?? this.document.createElementNS(HTML_NS, 'body');\n default: break;\n }\n const _ns_list = _.compact([\n _.includes(tags.svg, type) && SVG_NS,\n _.includes(tags.html, type) && HTML_NS,\n _.includes(tags.mathml, type) && MATHML_NS,\n ]);\n const parent = _.last(node.stack.toArray());\n const ns = _ns_list.length > 1 ? parent && _.first(_.intersection([this.#namespace_map.get(parent)], _ns_list)) : _.first(_ns_list);\n const elem = ns ? this.document.createElementNS(ns, type) : this.document.createElement(type);\n this.#namespace_map.set(node, ns);\n this.#tracked_elements.set(elem, {\n props: [],\n className: [],\n });\n return elem;\n }\n\n #createBuiltClassName(\n element: Element | DOMNativeNode,\n className: ClassName,\n style: StyleProp<ExtendedCSSProperties>,\n ) {\n const _className = _.compact(_.flattenDeep([className]));\n const built = this.#tracked_style.buildStyle(_.compact(_.flattenDeep([style])));\n const tracked = this.#tracked_elements.get(element);\n if (tracked) tracked.className = built;\n return [..._className, ...built].join(' ');\n }\n\n _updateElement(\n node: VNode,\n element: Element | DOMNativeNode,\n children: (string | Element | DOMNativeNode)[],\n force?: boolean\n ) {\n\n if (node.type !== 'html') {\n children = _.filter(children, x => x !== this.document.head && x !== this.document.body);\n }\n\n if (element instanceof DOMNativeNode) {\n const {\n props: { ref, className, style, inlineStyle, ..._props }\n } = node;\n if (ref) mergeRefs(ref)(element.target);\n const builtClassName = this.#createBuiltClassName(element, className, style);\n const { css } = processCss(inlineStyle);\n element.update({\n className: builtClassName ? builtClassName : undefined,\n style: css ? css : undefined,\n ..._props\n }, children);\n return;\n }\n\n const {\n type,\n props: { ref, className, style, inlineStyle, innerHTML, ..._props }\n } = node;\n\n if (!_.isString(type)) {\n DOMNativeNode.Utils.replaceChildren(element, children, (x) => !!force || this.#tracked_elements.has(x as any));\n return;\n }\n switch (type) {\n case 'head': {\n const tracked = node.parent?.type === 'html'\n ? this.#tracked_head_children\n : this.#tracked_body_head_children;\n tracked.set(node, children);\n return;\n }\n default: break;\n }\n\n if (ref) mergeRefs(ref)(element);\n\n const tracked = this.#tracked_elements.get(element);\n const removed = tracked ? _.difference(tracked.props, _.keys(_props)) : [];\n if (tracked) tracked.props = _.keys(_props);\n\n const builtClassName = this.#createBuiltClassName(element, className, style);\n if (_.isNil(innerHTML)) {\n DOMNativeNode.Utils.replaceChildren(element, children, (x) => !!force || this.#tracked_elements.has(x as any));\n } else if (element.innerHTML !== innerHTML) {\n element.innerHTML = innerHTML;\n }\n\n DOMNativeNode.Utils.update(element, {\n className: builtClassName,\n style: inlineStyle ? processCss(inlineStyle).css : undefined,\n ..._props,\n ..._.fromPairs(_.map(removed, x => [x, undefined])),\n });\n }\n\n _destroyElement(node: VNode, element: Element | DOMNativeNode) {\n this.#tracked_head_children.delete(node);\n this.#tracked_body_head_children.delete(node);\n if (element instanceof DOMNativeNode) {\n element.destroy();\n } else {\n DOMNativeNode.Utils.destroy(element);\n }\n }\n\n async renderToString(component: ComponentNode) {\n const root = this.createRoot();\n try {\n await root.mount(component, { skipMount: true });\n const elements = _.flatMap(_.castArray(root.root ?? []), x => x instanceof DOMNativeNode ? x.target : x);\n const str = _.map(elements, x => x.outerHTML).join('');\n if (elements.length !== 1) return str;\n return elements[0].tagName.toLowerCase() === 'html' ? `<!DOCTYPE html>${str}` : str;\n } finally {\n root.unmount();\n }\n }\n}\n"],"names":[],"mappings":";;;;AAEO,uBAAA,aAAA,SAAA,iBAAA;AACP;AACA,wBAAA,OAAA,kCAAA,MAAA;AACA;AACA;AACA;AACA,iCAAA,OAAA,sBAAA,SAAA,GAAA,aAAA,4BAAA,SAAA;AACA,yBAAA,OAAA;AACA;AACA,gCAAA,QAAA,YAAA,YAAA,KAAA,aAAA;AACA,2BAAA,OAAA,GAAA,OAAA;AACA,2BAAA,MAAA;AACA;AACA;AACA,2BAAA,OAAA,GAAA,aAAA;AACA;AACA;;ACbO,uBAAA,YAAA,SAAA,SAAA,CAAA,OAAA,GAAA,aAAA;AACP;AACA,wBAAA,MAAA,GAAA,SAAA;AACA,oBAAA,QAAA;AACA,kBAAA,MAAA,GAAA,SAAA;AACA;AACA;AACA,yBAAA,KAAA,GAAA,OAAA,GAAA,aAAA;AACA,yBAAA,KAAA,WAAA,OAAA,GAAA,aAAA,sBAAA,OAAA,GAAA,aAAA;AACA,0BAAA,KAAA,WAAA,OAAA,GAAA,aAAA;AACA,8BAAA,aAAA,GAAA,OAAA;AACA;;;;"}
|
|
1
|
+
{"version":3,"file":"renderer-Cb5O2Zol.d.ts","sources":["../../src/renderer/common/node.ts","../../src/renderer/common/renderer.ts"],"sourcesContent":["//\n// common.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport type { _DOMRenderer } from './renderer';\nimport { myersSync } from 'myers.js';\nimport { globalEvents } from '../../core/web/event';\nimport { NativeElementType } from '../../core/types/component';\nimport { svgProps, htmlProps } from '../../../generated/elements';\nimport { _propValue } from '../../core/web/props';\nimport { _Renderer } from '../../core/renderer';\n\nconst findPrototypeProperty = (object: any, propertyName: string) => {\n while (object && object.constructor && object.constructor.name !== 'Object') {\n let desc = Object.getOwnPropertyDescriptor(object, propertyName);\n if (desc) return desc;\n object = Object.getPrototypeOf(object);\n }\n return null;\n};\n\nconst isWriteable = (object: any, propertyName: string) => {\n let desc = findPrototypeProperty(object, propertyName);\n if (!desc) {\n return false;\n }\n if (desc.writable && typeof desc.value !== 'function') {\n return true;\n }\n return !!desc.set;\n};\n\nconst tracked_props = new WeakMap<Element, string[]>();\nconst tracked_listeners = new WeakMap<Element, Record<string, EventListener | undefined>>();\nconst _updateEventListener = (\n element: Element,\n key: string,\n listener: EventListener | undefined,\n) => {\n const event = key.endsWith('Capture') ? key.slice(2, -7).toLowerCase() : key.slice(2).toLowerCase();\n const listeners = tracked_listeners.get(element) ?? {};\n if (!tracked_listeners.has(element)) tracked_listeners.set(element, listeners);\n if (listeners[key] !== listener) {\n const options = { capture: key.endsWith('Capture') };\n if (_.isFunction(listeners[key])) element.removeEventListener(event, listeners[key], options);\n if (_.isFunction(listener)) element.addEventListener(event, listener, options);\n }\n listeners[key] = listener;\n}\n\nconst DOMUtils = new class {\n\n update(\n element: Element,\n { className, style, ...props }: Record<string, any> & {\n className?: string;\n style?: string;\n },\n ) {\n if (className) {\n if (element.className !== className)\n element.className = className;\n } else if (!_.isNil(element.getAttribute('class'))) {\n element.removeAttribute('class');\n }\n if (style) {\n const oldValue = element.getAttribute('style');\n if (oldValue !== style)\n element.setAttribute('style', style);\n } else if (!_.isNil(element.getAttribute('style'))) {\n element.removeAttribute('style');\n }\n for (const [key, value] of _.entries(props)) {\n if (_.includes(globalEvents, key)) {\n _updateEventListener(element, key, value);\n } else if (key.endsWith('Capture') && _.includes(globalEvents, key.slice(0, -7))) {\n _updateEventListener(element, key, value);\n } else if (key.startsWith('data-')) {\n const oldValue = element.getAttribute(key);\n if (value === false || _.isNil(value)) {\n if (!_.isNil(oldValue))\n element.removeAttribute(key);\n } else {\n const newValue = value === true ? '' : `${value}`;\n if (oldValue !== newValue)\n element.setAttribute(key, newValue);\n }\n } else {\n const tagName = _.toLower(element.tagName);\n const { type: _type, attr } = (htmlProps as any)['*'][key]\n ?? (htmlProps as any)[tagName]?.[key]\n ?? (svgProps as any)['*'][key]\n ?? (svgProps as any)[tagName]?.[key]\n ?? {};\n const tracked = tracked_props.get(element) ?? [];\n const writeable = isWriteable(element, key);\n if (!tracked_props.has(element)) tracked_props.set(element, tracked);\n const assigned = _.includes(tracked, key);\n if (writeable && !_.isNil(value)) {\n if (!assigned || (element as any)[key] !== value) (element as any)[key] = value;\n if (!assigned) tracked.push(key);\n } else if (_type && attr && (_propValue as any)[_type]) {\n const oldValue = element.getAttribute(attr);\n if (value === false || _.isNil(value)) {\n if (!_.isNil(oldValue))\n element.removeAttribute(attr);\n } else {\n const newValue = value === true ? '' : `${value}`;\n if (oldValue !== newValue)\n element.setAttribute(attr, newValue);\n }\n } else if (writeable) {\n if (!assigned || (element as any)[key] !== value) (element as any)[key] = value;\n if (!assigned) tracked.push(key);\n }\n }\n }\n }\n\n replaceChildren(\n element: Element,\n children: (string | ChildNode | DOMNativeNode)[],\n shouldRemove: (child: ChildNode) => boolean = () => true,\n ) {\n const document = element.ownerDocument;\n const diff = myersSync(\n _.map(element.childNodes, x => x.nodeType === document.TEXT_NODE ? x.textContent ?? '' : x),\n _.flatMap(children, x => x instanceof DOMNativeNode ? x.target : x),\n { compare: (a, b) => a === b },\n );\n let i = 0;\n for (const { remove, insert, equivalent } of diff) {\n if (equivalent) {\n i += equivalent.length;\n } else if (remove) {\n for (const child of remove) {\n if (_.isString(child) || shouldRemove(child)) {\n element.removeChild(element.childNodes[i]);\n } else {\n i++;\n }\n }\n }\n if (insert) {\n for (const child of insert) {\n const node = _.isString(child) ? document.createTextNode(child) : child;\n element.insertBefore(node, element.childNodes[i++]);\n }\n }\n }\n }\n\n destroy(element: Element) {\n const listeners = tracked_listeners.get(element);\n for (const [key, listener] of _.entries(listeners)) {\n const event = key.endsWith('Capture') ? key.slice(2, -7).toLowerCase() : key.slice(2).toLowerCase();\n if (_.isFunction(listener)) {\n element.removeEventListener(event, listener, { capture: key.endsWith('Capture') });\n }\n }\n tracked_listeners.delete(element);\n }\n}\n\nexport abstract class DOMNativeNode extends NativeElementType {\n\n static get Utils() { return DOMUtils; }\n\n static createElement: (doc: Document, renderer: _DOMRenderer) => DOMNativeNode;\n\n abstract get target(): Element | Element[];\n\n abstract update(\n props: Record<string, any> & {\n className?: string;\n style?: string;\n },\n children: (string | Element | DOMNativeNode)[]\n ): void;\n\n abstract destroy(): void;\n}\n","//\n// renderer.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2026 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { VNode } from '../../core/reconciler';\nimport { ComponentNode } from '../../core/types/component';\nimport { tags } from '../../../generated/elements';\nimport { _propValue } from '../../core/web/props';\nimport { ClassName, StyleProp } from '../../core/types/style';\nimport { ExtendedCSSProperties } from '../../core/web/styles/css';\nimport { _Renderer } from '../../core/renderer';\nimport { processCss } from '../../core/web/styles/process';\nimport { StyleBuilder } from '../style';\nimport { mergeRefs } from '../../core/utils';\nimport type { DOMWindow } from 'jsdom';\nimport { compress } from '../minify/compress';\nimport { DOMNativeNode } from './node';\n\nconst SVG_NS = 'http://www.w3.org/2000/svg';\nconst HTML_NS = 'http://www.w3.org/1999/xhtml';\nconst MATHML_NS = 'http://www.w3.org/1998/Math/MathML';\n\nexport abstract class _DOMRenderer extends _Renderer<Element | DOMNativeNode> {\n\n #window: Window | DOMWindow;\n #namespace_map = new WeakMap<VNode, string | undefined>();\n\n #tracked_head_children = new Map<VNode, (string | Element | DOMNativeNode)[]>();\n #tracked_body_head_children = new Map<VNode, (string | Element | DOMNativeNode)[]>();\n #tracked_style = new StyleBuilder();\n /** @internal */\n _tracked_server_resource = new Map<string, string>();\n #tracked_elements = new Map<Element | DOMNativeNode, { props: string[]; className: string[]; }>();\n\n #server_head_elements?: ChildNode[];\n\n constructor(window: Window | DOMWindow) {\n super();\n this.#window = window;\n }\n\n get document() {\n return this.window.document;\n }\n\n get window() {\n return this.#window;\n }\n\n _beforeUpdate() {\n const head = this.document.head\n if (this._server) {\n this._tracked_server_resource = new Map();\n } else if (head) {\n if (!this.#server_head_elements) {\n const found_marker = _.findIndex(head.childNodes, x => x.nodeType === head.COMMENT_NODE && x.textContent === 'frosty-server-head-marker');\n if (found_marker > -1) {\n this.#server_head_elements = _.slice(head.childNodes, 0, found_marker);\n } else {\n const styleElem = this.document.querySelector('style[data-frosty-style]');\n const ssrDataElem = this.document.querySelector('script[data-frosty-ssr-data]');\n this.#server_head_elements = _.filter([...head.childNodes], x => !_.includes([styleElem, ssrDataElem], x));\n }\n DOMNativeNode.Utils.replaceChildren(head, this.#server_head_elements);\n }\n }\n }\n\n _afterUpdate() {\n this.#tracked_style.select([...this.#tracked_elements.values().flatMap(({ className }) => className)]);\n const head = this.document.head ?? this.document.createElementNS(HTML_NS, 'head');\n const styleElem = this.document.querySelector('style[data-frosty-style]') ?? this.document.createElementNS(HTML_NS, 'style');\n styleElem.setAttribute('data-frosty-style', '');\n if (styleElem.textContent !== this.#tracked_style.css)\n styleElem.textContent = this.#tracked_style.css;\n if (this._server) {\n const ssrData = this._tracked_server_resource.size ? this.document.createElementNS(HTML_NS, 'script') : undefined;\n if (ssrData) {\n ssrData.setAttribute('data-frosty-ssr-data', '');\n ssrData.setAttribute('type', 'text/plain');\n ssrData.innerHTML = compress(JSON.stringify(Object.fromEntries(this._tracked_server_resource)));\n }\n const tracked_head_children = _.flattenDeep([...this.#tracked_body_head_children.values()]);\n const maker = this.document.createComment('frosty-server-head-marker');\n DOMNativeNode.Utils.replaceChildren(head, _.compact([\n ..._.flattenDeep([...this.#tracked_head_children.values()]),\n !_.isEmpty(tracked_head_children) && maker,\n ...tracked_head_children,\n styleElem.textContent && styleElem,\n ssrData,\n ]), (x) => this.#tracked_elements.has(x as any));\n } else {\n DOMNativeNode.Utils.replaceChildren(head, _.compact([\n ...this.#server_head_elements ?? [],\n ..._.flattenDeep([...this.#tracked_body_head_children.values()]),\n styleElem.textContent && styleElem,\n ]), (x) => this.#tracked_elements.has(x as any));\n }\n if (!this.document.head) {\n this.document.documentElement.insertBefore(head, this.document.body);\n }\n }\n\n _createElement(node: VNode) {\n const { type } = node;\n if (!_.isString(type) && type.prototype instanceof DOMNativeNode) {\n const ElementType = type as typeof DOMNativeNode;\n const elem = ElementType.createElement(this.document, this);\n this.#tracked_elements.set(elem, {\n props: [],\n className: [],\n });\n return elem;\n }\n if (!_.isString(type)) throw Error(`Invalid type ${type}`);\n switch (type) {\n case 'html': return this.document.documentElement;\n case 'head': return this.document.head ?? this.document.createElementNS(HTML_NS, 'head');\n case 'body': return this.document.body ?? this.document.createElementNS(HTML_NS, 'body');\n default: break;\n }\n const _ns_list = _.compact([\n _.includes(tags.svg, type) && SVG_NS,\n _.includes(tags.html, type) && HTML_NS,\n _.includes(tags.mathml, type) && MATHML_NS,\n ]);\n const parent = _.last(node.stack.toArray());\n const ns = _ns_list.length > 1 ? parent && _.first(_.intersection([this.#namespace_map.get(parent)], _ns_list)) : _.first(_ns_list);\n const elem = ns ? this.document.createElementNS(ns, type) : this.document.createElement(type);\n this.#namespace_map.set(node, ns);\n this.#tracked_elements.set(elem, {\n props: [],\n className: [],\n });\n return elem;\n }\n\n #createBuiltClassName(\n element: Element | DOMNativeNode,\n className: ClassName,\n style: StyleProp<ExtendedCSSProperties>,\n ) {\n const _className = _.compact(_.flattenDeep([className]));\n const built = this.#tracked_style.buildStyle(_.compact(_.flattenDeep([style])));\n const tracked = this.#tracked_elements.get(element);\n if (tracked) tracked.className = built;\n return [..._className, ...built].join(' ');\n }\n\n _updateElement(\n node: VNode,\n element: Element | DOMNativeNode,\n children: (string | Element | DOMNativeNode)[],\n force?: boolean\n ) {\n\n if (node.type !== 'html') {\n children = _.filter(children, x => x !== this.document.head && x !== this.document.body);\n }\n\n if (element instanceof DOMNativeNode) {\n const {\n props: { ref, className, style, inlineStyle, ..._props }\n } = node;\n if (ref) mergeRefs(ref)(element.target);\n const builtClassName = this.#createBuiltClassName(element, className, style);\n const { css } = processCss(inlineStyle);\n element.update({\n className: builtClassName ? builtClassName : undefined,\n style: css ? css : undefined,\n ..._props\n }, children);\n return;\n }\n\n const {\n type,\n props: { ref, className, style, inlineStyle, innerHTML, ..._props }\n } = node;\n\n if (!_.isString(type)) {\n DOMNativeNode.Utils.replaceChildren(element, children, (x) => !!force || this.#tracked_elements.has(x as any));\n return;\n }\n switch (type) {\n case 'head': {\n const tracked = node.parent?.type === 'html'\n ? this.#tracked_head_children\n : this.#tracked_body_head_children;\n tracked.set(node, children);\n return;\n }\n default: break;\n }\n\n if (ref) mergeRefs(ref)(element);\n\n const tracked = this.#tracked_elements.get(element);\n const removed = tracked ? _.difference(tracked.props, _.keys(_props)) : [];\n if (tracked) tracked.props = _.keys(_props);\n\n const builtClassName = this.#createBuiltClassName(element, className, style);\n if (_.isNil(innerHTML)) {\n DOMNativeNode.Utils.replaceChildren(element, children, (x) => !!force || this.#tracked_elements.has(x as any));\n } else if (element.innerHTML !== innerHTML) {\n element.innerHTML = innerHTML;\n }\n\n DOMNativeNode.Utils.update(element, {\n className: builtClassName,\n style: inlineStyle ? processCss(inlineStyle).css : undefined,\n ..._props,\n ..._.fromPairs(_.map(removed, x => [x, undefined])),\n });\n }\n\n _destroyElement(node: VNode, element: Element | DOMNativeNode) {\n this.#tracked_head_children.delete(node);\n this.#tracked_body_head_children.delete(node);\n if (element instanceof DOMNativeNode) {\n element.destroy();\n } else {\n DOMNativeNode.Utils.destroy(element);\n }\n }\n\n async renderToString(component: ComponentNode) {\n const root = this.createRoot();\n try {\n await root.mount(component, { skipMount: true });\n const elements = _.flatMap(_.castArray(root.root ?? []), x => x instanceof DOMNativeNode ? x.target : x);\n const str = _.map(elements, x => x.outerHTML).join('');\n if (elements.length !== 1) return str;\n return elements[0].tagName.toLowerCase() === 'html' ? `<!DOCTYPE html>${str}` : str;\n } finally {\n root.unmount();\n }\n }\n}\n"],"names":[],"mappings":";;;;AAEO,uBAAA,aAAA,SAAA,iBAAA;AACP;AACA,wBAAA,OAAA,kCAAA,MAAA;AACA;AACA;AACA;AACA,iCAAA,OAAA,sBAAA,SAAA,GAAA,aAAA,4BAAA,SAAA;AACA,yBAAA,OAAA;AACA;AACA,gCAAA,QAAA,YAAA,YAAA,KAAA,aAAA;AACA,2BAAA,OAAA,GAAA,OAAA;AACA,2BAAA,MAAA;AACA;AACA;AACA,2BAAA,OAAA,GAAA,aAAA;AACA;AACA;;ACbO,uBAAA,YAAA,SAAA,SAAA,CAAA,OAAA,GAAA,aAAA;AACP;AACA,wBAAA,MAAA,GAAA,SAAA;AACA,oBAAA,QAAA;AACA,kBAAA,MAAA,GAAA,SAAA;AACA;AACA;AACA,yBAAA,KAAA,GAAA,OAAA,GAAA,aAAA;AACA,yBAAA,KAAA,WAAA,OAAA,GAAA,aAAA,sBAAA,OAAA,GAAA,aAAA;AACA,0BAAA,KAAA,WAAA,OAAA,GAAA,aAAA;AACA,8BAAA,aAAA,GAAA,OAAA;AACA;;;;"}
|
|
@@ -4229,7 +4229,7 @@ class _DOMRenderer extends renderer._Renderer {
|
|
|
4229
4229
|
/** @internal */
|
|
4230
4230
|
_tracked_server_resource = new Map();
|
|
4231
4231
|
#tracked_elements = new Map();
|
|
4232
|
-
#server_head_elements
|
|
4232
|
+
#server_head_elements;
|
|
4233
4233
|
constructor(window) {
|
|
4234
4234
|
super();
|
|
4235
4235
|
this.#window = window;
|
|
@@ -4246,14 +4246,17 @@ class _DOMRenderer extends renderer._Renderer {
|
|
|
4246
4246
|
this._tracked_server_resource = new Map();
|
|
4247
4247
|
}
|
|
4248
4248
|
else if (head) {
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4249
|
+
if (!this.#server_head_elements) {
|
|
4250
|
+
const found_marker = _.findIndex(head.childNodes, x => x.nodeType === head.COMMENT_NODE && x.textContent === 'frosty-server-head-marker');
|
|
4251
|
+
if (found_marker > -1) {
|
|
4252
|
+
this.#server_head_elements = _.slice(head.childNodes, 0, found_marker);
|
|
4253
|
+
}
|
|
4254
|
+
else {
|
|
4255
|
+
const styleElem = this.document.querySelector('style[data-frosty-style]');
|
|
4256
|
+
const ssrDataElem = this.document.querySelector('script[data-frosty-ssr-data]');
|
|
4257
|
+
this.#server_head_elements = _.filter([...head.childNodes], x => !_.includes([styleElem, ssrDataElem], x));
|
|
4258
|
+
}
|
|
4259
|
+
DOMNativeNode.Utils.replaceChildren(head, this.#server_head_elements);
|
|
4257
4260
|
}
|
|
4258
4261
|
}
|
|
4259
4262
|
}
|
|
@@ -4283,10 +4286,10 @@ class _DOMRenderer extends renderer._Renderer {
|
|
|
4283
4286
|
}
|
|
4284
4287
|
else {
|
|
4285
4288
|
DOMNativeNode.Utils.replaceChildren(head, _.compact([
|
|
4286
|
-
...this.#server_head_elements
|
|
4289
|
+
...this.#server_head_elements ?? [],
|
|
4287
4290
|
..._.flattenDeep([...this.#tracked_body_head_children.values()]),
|
|
4288
4291
|
styleElem.textContent && styleElem,
|
|
4289
|
-
]));
|
|
4292
|
+
]), (x) => this.#tracked_elements.has(x));
|
|
4290
4293
|
}
|
|
4291
4294
|
if (!this.document.head) {
|
|
4292
4295
|
this.document.documentElement.insertBefore(head, this.document.body);
|
|
@@ -4412,4 +4415,4 @@ class _DOMRenderer extends renderer._Renderer {
|
|
|
4412
4415
|
|
|
4413
4416
|
exports.DOMNativeNode = DOMNativeNode;
|
|
4414
4417
|
exports._DOMRenderer = _DOMRenderer;
|
|
4415
|
-
//# sourceMappingURL=renderer-
|
|
4418
|
+
//# sourceMappingURL=renderer-Cz3yZBzr.js.map
|