Projekt

Obecné

Profil

Stáhnout (2.92 KB) Statistiky
| Větev: | Revize:
1
var ALPHA_INDEX = {
2
    '&lt': '<',
3
    '&gt': '>',
4
    '&quot': '"',
5
    '&apos': '\'',
6
    '&amp': '&',
7
    '&lt;': '<',
8
    '&gt;': '>',
9
    '&quot;': '"',
10
    '&apos;': '\'',
11
    '&amp;': '&'
12
};
13

    
14
var CHAR_INDEX = {
15
    60: 'lt',
16
    62: 'gt',
17
    34: 'quot',
18
    39: 'apos',
19
    38: 'amp'
20
};
21

    
22
var CHAR_S_INDEX = {
23
    '<': '&lt;',
24
    '>': '&gt;',
25
    '"': '&quot;',
26
    '\'': '&apos;',
27
    '&': '&amp;'
28
};
29

    
30
/**
31
 * @constructor
32
 */
33
function XmlEntities() {}
34

    
35
/**
36
 * @param {String} str
37
 * @returns {String}
38
 */
39
XmlEntities.prototype.encode = function(str) {
40
    if (!str || !str.length) {
41
        return '';
42
    }
43
    return str.replace(/<|>|"|'|&/g, function(s) {
44
        return CHAR_S_INDEX[s];
45
    });
46
};
47

    
48
/**
49
 * @param {String} str
50
 * @returns {String}
51
 */
52
 XmlEntities.encode = function(str) {
53
    return new XmlEntities().encode(str);
54
 };
55

    
56
/**
57
 * @param {String} str
58
 * @returns {String}
59
 */
60
XmlEntities.prototype.decode = function(str) {
61
    if (!str || !str.length) {
62
        return '';
63
    }
64
    return str.replace(/&#?[0-9a-zA-Z]+;?/g, function(s) {
65
        if (s.charAt(1) === '#') {
66
            var code = s.charAt(2).toLowerCase() === 'x' ?
67
                parseInt(s.substr(3), 16) :
68
                parseInt(s.substr(2));
69

    
70
            if (isNaN(code) || code < -32768 || code > 65535) {
71
                return '';
72
            }
73
            return String.fromCharCode(code);
74
        }
75
        return ALPHA_INDEX[s] || s;
76
    });
77
};
78

    
79
/**
80
 * @param {String} str
81
 * @returns {String}
82
 */
83
 XmlEntities.decode = function(str) {
84
    return new XmlEntities().decode(str);
85
 };
86

    
87
/**
88
 * @param {String} str
89
 * @returns {String}
90
 */
91
XmlEntities.prototype.encodeNonUTF = function(str) {
92
    if (!str || !str.length) {
93
        return '';
94
    }
95
    var strLength = str.length;
96
    var result = '';
97
    var i = 0;
98
    while (i < strLength) {
99
        var c = str.charCodeAt(i);
100
        var alpha = CHAR_INDEX[c];
101
        if (alpha) {
102
            result += "&" + alpha + ";";
103
            i++;
104
            continue;
105
        }
106
        if (c < 32 || c > 126) {
107
            result += '&#' + c + ';';
108
        } else {
109
            result += str.charAt(i);
110
        }
111
        i++;
112
    }
113
    return result;
114
};
115

    
116
/**
117
 * @param {String} str
118
 * @returns {String}
119
 */
120
 XmlEntities.encodeNonUTF = function(str) {
121
    return new XmlEntities().encodeNonUTF(str);
122
 };
123

    
124
/**
125
 * @param {String} str
126
 * @returns {String}
127
 */
128
XmlEntities.prototype.encodeNonASCII = function(str) {
129
    if (!str || !str.length) {
130
        return '';
131
    }
132
    var strLenght = str.length;
133
    var result = '';
134
    var i = 0;
135
    while (i < strLenght) {
136
        var c = str.charCodeAt(i);
137
        if (c <= 255) {
138
            result += str[i++];
139
            continue;
140
        }
141
        result += '&#' + c + ';';
142
        i++;
143
    }
144
    return result;
145
};
146

    
147
/**
148
 * @param {String} str
149
 * @returns {String}
150
 */
151
 XmlEntities.encodeNonASCII = function(str) {
152
    return new XmlEntities().encodeNonASCII(str);
153
 };
154

    
155
module.exports = XmlEntities;
(3-3/3)