@cocreate/utils 1.1.33 → 1.2.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,32 @@
1
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.36...v1.2.0) (2021-10-29)
2
+
3
+
4
+ ### Features
5
+
6
+ * cssPath and domParser ([25c5088](https://github.com/CoCreate-app/CoCreate-utils/commit/25c508855c663f5e2f64596549d1eccb14eb4292))
7
+
8
+ ## [1.1.36](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.35...v1.1.36) (2021-10-17)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * update dependendies ([c5a34a9](https://github.com/CoCreate-app/CoCreate-utils/commit/c5a34a9ce6cff492f76e8e614a35860037d1f722))
14
+
15
+ ## [1.1.35](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.34...v1.1.35) (2021-10-16)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * update socket package to fix bug in docs ([a5e71bd](https://github.com/CoCreate-app/CoCreate-utils/commit/a5e71bd6b6ae2a6cb025a50c91f6d90868c3f286))
21
+
22
+ ## [1.1.34](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.33...v1.1.34) (2021-10-15)
23
+
24
+
25
+ ### Bug Fixes
26
+
27
+ * update dependencies ([c0118c8](https://github.com/CoCreate-app/CoCreate-utils/commit/c0118c87f282ba84eaf9394ee82d9e8273842629))
28
+ * update dependencies ([f388916](https://github.com/CoCreate-app/CoCreate-utils/commit/f388916f3ba3fdf76aa9d9e6e80d9ea53cb36af1))
29
+
1
30
  ## [1.1.33](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.32...v1.1.33) (2021-10-15)
2
31
 
3
32
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/utils",
3
- "version": "1.1.33",
3
+ "version": "1.2.0",
4
4
  "description": "A simple utils component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "utils",
@@ -61,6 +61,6 @@
61
61
  "webpack-log": "^3.0.1"
62
62
  },
63
63
  "dependencies": {
64
- "@cocreate/docs": "^1.2.33"
64
+ "@cocreate/docs": "^1.2.42"
65
65
  }
66
66
  }
package/src/index.js CHANGED
@@ -12,34 +12,82 @@ export function parseTextToHtml(text) {
12
12
  else return doc.body.children[0];
13
13
  }
14
14
 
15
- export function cssPath(node) {
16
- let pathSplits = [];
17
- do {
18
- if (!node || !node.tagName) return false;
19
- let pathSplit = node.tagName.toLowerCase();
20
- if (node.id && node.tagName !== "BODY") pathSplit += "#" + node.id;
15
+ export function cssPath(node, container) {
16
+ let pathSplits = [];
17
+ do {
18
+ if (!node || !node.tagName) return false;
19
+ let pathSplit = node.tagName.toLowerCase();
20
+ if (node.id) pathSplit += "#" + node.id;
21
21
 
22
- if (node.classList.length && node.tagName !== "BODY") {
23
- node.classList.forEach((item) => {
24
- if (item.indexOf(":") === -1) pathSplit += "." + item;
25
- });
26
- }
22
+ if (node.classList.length) {
23
+ node.classList.forEach((item) => {
24
+ if (item.indexOf(":") === -1) pathSplit += "." + item;
25
+ });
26
+ }
27
27
 
28
- if (node.tagName !== "BODY" && node.parentNode) {
29
- let index = Array.prototype.indexOf.call(
30
- node.parentNode.children,
31
- node
32
- );
33
- pathSplit += `:nth-child(${index + 1})`;
34
- }
28
+ if (node.parentNode) {
29
+ let index = Array.prototype.indexOf.call(
30
+ node.parentNode.children,
31
+ node
32
+ );
33
+ pathSplit += `:nth-child(${index + 1})`;
34
+ }
35
+
36
+ pathSplits.unshift(pathSplit);
37
+ node = node.parentNode;
38
+ if (node.tagName == "HTML" || node.nodeName == "#document" || node.hasAttribute('contenteditable'))
39
+ node = '';
40
+ } while (node);
41
+ return pathSplits.join(" > ");
42
+ }
43
+
44
+ export function domParser(str) {
45
+ let mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
46
+ if (!mainTag)
47
+ throw new Error('find position: can not find the main tag');
35
48
 
36
- pathSplits.unshift(pathSplit);
37
- node = node.parentNode;
38
- } while (node.tagName !== "HTML");
49
+ let doc;
50
+ switch (mainTag) {
51
+ case 'html':
52
+ doc = new DOMParser().parseFromString(str, "text/html");
53
+ return doc.documentElement;
54
+ case 'body':
55
+ doc = new DOMParser().parseFromString(str, "text/html");
56
+ return doc.body;
57
+ case 'head':
58
+ doc = new DOMParser().parseFromString(str, "text/html");
59
+ return doc.head;
39
60
 
40
- return pathSplits.join(" > ");
61
+ default:
62
+ let con = document.createElement('div');
63
+ con.innerHTML = str;
64
+ return con;
65
+ }
66
+ }
67
+
68
+ export function queryFrameSelector(selector) {
69
+ if(selector.indexOf(';') !== -1) {
70
+ let [frameSelector, target] = selector.split(';');
71
+ let frame = document.querySelector(frameSelector);
72
+ if (frame) {
73
+ let Document = frame.contentDocument;
74
+ let elements = Document.querySelectorAll(target);
75
+ return elements;
76
+ }
77
+ }
41
78
  }
42
79
 
80
+ export function queryFrameSelectorAll(selector) {
81
+ if(selector.indexOf(';') !== -1) {
82
+ let [frameSelector, target] = selector.split(';');
83
+ let frame = document.querySelector(frameSelector);
84
+ if (frame) {
85
+ let Document = frame.contentDocument;
86
+ let element = Document.querySelector(target);
87
+ return element;
88
+ }
89
+ }
90
+ }
43
91
  // export function computeStyles(el, properties) {
44
92
  // let computed = window.getComputedStyle(el);
45
93
  // let result = {};
@@ -64,5 +112,6 @@ export function cssPath(node) {
64
112
  export default {
65
113
  parseTextToHtml,
66
114
  getAttributes,
67
- cssPath
115
+ cssPath,
116
+ domParser
68
117
  };