2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2012-05-27 10:36:16 +00:00
|
|
|
(function() {
|
2011-10-07 01:04:47 +00:00
|
|
|
|
|
|
|
var defaultTags = [
|
2011-10-27 18:50:23 +00:00
|
|
|
// inline formatting
|
2013-08-27 08:52:22 +00:00
|
|
|
'b', 'bdi', 'code', 'em', 'i', 'q', 's', 'span', 'strong', 'sub', 'sup', 'u',
|
2011-12-31 12:57:02 +00:00
|
|
|
// block formatting
|
2013-08-27 08:52:22 +00:00
|
|
|
'blockquote', 'cite', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'pre',
|
2011-10-27 18:50:23 +00:00
|
|
|
// lists
|
|
|
|
'li', 'ol', 'ul',
|
|
|
|
// tables
|
|
|
|
'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr',
|
|
|
|
// other
|
2013-08-27 08:52:22 +00:00
|
|
|
'a', 'br', 'img', 'figure', 'figcaption',
|
2013-10-25 12:38:11 +00:00
|
|
|
// iframe
|
2013-10-24 16:39:26 +00:00
|
|
|
'iframe',
|
2011-10-27 18:50:23 +00:00
|
|
|
// special
|
|
|
|
'rtl', '[]'
|
2011-10-07 01:04:47 +00:00
|
|
|
],
|
2012-05-27 10:36:16 +00:00
|
|
|
htmlEntities = {
|
|
|
|
'"': '"', '&': '&', "'": ''', '<': '<', '>': '>'
|
|
|
|
},
|
|
|
|
regexp = {
|
2012-06-16 20:45:05 +00:00
|
|
|
entity: /&[^\s]+?;/g,
|
|
|
|
html: /[<&]/,
|
2012-05-27 10:36:16 +00:00
|
|
|
tag: new RegExp('<\\/?(' + [
|
|
|
|
'a', 'b', 'br', 'code', 'i', 's', 'span', 'u'
|
|
|
|
].join('|') + ')\\/?>', 'gi')
|
|
|
|
},
|
|
|
|
replace = {
|
2012-05-30 22:47:19 +00:00
|
|
|
a: [
|
|
|
|
[
|
2013-02-27 10:57:44 +00:00
|
|
|
/<a [^<>]*?href="((\/|https?:\/\/|mailto:).*?)".*?>/gi,
|
2012-05-30 22:47:19 +00:00
|
|
|
'<a href="{1}">',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
/<\/a>/gi,
|
|
|
|
'</a>'
|
|
|
|
]
|
2012-05-27 10:36:16 +00:00
|
|
|
],
|
2012-05-30 22:47:19 +00:00
|
|
|
img: [
|
|
|
|
[
|
|
|
|
/<img [^<>]*?src="((\/|https?:\/\/).+?)".*?>/gi,
|
2013-02-15 12:31:28 +00:00
|
|
|
'<img src="{1}">'
|
2012-05-30 22:47:19 +00:00
|
|
|
]
|
2012-05-27 10:36:16 +00:00
|
|
|
],
|
2013-10-24 16:39:26 +00:00
|
|
|
iframe: [
|
|
|
|
[
|
|
|
|
/<iframe [^<>]*?width="(\d+)" height="(\d+)"[^<>]*?src="((\/|https?:\/\/).+?)".*?>/gi,
|
|
|
|
'<iframe width="{1}" height="{2}" src="{3}">'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
/<iframe [^<>]*?src="((\/|https?:\/\/).+?)".*?>/gi,
|
|
|
|
'<iframe src="{1}">'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
/<\/iframe>/gi,
|
|
|
|
'</iframe>'
|
|
|
|
]
|
|
|
|
],
|
2012-05-30 22:47:19 +00:00
|
|
|
rtl: [
|
|
|
|
[
|
|
|
|
/<rtl>/gi,
|
|
|
|
'<div style="direction: rtl">'
|
2012-05-27 10:36:16 +00:00
|
|
|
],
|
2012-05-30 22:47:19 +00:00
|
|
|
[
|
|
|
|
/<\/rtl>/gi,
|
|
|
|
'</div>'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'*': function(tag) {
|
|
|
|
return [
|
2012-05-27 10:36:16 +00:00
|
|
|
[
|
2012-05-30 22:47:19 +00:00
|
|
|
new RegExp('</?' + tag + ' ?/?>', 'gi'),
|
|
|
|
'{0}'
|
2012-05-27 10:36:16 +00:00
|
|
|
]
|
2012-05-30 22:47:19 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
salt = Ox.range(2).map(function(){
|
|
|
|
return Ox.range(16).map(function() {
|
2012-09-03 22:45:43 +00:00
|
|
|
return Ox.char(65 + Ox.random(26));
|
2012-05-30 22:47:19 +00:00
|
|
|
}).join('');
|
|
|
|
});
|
|
|
|
|
|
|
|
function addLinks(string, obfuscate) {
|
|
|
|
return string
|
|
|
|
.replace(
|
2012-06-24 13:37:46 +00:00
|
|
|
/\b((https?:\/\/|www\.).+?)([.,:;!?)\]]*?(\s|$))/gi,
|
2012-05-30 11:32:33 +00:00
|
|
|
function(match, url, prefix, end) {
|
2012-05-27 10:36:16 +00:00
|
|
|
prefix = prefix.toLowerCase() == 'www.' ? 'http://' : '';
|
|
|
|
return Ox.formatString(
|
|
|
|
'<a href="{prefix}{url}">{url}</a>{end}',
|
|
|
|
{end: end, prefix: prefix, url: url}
|
|
|
|
);
|
|
|
|
}
|
2012-05-30 22:47:19 +00:00
|
|
|
)
|
|
|
|
.replace(
|
2012-06-24 13:37:46 +00:00
|
|
|
/\b([0-9A-Z.+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,6})\b/gi,
|
2012-05-30 22:47:19 +00:00
|
|
|
obfuscate ? function(match, mail) {
|
|
|
|
return Ox.encodeEmailAddress(mail);
|
|
|
|
} : '<a href="mailto:$1">$1</a>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeHTMLEntities(string) {
|
|
|
|
return string
|
|
|
|
.replace(
|
|
|
|
new RegExp('(' + Ox.values(htmlEntities).join('|') + ')', 'g'),
|
|
|
|
function(match) {
|
|
|
|
return Ox.keyOf(htmlEntities, match);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
/&#([0-9A-FX]+);/gi,
|
|
|
|
function(match, code) {
|
|
|
|
return Ox.char(
|
|
|
|
/^X/i.test(code)
|
|
|
|
? parseInt(code.slice(1), 16)
|
|
|
|
: parseInt(code, 10)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2012-05-27 10:36:16 +00:00
|
|
|
|
|
|
|
// Splits a string into text (even indices) and tags (odd indices), ignoring
|
|
|
|
// tags with starting positions that are included in the ignore array
|
|
|
|
function splitHTMLTags(string, ignore) {
|
|
|
|
var isTag = false, ret = [''];
|
|
|
|
ignore = ignore || [];
|
|
|
|
Ox.forEach(string, function(char, i) {
|
|
|
|
if (!isTag && char == '<' && ignore.indexOf(i) == -1) {
|
|
|
|
isTag = true;
|
|
|
|
ret.push('');
|
|
|
|
}
|
|
|
|
ret[ret.length - 1] += char;
|
|
|
|
if (isTag && char == '>') {
|
|
|
|
isTag = false;
|
|
|
|
ret.push('');
|
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2012-05-27 10:36:16 +00:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.addLinks <f> Takes a string and adds links for e-mail addresses and URLs
|
|
|
|
(string[, isHTML]) -> <s> Formatted string
|
|
|
|
string <s> String
|
|
|
|
isHTML <b|false> If true, ignore matches in tags or enclosed by links
|
|
|
|
> Ox.addLinks('foo bar <foo@bar.com>')
|
|
|
|
'foo bar <<a href="mailto:foo@bar.com">foo@bar.com</a>>'
|
|
|
|
> Ox.addLinks('www.foo.com/bar#baz, etc.')
|
|
|
|
'<a href="http://www.foo.com/bar#baz">www.foo.com/bar#baz</a>, etc.'
|
2012-05-27 19:32:08 +00:00
|
|
|
> Ox.addLinks('<a href="http://www.foo.com">www.foo.com</a>', true)
|
|
|
|
'<a href="http://www.foo.com">www.foo.com</a>'
|
2012-05-27 10:36:16 +00:00
|
|
|
@*/
|
|
|
|
Ox.addLinks = function(string, isHTML) {
|
|
|
|
var isLink = false;
|
|
|
|
return isHTML
|
|
|
|
? splitHTMLTags(string).map(function(string, i) {
|
|
|
|
var isTag = i % 2;
|
|
|
|
if (isTag) {
|
|
|
|
if (/^<a/.test(string)) {
|
|
|
|
isLink = true;
|
|
|
|
} else if (/^<\/a/.test(string)) {
|
|
|
|
isLink = false;
|
|
|
|
}
|
|
|
|
}
|
2012-05-30 22:47:19 +00:00
|
|
|
return isTag || isLink ? string : addLinks(string);
|
2012-05-27 10:36:16 +00:00
|
|
|
}).join('')
|
2012-05-30 22:47:19 +00:00
|
|
|
: Ox.normalizeHTML(addLinks(string));
|
2012-02-10 09:52:09 +00:00
|
|
|
};
|
2011-10-07 01:04:47 +00:00
|
|
|
|
2012-05-30 11:32:33 +00:00
|
|
|
/*@
|
|
|
|
Ox.encodeEmailAddress <f> Returns obfuscated mailto: link
|
|
|
|
> Ox.encodeEmailAddress('mailto:foo@bar.com').indexOf(':') > -1
|
|
|
|
true
|
|
|
|
@*/
|
|
|
|
Ox.encodeEmailAddress = function(string) {
|
|
|
|
var parts = ['mailto:' + string, string].map(function(part) {
|
|
|
|
return Ox.map(part, function(char) {
|
|
|
|
var code = char.charCodeAt(0);
|
|
|
|
return char == ':' ? ':'
|
|
|
|
: '&#'
|
|
|
|
+ (Math.random() < 0.5 ? code : 'x' + code.toString(16))
|
|
|
|
+ ';'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return '<a href="' + parts[0] + '">' + parts[1] + '</a>';
|
|
|
|
};
|
|
|
|
|
2012-05-27 10:36:16 +00:00
|
|
|
/*@
|
|
|
|
Ox.encodeHTMLEntities <f> Encodes HTML entities
|
|
|
|
(string[, encodeAll]) -> <s> String
|
|
|
|
string <s> String
|
|
|
|
encodeAll <b|false> If true, encode characters > 127 as numeric entities
|
|
|
|
> Ox.encodeHTMLEntities('<\'&"> äbçdê')
|
|
|
|
'<'&"> äbçdê'
|
|
|
|
> Ox.encodeHTMLEntities('<\'&"> äbçdê', true)
|
|
|
|
'<'&"> äbçdê'
|
|
|
|
@*/
|
|
|
|
Ox.encodeHTMLEntities = function(string, encodeAll) {
|
|
|
|
return Ox.map(String(string), function(char) {
|
|
|
|
var code = char.charCodeAt(0);
|
|
|
|
if (code < 128) {
|
|
|
|
char = char in htmlEntities ? htmlEntities[char] : char;
|
|
|
|
} else if (encodeAll) {
|
2012-06-04 09:08:38 +00:00
|
|
|
char = '&#x'
|
|
|
|
+ Ox.pad(code.toString(16).toUpperCase(), 'left', 4, '0')
|
|
|
|
+ ';';
|
2012-05-27 10:36:16 +00:00
|
|
|
}
|
|
|
|
return char;
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-05-27 10:36:16 +00:00
|
|
|
/*@
|
|
|
|
Ox.decodeHTMLEntities <f> Decodes HTML entities
|
|
|
|
(string[, decodeAll]) -> <s> String
|
|
|
|
string <s> String
|
|
|
|
decodeAll <b|false> If true, decode named entities for characters > 127
|
2012-06-02 11:06:44 +00:00
|
|
|
Note that `decodeAll` relies on `Ox.normalizeHTML`, which uses the
|
|
|
|
DOM and may transform the string
|
2012-05-27 10:36:16 +00:00
|
|
|
> Ox.decodeHTMLEntities('<'&">')
|
|
|
|
'<\'&">'
|
|
|
|
> Ox.decodeHTMLEntities('<'&">')
|
|
|
|
'<\'&">'
|
|
|
|
> Ox.decodeHTMLEntities('äbçdê')
|
|
|
|
'äbçdê'
|
|
|
|
> Ox.decodeHTMLEntities('äbçdê')
|
|
|
|
'äbçdê'
|
|
|
|
> Ox.decodeHTMLEntities('äbçdê', true)
|
|
|
|
'äbçdê'
|
|
|
|
> Ox.decodeHTMLEntities('<b>β')
|
|
|
|
'<b>β'
|
|
|
|
> Ox.decodeHTMLEntities('<b>β', true)
|
|
|
|
'<b>β</b>'
|
|
|
|
> Ox.decodeHTMLEntities('<b>')
|
|
|
|
'<b>'
|
|
|
|
@*/
|
|
|
|
Ox.decodeHTMLEntities = function(string, decodeAll) {
|
|
|
|
return decodeAll
|
|
|
|
? Ox.decodeHTMLEntities(Ox.normalizeHTML(string))
|
2012-05-30 22:47:19 +00:00
|
|
|
: decodeHTMLEntities(string);
|
2012-05-27 10:36:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-06-03 08:40:46 +00:00
|
|
|
Ox.highlight <f> Highlight matches in string
|
2012-05-27 10:36:16 +00:00
|
|
|
(string, query, classname[, isHTML]) -> Output string
|
|
|
|
string <s> Input string
|
|
|
|
query <r|s> Case-insentitive query string, or regular expression
|
|
|
|
classname <s> Class name for matches
|
|
|
|
isHTML <b|false> If true, the input string is treated as HTML
|
2012-06-16 20:45:05 +00:00
|
|
|
> Ox.highlight('<foo><bar>', 'foo', 'c')
|
|
|
|
'<<span class="c">foo</span>><bar>'
|
|
|
|
> Ox.highlight('&', '&', 'c')
|
|
|
|
'<span class="c">&amp;</span>'
|
|
|
|
> Ox.highlight('&', '&', 'c')
|
|
|
|
'&'
|
|
|
|
> Ox.highlight('<foo> <foo>', '<foo>', 'c', true)
|
|
|
|
'<span class="c"><foo></span> <span class="c"><foo></span>'
|
2012-06-03 08:40:46 +00:00
|
|
|
> Ox.highlight('<span class="name">name</span>', 'name', 'c', true)
|
2012-05-27 10:36:16 +00:00
|
|
|
'<span class="name"><span class="c">name</span></span>'
|
2012-06-03 08:40:46 +00:00
|
|
|
> Ox.highlight('amp & amp', 'amp', 'c', true)
|
2012-05-27 10:36:16 +00:00
|
|
|
'<span class="c">amp</span> & <span class="c">amp</span>'
|
2012-06-03 08:40:46 +00:00
|
|
|
> Ox.highlight('amp & amp', 'amp & amp', 'c', true)
|
2012-05-27 10:36:16 +00:00
|
|
|
'<span class="c">amp & amp</span>'
|
2012-06-03 08:40:46 +00:00
|
|
|
> Ox.highlight('<b><b></b>', '<b>', 'c', true)
|
2012-05-27 10:36:16 +00:00
|
|
|
'<span class="c"><b><b></b></span>'
|
2012-06-03 08:40:46 +00:00
|
|
|
> Ox.highlight('<b><b></b>', '<b>', 'c', true)
|
2012-05-27 10:36:16 +00:00
|
|
|
'<b><b></b>'
|
2012-06-03 08:40:46 +00:00
|
|
|
> Ox.highlight('foo<b>bar</b>baz', 'foobar', 'c', true)
|
2012-05-27 10:36:16 +00:00
|
|
|
'<span class="c">foo<b>bar</b></span>baz'
|
2012-06-03 08:40:46 +00:00
|
|
|
> Ox.highlight('foo<p>bar</p>baz', 'foobar', 'c', true)
|
2012-05-27 10:36:16 +00:00
|
|
|
'foo<p>bar</p>baz'
|
2012-06-03 08:40:46 +00:00
|
|
|
> Ox.highlight('foo <br/>bar baz', 'foo bar', 'c', true)
|
2012-05-27 10:36:16 +00:00
|
|
|
'<span class="c">foo <br>bar</span> baz'
|
|
|
|
@*/
|
2012-06-03 08:40:46 +00:00
|
|
|
Ox.highlight = function(string, query, classname, isHTML) {
|
2012-06-16 16:17:28 +00:00
|
|
|
if (!query) {
|
|
|
|
return string;
|
|
|
|
}
|
2012-05-27 10:36:16 +00:00
|
|
|
var cursor = 0,
|
|
|
|
entities = [],
|
|
|
|
matches = [],
|
2012-06-16 20:45:05 +00:00
|
|
|
offset = 0,
|
2012-05-27 10:36:16 +00:00
|
|
|
re = Ox.isRegExp(query) ? query
|
|
|
|
: new RegExp(Ox.escapeRegExp(query), 'gi'),
|
|
|
|
span = ['<span class="' + classname + '">', '</span>'],
|
|
|
|
tags = [];
|
|
|
|
function insert(array) {
|
|
|
|
// for each replacement
|
|
|
|
array.forEach(function(v) {
|
|
|
|
// replace the modified value with the original value
|
|
|
|
string = Ox.splice(string, v.position, v.length, v.value);
|
|
|
|
// for each match
|
|
|
|
matches.forEach(function(match) {
|
|
|
|
if (v.position < match.position) {
|
|
|
|
// replacement is before match, update match position
|
|
|
|
match.position += v.value.length - v.length;
|
|
|
|
} else if (
|
|
|
|
v.position < match.position + match.value.length
|
|
|
|
) {
|
|
|
|
// replacement is inside match, update match value
|
|
|
|
match.value = Ox.splice(
|
|
|
|
match.value, v.position - match.position, v.length,
|
|
|
|
v.value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2012-06-16 20:45:05 +00:00
|
|
|
if (isHTML && regexp.html.test(string)) {
|
|
|
|
string = string // Ox.normalizeHTML(string)
|
2012-05-27 10:36:16 +00:00
|
|
|
// remove inline tags
|
|
|
|
.replace(regexp.tag, function(value, tag, position) {
|
|
|
|
tags.push({
|
|
|
|
length: 0, position: position, value: value
|
|
|
|
});
|
|
|
|
return '';
|
|
|
|
})
|
|
|
|
// decode html entities
|
|
|
|
.replace(regexp.entity, function(value, position) {
|
|
|
|
var ret = Ox.decodeHTMLEntities(value, true);
|
|
|
|
entities.push({
|
|
|
|
length: ret.length, position: position, value: value
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
// if decoding entities has created new tags, ignore them
|
|
|
|
splitHTMLTags(string, entities.map(function(entity) {
|
2012-06-16 20:45:05 +00:00
|
|
|
var ret = entity.position + offset;
|
|
|
|
offset += entity.length - entity.value.length
|
|
|
|
return ret;
|
2012-05-27 10:36:16 +00:00
|
|
|
})).forEach(function(v, i) {
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
// outside tags, find matches and save position and value
|
|
|
|
v.replace(re, function(value, position) {
|
|
|
|
matches.push(
|
|
|
|
{position: cursor + position, value: value}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cursor += v.length;
|
|
|
|
});
|
|
|
|
insert(entities);
|
|
|
|
insert(tags);
|
|
|
|
// for each match (in reverse order, so that positions are correct)
|
|
|
|
matches.reverse().forEach(function(match) {
|
|
|
|
// wrap it in a span
|
|
|
|
string = Ox.splice(
|
|
|
|
string, match.position, match.value.length,
|
|
|
|
span.join(match.value)
|
|
|
|
);
|
|
|
|
});
|
2012-06-16 20:45:05 +00:00
|
|
|
// we may have enclosed single opening or closing tags in a span
|
|
|
|
if (matches.length && tags.length) {
|
|
|
|
string = Ox.normalizeHTML(string);
|
|
|
|
}
|
2012-05-27 10:36:16 +00:00
|
|
|
} else {
|
2012-06-16 20:45:05 +00:00
|
|
|
string = Ox.encodeHTMLEntities(
|
|
|
|
string.replace(re, function(value) {
|
|
|
|
matches.push(span.join(Ox.encodeHTMLEntities(value)));
|
|
|
|
return salt.join(matches.length - 1);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
matches.forEach(function(match, i) {
|
|
|
|
string = string.replace(new RegExp(salt.join(i)), match);
|
2012-05-27 10:36:16 +00:00
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2012-06-16 20:45:05 +00:00
|
|
|
return string;
|
2012-05-27 10:36:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.normalizeHTML <f> Normalize HTML (using the DOM)
|
|
|
|
> Ox.normalizeHTML('<b>foo')
|
|
|
|
'<b>foo</b>'
|
|
|
|
> Ox.normalizeHTML('<b>foo</b></b>')
|
|
|
|
'<b>foo</b>'
|
|
|
|
> Ox.normalizeHTML('<'&"> äbçdê')
|
|
|
|
'<\'&"> äbçdê'
|
|
|
|
@*/
|
|
|
|
Ox.normalizeHTML = function(html) {
|
2012-06-16 20:45:05 +00:00
|
|
|
return regexp.html.test(html) ? Ox.$('<div>').html(html).html() : html;
|
2012-05-27 10:36:16 +00:00
|
|
|
};
|
|
|
|
|
2012-05-30 11:32:33 +00:00
|
|
|
/*@
|
|
|
|
Ox.parseMarkdown <f> Parses (a tiny subset of) Markdown.
|
2012-06-24 13:37:46 +00:00
|
|
|
Supports `*emphasis*`, `_emphasis_`, `**strong**`, `__strong__`,
|
|
|
|
`` `code` ``, ``` ``code with backtick (`)`` ```,
|
|
|
|
```` ```classname\ngithub-style\ncode blocks\n``` ````,
|
|
|
|
`<mail@example.com>`, `<http://example.com>` and
|
|
|
|
`[text](http://example.com "title")`.
|
2012-05-30 11:32:33 +00:00
|
|
|
> Ox.parseMarkdown('*foo* **bar** `baz` ``back`tick``')
|
|
|
|
'<em>foo</em> <strong>bar</strong> <code>baz</code> <code>back`tick</code>'
|
2012-06-23 11:30:33 +00:00
|
|
|
> Ox.parseMarkdown('foo\n\nbar\n\nbaz')
|
|
|
|
'foo<br><br>bar<br><br>baz'
|
2012-06-24 13:37:46 +00:00
|
|
|
> Ox.parseMarkdown('```foo\n\nbar\n\nbaz\n```')
|
|
|
|
'<pre><code class="foo">bar\n\nbaz\n</code></pre>'
|
2012-05-30 22:47:19 +00:00
|
|
|
> Ox.parseMarkdown('<http://example.com>')
|
|
|
|
'<a href="http://example.com">http://example.com</a>'
|
2012-06-24 13:37:46 +00:00
|
|
|
> Ox.parseMarkdown('`<http://example.com>`')
|
|
|
|
'<code><http://example.com></code>'
|
2012-05-30 11:32:33 +00:00
|
|
|
> Ox.parseMarkdown('[example](http://example.com "example.com")')
|
|
|
|
'<a href="http://example.com" title="example.com">example</a>'
|
|
|
|
> Ox.parseMarkdown('[example](http://example.com?foo=bar&bar=baz)')
|
|
|
|
'<a href="http://example.com?foo=bar&bar=baz">example</a>'
|
2012-05-30 22:47:19 +00:00
|
|
|
> Ox(Ox.parseMarkdown('<mail@example.com>')).startsWith('<a href="')
|
|
|
|
true
|
|
|
|
> Ox(Ox.parseMarkdown('<mail@example.com>')).endsWith('</a>')
|
|
|
|
true
|
|
|
|
> Ox(Ox.parseMarkdown('<mail@example.com>')).count(':')
|
|
|
|
1
|
|
|
|
> Ox(Ox.parseMarkdown('<mail@example.com>')).decodeHTMLEntities()
|
|
|
|
'<a href="mailto:mail@example.com">mail@example.com</a>'
|
2012-05-30 11:32:33 +00:00
|
|
|
*/
|
|
|
|
Ox.parseMarkdown = function(string) {
|
|
|
|
// see https://github.com/coreyti/showdown/blob/master/src/showdown.js
|
2012-06-24 13:37:46 +00:00
|
|
|
var array = [];
|
2012-05-30 11:32:33 +00:00
|
|
|
return string.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
|
|
|
|
.replace(
|
2012-06-24 13:37:46 +00:00
|
|
|
/(?:^|\n)```(.*)\n([^`]+)\n```/g,
|
2012-05-30 22:47:19 +00:00
|
|
|
function(match, classname, code) {
|
2012-06-24 13:37:46 +00:00
|
|
|
array.push(
|
|
|
|
'<pre><code'
|
2012-06-22 08:24:25 +00:00
|
|
|
+ (classname ? ' class="' + classname + '"' : '') + '>'
|
2012-06-24 13:37:46 +00:00
|
|
|
+ code.trim().replace(/</g, '<') + '\n</code></pre>'
|
|
|
|
);
|
|
|
|
return salt.join(array.length - 1);
|
2012-05-30 11:32:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
|
2012-05-30 22:47:19 +00:00
|
|
|
function(match, prev, backticks, code, next) {
|
2012-06-24 13:37:46 +00:00
|
|
|
array.push(
|
|
|
|
prev + '<code>'
|
|
|
|
+ code.trim().replace(/</g, '<') + '</code>'
|
|
|
|
);
|
|
|
|
return salt.join(array.length - 1);
|
2013-06-02 20:19:36 +00:00
|
|
|
}
|
2012-05-30 11:32:33 +00:00
|
|
|
)
|
2012-06-24 13:37:46 +00:00
|
|
|
.replace(
|
|
|
|
/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g,
|
|
|
|
'<strong>$2</strong>'
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
/(\*|_)(?=\S)([^\r]*?\S)\1/g,
|
|
|
|
'<em>$2</em>'
|
|
|
|
)
|
2012-05-30 11:32:33 +00:00
|
|
|
.replace(
|
|
|
|
/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?(.*?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
|
2012-05-30 22:47:19 +00:00
|
|
|
function(match, all, text, id, url, rest, quote, title) {
|
|
|
|
return '<a href="' + Ox.encodeHTMLEntities(url) + '"' + (
|
|
|
|
title ? ' title="' + Ox.encodeHTMLEntities(title) + '"' : ''
|
|
|
|
) + '>' + text + '</a>';
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
/<((https?|ftp|dict):[^'">\s]+)>/gi,
|
|
|
|
'<a href=\"$1\">$1</a>'
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,
|
|
|
|
function(match, mail) {
|
|
|
|
return Ox.encodeEmailAddress(mail);
|
2012-05-30 11:32:33 +00:00
|
|
|
}
|
2012-06-23 11:30:33 +00:00
|
|
|
)
|
2012-06-24 13:37:46 +00:00
|
|
|
.replace(/\n\n/g, '<br><br>')
|
|
|
|
.replace(
|
|
|
|
new RegExp(salt.join('(\\d+)'), 'g'),
|
|
|
|
function(match, index) {
|
|
|
|
return array[parseInt(index)];
|
|
|
|
}
|
|
|
|
);
|
2012-05-30 11:32:33 +00:00
|
|
|
};
|
|
|
|
|
2012-05-27 10:36:16 +00:00
|
|
|
/*@
|
|
|
|
Ox.sanitizeHTML <f> Takes untrusted HTML and returns something trustworthy
|
2012-05-30 11:32:33 +00:00
|
|
|
> Ox.sanitizeHTML('http://foo.com, ...')
|
|
|
|
'<a href="http://foo.com">http://foo.com</a>, ...'
|
|
|
|
> Ox.sanitizeHTML('http://foo.com/foo?bar&baz, ...')
|
|
|
|
'<a href="http://foo.com/foo?bar&baz">http://foo.com/foo?bar&baz</a>, ...'
|
2012-05-27 10:36:16 +00:00
|
|
|
> Ox.sanitizeHTML('(see: www.foo.com)')
|
|
|
|
'(see: <a href="http://www.foo.com">www.foo.com</a>)'
|
|
|
|
> Ox.sanitizeHTML('foo@bar.com')
|
|
|
|
'<a href="mailto:foo@bar.com">foo@bar.com</a>'
|
2012-05-27 11:28:08 +00:00
|
|
|
> Ox.sanitizeHTML('<a href="mailto:foo@bar.com">foo</a>')
|
|
|
|
'<a href="mailto:foo@bar.com">foo</a>'
|
|
|
|
> Ox.sanitizeHTML('<a href="http://foo.com">foo</a>')
|
|
|
|
'<a href="http://foo.com">foo</a>'
|
2012-06-16 11:16:30 +00:00
|
|
|
> Ox.sanitizeHTML('<a href="http://www.foo.com/">http://www.foo.com/</a>')
|
|
|
|
'<a href="http://www.foo.com/">http://www.foo.com/</a>'
|
2012-05-27 10:36:16 +00:00
|
|
|
> Ox.sanitizeHTML('<a href="http://foo.com" onclick="alert()">foo</a>')
|
|
|
|
'<a href="http://foo.com">foo</a>'
|
|
|
|
> Ox.sanitizeHTML('<a href="javascript:alert()">foo</a>')
|
|
|
|
'<a href="javascript:alert()">foo'
|
2012-05-27 11:42:55 +00:00
|
|
|
> Ox.sanitizeHTML('<a href="foo">foo</a>')
|
|
|
|
'<a href="foo">foo'
|
2012-05-27 11:40:19 +00:00
|
|
|
> Ox.sanitizeHTML('<a href="/foo">foo</a>')
|
|
|
|
'<a href="/foo">foo</a>'
|
2013-02-27 10:57:44 +00:00
|
|
|
> Ox.sanitizeHTML('<a href="/">foo</a>')
|
|
|
|
'<a href="/">foo</a>'
|
2012-05-27 10:36:16 +00:00
|
|
|
> Ox.sanitizeHTML('[http://foo.com foo]')
|
|
|
|
'<a href="http://foo.com">foo</a>'
|
|
|
|
> Ox.sanitizeHTML('<rtl>foo</rtl>')
|
|
|
|
'<div style="direction: rtl">foo</div>'
|
|
|
|
> Ox.sanitizeHTML('<script>alert()</script>')
|
|
|
|
'<script>alert()</script>'
|
|
|
|
> Ox.sanitizeHTML('\'foo\' < \'bar\' && "foo" > "bar"')
|
|
|
|
'\'foo\' < \'bar\' && "foo" > "bar"'
|
|
|
|
> Ox.sanitizeHTML('<b>foo')
|
|
|
|
'<b>foo</b>'
|
|
|
|
> Ox.sanitizeHTML('<b>foo</b></b>')
|
|
|
|
'<b>foo</b>'
|
2012-06-22 08:29:09 +00:00
|
|
|
> Ox.sanitizeHTML('&&')
|
|
|
|
'&&'
|
2012-06-24 13:37:46 +00:00
|
|
|
> Ox.sanitizeHTML('<http://foo.com>')
|
|
|
|
'<<a href="http://foo.com">http://foo.com</a>>'
|
2012-05-27 10:36:16 +00:00
|
|
|
@*/
|
2013-02-15 12:31:28 +00:00
|
|
|
Ox.sanitizeHTML = function(html, tags, replaceTags) {
|
2012-05-27 11:28:08 +00:00
|
|
|
var matches = [];
|
|
|
|
tags = tags || defaultTags;
|
2013-02-15 12:31:28 +00:00
|
|
|
replaceTags = replaceTags || {};
|
2012-05-27 11:28:08 +00:00
|
|
|
// html = Ox.clean(html); fixme: can this be a parameter?
|
|
|
|
if (tags.indexOf('[]') > -1) {
|
2012-06-24 13:37:46 +00:00
|
|
|
html = html.replace(
|
|
|
|
/\[((\/|https?:\/\/|mailto:).+?) (.+?)\]/gi,
|
|
|
|
'<a href="$1">$3</a>'
|
|
|
|
);
|
2012-05-27 11:28:08 +00:00
|
|
|
tags = tags.filter(function(tag) {
|
|
|
|
return tag != '[]';
|
2012-05-27 10:36:16 +00:00
|
|
|
});
|
2012-05-27 11:28:08 +00:00
|
|
|
}
|
|
|
|
tags.forEach(function(tag) {
|
2013-02-15 12:31:28 +00:00
|
|
|
var array = replaceTags[tag] || replace[tag] || replace['*'](tag);
|
2012-05-27 11:28:08 +00:00
|
|
|
Ox.forEach(array, function(value) {
|
|
|
|
html = html.replace(value[0], function() {
|
2013-02-16 07:09:16 +00:00
|
|
|
var match;
|
|
|
|
if (Ox.isFunction(value[1])) {
|
|
|
|
match = value[1].apply(null, arguments);
|
|
|
|
} else {
|
|
|
|
match = Ox.formatString(value[1], arguments);
|
|
|
|
}
|
|
|
|
matches.push(match);
|
2012-05-27 11:28:08 +00:00
|
|
|
return salt.join(matches.length - 1);
|
|
|
|
});
|
2012-05-27 10:36:16 +00:00
|
|
|
});
|
2012-05-27 11:28:08 +00:00
|
|
|
});
|
2012-06-22 08:24:25 +00:00
|
|
|
html = Ox.encodeHTMLEntities(Ox.decodeHTMLEntities(html));
|
2012-05-27 11:28:08 +00:00
|
|
|
matches.forEach(function(match, i) {
|
|
|
|
html = html.replace(new RegExp(salt.join(i)), match);
|
|
|
|
});
|
2012-06-16 11:16:30 +00:00
|
|
|
html = Ox.addLinks(html, true);
|
2012-05-27 11:28:08 +00:00
|
|
|
html = html.replace(/\n\n/g, '<br/><br/>');
|
|
|
|
// Close extra opening and remove extra closing tags.
|
|
|
|
// Note: this converts ''' to "'" and '"' to '"'
|
|
|
|
return Ox.normalizeHTML(html);
|
|
|
|
};
|
2012-05-27 10:36:16 +00:00
|
|
|
|
2012-06-18 17:11:17 +00:00
|
|
|
/*@
|
|
|
|
Ox.stripTags <f> Strips HTML tags from a string
|
|
|
|
> Ox.stripTags('f<span>o</span>o')
|
|
|
|
'foo'
|
|
|
|
@*/
|
|
|
|
Ox.stripTags = function(string) {
|
|
|
|
return string.replace(/<.*?>/g, '');
|
|
|
|
};
|
|
|
|
|
2012-05-27 10:36:16 +00:00
|
|
|
}());
|