@servicenow/ui-renderer-snabbdom
Version:
Snabbdom renderer for UI Framework on Next Experience
59 lines (54 loc) • 2.34 kB
JavaScript
/**
* Copyright (c) 2020 ServiceNow, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// TODO (webpack v5): drop the `build/package/` part of these import paths.
// Snabbdom>=1 uses the package.json#exports field that webpack v4 doesn't know
// about
//
// https://github.com/webpack/webpack/issues/9509
// https://webpack.js.org/guides/package-exports/
import { toVNode } from 'snabbdom/build/package/tovnode';
import { sign } from 'snabbdom-signature';
/**
* Creates an element for the virtual DOM from an existing DOM node
* @method createElementFromNode
* @param {Node} node DOM node to convert
* @returns {VNode} The vnode to use in the virtual DOM
*/
export default function createElementFromNode(node) {
const vnode = sign(toVNode(node));
fixNamespace(vnode); // fixes issue with missing namespace for svgs: https://github.com/snabbdom/snabbdom/issues/388
return vnode;
}
const fixNamespace = (vnode, xmlns = vnode.data && vnode.data.attrs && vnode.data.attrs.xmlns) => {
const {
children,
data
} = vnode;
if (data && !data.ns && xmlns) data.ns = xmlns;
if (children) {
const {
length: childrenCount
} = children;
for (let i = 0; i < childrenCount; i++) fixNamespace(children[i], xmlns);
}
};
//# sourceMappingURL=createElementFromNode.js.map