-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
244 lines (209 loc) · 8.95 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* global define */
define(['jquery'], function($) {
'use strict';
var utils = {
abs: function (str) {
return Math.abs(parseInt(str));
},
isTextNode: function (ele) {
return ele && ele.nodeType === 3;
},
getAll: function (sel, root) {
return [].slice.call((root || document).querySelectorAll(sel));
},
get: function (sel, root) {
return (root || document).querySelector(sel);
},
toArray: function (a) {
return [].slice.call(a);
},
realLength: function (obj) {
return Object.keys(obj).length;
},
viewportWidth: function () {
return Math.min(window.innerWidth, document.body.offsetWidth);
},
viewportHeight: function () {
return Math.min(window.innerHeight, document.body.offsetHeight);
},
isLastItem: function (array, item) {
return item === array[array.length - 1];
},
text: function (a) {
return (a.innerText || a.textContent).replace(/(\r?\n){2,}/g, '\r\n');
},
stopPropagation: function (event) {
event.stopPropagation();
},
matches: function (a, b) {
if (a && a.matches && a.matches(b)) {
return true;
}
return false;
},
isVisibleNode: function (ele) {
if (ele.nodeType !== 1 && ele.nodeType !== 3) {
return false;
}
if (utils.isTextNode(ele)) {
ele = ele.parentNode;
}
var style = getComputedStyle(ele);
return utils.abs(style['text-indent']) < 200 &&
ele.offsetWidth > 3 &&
ele.offsetHeight > 3;
},
isInlineAndVisibleElement: function (ele) {
if (utils.isTextNode(ele)) {
return false;
}
var style = getComputedStyle(ele);
return /inline/.test(style.display) && utils.isVisibleNode(ele);
},
flattenArray: function (arr) {
return [].concat.apply([], arr);
},
determineParagraphContainer: function (groups) {
var $containers = $(utils.flattenArray(groups));
var prev = {};
var paragraphScore = {};
// lets score based on how different containers childsare
// more different == more likely to be a paragraph container
groups.forEach(function (group) {
var allChilds = group.reduce(function (prev, tag) {
return prev.concat(utils.getAll('*', tag));
}, []);
allChilds.forEach(function(tag){
if (tag.children.length) {
var selector = utils.getSelector(tag, group);
// if matched element has no classes at all
// lets keep the nth-child for it
if (/ [^ .]+$/.test(selector)) {
selector = selector.replace(/:nth-child\(\d+?\)(?= )/g, '');
} else {
selector = selector.replace(/:nth-child\(\d+?\)/g, '');
}
selector = selector.replace(/^.+? (> )?/, '');
var contentMark = utils.toArray(tag.children).map(function (child) {
return child.tagName + '.' + child.className.trim().replace(/\s+/, '.');
}).join(' + ');
if (!prev[selector]) {
paragraphScore[selector] = 0;
} else if (prev[selector] !== contentMark) {
paragraphScore[selector]++;
} else {
paragraphScore[selector]--;
}
prev[selector] = contentMark;
}
});
});
var biggestContainer = 0;
var paragraphContainer = '';
for (var sel in paragraphScore) {
// check if there is approximately one match in each container
// 10% tolerance
var diff = groups.length - $containers.find(sel).closest($containers).length;
if (Math.abs(diff) < groups.length / 10) {
// check if score is good enough =
if (paragraphScore[sel] * 2 > $containers.length) {
if (paragraphScore[sel] > biggestContainer) {
biggestContainer = paragraphScore[sel];
paragraphContainer = sel;
}
}
}
}
return paragraphContainer;
},
breakByTextPatterns: function (arr) {
var words_already_analized = [];
var new_columns = {};
arr.forEach(function (phrase) {
if (!phrase) {
return;
}
phrase.split(/\s+/).forEach(function (word) {
// Detection of number followed by word, e.g. 24 comments
var singular = word.replace(/ies$/, 'y').replace(/(.)s$/, '$1');
if (/^\d+$/.test(word) || words_already_analized.indexOf(singular) !== -1) {
return;
} else {
words_already_analized.push(singular);
}
var column = singular;
new_columns[column] = [];
arr.forEach(function (phrase, k) {
var numberRegex = /\d+\s+/g;
var submatches;
while ((submatches = numberRegex.exec(phrase)) !== null) {
if (numberRegex.lastIndex === phrase.indexOf(word)) {
new_columns[column][k] = submatches[0] + word;
break;
}
if (numberRegex.lastIndex === phrase.indexOf(singular)) {
new_columns[column][k] = submatches[0] + singular;
break;
}
}
});
// at least half rows must have occurrence
if (Object.keys(new_columns[column]).length * 2 < arr.length) {
delete new_columns[column];
}
});
});
return new_columns;
},
getDeepestChildNodesInOrder: function (tag, avoidChildrenOf, result, alreadyTags) {
result = result || [];
alreadyTags = alreadyTags || [];
if (alreadyTags.indexOf(tag) === -1) {
alreadyTags.push(tag);
var isVisible = utils.isVisibleNode(tag);
var isChildless = (tag.childNodes.length === 0 || (avoidChildrenOf && utils.matches(tag, avoidChildrenOf)));
if (isVisible && isChildless && !utils.matches(tag, 'br, hr')) {
result.push(tag);
}
if (tag.childNodes[0] && !(avoidChildrenOf && utils.matches(tag, avoidChildrenOf))) {
return utils.getDeepestChildNodesInOrder(tag.childNodes[0], avoidChildrenOf, result, alreadyTags);
}
}
// if already reached initial tag lets finish this recursion
if (tag === alreadyTags[0]) {
return result;
}
return utils.getDeepestChildNodesInOrder(tag.nextSibling || tag.parentNode, avoidChildrenOf, result, alreadyTags);
},
getSelector: function (a, roots) {
var curr = a;
var selector = '';
while (curr) {
if (curr.tagName === 'HTML') {
break;
}
var sel = '';
sel = curr.tagName.toLowerCase();
if (curr.className && curr.tagName !== 'BODY') {
sel += curr.className.trim().replace(/\s+|^/g, '.');
}
if (curr.parentElement && curr.tagName !== 'BODY') {
var siblings = [].slice.call(curr.parentElement.children);
var nth_child = siblings.indexOf(curr) + 1;
// if (nth_child !== 1)
sel += ':nth-child(' + nth_child + ')';
}
if (curr !== a) {
sel += ' > ';
}
selector = sel + selector;
if (roots && curr && roots.indexOf(curr) !== -1) {
break;
}
curr = curr.parentElement;
}
return selector.trim();
}
};
return utils;
});