1
0
mirror of https://github.com/twitter/twemoji.git synced 2024-09-28 23:00:50 +00:00

made consistent with already used style

This commit is contained in:
Andrea Giammarchi 2015-04-11 18:09:02 +01:00
parent 226e5ac6d5
commit ca1c298ddd
5 changed files with 229 additions and 141 deletions

View File

@ -500,10 +500,22 @@ function createTwemoji(re) {
test: test test: test
}, },
// used to escape HTML special chars in attributes
escaper = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
"'": '&#39;',
'"': '&quot;'
},
// RegExp based on emoji's official Unicode standards // RegExp based on emoji's official Unicode standards
// http://www.unicode.org/Public/UNIDATA/EmojiSources.txt // http://www.unicode.org/Public/UNIDATA/EmojiSources.txt
re = /twemoji/, re = /twemoji/,
// used to find HTML special chars in attributes
rescaper = /[&<>'"]/g,
// nodes with type 1 which should **not** be parsed // nodes with type 1 which should **not** be parsed
shouldntBeParsed = /IFRAME|NOFRAMES|NOSCRIPT|SCRIPT|SELECT|STYLE|TEXTAREA/, shouldntBeParsed = /IFRAME|NOFRAMES|NOSCRIPT|SCRIPT|SELECT|STYLE|TEXTAREA/,
@ -533,18 +545,9 @@ function createTwemoji(re) {
* @return string text encoded to use in HTML attribute * @return string text encoded to use in HTML attribute
*/ */
function escapeHTML(s) { function escapeHTML(s) {
var escaped = { return s.replace(rescaper, replacer);
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
"'": '&#39;',
'"': '&quot;'
};
return s.replace(/[&<>'"]/g, function (m) {
return escaped[m];
});
} }
/** /**
* Default callback used to generate emoji src * Default callback used to generate emoji src
* based on Twitter CDN * based on Twitter CDN
@ -622,6 +625,8 @@ function createTwemoji(re) {
var var
allText = grabAllTextNodes(node, []), allText = grabAllTextNodes(node, []),
length = allText.length, length = allText.length,
attrib,
attrname,
modified, modified,
fragment, fragment,
subnode, subnode,
@ -661,19 +666,17 @@ function createTwemoji(re) {
img = new Image(); img = new Image();
img.onerror = twemoji.onerror; img.onerror = twemoji.onerror;
img.setAttribute('draggable', 'false'); img.setAttribute('draggable', 'false');
attrib = options.attributes(icon, variant);
var attrib = options.attributes(icon, variant); for (attrname in attrib) {
if (attrib) { if (
for (var attrname in attrib) { attrib.hasOwnProperty(attrname) &&
if (attrib.hasOwnProperty(attrname)) { // don't allow any handlers to be set + don't allow overrides
// don't allow any handlers to be set, don't allow overrides attrname.indexOf('on') !== 0 &&
if (attrname.indexOf('on') !== 0 && !img.hasAttribute(attrname)) { !img.hasAttribute(attrname)
img.setAttribute(attrname, attrib[attrname]); ) {
} img.setAttribute(attrname, attrib[attrname]);
}
} }
} }
img.className = options.className; img.className = options.className;
img.alt = alt; img.alt = alt;
img.src = src; img.src = src;
@ -715,8 +718,11 @@ function createTwemoji(re) {
*/ */
function parseString(str, options) { function parseString(str, options) {
return replace(str, function (match, icon, variant) { return replace(str, function (match, icon, variant) {
var src; var
var ret = match; ret = match,
attrib,
attrname,
src;
// verify the variant is not the FE0E one // verify the variant is not the FE0E one
// this variant means "emoji as text" and should not // this variant means "emoji as text" and should not
// require any action/replacement // require any action/replacement
@ -742,18 +748,17 @@ function createTwemoji(re) {
src, src,
'"' '"'
); );
var attrib = options.attributes(icon, variant); attrib = options.attributes(icon, variant);
if (attrib) { for (attrname in attrib) {
for (var attrname in attrib) { if (
if (attrib.hasOwnProperty(attrname)) { attrib.hasOwnProperty(attrname) &&
// don't allow any handlers to be set, don't allow overrides // don't allow any handlers to be set + don't allow overrides
if (attrname.indexOf('on') !== 0 && ret.indexOf(' ' + attrname + '=') === -1) { attrname.indexOf('on') !== 0 &&
ret = ret.concat(' ', attrname, '="', escapeHTML(attrib[attrname]), '"'); ret.indexOf(' ' + attrname + '=') === -1
} ) {
} ret = ret.concat(' ', attrname, '="', escapeHTML(attrib[attrname]), '"');
} }
} }
ret = ret.concat('>'); ret = ret.concat('>');
} }
} }
@ -761,6 +766,23 @@ function createTwemoji(re) {
}); });
} }
/**
* Function used to actually replace HTML special chars
* @param string HTML special char
* @return string encoded HTML special char
*/
function replacer(m) {
return escaper[m];
}
/**
* Default options.attribute callback
* @return null
*/
function returnNull() {
return null;
}
/** /**
* Given a generic value, creates its squared counterpart if it's a number. * Given a generic value, creates its squared counterpart if it's a number.
* As example, number 36 will return '36x36'. * As example, number 36 will return '36x36'.
@ -802,7 +824,7 @@ function createTwemoji(re) {
// otherwise use the DOM tree and parse text nodes only // otherwise use the DOM tree and parse text nodes only
return (typeof what === 'string' ? parseString : parseNode)(what, { return (typeof what === 'string' ? parseString : parseNode)(what, {
callback: how.callback || defaultImageSrcGenerator, callback: how.callback || defaultImageSrcGenerator,
attributes: typeof how.attributes === 'function' ? how.attributes : function() {return {};}, attributes: typeof how.attributes === 'function' ? how.attributes : returnNull,
base: typeof how.base === 'string' ? how.base : twemoji.base, base: typeof how.base === 'string' ? how.base : twemoji.base,
ext: how.ext || twemoji.ext, ext: how.ext || twemoji.ext,
size: how.folder || toSizeSquaredAsset(how.size || twemoji.size), size: how.folder || toSizeSquaredAsset(how.size || twemoji.size),

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
twemoji.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long