-
Notifications
You must be signed in to change notification settings - Fork 10
/
ASpace.js
48 lines (43 loc) · 1.63 KB
/
ASpace.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
/**
* ASpace function
*
* @param {Object} target - it can be element array, element, string
*/
function aSpace(target) {
var EnglishCJK = /([a-zA-Z0-9\,\:\?\!\+\*])([\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af])/g;
var CJKEnglish = /([\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af])([a-zA-Z0-9\+\*])/g;
var ignoreTags = ['script', 'title', 'meta', 'style'];
var ignoreClass = 'no-space';
var type = Object.prototype.toString.call(target);
if (type === '[object String]') {
return space(target);
}
else if (type === '[object HTMLDivElement]') {
spaceNode(target);
}
else if (type === '[object HTMLCollection]') {
for (var i = 0; i < target.length; i++) {
spaceNode(target[i]);
}
}
function test(string) {
return EnglishCJK.test(string) || CJKEnglish.test(string)
}
function space(string) {
return string.replace(CJKEnglish, '$1 $2').replace(EnglishCJK, '$1 $2');
}
function spaceNode(node) {
for (var i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].nodeType === 3
&& node.childNodes[i].nodeValue
&& test(node.childNodes[i].nodeValue)) {
node.childNodes[i].nodeValue = space(node.childNodes[i].nodeValue);
}
else if (node.childNodes[i].nodeType === 1
&& !node.childNodes[i].classList.contains(ignoreClass)
&& ignoreTags.indexOf(node.childNodes[i].nodeName.toLowerCase()) === -1) {
spaceNode(node.childNodes[i]);
}
}
}
}