@cocreate/selection 1.1.1 → 1.1.2

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,10 @@
1
+ ## [1.1.2](https://github.com/CoCreate-app/CoCreate-selection/compare/v1.1.1...v1.1.2) (2021-10-30)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * removed for loop in getElementPosition ([20b7ff4](https://github.com/CoCreate-app/CoCreate-selection/commit/20b7ff45dc1f321a246d4fe9f22590c5b1e17c0e))
7
+
1
8
  ## [1.1.1](https://github.com/CoCreate-app/CoCreate-selection/compare/v1.1.0...v1.1.1) (2021-10-29)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/selection",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "A simple selection component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "selection",
package/src/index.js CHANGED
@@ -124,89 +124,85 @@ export function hasSelection(el) {
124
124
 
125
125
  export function getElementPosition(str, start, end) {
126
126
  let response = {};
127
- let selection = [start];
128
- // if (start != end) {
129
- // selection = [start, end];
130
- // }
131
-
132
- for (let pos of selection) {
133
- let startString = str.substr(0, pos);
134
- let endString = str.substr(pos);
135
- let angleStart = startString.lastIndexOf("<");
136
- let angleEnd = startString.lastIndexOf(">");
137
- let endStringAngleEnd = endString.indexOf(">");
138
- let element, position, nodeStart, nodeEnd, startNode, type;
139
- if (angleEnd > angleStart) {
140
- let string = str.customSplice(start, 0, '<findelement></findelement>');
127
+ let startString = str.substr(0, start);
128
+ let endString = str.substr(start);
129
+ let angleStart = startString.lastIndexOf("<");
130
+ let angleEnd = startString.lastIndexOf(">");
131
+ let endStringAngleEnd = endString.indexOf(">");
132
+ let element, position, nodeStart, nodeEnd, startNode, type;
133
+ if (angleEnd > angleStart) {
134
+ let length = 0;
135
+ if (start != end)
136
+ length = end - start;
137
+ let string = str.customSplice(start, length, '<findelement></findelement>');
138
+ let newDom = domParser(string);
139
+ let findEl = newDom.querySelector('findelement');
140
+ if (findEl) {
141
+ let insert = getInsertPosition(findEl);
142
+ element = insert.target;
143
+ position = insert.position;
144
+ type = 'insertAdjacent';
145
+ if(!position)
146
+ type = 'textNode';
147
+ if (type == 'textNode' || type == 'afterbegin');
148
+ nodeStart = start - angleEnd - 1;
149
+ findEl.remove();
150
+ }
151
+ }
152
+ else {
153
+ let node = str.slice(angleStart, startString.length + endStringAngleEnd + 1);
154
+ if (node.startsWith("</")) {
155
+ startNode = node.slice(0, 1) + node.slice(2);
156
+ startNode = startNode.substr(0, startNode.length - 1);
157
+ nodeStart = startString.lastIndexOf(startNode);
158
+ let endString1 = str.substr(nodeStart);
159
+ let end = endString1.indexOf(">");
160
+ nodeEnd = nodeStart + end + 1;
161
+ type = 'isEndTag';
162
+ }
163
+ else {
164
+ nodeEnd = startString.length + endStringAngleEnd + 1;
165
+ startNode = node;
166
+ nodeStart = angleStart;
167
+ type = 'isStartTag';
168
+ }
169
+ if (nodeEnd > 0) {
170
+ let string = str.customSplice(nodeEnd - 1, 0, ' findelement');
141
171
  let newDom = domParser(string);
142
- let findEl = newDom.querySelector('findelement');
143
- if (findEl) {
144
- let insert = getInsertPosition(findEl);
145
- element = insert.target;
146
- position = insert.position;
147
- type = 'insertAdjacent';
148
- if(!position)
149
- type = 'textNode';
150
- if (type == 'textNode' || type == 'afterbegin');
151
- nodeStart = start - angleEnd - 1;
152
- findEl.remove();
153
- }
172
+ element = newDom.querySelector('[findelement]');
173
+ if (!element && newDom.tagName == 'HTML')
174
+ element = newDom;
175
+ else if (type == "isEndTag")
176
+ element = element.parentElement;
177
+ element.removeAttribute('findelement');
154
178
  }
155
179
  else {
156
- let node = str.slice(angleStart, startString.length + endStringAngleEnd + 1);
157
- if (node.startsWith("</")) {
158
- startNode = node.slice(0, 1) + node.slice(2);
159
- startNode = startNode.substr(0, startNode.length - 1);
160
- nodeStart = startString.lastIndexOf(startNode);
161
- let endString1 = str.substr(nodeStart);
162
- let end = endString1.indexOf(">");
163
- nodeEnd = nodeStart + end + 1;
164
- type = 'isEndTag';
165
- }
166
- else {
167
- nodeEnd = startString.length + endStringAngleEnd + 1;
168
- startNode = node;
169
- nodeStart = angleStart;
170
- type = 'isStartTag';
171
- }
172
- if (nodeEnd > 0) {
173
- let string = str.customSplice(nodeEnd - 1, 0, ' findelement');
174
- let newDom = domParser(string);
175
- element = newDom.querySelector('[findelement]');
176
- if (type == "isEndTag")
180
+ let string = str.customSplice(angleStart, 0, '<findelement></findelement>');
181
+ let newDom = domParser(string);
182
+ element = newDom.querySelector('findelement');
183
+ if (element) {
184
+ let insert = getInsertPosition(element);
185
+ element = insert.target.parentElement;
186
+ position = insert.position;
187
+ if(position == 'afterend')
177
188
  element = element.parentElement;
178
- if (!element && newDom.tagName == 'HTML')
179
- element = newDom;
180
- element.removeAttribute('findelement');
189
+ type = 'innerHTML';
181
190
  }
182
- else {
183
- let string = str.customSplice(angleStart, 0, '<findelement></findelement>');
184
- let newDom = domParser(string);
185
- element = newDom.querySelector('findelement');
186
- if (element) {
187
- let insert = getInsertPosition(element);
188
- element = insert.target.parentElement;
189
- position = insert.position;
190
- if(position == 'afterend')
191
- element = element.parentElement;
192
- type = 'innerHTML';
193
- }
194
- if (!element) {
195
- console.log('Could not find element');
196
- }
191
+ if (!element) {
192
+ console.log('Could not find element');
197
193
  }
198
194
  }
199
-
200
- if (element) {
201
- response.element = element;
202
- response.path = cssPath(element);
203
- response.position = position;
204
- response.start = nodeStart;
205
- response.end = nodeEnd;
206
- response.type = type;
207
- }
208
-
209
195
  }
196
+
197
+ if (element) {
198
+ response.element = element;
199
+ response.path = cssPath(element);
200
+ response.position = position;
201
+ response.start = nodeStart;
202
+ response.end = nodeEnd;
203
+ response.type = type;
204
+ }
205
+
210
206
  return response;
211
207
  }
212
208
 
@@ -248,7 +244,8 @@ export function getStringPosition({string, target, position, attribute, property
248
244
  let dom = domParser(string);
249
245
  let element = dom.querySelector(selector);
250
246
  if (!element){
251
- console.log('element could not be found using selector:', selector)
247
+ console.log('element could not be found using selector:', selector);
248
+ return;
252
249
  }
253
250
  let start = 0, end = 0;
254
251