|
@@ -0,0 +1,80 @@
|
|
|
+<header>
|
|
|
+ HTML Tree
|
|
|
+</header>
|
|
|
+
|
|
|
+<body>
|
|
|
+ <script>
|
|
|
+ function walker(parent) {
|
|
|
+ let res = "";
|
|
|
+ let isObject = parent != null && typeof parent === "object";
|
|
|
+ let isArray = Array.isArray(parent);
|
|
|
+ if (isArray) {
|
|
|
+ res += "[";
|
|
|
+ }
|
|
|
+ else if (isObject) {
|
|
|
+ res += "{";
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ res += `"${parent}"`;
|
|
|
+ }
|
|
|
+ if (isObject || isArray) {
|
|
|
+ let childArr = [];
|
|
|
+ for (let el in parent) {
|
|
|
+ let childStr = `${walker(parent[el])}`; // разбор вложенного значения єлемента
|
|
|
+ if (!isArray) {
|
|
|
+ childStr = `"${el}":${childStr}`;
|
|
|
+ }
|
|
|
+ childArr.push(childStr);
|
|
|
+ }
|
|
|
+ res += childArr.join(",");
|
|
|
+ }
|
|
|
+ if (isArray) {
|
|
|
+ res += "]";
|
|
|
+ }
|
|
|
+ else if (isObject) {
|
|
|
+ res += "}";
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ const table = {
|
|
|
+ tagName: 'table',
|
|
|
+ attrs: {
|
|
|
+ border: "1",
|
|
|
+ },
|
|
|
+ children: [
|
|
|
+ {
|
|
|
+ tagName: 'tr',
|
|
|
+ children: [
|
|
|
+ {
|
|
|
+ tagName: "td",
|
|
|
+ children: ["1x1"],
|
|
|
+ },
|
|
|
+ {
|
|
|
+ tagName: "td",
|
|
|
+ children: ["1x2"],
|
|
|
+ },
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ tagName: 'tr',
|
|
|
+ children: [
|
|
|
+ {
|
|
|
+ tagName: "td",
|
|
|
+ children: ["2x1"],
|
|
|
+ },
|
|
|
+ {
|
|
|
+ tagName: "td",
|
|
|
+ children: ["2x2"],
|
|
|
+ },
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ var objStr = walker(table);
|
|
|
+ var obj = JSON.parse(objStr);
|
|
|
+ document.write(objStr);
|
|
|
+ var testObjStr = JSON.stringify(table);
|
|
|
+ if (objStr != testObjStr)
|
|
|
+ alert("Khren'");
|
|
|
+ </script>
|
|
|
+</body>
|