Browse Source

markout: improve `` escaping

readme-update
Simon Grim 8 years ago
parent
commit
a2619a3bff
  1. 85
      js/twister_formatpost.js

85
js/twister_formatpost.js

@ -496,56 +496,31 @@ function htmlFormatMsg(msg, opt) {
} }
// changing the string // changing the string
if (chr === '`' && markoutOpt === 'apply') { // if markoutOpt === 'clear' then ` does not escape anythyng so it needs to be handled like other tags if (markoutOpt === 'apply') {
for (i = 0; i < p.length; i++) { t = '</' + tag + '>';
if (p[i].a > -1) { tag = '<' + tag + '>';
if (p[i].t === -1 || (p[i].t === 0 && p[i].a > i)) { } else { // markoutOpt === 'clear' so we're clearing markup
if (p[i].k > 1) t = '';
t = Array(p[i].k).join(chr); tag = '';
else }
t = ''; for (i = 0; i < p.length; i++) {
j = p[i].a; if (p[i].a > -1) {
t = t + msg.str.slice(p[i].i + p[i].k, p[j].i); if (p[i].t === -1 || (p[i].t === 0 && p[i].a > i)) {
if (p[j].k > 1) if (p[i].k > 1)
t = t + Array(p[i].k).join(chr); msg.htmlEntities.push(tag + Array(p[i].k).join(chr));
t = '<' + tag + '>' + t + '</' + tag + '>'; else
msg.htmlEntities.push(t.replace(/&(?!lt;|gt;)/g, '&amp;')); msg.htmlEntities.push(tag);
htmlEntityEncoded = '>' + (msg.htmlEntities.length - 1).toString() + '<'; } else if (p[i].t === 1 || (p[i].t === 0 && p[i].a < i)) {
msg.str = msg.str.slice(0, p[i].i) + htmlEntityEncoded + msg.str.slice(p[j].i + p[j].k); if (p[i].k > 1)
l = htmlEntityEncoded.length - p[j].i - p[j].k + p[i].i; msg.htmlEntities.push(Array(p[i].k).join(chr) + t);
i = j; else
for (j += 1; j < p.length; j++) msg.htmlEntities.push(t);
p[j].i += l;
}
}
}
} else {
if (markoutOpt === 'apply') {
t = '</' + tag + '>';
tag = '<' + tag + '>';
} else { // markoutOpt === 'clear' so we're clearing markup
t = '';
tag = '';
}
for (i = 0; i < p.length; i++) {
if (p[i].a > -1) {
if (p[i].t === -1 || (p[i].t === 0 && p[i].a > i)) {
if (p[i].k > 1)
msg.htmlEntities.push(tag + Array(p[i].k).join(chr));
else
msg.htmlEntities.push(tag);
} else if (p[i].t === 1 || (p[i].t === 0 && p[i].a < i)) {
if (p[i].k > 1)
msg.htmlEntities.push(Array(p[i].k).join(chr) + t);
else
msg.htmlEntities.push(t);
}
htmlEntityEncoded = '>' + (msg.htmlEntities.length - 1).toString() + '<';
msg.str = msg.str.slice(0, p[i].i) + htmlEntityEncoded + msg.str.slice(p[i].i + p[i].k);
l = htmlEntityEncoded.length - p[i].k;
for (j = i + 1; j < p.length; j++)
p[j].i += l;
} }
htmlEntityEncoded = '>' + (msg.htmlEntities.length - 1).toString() + '<';
msg.str = msg.str.slice(0, p[i].i) + htmlEntityEncoded + msg.str.slice(p[i].i + p[i].k);
l = htmlEntityEncoded.length - p[i].k;
for (j = i + 1; j < p.length; j++)
p[j].i += l;
} }
} }
@ -610,7 +585,17 @@ function htmlFormatMsg(msg, opt) {
msg = {str: escapeHtmlEntities(msg), htmlEntities: []}; msg = {str: escapeHtmlEntities(msg), htmlEntities: []};
msg = markout(msg, markoutOpt, '`', 'samp'); // <samp> tag is kind of monospace, here sequence of chars inside it will be escaped from markup // markout is not applied for chars inside of ``; to escape ` use backslash
if (markoutOpt === 'apply')
msg.str = msg.str.replace(/`(?:[^`\\]|\\.)*`/g, function (s) {
msg.htmlEntities.push('<samp>' + s.slice(1, -1).replace(/\\`/g, '`')
.replace(/&(?!lt;|gt;)/g, '&amp;') + '</samp>');
return '>' + (msg.htmlEntities.length - 1).toString() + '<';
});
else if (markoutOpt === 'clear')
msg.str = msg.str.replace(/`((?:[^`\\]|\\.)*)`/g, function (s) {
return s.slice(1, -1).replace(/\\`/g, '`');
});
// handling links // handling links
for (i = 0; i < msg.str.length - 7; i++) { for (i = 0; i < msg.str.length - 7; i++) {

Loading…
Cancel
Save