forked from prettydiff/prettydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharDecoder.js
More file actions
134 lines (125 loc) · 5.24 KB
/
charDecoder.js
File metadata and controls
134 lines (125 loc) · 5.24 KB
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
/*
This code may be used internally to Travelocity without limitation,
exclusion, or restriction. If this code is used externally the
following comment must be included everywhere this code is used.
*/
/***********************************************************************
This is written by Austin Cheney on 21 Apr 2010. Anybody may use this
code without permission so long as this comment exists verbatim in each
instance of its use.
http://www.travelocity.com/
http://mailmarkup.org/
http://prettydiff.com/
**********************************************************************/
/*
This code transforms any combination of decimal and hexidecimal Unicode
character entities into character literals. This function accepts
entities as decimal and hex entities. This code does not provide a
Unicode character map. Instead it is reliant upon the character map
available by the interpreting software. In order to use the processing
application's character map the function must fill an arbitrary XML or
HTML element with a processed character entity and then retrieve it
back. The variable "o.rh" represents the arbitrary XML or HTML element
and is not defined in this function. Therefore this function requires
access to a DOM with an arbitrary element mapped to a variable named
"o.rh". If that element is not present and does not have the innerHTML
property then this function terminates without altering the input.
If the processing software does not recognize characters supplied by
the provided entity then this function should not expect to work. The
limitation, in this case, is not my code.
Decimal input requirements:
This function will only recognize decimal entities if they begin with
ampersand and pound characters "&#", a decimal number one to six digits
long and terminated by a simicolon.
Decimal input examples:
	
%
𱱣
Hexidecimal input requirements:
This function will only recognize hexidecimal entities if they begin
with a lowercase u, a plus, a four or five digit hexidecimal value and
terminated by a plus. Since a proper Unicode entity in hexidecimal may
be four or five digits a terminating character is required so that my
code knows in advance if the entity ends at only four digits. If a
numeric value less than four digits is supplied it must be padded with
0s until it becomes four characters.
Hexidecimal input examples:
u+0009+
u+004b+
u+1037a+
*/
var charDecoder = function (input) {
"use strict";
var b = 0,
d = 0,
a = 0,
f = 0,
g = 0,
c = input,
e = [],
x = [],
y = [],
uni = (/u\+[0-9a-f]{4,5}\+/),
unit = (/u\![0-9a-f]{4,5}\+/),
htmln = (/\&\#[0-9]{1,6}\;/),
htmlt = (/\&\![0-9]{1,6}\;/);
if ((!o.rh && !o.rh.innerHTML) || (c.search(unit) === -1 && c.search(uni) === -1 && c.search(htmlt) === -1 && c.search(htmln) === -1)) {
return input;
}
f = input.length;
for (b = 0; b < f; b += 1) {
if (c.search(htmln) === -1 || (c.search(uni) < c.search(htmln) && c.search(uni) !== -1)) {
d = c.search(uni);
y.push(d + "|h");
g = c.length;
for (a = d; a < g; a += 1) {
if (c.charAt(a) === "+" && c.charAt(a - 1) === "u") {
e = c.split("");
e.splice(a, 1, "!");
c = e.join("");
}
if (c.charAt(a) === "+" && c.charAt(a - 1) !== "u") {
a += 1;
break;
}
}
x.push(c.slice(d + 2, a - 1));
c = c.replace(unit, "");
} else if (c.search(uni) === -1 || (c.search(htmln) < c.search(uni) && c.search(htmln) !== -1)) {
d = c.search(htmln);
y.push(d + "|d");
g = c.length;
for (a = d; a < g; a += 1) {
if (c.charAt(a) === "#") {
e = c.split("");
e.splice(a, 1, "!");
c = e.join("");
}
if (c.charAt(a) === ";") {
a += 1;
break;
}
}
x.push(c.slice(d + 2, a - 1));
c = c.replace(htmlt, "");
}
if (c.search(uni) === -1 && c.search(htmln) === -1) {
break;
}
}
c = c.replace(/u\![0-9a-f]{4,5}\+/g, "").replace(/\&\![0-9]{1,6}\;/g, "").split("");
d = x.length;
e = [];
for (b = 0; b < d; b += 1) {
y[b] = y[b].split("|");
if (y[b][1] === "h") {
x[b] = parseInt(x[b], 16);
}
//o.rh represents a pointer to an object in a DOM and is not
//defined in charDecoder
o.rh.innerHTML = "&#" + parseInt(x[b], 10) + ";";
x[b] = o.rh.innerHTML;
e.push(x[b]);
}
return e.join("");
};