2020-01-30 23:05:23 +02:00
|
|
|
const Entities = require('html-entities').AllHtmlEntities;
|
|
|
|
const htmlentities = new Entities().encode;
|
2020-11-07 17:59:37 +02:00
|
|
|
const htmlparser2 = require('@joplin/fork-htmlparser2');
|
2020-01-30 23:05:23 +02:00
|
|
|
|
|
|
|
// [\s\S] instead of . for multiline matching
|
|
|
|
// https://stackoverflow.com/a/16119722/561309
|
|
|
|
const imageRegex = /<img([\s\S]*?)src=["']([\s\S]*?)["']([\s\S]*?)>/gi;
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
const anchorRegex = /<a([\s\S]*?)href=["']([\s\S]*?)["']([\s\S]*?)>/gi;
|
|
|
|
|
2020-03-06 02:54:21 +02:00
|
|
|
const selfClosingElements = [
|
|
|
|
'area',
|
|
|
|
'base',
|
|
|
|
'basefont',
|
|
|
|
'br',
|
|
|
|
'col',
|
|
|
|
'command',
|
|
|
|
'embed',
|
|
|
|
'frame',
|
|
|
|
'hr',
|
|
|
|
'img',
|
|
|
|
'input',
|
|
|
|
'isindex',
|
|
|
|
'keygen',
|
|
|
|
'link',
|
|
|
|
'meta',
|
|
|
|
'param',
|
|
|
|
'source',
|
|
|
|
'track',
|
|
|
|
'wbr',
|
|
|
|
];
|
|
|
|
|
2020-01-30 23:05:23 +02:00
|
|
|
class HtmlUtils {
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
attributesHtml(attr: any) {
|
2020-01-30 23:05:23 +02:00
|
|
|
const output = [];
|
|
|
|
|
|
|
|
for (const n in attr) {
|
|
|
|
if (!attr.hasOwnProperty(n)) continue;
|
|
|
|
output.push(`${n}="${htmlentities(attr[n])}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.join(' ');
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
processImageTags(html: string, callback: Function) {
|
2020-01-30 23:05:23 +02:00
|
|
|
if (!html) return '';
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
return html.replace(imageRegex, (_v, before, src, after) => {
|
2020-01-30 23:05:23 +02:00
|
|
|
const action = callback({ src: src });
|
|
|
|
|
|
|
|
if (!action) return `<img${before}src="${src}"${after}>`;
|
|
|
|
|
|
|
|
if (action.type === 'replaceElement') {
|
|
|
|
return action.html;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'replaceSource') {
|
|
|
|
return `<img${before}src="${action.src}"${after}>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'setAttributes') {
|
|
|
|
const attrHtml = this.attributesHtml(action.attrs);
|
|
|
|
return `<img${before}${attrHtml}${after}>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Invalid action: ${action.type}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
processAnchorTags(html: string, callback: Function) {
|
|
|
|
if (!html) return '';
|
|
|
|
|
|
|
|
return html.replace(anchorRegex, (_v, before, href, after) => {
|
|
|
|
const action = callback({ href: href });
|
|
|
|
|
|
|
|
if (!action) return `<a${before}href="${href}"${after}>`;
|
|
|
|
|
|
|
|
if (action.type === 'replaceElement') {
|
|
|
|
return action.html;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'replaceSource') {
|
|
|
|
return `<img${before}href="${action.href}"${after}>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'setAttributes') {
|
|
|
|
const attrHtml = this.attributesHtml(action.attrs);
|
|
|
|
return `<img${before}${attrHtml}${after}>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Invalid action: ${action.type}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
isSelfClosingTag(tagName: string) {
|
2020-03-06 02:54:21 +02:00
|
|
|
return selfClosingElements.includes(tagName.toLowerCase());
|
|
|
|
}
|
|
|
|
|
2020-11-07 17:59:37 +02:00
|
|
|
// TODO: copied from @joplin/lib
|
2020-11-20 18:04:47 +02:00
|
|
|
stripHtml(html: string) {
|
|
|
|
const output: string[] = [];
|
2020-11-05 18:58:23 +02:00
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
const tagStack: string[] = [];
|
2020-11-05 18:58:23 +02:00
|
|
|
|
|
|
|
const currentTag = () => {
|
|
|
|
if (!tagStack.length) return '';
|
|
|
|
return tagStack[tagStack.length - 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
const disallowedTags = ['script', 'style', 'head', 'iframe', 'frameset', 'frame', 'object', 'base'];
|
|
|
|
|
|
|
|
const parser = new htmlparser2.Parser({
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
onopentag: (name: string) => {
|
2020-11-05 18:58:23 +02:00
|
|
|
tagStack.push(name.toLowerCase());
|
|
|
|
},
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
ontext: (decodedText: string) => {
|
2020-11-05 18:58:23 +02:00
|
|
|
if (disallowedTags.includes(currentTag())) return;
|
|
|
|
output.push(decodedText);
|
|
|
|
},
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
onclosetag: (name: string) => {
|
2020-11-05 18:58:23 +02:00
|
|
|
if (currentTag() === name.toLowerCase()) tagStack.pop();
|
|
|
|
},
|
|
|
|
|
|
|
|
}, { decodeEntities: true });
|
|
|
|
|
|
|
|
parser.write(html);
|
|
|
|
parser.end();
|
|
|
|
|
|
|
|
return output.join('').replace(/\s+/g, ' ');
|
|
|
|
}
|
|
|
|
|
2021-05-10 11:22:12 +02:00
|
|
|
public sanitizeHtml(html: string, options: any = null) {
|
2020-04-10 19:12:41 +02:00
|
|
|
options = Object.assign({}, {
|
|
|
|
// If true, adds a "jop-noMdConv" class to all the tags.
|
|
|
|
// It can be used afterwards to restore HTML tags in Markdown.
|
|
|
|
addNoMdConvClass: false,
|
|
|
|
}, options);
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
const output: string[] = [];
|
2020-03-06 02:54:21 +02:00
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
const tagStack: string[] = [];
|
2020-03-06 02:54:21 +02:00
|
|
|
|
|
|
|
const currentTag = () => {
|
|
|
|
if (!tagStack.length) return '';
|
|
|
|
return tagStack[tagStack.length - 1];
|
2020-02-14 01:59:23 +02:00
|
|
|
};
|
|
|
|
|
2020-10-29 18:19:56 +02:00
|
|
|
// The BASE tag allows changing the base URL from which files are
|
|
|
|
// loaded, and that can break several plugins, such as Katex (which
|
|
|
|
// needs to load CSS files using a relative URL). For that reason
|
|
|
|
// it is disabled. More info:
|
|
|
|
// https://github.com/laurent22/joplin/issues/3021
|
|
|
|
//
|
|
|
|
// "link" can be used to escape the parser and inject JavaScript.
|
|
|
|
// Adding "meta" too for the same reason as it shouldn't be used in
|
|
|
|
// notes anyway.
|
2021-06-11 20:17:45 +02:00
|
|
|
const disallowedTags = ['script', 'iframe', 'frameset', 'frame', 'object', 'base', 'embed', 'link', 'meta', 'noscript', 'button', 'form', 'input', 'select', 'textarea', 'option', 'optgroup'];
|
2020-03-06 02:54:21 +02:00
|
|
|
|
|
|
|
const parser = new htmlparser2.Parser({
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
onopentag: (name: string, attrs: any) => {
|
2020-03-06 02:54:21 +02:00
|
|
|
tagStack.push(name.toLowerCase());
|
|
|
|
|
|
|
|
if (disallowedTags.includes(currentTag())) return;
|
|
|
|
|
|
|
|
attrs = Object.assign({}, attrs);
|
2020-05-10 17:51:15 +02:00
|
|
|
|
|
|
|
// Remove all the attributes that start with "on", which
|
|
|
|
// normally should be JavaScript events. A better solution
|
|
|
|
// would be to blacklist known events only but it seems the
|
|
|
|
// list is not well defined [0] and we don't want any to slip
|
|
|
|
// throught the cracks. A side effect of this change is a
|
|
|
|
// regular harmless attribute that starts with "on" will also
|
|
|
|
// be removed.
|
|
|
|
// 0: https://developer.mozilla.org/en-US/docs/Web/Events
|
|
|
|
for (const name in attrs) {
|
|
|
|
if (!attrs.hasOwnProperty(name)) continue;
|
|
|
|
if (name.length <= 2) continue;
|
|
|
|
if (name.toLowerCase().substr(0, 2) !== 'on') continue;
|
|
|
|
delete attrs[name];
|
2020-03-06 02:54:21 +02:00
|
|
|
}
|
2020-04-10 19:12:41 +02:00
|
|
|
|
|
|
|
if (options.addNoMdConvClass) {
|
|
|
|
let classAttr = attrs['class'] || '';
|
|
|
|
if (!classAttr.includes('jop-noMdConv')) {
|
|
|
|
classAttr += ' jop-noMdConv';
|
|
|
|
attrs['class'] = classAttr.trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 20:33:28 +02:00
|
|
|
// For some reason, entire parts of HTML notes don't show up in
|
|
|
|
// the viewer when there's an anchor tag without an "href"
|
|
|
|
// attribute. It doesn't always happen and it seems to depend on
|
|
|
|
// what else is in the note but in any case adding the "href"
|
|
|
|
// fixes it. https://github.com/laurent22/joplin/issues/5687
|
|
|
|
if (name.toLowerCase() === 'a' && !attrs['href']) {
|
|
|
|
attrs['href'] = '#';
|
|
|
|
}
|
|
|
|
|
2020-03-06 02:54:21 +02:00
|
|
|
let attrHtml = this.attributesHtml(attrs);
|
|
|
|
if (attrHtml) attrHtml = ` ${attrHtml}`;
|
|
|
|
const closingSign = this.isSelfClosingTag(name) ? '/>' : '>';
|
|
|
|
output.push(`<${name}${attrHtml}${closingSign}`);
|
|
|
|
},
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
ontext: (decodedText: string) => {
|
2020-03-06 02:54:21 +02:00
|
|
|
if (disallowedTags.includes(currentTag())) return;
|
|
|
|
|
2020-03-06 20:22:40 +02:00
|
|
|
if (currentTag() === 'style') {
|
|
|
|
// For CSS, we have to put the style as-is inside the tag because if we html-entities encode
|
|
|
|
// it, it's not going to work. But it's ok because JavaScript won't run within the style tag.
|
|
|
|
// Ideally CSS should be loaded from an external file.
|
|
|
|
output.push(decodedText);
|
|
|
|
} else {
|
|
|
|
output.push(htmlentities(decodedText));
|
|
|
|
}
|
2020-03-06 02:54:21 +02:00
|
|
|
},
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
onclosetag: (name: string) => {
|
2020-03-06 02:54:21 +02:00
|
|
|
const current = currentTag();
|
|
|
|
|
|
|
|
if (current === name.toLowerCase()) tagStack.pop();
|
|
|
|
|
|
|
|
if (disallowedTags.includes(current)) return;
|
|
|
|
|
|
|
|
if (this.isSelfClosingTag(name)) return;
|
|
|
|
output.push(`</${name}>`);
|
|
|
|
},
|
|
|
|
|
|
|
|
}, { decodeEntities: true });
|
|
|
|
|
|
|
|
parser.write(html);
|
|
|
|
parser.end();
|
2020-02-14 01:59:23 +02:00
|
|
|
|
2020-03-06 02:54:21 +02:00
|
|
|
return output.join('');
|
2020-02-14 01:59:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-30 23:05:23 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
export default new HtmlUtils();
|