-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.js
176 lines (157 loc) · 4.41 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
/*
* Copyright (C) Pootle contributors.
* Copyright (C) Zing contributors.
*
* This file is a part of the Zing project. It is distributed under the GPL3
* or later license. See the LICENSE file for a copy of the license and the
* AUTHORS file for copyright and authorship information.
*/
import $ from 'jquery';
import { qAll } from 'utils/dom';
import {
highlightPunctuation,
highlightEscapes,
highlightHtml,
highlightSymbols,
nl2br,
} from './editor/utils';
import { raw2sym } from './editor/utils/font';
/* Gets current URL's hash */
export function getHash(win) {
// Mozilla has a bug when it automatically unescapes %26 to '&'
// when getting hash from `window.location.hash'.
// So, we have to extract it from the `window.location'.
// Also, we don't need to decodeURIComponent() the hash
// as it will break encoded ampersand again
// (decoding can be done on the higher level, if needed)
return (win || window).location.toString().split('#', 2)[1] || '';
}
function decodeURIParameter(s) {
return decodeURIComponent(s.replace(/\+/g, ' '));
}
export function getParsedHash(hash) {
const params = {};
const r = /([^&;=]+)=?([^&;]*)/g;
let h = hash;
if (h === undefined) {
h = getHash();
}
let e = r.exec(h);
while (e !== null) {
params[decodeURIParameter(e[1])] = decodeURIParameter(e[2]);
e = r.exec(h);
}
return params;
}
/* Updates current URL's hash */
export function updateHashPart(part, newVal, removeArray, hash) {
const r = /([^&;=]+)=?([^&;]*)/g;
const params = [];
let h = hash;
if (h === undefined) {
h = getHash();
}
let ok = false;
let e = r.exec(h);
while (e !== null) {
const p = decodeURIParameter(e[1]);
if (p === part) {
// replace with the given value
params.push([e[1], encodeURIComponent(newVal)].join('='));
ok = true;
} else if ($.inArray(p, removeArray) === -1) {
// use the parameter as is
params.push([e[1], e[2]].join('='));
}
e = r.exec(h);
}
// if there was no old parameter, push the param at the end,
// if passed parameter is defined
if (!ok && part) {
params.push([encodeURIComponent(part), encodeURIComponent(newVal)].join('='));
}
return params.join('&');
}
/* Cross-browser comparison function */
export function strCmp(a, b) {
let rv;
if (a === b) {
rv = 0;
} else if (a < b) {
rv = -1;
} else {
rv = 1;
}
return rv;
}
export function highlightRO(text) {
return nl2br(
highlightEscapes(
highlightHtml(
raw2sym(
// FIXME: CRLF => LF replacement happens here because highlighting
// currently happens via many DOM sources, and this ensures the less
// error-prone behavior. This won't be needed when the entire editor
// is managed as a component.
text.replace(/\r\n/g, '\n')
)
)
)
);
}
export function highlightRW(text) {
return highlightSymbols(
nl2br(
highlightPunctuation(
highlightEscapes(
highlightHtml(
raw2sym(
// FIXME: CRLF => LF replacement happens here because highlighting
// currently happens via many DOM sources, and this ensures the less
// error-prone behavior. This won't be needed when the entire editor
// is managed as a component.
text.replace(/\r\n/g, '\n')
),
'js-editor-copytext'
),
'js-editor-copytext'
),
'js-editor-copytext'
)
),
'js-editor-copytext'
);
}
function highlightNodes(selector, highlightFn) {
qAll(selector).forEach((translationTextNode) => {
const dataString = translationTextNode.dataset.string;
const textValue = dataString
? JSON.parse(`"${dataString}"`)
: translationTextNode.textContent;
// eslint-disable-next-line no-param-reassign
translationTextNode.innerHTML = highlightFn(textValue);
});
}
export function highlightRONodes(selector) {
return highlightNodes(selector, highlightRO);
}
export function highlightRWNodes(selector) {
return highlightNodes(selector, highlightRW);
}
export function blinkClass($elem, className, n, delay) {
$elem.toggleClass(className);
if (n > 1) {
setTimeout(() => blinkClass($elem, className, n - 1, delay), delay);
}
}
export default {
blinkClass,
highlightRO,
highlightRW,
highlightRONodes,
highlightRWNodes,
getHash,
getParsedHash,
strCmp,
updateHashPart,
};