diff --git a/dist/morphlex.d.ts b/dist/morphlex.d.ts index 8cd6f4d..e9d8d52 100644 --- a/dist/morphlex.d.ts +++ b/dist/morphlex.d.ts @@ -12,8 +12,6 @@ interface Options { beforePropertyUpdated?: (node: Node, propertyName: PropertyKey, newValue: unknown) => boolean; afterPropertyUpdated?: (node: Node, propertyName: PropertyKey, previousValue: unknown) => void; } -export declare function morph(node: ChildNode, reference: ChildNode, options?: Options): void; -export declare function morphInner(element: Element, reference: Element, options?: Options): void; -export declare function morphFromString(node: ChildNode, reference: string, options?: Options): void; -export declare function morphInnerFromString(element: Element, reference: string, options?: Options): void; +export declare function morph(node: ChildNode, reference: ChildNode | string, options?: Options): void; +export declare function morphInner(element: Element, reference: Element | string, options?: Options): void; export {}; diff --git a/dist/morphlex.js b/dist/morphlex.js index 79dcfd8..069ad76 100644 --- a/dist/morphlex.js +++ b/dist/morphlex.js @@ -1,15 +1,11 @@ export function morph(node, reference, options = {}) { + if (typeof reference === "string") reference = parseChildNodeFromString(reference); new Morph(options).morph([node, reference]); } export function morphInner(element, reference, options = {}) { + if (typeof reference === "string") reference = parseElementFromString(reference); new Morph(options).morphInner([element, reference]); } -export function morphFromString(node, reference, options = {}) { - morph(node, parseChildNodeFromString(reference), options); -} -export function morphInnerFromString(element, reference, options = {}) { - morphInner(element, parseElementFromString(reference), options); -} function parseElementFromString(string) { const node = parseChildNodeFromString(string); if (isElement(node)) return node; @@ -18,8 +14,7 @@ function parseElementFromString(string) { function parseChildNodeFromString(string) { const parser = new DOMParser(); const doc = parser.parseFromString(string, "text/html"); - const firstChild = doc.body.firstChild; - if (doc.childNodes.length === 1) return firstChild; + if (doc.childNodes.length === 1) return doc.body.firstChild; else throw new Error("[Morphlex] The string was not a valid HTML node."); } class Morph { diff --git a/src/morphlex.ts b/src/morphlex.ts index 6c23836..611396c 100644 --- a/src/morphlex.ts +++ b/src/morphlex.ts @@ -49,22 +49,16 @@ interface Options { afterPropertyUpdated?: (node: Node, propertyName: PropertyKey, previousValue: unknown) => void; } -export function morph(node: ChildNode, reference: ChildNode, options: Options = {}): void { +export function morph(node: ChildNode, reference: ChildNode | string, options: Options = {}): void { + if (typeof reference === "string") reference = parseChildNodeFromString(reference); new Morph(options).morph([node, reference]); } -export function morphInner(element: Element, reference: Element, options: Options = {}): void { +export function morphInner(element: Element, reference: Element | string, options: Options = {}): void { + if (typeof reference === "string") reference = parseElementFromString(reference); new Morph(options).morphInner([element, reference]); } -export function morphFromString(node: ChildNode, reference: string, options: Options = {}): void { - morph(node, parseChildNodeFromString(reference), options); -} - -export function morphInnerFromString(element: Element, reference: string, options: Options = {}): void { - morphInner(element, parseElementFromString(reference), options); -} - function parseElementFromString(string: string): Element { const node = parseChildNodeFromString(string); @@ -75,9 +69,8 @@ function parseElementFromString(string: string): Element { function parseChildNodeFromString(string: string): ChildNode { const parser = new DOMParser(); const doc = parser.parseFromString(string, "text/html"); - const firstChild = doc.body.firstChild; - if (doc.childNodes.length === 1) return firstChild as ChildNode; + if (doc.childNodes.length === 1) return doc.body.firstChild as ChildNode; else throw new Error("[Morphlex] The string was not a valid HTML node."); }