2021-07-16 22:12:26 +02:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-07-12 23:46:42 +02:00
|
|
|
import DOMPurify from "../../../../../lib/dompurify/index.js"
|
|
|
|
|
|
|
|
class HTMLParseResult {
|
2021-07-03 00:05:50 +02:00
|
|
|
constructor(bodyNode) {
|
|
|
|
this._bodyNode = bodyNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rootNodes() {
|
2021-07-07 23:12:24 +02:00
|
|
|
return Array.from(this._bodyNode.childNodes);
|
2021-07-03 00:05:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getChildNodes(node) {
|
2021-07-07 23:12:24 +02:00
|
|
|
return Array.from(node.childNodes);
|
2021-07-03 00:05:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getAttributeNames(node) {
|
2021-07-07 23:12:24 +02:00
|
|
|
return Array.from(node.getAttributeNames());
|
2021-07-03 00:05:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getAttributeValue(node, attr) {
|
|
|
|
return node.getAttribute(attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
isTextNode(node) {
|
|
|
|
return node.nodeType === Node.TEXT_NODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
getNodeText(node) {
|
2021-07-16 20:32:37 +02:00
|
|
|
return node.textContent;
|
2021-07-03 00:05:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isElementNode(node) {
|
|
|
|
return node.nodeType === Node.ELEMENT_NODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
getNodeElementName(node) {
|
|
|
|
return node.tagName;
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 23:46:42 +02:00
|
|
|
|
|
|
|
const sanitizeConfig = {
|
2021-07-16 23:21:39 +02:00
|
|
|
ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp|xxx|mxc):|[^a-z]|[a-z+.-]+(?:[^a-z+.-:]|$))/i,
|
2021-07-12 23:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function parseHTML(html) {
|
|
|
|
// If DOMPurify uses DOMParser, can't we just get the built tree from it
|
|
|
|
// instead of re-parsing?
|
|
|
|
const sanitized = DOMPurify.sanitize(html, sanitizeConfig);
|
|
|
|
const bodyNode = new DOMParser().parseFromString(sanitized, "text/html").body;
|
|
|
|
return new HTMLParseResult(bodyNode);
|
|
|
|
}
|