@cocreate/utils 1.1.35 → 1.3.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,36 @@
1
+ # [1.3.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.2.1...v1.3.0) (2021-11-01)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * cssPath did not push and return path if it was id ([4af00bd](https://github.com/CoCreate-app/CoCreate-utils/commit/4af00bd4c3fd376a1fd15d7dd1eeeec63ff0ddcf))
7
+
8
+
9
+ ### Features
10
+
11
+ * cssPath will stop and return path if id or eid found... rather then continuing to travel up the dom tree ([17d6ced](https://github.com/CoCreate-app/CoCreate-utils/commit/17d6ced222f0552f870f2feeffecf7970ad653a3))
12
+
13
+ ## [1.2.1](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.2.0...v1.2.1) (2021-10-29)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * update dependencies ([f77e945](https://github.com/CoCreate-app/CoCreate-utils/commit/f77e945e2b17310d20769434492bd074e99a1d78))
19
+
20
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.36...v1.2.0) (2021-10-29)
21
+
22
+
23
+ ### Features
24
+
25
+ * cssPath and domParser ([25c5088](https://github.com/CoCreate-app/CoCreate-utils/commit/25c508855c663f5e2f64596549d1eccb14eb4292))
26
+
27
+ ## [1.1.36](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.35...v1.1.36) (2021-10-17)
28
+
29
+
30
+ ### Bug Fixes
31
+
32
+ * update dependendies ([c5a34a9](https://github.com/CoCreate-app/CoCreate-utils/commit/c5a34a9ce6cff492f76e8e614a35860037d1f722))
33
+
1
34
  ## [1.1.35](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.34...v1.1.35) (2021-10-16)
2
35
 
3
36
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/utils",
3
- "version": "1.1.35",
3
+ "version": "1.3.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.38"
64
+ "@cocreate/docs": "^1.2.43"
65
65
  }
66
66
  }
package/src/index.js CHANGED
@@ -12,34 +12,94 @@ 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){
21
+ pathSplit += "#" + node.id;
22
+ node = '';
23
+ }
24
+ else {
25
+ let eid = node.getAttribute('eid');
26
+ if (eid) {
27
+ pathSplit += `[eid="${eid}"]`;
28
+ node = '';
29
+ }
30
+ else {
31
+ if (node.classList.length) {
32
+ node.classList.forEach((item) => {
33
+ if (item.indexOf(":") === -1) pathSplit += "." + item;
34
+ });
35
+ }
36
+
37
+ if (node.parentNode) {
38
+ let index = Array.prototype.indexOf.call(
39
+ node.parentNode.children,
40
+ node
41
+ );
42
+ pathSplit += `:nth-child(${index + 1})`;
43
+ }
44
+
45
+ // pathSplits.unshift(pathSplit);
46
+ node = node.parentNode;
47
+ if (node.tagName == "HTML" || node.nodeName == "#document" || node.hasAttribute('contenteditable'))
48
+ node = '';
49
+ }
50
+ }
51
+ pathSplits.unshift(pathSplit);
52
+ } while (node);
53
+ return pathSplits.join(" > ");
54
+ }
21
55
 
22
- if (node.classList.length && node.tagName !== "BODY") {
23
- node.classList.forEach((item) => {
24
- if (item.indexOf(":") === -1) pathSplit += "." + item;
25
- });
26
- }
56
+ export function domParser(str) {
57
+ let mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
58
+ if (!mainTag)
59
+ throw new Error('find position: can not find the main tag');
27
60
 
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
- }
61
+ let doc;
62
+ switch (mainTag) {
63
+ case 'html':
64
+ doc = new DOMParser().parseFromString(str, "text/html");
65
+ return doc.documentElement;
66
+ case 'body':
67
+ doc = new DOMParser().parseFromString(str, "text/html");
68
+ return doc.body;
69
+ case 'head':
70
+ doc = new DOMParser().parseFromString(str, "text/html");
71
+ return doc.head;
35
72
 
36
- pathSplits.unshift(pathSplit);
37
- node = node.parentNode;
38
- } while (node.tagName !== "HTML");
73
+ default:
74
+ let con = document.createElement('div');
75
+ con.innerHTML = str;
76
+ return con;
77
+ }
78
+ }
39
79
 
40
- return pathSplits.join(" > ");
80
+ export function queryFrameSelector(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 elements = Document.querySelectorAll(target);
87
+ return elements;
88
+ }
89
+ }
41
90
  }
42
91
 
92
+ export function queryFrameSelectorAll(selector) {
93
+ if(selector.indexOf(';') !== -1) {
94
+ let [frameSelector, target] = selector.split(';');
95
+ let frame = document.querySelector(frameSelector);
96
+ if (frame) {
97
+ let Document = frame.contentDocument;
98
+ let element = Document.querySelector(target);
99
+ return element;
100
+ }
101
+ }
102
+ }
43
103
  // export function computeStyles(el, properties) {
44
104
  // let computed = window.getComputedStyle(el);
45
105
  // let result = {};
@@ -64,5 +124,6 @@ export function cssPath(node) {
64
124
  export default {
65
125
  parseTextToHtml,
66
126
  getAttributes,
67
- cssPath
127
+ cssPath,
128
+ domParser
68
129
  };
package/src/index.old.js CHANGED
@@ -319,6 +319,33 @@ export function frameQuerySelectorAll(comSelector) {
319
319
 
320
320
  }
321
321
 
322
+ async function complexSelector(comSelector, callback) {
323
+ let [canvasSelector, selector] = comSelector.split(';');
324
+ let canvas = document.querySelector(canvasSelector);
325
+ if(!canvas) {
326
+ console.warn('complex selector canvas now found for', comSelector);
327
+ return;
328
+ }
329
+
330
+ if(canvas.contentDocument.readyState === 'loading') {
331
+ try {
332
+ await new Promise((resolve, reject) => {
333
+ canvas.contentWindow.addEventListener('load', (e) => resolve());
334
+ });
335
+ }
336
+ catch(err) {
337
+ console.error('iframe can not be loaded');
338
+ }
339
+ }
340
+
341
+ if(canvas.contentWindow.parent.CoCreate.observer && !observerInit.has(canvas.contentWindow)) {
342
+ observerElements(canvas.contentWindow);
343
+ }
344
+
345
+ return callback(canvas.contentWindow.document, selector);
346
+ }
347
+
348
+
322
349
  // export function computeStyles(el, properties) {
323
350
  // let computed = window.getComputedStyle(el);
324
351
  // let result = {};