-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtlthis.js
204 lines (177 loc) · 6.21 KB
/
rtlthis.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
!function(){
var rtlThis = {
version: '0.1.0',
options: {
classPrefix: 'rtl-',
template: '<bdo dir="rtl" class="{{prefix}}{{code}}">$&</bdo>',
scripts: {
ara: {
name: 'Arabic',
regexp: /[[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]]{1}[[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]\d\s]+[[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]]{1}/g
},
div: {
name: 'Dhivehi',
regexp: /[\u0780-\u07BF]{1}[\u0780-\u07BF\d\s]+[\u0780-\u07BF]{1}/g,
},
heb: {
name: 'Hebrew',
regexp: /[\u0590-\u05FF]{1}[\u0590-\u05FF\d\s]+[\u0590-\u05FF]{1}/g
},
man: {
name: 'Mandaic',
regexp: /[\u0840–\u085F]{1}[\u0840–\u085F\d\s]+[\u0840–\u085F]{1}/g
},
men: {
name: 'Mende Kikakui',
regexp: /[\u1E800–\u1E8DF]{1}[\u1E800–\u1E8DF\d\s]+[\u1E800–\u1E8DF]{1}/g
},
nko: {
name: 'N\'Ko',
regexp: /[\u07C0-\u07FF]{1}[\u07C0-\u07FF\d\s]+[\u07C0-\u07FF]{1}/g
},
syc: {
name: 'Syriac',
regexp: /[\u0700-\u074F]{1}[\u0700-\u074F\d\s]+[\u0700-\u074F]{1}/g
},
tif: {
name: 'Tifinagh',
regexp: /[\u2D30-\u2D7F]{1}[\u2D30-\u2D7F\d\s]+[\u2D30-\u2D7F]{1}/g
},
urd: {
name: 'Urdu',
regexp: /[\u0600-\u06FF]{1}[\u0600-\u06FF\d\s]+[\u0600-\u06FF]{1}/g
}
}
}
};
/**
* Run rtlThis
*
* @param elemOrQuery DOM Element or Selector Query string
* @param options Options object
*/
rtlThis.run = function (elemOrQuery, options) {
// Prepare the options
var options = extend(this.options, options);
// Prepare the templates
for (var code in options.scripts) {
if (!options.scripts[code].template) {
options.scripts[code].template = options.template;
}
options.scripts[code].template = options.scripts[code].template.replace(/{{.+?}}/g, function (key) {
var value = '';
switch (key) {
case '{{prefix}}':
value = options.classPrefix;
break;
case '{{code}}':
value = code;
break;
case '{{name}}':
value = options.scripts[code].name;
break;
}
return value;
})
}
// RTL the target elements and return successfully applied elements
return getElements(elemOrQuery).filter(function (node) {
// Scan
domWalk(node, function (node) {
var code = rtlThis.matchScript(node, options.scripts);
//console.log(node);
if (code !== false) {
rtlThis.decorateText(node, options.scripts[code]);
}
});
return true;
}, this);
return this;
}
/**
* Detect and return the script used in the text content of the given node
*
* @param node Node
* @param scripts Array of scripts and matching regexes
* @return string
*/
rtlThis.matchScript = function (node, scripts) {
if (node.nodeType === Node.TEXT_NODE) {
for (var code in scripts) {
if (scripts[code].regexp.test(node.data)) {
return code;
}
}
}
return false;
}
/**
* Decorate the RegExp matches in the given node as per a template
*
* @param node
* @param script
* @return boolean
*/
rtlThis.decorateText = function (node, script) {
// Wrap script as per the given template
var html = node.textContent.replace(script.regexp, script.template);
if (html) {
// Replace the node with the new fragment
node.parentNode.replaceChild(document.createRange().createContextualFragment(html), node);
return true;
}
return false;
}
/**
* Return the (list of) elements specified
*
* @param elemOrQuery DOM Element or string specifying a selector query
* @return Array
*/
var getElements = function (elemOrQuery) {
var elems = [];
// Check type of argument (DOM Element or Selector Query):
if (typeof elemOrQuery == 'object' && elemOrQuery.nodeType === Node.ELEMENT_NODE) {
// DOM Element:
elems.push(elemOrQuery);
}
else if (typeof elemOrQuery == 'string') {
// Selector query:
nodeList = document.querySelectorAll(elemOrQuery);
// Convert nodelist to array
elems = Array.prototype.slice.apply(nodeList);
}
return elems;
}
/**
* DOM walker
*
* Source: http://www.javascriptcookbook.com/article/Traversing-DOM-subtrees-with-a-recursive-walk-the-DOM-function/
*/
var domWalk = function (node, func) {
func(node, this);
node = node.firstChild;
while (node) {
domWalk(node, func);
node = node.nextSibling;
}
}
/**
* Create a new object, extending from two objects
*
* @param a Base object
* @param b Second object
* @return Object
*/
var extend = function (a, b) {
for (var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
if (typeof define === "function" && define.amd) define(rtlThis);
else if (typeof module === "object" && module.exports) module.exports = rtlThis;
this.rtlThis = rtlThis;
}();