-
Notifications
You must be signed in to change notification settings - Fork 0
/
srbtranslitToCyr.js
executable file
·144 lines (137 loc) · 2.9 KB
/
srbtranslitToCyr.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
/*eslint-env es6*/
/*global document*/
// noinspection JSNonASCIINames
let replaceLatToCyr = {
// multi letters first
"Nja": "Ња",
"Nje": "Ње",
"Nji": "Њи",
"Njo": "Њо",
"Nju": "Њу",
"Lja": "Ља",
"Lje": "Ље",
"Lji": "Љи",
"Ljo": "Љо",
"Lju": "Љу",
"Dža": "Џа",
"Dže": "Џе",
"Dži": "Џи",
"Džo": "Џо",
"Džu": "Џу",
"LJ": "Љ",
"NJ": "Њ",
"DŽ": "Џ",
"nj": "њ",
"lj": "љ",
"dž": "џ",
// then the rest
"A": "А",
"B": "Б",
"V": "В",
"G": "Г",
"D": "Д",
"Đ": "Ђ",
"E": "Е",
"Ž": "Ж",
"Z": "З",
"I": "И",
"J": "Ј",
"K": "К",
"L": "Л",
"M": "М",
"N": "Н",
"O": "О",
"P": "П",
"R": "Р",
"S": "С",
"Š": "Ш",
"T": "Т",
"Ć": "Ћ",
"U": "У",
"F": "Ф",
"H": "Х",
"C": "Ц",
"Č": "Ч",
"a": "а",
"b": "б",
"v": "в",
"g": "г",
"d": "д",
"đ": "ђ",
"e": "е",
"ž": "ж",
"z": "з",
"i": "и",
"j": "ј",
"k": "к",
"l": "л",
"m": "м",
"n": "н",
"o": "о",
"p": "п",
"r": "р",
"s": "с",
"š": "ш",
"t": "т",
"ć": "ћ",
"u": "у",
"f": "ф",
"h": "х",
"c": "ц",
"č": "ч",
},
getElementsWithNoChildren = function (target) {
'use strict';
let candidates;
if (target && typeof target.querySelectorAll === 'function') {
candidates = target.querySelectorAll('*');
} else if (target && typeof target.length === 'number') {
candidates = target;
} else {
candidates = document.querySelectorAll('*');
}
return Array.from(candidates).filter((elem) => {
if (elem.children.length === 0) {
return true;
} else {
let pass = false;
elem.childNodes.forEach(function (text) {
if (typeof text.nodeValue !== 'undefined') {
pass = typeof text.nodeValue !== 'undefined';
}
});
return pass;
}
// return elem.children.length === 0;
});
},
allElements;
function transliterateToCyr(word) {
'use strict';
//TODO needs better splitting to assume multi characters
return word.split('').map(function (char) {
return replaceLatToCyr[char] || char;
}).join('');
}
function iterator(text) {
if (text.nodeValue && text.nodeValue.trim() !== '') {
text.nodeValue = transliterateToCyr(text.nodeValue);
}
}
function srbTranslit() {
'use strict';
let x,
elem;
allElements = getElementsWithNoChildren(document);
for (x = 0; x < allElements.length; x += 1) {
elem = allElements[x];
if (elem.children.length === 0) {
if (elem.textContent && elem.textContent !== '') {
elem.textContent = transliterateToCyr(elem.textContent);
}
} else {
elem.childNodes.forEach(iterator);
}
}
}
srbTranslit();