Skip to content

Commit 81115bc

Browse files
committed
version 3.2.0-alpha.47
1 parent 167f279 commit 81115bc

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cample",
3-
"version": "3.2.0-alpha.46",
3+
"version": "3.2.0-alpha.47",
44
"description": "Cample.js - one of the fastest frameworks without a virtual DOM on the Internet!",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/core/cample.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ export class Cample {
6262
};
6363
const el: Element | null = document.querySelector(this.selector);
6464
if (el !== null) {
65-
el.innerHTML = this.template;
65+
const elementDocument = new DOMParser().parseFromString(
66+
`<template>${this.template}</template>`,
67+
"text/html"
68+
);
69+
const elWrapper = elementDocument.childNodes[0].childNodes[0]
70+
.firstChild as HTMLTemplateElement;
71+
const nodes = elWrapper.content.childNodes;
72+
el.innerHTML = "";
73+
for (let i = 0; i < nodes.length; i++) {
74+
const currentNode = nodes[i];
75+
el.appendChild(currentNode);
76+
}
6677
this._el = el;
6778
}
6879
const setEventListener = () => {

src/shared/utils.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ export const getIsValue = (key: string) => {
7878
} else return false;
7979
};
8080
export const getElement = (template: string, trim?: boolean) => {
81-
const elWrapper = document.createElement("template");
82-
elWrapper.innerHTML = trim ? template.trim() : template;
81+
const elementDocument = new DOMParser().parseFromString(
82+
`<template>${trim ? template.trim() : template}</template>`,
83+
"text/html"
84+
);
85+
const elWrapper = elementDocument.childNodes[0].childNodes[0]
86+
.firstChild as HTMLTemplateElement;
8387
if (elWrapper.content.children.length > 1) {
8488
createError("Component include only one node with type 'Element'");
8589
}
@@ -89,7 +93,7 @@ export const getElement = (template: string, trim?: boolean) => {
8993
if ((node as Element).tagName === "pre") return;
9094
break;
9195
case Node.TEXT_NODE:
92-
if (!/\S/.test(node.textContent as string)) {
96+
if (!/\S/.test(node.textContent!)) {
9397
node.remove();
9498
return;
9599
}
@@ -100,7 +104,9 @@ export const getElement = (template: string, trim?: boolean) => {
100104
}
101105
};
102106
prepareNode(elWrapper.content.childNodes[0]);
103-
return elWrapper.content.firstElementChild;
107+
const currentEl = elWrapper.content.firstElementChild;
108+
if (!currentEl) createError("Element is undefined");
109+
return currentEl;
104110
};
105111
export const isValuesEqual = (val: any, newVal: any): boolean => {
106112
if (val === newVal) return true;
@@ -485,12 +491,6 @@ export const getArrImportString = (
485491
}
486492
};
487493

488-
export const checkNodes = (template: string): boolean => {
489-
const el = document.createElement("template");
490-
el.innerHTML = template;
491-
return el.content.childNodes.length === 1;
492-
};
493-
494494
export const getElements = (el: Element | null) => {
495495
const els: Array<Element> = [];
496496
if (el) {

0 commit comments

Comments
 (0)