remove Ox.encodeHTML and Ox.decodeHTML; move Ox.encodeHTMLEntities and Ox.decodeHTMLEntities to HTML.js

This commit is contained in:
rolux 2012-05-27 12:37:09 +02:00
parent b93750e325
commit 40d1d021d4

View file

@ -235,60 +235,6 @@ Ox.decodeDeflate = function(string, callback) {
image.src = 'data:image/png;base64,' + btoa(data);
};
/*@
Ox.encodeHTML <f> HTML-encodes a string
> Ox.encodeHTML('\'<"&">\'')
'&apos;&lt;&quot;&amp;&quot;&gt;&apos;'
> Ox.encodeHTML('äbçdê')
'&#x00E4;b&#x00E7;d&#x00EA;'
@*/
Ox.encodeHTML = function(string) {
string = Ox.isUndefined(string) ? '' : string.toString();
return Ox.map(string, function(char) {
var code = char.charCodeAt(0);
return code < 128
? (char in Ox.HTML_ENTITIES ? Ox.HTML_ENTITIES[char] : char)
: '&#x' + Ox.pad(code.toString(16).toUpperCase(), 4) + ';';
});
};
/*@
Ox.decodeHTML <f> Decodes an HTML-encoded string
> Ox.decodeHTML('&apos;&lt;&quot;&amp;&quot;&gt;&apos;')
'\'<"&">\''
> Ox.decodeHTML('&#x0027;&#x003C;&#x0022;&#x0026;&#x0022;&#x003E;&#x0027;')
'\'<"&">\''
> Ox.decodeHTML('&auml;b&ccedil;d&ecirc;')
'äbçdê'
> Ox.decodeHTML('&#x00E4;b&#x00E7;d&#x00EA;')
'äbçdê'
> Ox.decodeHTML('<b>bold</b>')
'<b>bold</b>'
@*/
Ox.decodeHTML = function(string) {
// relies on dom, but shorter than using this:
// http://www.w3.org/TR/html5/named-character-references.html
return Ox.decodeHTMLEntities(Ox.element('<div>').html(string).html());
};
Ox.encodeHTMLEntities = function(string) {
return string.replace(
new RegExp('(' + Object.keys(Ox.HTML_ENTITIES).join('|') + ')', 'g'),
function(match) {
return Ox.HTML_ENTITIES[match];
}
);
};
Ox.decodeHTMLEntities = function(string) {
return string.replace(
new RegExp('(' + Ox.values(Ox.HTML_ENTITIES).join('|') + ')', 'g'),
function(match) {
return Ox.keyOf(Ox.HTML_ENTITIES, match);
}
);
};
/*@
Ox.encodeUTF8 <f> Encodes a string as UTF-8
see http://en.wikipedia.org/wiki/UTF-8