1
|
|
|
|
2
|
|
|
export function getAttr (str, attr) { |
3
|
|
|
var rex = new RegExp(attr + '=["|\']([\\S\\s]*?)["|\']( +[a-zA-Z0-9-]*?=|}})') |
4
|
|
|
var res = rex.exec(str) |
5
|
|
|
res = (typeof res !== null && res !== null && res.length > 1) ? res[1] : '' |
6
|
|
|
return res |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Return array of tag found into html text |
11
|
|
|
* |
12
|
|
|
* @param {String} text html |
13
|
|
|
* @param {String} tag name |
14
|
|
|
* @return {Array} array of tags |
15
|
|
|
* |
16
|
|
|
* if a tag is inside a tag |
17
|
|
|
* |
18
|
|
|
* <div> |
19
|
|
|
* <div></div> |
20
|
|
|
* <div> |
21
|
|
|
* |
22
|
|
|
* Will return the whole div (use querySelectorTags for that) |
23
|
|
|
*/ |
24
|
|
|
function explodeTag(text, tag) { |
25
|
|
|
var startWithTags = false |
|
|
|
|
26
|
|
|
if(text.indexOf('<' + tag) === 0) { |
27
|
|
|
startWithTags = true |
28
|
|
|
} |
29
|
|
|
var tags = text.split('<' + tag) |
30
|
|
|
|
31
|
|
|
var i = 0 |
32
|
|
|
var reconstruct = [] |
33
|
|
|
var wait = '' |
34
|
|
|
|
35
|
|
|
reconstruct.push(tags.shift()) |
36
|
|
|
Array.prototype.forEach.call(tags, function(ele) { |
37
|
|
|
i++ |
38
|
|
|
var matches = ele.match(escapeTextToRegex('</' + tag, 'g')) |
39
|
|
|
if(typeof matches !== 'undefined' && matches !== null && matches.length > 0) { |
40
|
|
|
Array.prototype.forEach.call(matches, function(match) { |
|
|
|
|
41
|
|
|
i-- |
42
|
|
|
}) |
43
|
|
|
|
44
|
|
|
if(i === 0) { |
45
|
|
|
reconstruct.push(wait + '<' + tag + ele) |
46
|
|
|
wait = '' |
47
|
|
|
}else { |
48
|
|
|
wait += '<' + tag + ele |
49
|
|
|
} |
50
|
|
|
}else { |
51
|
|
|
wait += '<' + tag + ele |
52
|
|
|
} |
53
|
|
|
}) |
54
|
|
|
return reconstruct |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return all tags inside text html |
59
|
|
|
* |
60
|
|
|
* @param {String} text html |
61
|
|
|
* @param {String} tag name |
62
|
|
|
* @return {Array} array of tags |
63
|
|
|
* |
64
|
|
|
* Exemple |
65
|
|
|
* |
66
|
|
|
* <div class="cl-1"> |
67
|
|
|
* <div class="cl-2"></div> |
68
|
|
|
* <div> |
69
|
|
|
* |
70
|
|
|
* will return an array with 2 |
71
|
|
|
* |
72
|
|
|
* [ |
73
|
|
|
* '<div class="cl-1"> |
74
|
|
|
* <div class="cl-2"></div> |
75
|
|
|
* <div>', |
76
|
|
|
* |
77
|
|
|
* '<div class="cl-2"></div>' |
78
|
|
|
* ] |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
|
|
function querySelectorTags(text, tag) { |
82
|
|
|
var res = [] |
83
|
|
|
var finalRes = [] |
84
|
|
|
|
85
|
|
|
while(text !== ''){ |
86
|
|
|
var checkTags = explodeTag(text, tag) |
87
|
|
|
text = '' |
88
|
|
|
Array.prototype.forEach.call(checkTags, function(ele) { |
89
|
|
|
res.push(ele) |
90
|
|
|
|
91
|
|
|
var matches = ele.match(escapeTextToRegex('<' + tag, 'g')) |
92
|
|
|
if(typeof matches !== 'undefined' && matches !== null && matches.length > 1) { |
93
|
|
|
var tagLength = '<' + tag |
94
|
|
|
tagLength = tagLength.length |
95
|
|
|
|
96
|
|
|
// remove first tag |
97
|
|
|
var start = ele.indexOf('<' + tag) |
98
|
|
|
ele = ele.substring(start + tagLength) |
99
|
|
|
|
100
|
|
|
// remove end tag |
101
|
|
|
var end = ele.lastIndexOf('</' + tag) |
102
|
|
|
ele = ele.substring(0, end) |
103
|
|
|
|
104
|
|
|
text += ele |
|
|
|
|
105
|
|
|
} |
106
|
|
|
}) |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
Array.prototype.forEach.call(res, function(ele) { |
110
|
|
|
var matches = ele.match(escapeTextToRegex('<' + tag, 'g')) |
111
|
|
|
if(typeof matches !== 'undefined' && matches !== null && matches.length > 0) { |
112
|
|
|
var finalEleReg = new RegExp('<' + tag + '([\\s\\S]*?)<\\/' + tag + '>(?![\\s\\S]*<\/' + tag + '>)') |
113
|
|
|
finalEleReg = finalEleReg.exec(ele) |
114
|
|
|
if(typeof finalEleReg !== 'undefined' && finalEleReg !== null && finalEleReg.length > 0) { |
115
|
|
|
finalRes.push(finalEleReg[0]) |
116
|
|
|
}else { |
117
|
|
|
finalRes.push(ele) |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
}) |
121
|
|
|
|
122
|
|
|
return finalRes |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check if an html tag wrap an other tag and return only the deepest one |
127
|
|
|
* |
128
|
|
|
* @param {Array} arr of tags |
129
|
|
|
* @return {Boolean} true|false |
130
|
|
|
*/ |
131
|
|
|
function isInsideOtherTag(arr) { |
132
|
|
|
var res = [] |
|
|
|
|
133
|
|
|
|
134
|
|
|
arr = arr.reverse() |
135
|
|
|
|
136
|
|
|
var uniq = arr.reduce(function(a, b) { |
137
|
|
|
var isUniq = true |
138
|
|
|
Array.prototype.forEach.call(arr, function(c) { |
139
|
|
|
if(c !== b && b.indexOf(c) !== -1) { |
140
|
|
|
isUniq = false |
141
|
|
|
} |
142
|
|
|
}) |
143
|
|
|
if(isUniq) a.push(b) |
|
|
|
|
144
|
|
|
|
145
|
|
|
return a |
146
|
|
|
},[]) |
147
|
|
|
|
148
|
|
|
return uniq |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* return html tags that enclose a string |
153
|
|
|
* |
154
|
|
|
* @param {String} text html |
155
|
|
|
* @param {String} match string to match |
156
|
|
|
* @param {String} tag html tag name |
157
|
|
|
* @return {Array} array of matches |
158
|
|
|
*/ |
159
|
|
|
export function getEnclosingTags(text, match, tag) { |
160
|
|
|
var res = [] |
161
|
|
|
var tags = querySelectorTags(text, tag) |
162
|
|
|
|
163
|
|
|
Array.prototype.forEach.call(tags, function(tag) { |
164
|
|
|
var matches = tag.match(escapeTextToRegex(match, 'g')) |
165
|
|
|
if(typeof matches !== 'undefined' && matches !== null && matches.length > 0) { |
166
|
|
|
res.push(tag) |
167
|
|
|
} |
168
|
|
|
}) |
169
|
|
|
|
170
|
|
|
res = isInsideOtherTag(res) |
171
|
|
|
|
172
|
|
|
return res |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* escape a regex |
177
|
|
|
* @param {String} str |
178
|
|
|
* @param {String} params g,m,i |
179
|
|
|
* @return {Object} RegExp |
180
|
|
|
*/ |
181
|
|
|
export function escapeTextToRegex(str, params) { |
182
|
|
|
str = str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') |
183
|
|
|
return new RegExp(str, params) |
184
|
|
|
} |