2 * This is based loosely on tinymce
3 * @class Roo.htmleditor.TidyEntities
5 * https://github.com/thorn0/tinymce.html/blob/master/tinymce.html.js
7 * Not 100% sure this is actually used or needed.
10 Roo.htmleditor.TidyEntities = {
17 this.namedEntities = this.buildEntitiesLookup(this.namedEntitiesData, 32);
22 buildEntitiesLookup: function(items, radix) {
23 var i, chr, entity, lookup = {};
27 items = typeof(items) == 'string' ? items.split(',') : items;
29 // Build entities lookup table
30 for (i = 0; i < items.length; i += 2) {
31 chr = String.fromCharCode(parseInt(items[i], radix));
32 // Only add non base entities
33 if (!this.baseEntities[chr]) {
34 entity = '&' + items[i + 1] + ';';
75 // Needs to be escaped since the YUI compressor would otherwise break the code
82 // Reverse lookup table for raw entities
91 attrsCharsRegExp : /[&<>\"\u0060\u007E-\uD7FF\uE000-\uFFEF]|[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
92 textCharsRegExp : /[<>&\u007E-\uD7FF\uE000-\uFFEF]|[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
93 rawCharsRegExp : /[<>&\"\']/g,
94 entityRegExp : /&#([a-z0-9]+);?|&([a-z0-9]+);/gi,
95 namedEntities : false,
597 * Encodes the specified string using raw entities. This means only the required XML base entities will be encoded.
600 * @param {String} text Text to encode.
601 * @param {Boolean} attr Optional flag to specify if the text is attribute contents.
602 * @return {String} Entity encoded text.
604 encodeRaw: function(text, attr)
607 return text.replace(attr ? this.attrsCharsRegExp : this.textCharsRegExp, function(chr) {
608 return t.baseEntities[chr] || chr;
612 * Encoded the specified text with both the attributes and text entities. This function will produce larger text contents
613 * since it doesn't know if the context is within a attribute or text node. This was added for compatibility
614 * and is exposed as the DOMUtils.encode function.
616 * @method encodeAllRaw
617 * @param {String} text Text to encode.
618 * @return {String} Entity encoded text.
620 encodeAllRaw: function(text) {
622 return ('' + text).replace(this.rawCharsRegExp, function(chr) {
623 return t.baseEntities[chr] || chr;
627 * Encodes the specified string using numeric entities. The core entities will be
628 * encoded as named ones but all non lower ascii characters will be encoded into numeric entities.
630 * @method encodeNumeric
631 * @param {String} text Text to encode.
632 * @param {Boolean} attr Optional flag to specify if the text is attribute contents.
633 * @return {String} Entity encoded text.
635 encodeNumeric: function(text, attr) {
637 return text.replace(attr ? this.attrsCharsRegExp : this.textCharsRegExp, function(chr) {
638 // Multi byte sequence convert it to a single entity
639 if (chr.length > 1) {
640 return '&#' + (1024 * (chr.charCodeAt(0) - 55296) + (chr.charCodeAt(1) - 56320) + 65536) + ';';
642 return t.baseEntities[chr] || '&#' + chr.charCodeAt(0) + ';';
646 * Encodes the specified string using named entities. The core entities will be encoded
647 * as named ones but all non lower ascii characters will be encoded into named entities.
649 * @method encodeNamed
650 * @param {String} text Text to encode.
651 * @param {Boolean} attr Optional flag to specify if the text is attribute contents.
652 * @param {Object} entities Optional parameter with entities to use.
653 * @return {String} Entity encoded text.
655 encodeNamed: function(text, attr, entities) {
657 entities = entities || this.namedEntities;
658 return text.replace(attr ? this.attrsCharsRegExp : this.textCharsRegExp, function(chr) {
659 return t.baseEntities[chr] || entities[chr] || chr;
663 * Returns an encode function based on the name(s) and it's optional entities.
665 * @method getEncodeFunc
666 * @param {String} name Comma separated list of encoders for example named,numeric.
667 * @param {String} entities Optional parameter with entities to use instead of the built in set.
668 * @return {function} Encode function to be used.
670 getEncodeFunc: function(name, entities) {
671 entities = this.buildEntitiesLookup(entities) || this.namedEntities;
673 function encodeNamedAndNumeric(text, attr) {
674 return text.replace(attr ? t.attrsCharsRegExp : t.textCharsRegExp, function(chr) {
675 return t.baseEntities[chr] || entities[chr] || '&#' + chr.charCodeAt(0) + ';' || chr;
679 function encodeCustomNamed(text, attr) {
680 return t.encodeNamed(text, attr, entities);
682 // Replace + with , to be compatible with previous TinyMCE versions
683 name = this.makeMap(name.replace(/\+/g, ','));
684 // Named and numeric encoder
685 if (name.named && name.numeric) {
686 return this.encodeNamedAndNumeric;
692 return encodeCustomNamed;
694 return this.encodeNamed;
698 return this.encodeNumeric;
701 return this.encodeRaw;
704 * Decodes the specified string, this will replace entities with raw UTF characters.
707 * @param {String} text Text to entity decode.
708 * @return {String} Entity decoded string.
710 decode: function(text)
713 return text.replace(this.entityRegExp, function(all, numeric) {
715 numeric = 'x' === numeric.charAt(0).toLowerCase() ? parseInt(numeric.substr(1), 16) : parseInt(numeric, 10);
717 if (numeric > 65535) {
719 return String.fromCharCode(55296 + (numeric >> 10), 56320 + (1023 & numeric));
721 return t.asciiMap[numeric] || String.fromCharCode(numeric);
723 return t.reverseEntities[all] || t.namedEntities[all] || t.nativeDecode(all);
726 nativeDecode : function (text) {
729 makeMap : function (items, delim, map) {
732 delim = delim || ',';
733 if (typeof items == "string") {
734 items = items.split(delim);
747 Roo.htmleditor.TidyEntities.init();