-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathescape.js
77 lines (65 loc) · 2.24 KB
/
escape.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
define(["dojo"], function(dojo){
var d = dojo;
// two regexp functions to replace a character with an encoded entity
var ere = /.{1}/g,
_encodeChar = function(ch){
return ch in d._encoding ? "&" + d._encoding[ch] + ";" : ch;
},
// or to replace an entity match
dre = /&\w+;/g,
_decodeFrag = function(frag){
return frag in d._reverse ? d._reverse[frag] : frag;
}
;
d.mixin(d,{
// _encoding: Object
// An encoding map. Mix into this object and call `dojo._setupReverse`
// to extend the encoding/decoding scope. object is hash of key/pairs
// of characters and their html entity without the & or ;
// Includes a minimal set of encoding entities to solve simple cases.
_encoding:{
"<":"lt", ">":"gt", '"':"quot", "&":"amp", "©":"copy"
},
// _reverse: Object
// The reverse encoding map. populated by `dojo._encoding` map,
// but only after `dojo._setupReverse` is called.
_reverse:{},
_encode: function(/* String */str){
// summary: convert a string of markup into printable characters
return str.replace(ere, _encodeChar) // String
},
_decode: function(str){
// summary: convert an html-entity encoded string into raw markup
return str.replace(dre, _decodeFrag);
},
html_entities: function(/* String */str, /* Boolean? */decode){
// summary:
// Convert some string into html-printable characters.
// Can be extended by mixing into `dojo._encoding`
//
// decode: Boolean?
// If passed, an attempt to re-encode some valid encoded string
// into pure text.
//
// example:
// | var n = dojo.byId('someId');
// | var str = dojo.html_entites(n.innerHTML);
// | dojo.byId("aPreNode").innerHTML = str;
// FIXME: how would you determine if you needed to encode or decode? str.match(/&\w+/)? or not.
return d[(!decode ? "_encode" : "_decode")](str);
}
});
d._setupReverse = function(){
// summary: setup the reverse map for `dojo.html_entities`
//
// description:
// setup the reverse map for `dojo.html_entities`.
// call it once, and if anyone adds to the `dojo._encoding` map, they must call it again:
//
for(var i in d._encoding){
d._reverse["&" + d._encoding[i] + ";"] = i + "";
}
}
d._setupReverse();
return d;
});