2021-07-14 01:33:14 +02:00
|
|
|
import { MessageBody, HeaderBlock, TableBlock, ListBlock, CodeBlock, PillPart, FormatPart, NewLinePart, RulePart, TextPart, LinkPart, ImagePart } from "./MessageBody.js"
|
2021-07-14 01:56:46 +02:00
|
|
|
import { linkify } from "./linkify/linkify.js";
|
2021-07-02 09:18:37 +02:00
|
|
|
|
|
|
|
/* At the time of writing (Jul 1 2021), Matrix Spec recommends
|
|
|
|
* allowing the following HTML tags:
|
|
|
|
* font, del, h1, h2, h3, h4, h5, h6, blockquote, p, a, ul, ol, sup, sub, li, b, i, u,
|
|
|
|
* strong, em, strike, code, hr, br, div, table, thead, tbody, tr, th, td, caption, pre, span, img
|
|
|
|
*/
|
|
|
|
|
2021-07-02 09:23:59 +02:00
|
|
|
/**
|
|
|
|
* Nodes that don't have any properties to them other than their tag.
|
|
|
|
* While <a> has `href`, and <img> has `src`, these have... themselves.
|
|
|
|
*/
|
2021-07-13 23:47:10 +02:00
|
|
|
const basicInline = ["EM", "STRONG", "CODE", "DEL", "SPAN" ];
|
2021-07-14 21:38:55 +02:00
|
|
|
const basicBlock = ["DIV", "BLOCKQUOTE"];
|
2021-07-16 18:32:40 +02:00
|
|
|
const safeSchemas = ["https", "http", "ftp", "mailto", "magnet"].map(name => `${name}://`);
|
2021-07-16 22:01:33 +02:00
|
|
|
const baseUrl = 'https://matrix.to';
|
|
|
|
const linkPrefix = `${baseUrl}/#/`;
|
2021-07-02 09:18:37 +02:00
|
|
|
|
2021-07-12 21:19:43 +02:00
|
|
|
class Deserializer {
|
|
|
|
constructor(result, mediaRepository) {
|
|
|
|
this.result = result;
|
|
|
|
this.mediaRepository = mediaRepository;
|
|
|
|
}
|
2021-07-02 09:18:37 +02:00
|
|
|
|
2021-07-16 22:01:33 +02:00
|
|
|
parsePillLink(link) {
|
|
|
|
if (!link.startsWith(linkPrefix)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const contents = link.substring(linkPrefix.length);
|
|
|
|
if (contents[0] === '@') {
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:19:43 +02:00
|
|
|
parseLink(node, children) {
|
2021-07-13 23:47:10 +02:00
|
|
|
const href = this.result.getAttributeValue(node, "href");
|
2021-07-16 18:32:40 +02:00
|
|
|
const lcUrl = href?.toLowerCase();
|
|
|
|
// urls should be absolute and with a safe schema, as listed in the spec
|
|
|
|
if (!lcUrl || !safeSchemas.some(schema => lcUrl.startsWith(schema))) {
|
2021-07-15 22:15:56 +02:00
|
|
|
return new FormatPart("span", children);
|
|
|
|
}
|
2021-07-16 22:01:33 +02:00
|
|
|
const pillId = this.parsePillLink(href);
|
|
|
|
if (pillId) {
|
|
|
|
return new PillPart(pillId, href, children);
|
2021-07-15 02:57:53 +02:00
|
|
|
}
|
2021-07-12 21:19:43 +02:00
|
|
|
return new LinkPart(href, children);
|
2021-07-07 01:14:35 +02:00
|
|
|
}
|
2021-07-12 21:19:43 +02:00
|
|
|
|
|
|
|
parseList(node) {
|
|
|
|
const result = this.result;
|
|
|
|
let start = null;
|
|
|
|
if (result.getNodeElementName(node) === "OL") {
|
|
|
|
// Will return 1 for, say, '1A', which may not be intended?
|
|
|
|
start = parseInt(result.getAttributeValue(node, "start")) || 1;
|
|
|
|
}
|
2021-07-16 22:01:33 +02:00
|
|
|
const items = [];
|
2021-07-12 21:19:43 +02:00
|
|
|
for (const child of result.getChildNodes(node)) {
|
|
|
|
if (result.getNodeElementName(child) !== "LI") {
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-13 23:47:10 +02:00
|
|
|
const item = this.parseAnyNodes(result.getChildNodes(child));
|
2021-07-16 22:01:33 +02:00
|
|
|
items.push(item);
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
2021-07-12 21:19:43 +02:00
|
|
|
return new ListBlock(start, nodes);
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-14 01:33:14 +02:00
|
|
|
_ensureElement(node, tag) {
|
|
|
|
return node &&
|
|
|
|
this.result.isElementNode(node) &&
|
|
|
|
this.result.getNodeElementName(node) === tag;
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:19:43 +02:00
|
|
|
parseCodeBlock(node) {
|
|
|
|
const result = this.result;
|
|
|
|
let codeNode;
|
|
|
|
for (const child of result.getChildNodes(node)) {
|
|
|
|
codeNode = child;
|
2021-07-02 09:18:37 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-07-16 20:32:37 +02:00
|
|
|
let language = null;
|
2021-07-14 01:33:14 +02:00
|
|
|
if (!this._ensureElement(codeNode, "CODE")) {
|
2021-07-16 20:32:37 +02:00
|
|
|
return new CodeBlock(language, this.result.getNodeText(node));
|
2021-07-12 21:19:43 +02:00
|
|
|
}
|
|
|
|
const cl = result.getAttributeValue(codeNode, "class") || ""
|
|
|
|
for (const clname of cl.split(" ")) {
|
|
|
|
if (clname.startsWith("language-") && !clname.startsWith("language-_")) {
|
|
|
|
language = clname.substring(9) // "language-".length
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 20:32:37 +02:00
|
|
|
return new CodeBlock(language, this.result.getNodeText(codeNode));
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 21:19:43 +02:00
|
|
|
parseImage(node) {
|
|
|
|
const result = this.result;
|
|
|
|
const src = result.getAttributeValue(node, "src") || "";
|
|
|
|
const url = this.mediaRepository.mxcUrl(src);
|
|
|
|
// We just ignore non-mxc `src` attributes.
|
|
|
|
if (!url) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-07-12 22:42:39 +02:00
|
|
|
const width = parseInt(result.getAttributeValue(node, "width")) || null;
|
|
|
|
const height = parseInt(result.getAttributeValue(node, "height")) || null;
|
2021-07-12 21:19:43 +02:00
|
|
|
const alt = result.getAttributeValue(node, "alt");
|
|
|
|
const title = result.getAttributeValue(node, "title");
|
2021-07-12 22:42:39 +02:00
|
|
|
return new ImagePart(url, width, height, alt, title);
|
2021-07-08 08:40:16 +02:00
|
|
|
}
|
2021-07-02 09:18:37 +02:00
|
|
|
|
2021-07-14 01:33:14 +02:00
|
|
|
parseTableRow(row, tag) {
|
|
|
|
const cells = [];
|
|
|
|
for (const node of this.result.getChildNodes(row)) {
|
|
|
|
if(!this._ensureElement(node, tag)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const children = this.result.getChildNodes(node);
|
|
|
|
const inlines = this.parseInlineNodes(children);
|
|
|
|
cells.push(inlines);
|
|
|
|
}
|
|
|
|
return cells;
|
|
|
|
}
|
|
|
|
|
|
|
|
parseTableHead(head) {
|
|
|
|
let headRow = null;
|
|
|
|
for (const node of this.result.getChildNodes(head)) {
|
|
|
|
headRow = node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (this._ensureElement(headRow, "TR")) {
|
|
|
|
return this.parseTableRow(headRow, "TH");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
parseTableBody(body) {
|
|
|
|
const rows = [];
|
|
|
|
for (const node of this.result.getChildNodes(body)) {
|
|
|
|
if(!this._ensureElement(node, "TR")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rows.push(this.parseTableRow(node, "TD"));
|
|
|
|
}
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
parseTable(node) {
|
|
|
|
// We are only assuming iterable, so convert to arrary for indexing.
|
|
|
|
const children = Array.from(this.result.getChildNodes(node));
|
|
|
|
let head, body;
|
|
|
|
if (this._ensureElement(children[0], "THEAD") && this._ensureElement(children[1], "TBODY")) {
|
|
|
|
head = this.parseTableHead(children[0]);
|
|
|
|
body = this.parseTableBody(children[1]);
|
|
|
|
} else if (this._ensureElement(children[0], "TBODY")) {
|
|
|
|
head = null;
|
|
|
|
body = this.parseTableBody(children[0]);
|
|
|
|
}
|
|
|
|
return new TableBlock(head, body);
|
|
|
|
}
|
|
|
|
|
2021-07-13 23:47:10 +02:00
|
|
|
/** Once a node is known to be an element,
|
|
|
|
* attempt to interpret it as an inline element.
|
|
|
|
*
|
|
|
|
* @returns the inline message part, or null if the element
|
|
|
|
* is not inline or not allowed.
|
|
|
|
*/
|
|
|
|
parseInlineElement(node) {
|
2021-07-12 21:19:43 +02:00
|
|
|
const result = this.result;
|
|
|
|
const tag = result.getNodeElementName(node);
|
2021-07-13 23:47:10 +02:00
|
|
|
const children = result.getChildNodes(node);
|
|
|
|
switch (tag) {
|
|
|
|
case "A": {
|
|
|
|
const inlines = this.parseInlineNodes(children);
|
|
|
|
return this.parseLink(node, inlines);
|
|
|
|
}
|
|
|
|
case "BR":
|
|
|
|
return new NewLinePart();
|
|
|
|
default: {
|
|
|
|
if (!basicInline.includes(tag)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const inlines = this.parseInlineNodes(children);
|
|
|
|
return new FormatPart(tag, inlines);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Attempt to interpret a node as inline.
|
|
|
|
*
|
|
|
|
* @returns the inline message part, or null if the
|
|
|
|
* element is not inline or not allowed.
|
|
|
|
*/
|
|
|
|
parseInlineNode(node) {
|
2021-07-14 01:56:46 +02:00
|
|
|
if (this.result.isElementNode(node)) {
|
2021-07-13 23:47:10 +02:00
|
|
|
return this.parseInlineElement(node);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Once a node is known to be an element, attempt
|
|
|
|
* to interpret it as a block element.
|
|
|
|
*
|
|
|
|
* @returns the block message part, or null of the
|
|
|
|
* element is not a block or not allowed.
|
|
|
|
*/
|
|
|
|
parseBlockElement(node) {
|
|
|
|
const result = this.result;
|
|
|
|
const tag = result.getNodeElementName(node);
|
|
|
|
const children = result.getChildNodes(node);
|
2021-07-12 21:19:43 +02:00
|
|
|
switch (tag) {
|
|
|
|
case "H1":
|
|
|
|
case "H2":
|
|
|
|
case "H3":
|
|
|
|
case "H4":
|
|
|
|
case "H5":
|
|
|
|
case "H6": {
|
2021-07-13 23:47:10 +02:00
|
|
|
const inlines = this.parseInlineNodes(children);
|
|
|
|
return new HeaderBlock(parseInt(tag[1]), inlines)
|
2021-07-12 21:19:43 +02:00
|
|
|
}
|
|
|
|
case "UL":
|
|
|
|
case "OL":
|
|
|
|
return this.parseList(node);
|
|
|
|
case "PRE":
|
|
|
|
return this.parseCodeBlock(node);
|
|
|
|
case "HR":
|
|
|
|
return new RulePart();
|
|
|
|
case "IMG":
|
|
|
|
return this.parseImage(node);
|
2021-07-13 23:47:10 +02:00
|
|
|
case "P": {
|
|
|
|
const inlines = this.parseInlineNodes(children);
|
|
|
|
return new FormatPart(tag, inlines);
|
|
|
|
}
|
2021-07-14 01:33:14 +02:00
|
|
|
case "TABLE":
|
|
|
|
return this.parseTable(node);
|
2021-07-12 21:19:43 +02:00
|
|
|
default: {
|
2021-07-13 23:47:10 +02:00
|
|
|
if (!basicBlock.includes(tag)) {
|
2021-07-12 21:19:43 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-07-13 23:47:10 +02:00
|
|
|
const blocks = this.parseAnyNodes(children);
|
|
|
|
return new FormatPart(tag, blocks);
|
2021-07-12 21:19:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-13 23:47:10 +02:00
|
|
|
/** Attempt to parse a node as a block.
|
|
|
|
*
|
|
|
|
* @return the block message part, or null if the node
|
|
|
|
* is not a block element.
|
|
|
|
*/
|
|
|
|
parseBlockNode(node) {
|
2021-07-14 01:56:46 +02:00
|
|
|
if (this.result.isElementNode(node)) {
|
2021-07-13 23:47:10 +02:00
|
|
|
return this.parseBlockElement(node);
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
2021-07-12 21:19:43 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-14 01:56:46 +02:00
|
|
|
_parseTextParts(node, into) {
|
|
|
|
if(!this.result.isTextNode(node)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX pretty much identical to `MessageBody`'s.
|
|
|
|
const linkifyCallback = (text, isLink) => {
|
|
|
|
if (isLink) {
|
|
|
|
into.push(new LinkPart(text, [new TextPart(text)]));
|
|
|
|
} else {
|
|
|
|
into.push(new TextPart(text));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
linkify(this.result.getNodeText(node), linkifyCallback);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-13 23:47:10 +02:00
|
|
|
_parseInlineNodes(nodes, into) {
|
|
|
|
for (const htmlNode of nodes) {
|
2021-07-14 01:56:46 +02:00
|
|
|
if (this._parseTextParts(htmlNode, into)) {
|
|
|
|
// This was a text node, and we already
|
|
|
|
// dumped its parts into our list.
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-13 23:47:10 +02:00
|
|
|
const node = this.parseInlineNode(htmlNode);
|
|
|
|
if (node) {
|
|
|
|
into.push(node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Node is either block or unrecognized. In
|
|
|
|
// both cases, just move on to its children.
|
|
|
|
this._parseInlineNodes(this.result.getChildNodes(htmlNode), into);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parseInlineNodes(nodes) {
|
|
|
|
const into = [];
|
|
|
|
this._parseInlineNodes(nodes, into);
|
|
|
|
return into;
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-14 01:56:46 +02:00
|
|
|
// XXX very similar to `_parseInlineNodes`.
|
2021-07-13 23:47:10 +02:00
|
|
|
_parseAnyNodes(nodes, into) {
|
2021-07-12 21:19:43 +02:00
|
|
|
for (const htmlNode of nodes) {
|
2021-07-14 01:56:46 +02:00
|
|
|
if (this._parseTextParts(htmlNode, into)) {
|
|
|
|
// This was a text node, and we already
|
|
|
|
// dumped its parts into our list.
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-13 23:47:10 +02:00
|
|
|
const node = this.parseInlineNode(htmlNode) || this.parseBlockNode(htmlNode);
|
2021-07-12 21:19:43 +02:00
|
|
|
if (node) {
|
2021-07-13 23:47:10 +02:00
|
|
|
into.push(node);
|
|
|
|
continue;
|
2021-07-12 21:19:43 +02:00
|
|
|
}
|
2021-07-13 23:47:10 +02:00
|
|
|
// Node is unrecognized. Just move on to its children.
|
|
|
|
this._parseAnyNodes(this.result.getChildNodes(htmlNode), into);
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
2021-07-13 23:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parseAnyNodes(nodes) {
|
|
|
|
const into = [];
|
|
|
|
this._parseAnyNodes(nodes, into);
|
|
|
|
return into;
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:55:33 +02:00
|
|
|
export function parseHTMLBody(platform, mediaRepository, html) {
|
2021-07-12 23:46:42 +02:00
|
|
|
const parseResult = platform.parseHTML(html);
|
2021-07-12 21:19:43 +02:00
|
|
|
const deserializer = new Deserializer(parseResult, mediaRepository);
|
2021-07-13 23:47:10 +02:00
|
|
|
const parts = deserializer.parseAnyNodes(parseResult.rootNodes);
|
2021-07-03 03:20:07 +02:00
|
|
|
return new MessageBody(html, parts);
|
2021-07-02 09:18:37 +02:00
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-07 21:21:10 +02:00
|
|
|
import parse from '../../../../../lib/node-html-parser/index.js';
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
export function tests() {
|
|
|
|
class HTMLParseResult {
|
|
|
|
constructor(bodyNode) {
|
|
|
|
this._bodyNode = bodyNode;
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
get rootNodes() {
|
|
|
|
return this._bodyNode.childNodes;
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
getChildNodes(node) {
|
|
|
|
return node.childNodes;
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
getAttributeNames(node) {
|
|
|
|
return node.getAttributeNames();
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
getAttributeValue(node, attr) {
|
|
|
|
return node.getAttribute(attr);
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
isTextNode(node) {
|
|
|
|
return !node.tagName;
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
getNodeText(node) {
|
|
|
|
return node.text;
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
isElementNode(node) {
|
|
|
|
return !!node.tagName;
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
getNodeElementName(node) {
|
|
|
|
return node.tagName;
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 22:31:33 +02:00
|
|
|
const platform = {
|
|
|
|
parseHTML: (html) => new HTMLParseResult(parse(html))
|
|
|
|
};
|
2021-07-07 04:11:05 +02:00
|
|
|
|
|
|
|
function test(assert, input, output) {
|
2021-07-12 22:31:33 +02:00
|
|
|
assert.deepEqual(parseHTMLBody(platform, null, input), new MessageBody(input, output));
|
2021-07-07 04:11:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"Text only": assert => {
|
|
|
|
const input = "This is a sentence";
|
|
|
|
const output = [new TextPart(input)];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
|
|
|
"Text with inline code format": assert => {
|
|
|
|
const input = "Here's <em>some</em> <code>code</code>!";
|
|
|
|
const output = [
|
|
|
|
new TextPart("Here's "),
|
|
|
|
new FormatPart("em", [new TextPart("some")]),
|
|
|
|
new TextPart(" "),
|
|
|
|
new FormatPart("code", [new TextPart("code")]),
|
|
|
|
new TextPart("!")
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
|
|
|
"Text with ordered list with no attributes": assert => {
|
|
|
|
const input = "<ol><li>Lorem</li><li>Ipsum</li></ol>";
|
|
|
|
const output = [
|
|
|
|
new ListBlock(1, [
|
|
|
|
[ new TextPart("Lorem") ],
|
|
|
|
[ new TextPart("Ipsum") ]
|
|
|
|
])
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
|
|
|
"Text with ordered list starting at 3": assert => {
|
|
|
|
const input = '<ol start="3"><li>Lorem</li><li>Ipsum</li></ol>';
|
|
|
|
const output = [
|
|
|
|
new ListBlock(3, [
|
|
|
|
[ new TextPart("Lorem") ],
|
|
|
|
[ new TextPart("Ipsum") ]
|
|
|
|
])
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
|
|
|
"Text with unordered list": assert => {
|
|
|
|
const input = '<ul start="3"><li>Lorem</li><li>Ipsum</li></ul>';
|
|
|
|
const output = [
|
|
|
|
new ListBlock(null, [
|
|
|
|
[ new TextPart("Lorem") ],
|
|
|
|
[ new TextPart("Ipsum") ]
|
|
|
|
])
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
2021-07-13 00:16:04 +02:00
|
|
|
"Auto-closed tags": assert => {
|
|
|
|
const input = '<p>hello<p>world</p></p>';
|
|
|
|
const output = [
|
|
|
|
new FormatPart("p", [new TextPart("hello")]),
|
|
|
|
new FormatPart("p", [new TextPart("world")])
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
2021-07-13 23:47:10 +02:00
|
|
|
"Block elements ignored inside inline elements": assert => {
|
|
|
|
const input = '<span><p><code>Hello</code></p></span>';
|
|
|
|
const output = [
|
|
|
|
new FormatPart("span", [new FormatPart("code", [new TextPart("Hello")])])
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
|
|
|
"Unknown tags are ignored, but their children are kept": assert => {
|
|
|
|
const input = '<span><dfn><code>Hello</code></dfn><footer><em>World</em></footer></span>';
|
|
|
|
const output = [
|
|
|
|
new FormatPart("span", [
|
|
|
|
new FormatPart("code", [new TextPart("Hello")]),
|
|
|
|
new FormatPart("em", [new TextPart("World")])
|
|
|
|
])
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
2021-07-14 00:14:03 +02:00
|
|
|
"Unknown and invalid attributes are stripped": assert => {
|
|
|
|
const input = '<em onmouseover=alert("Bad code!")>Hello</em>';
|
|
|
|
const output = [
|
|
|
|
new FormatPart("em", [new TextPart("Hello")])
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
2021-07-16 20:32:37 +02:00
|
|
|
"Text with code block but no <code> tag": assert => {
|
|
|
|
const code = 'main :: IO ()\nmain = putStrLn "Hello"'
|
|
|
|
const input = `<pre>${code}</pre>`;
|
|
|
|
const output = [
|
|
|
|
new CodeBlock(null, code)
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
|
|
|
"Text with code block and 'unsupported' tag": assert => {
|
|
|
|
const code = '<em>Hello, world</em>'
|
|
|
|
const input = `<pre>${code}</pre>`;
|
|
|
|
const output = [
|
|
|
|
new CodeBlock(null, code)
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
}
|
2021-07-07 04:11:05 +02:00
|
|
|
/* Doesnt work: HTML library doesn't handle <pre><code> properly.
|
|
|
|
"Text with code block": assert => {
|
|
|
|
const code = 'main :: IO ()\nmain = putStrLn "Hello"'
|
|
|
|
const input = `<pre><code>${code}</code></pre>`;
|
|
|
|
const output = [
|
|
|
|
new CodeBlock(null, code)
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
}
|