merge: use XMLSerializer for toMastoApiHtml - fixes #556 (!591)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/591

Closes #556

Approved-by: Marie <marie@kaifa.ch>
Approved-by: fEmber <acomputerdog@gmail.com>
This commit is contained in:
dakkar 2024-08-06 14:09:35 +00:00
commit 2f97b5976f

View File

@ -6,7 +6,7 @@
import { URL } from 'node:url'; import { URL } from 'node:url';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import * as parse5 from 'parse5'; import * as parse5 from 'parse5';
import { Window, XMLSerializer } from 'happy-dom'; import { Window, DocumentFragment, XMLSerializer } from 'happy-dom';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import { intersperse } from '@/misc/prelude/array.js'; import { intersperse } from '@/misc/prelude/array.js';
@ -483,6 +483,8 @@ export class MfmService {
const doc = window.document; const doc = window.document;
const body = doc.createElement('p');
async function appendChildren(children: mfm.MfmNode[], targetElement: any): Promise<void> { async function appendChildren(children: mfm.MfmNode[], targetElement: any): Promise<void> {
if (children) { if (children) {
for (const child of await Promise.all(children.map(async (x) => await (handlers as any)[x.type](x)))) targetElement.appendChild(child); for (const child of await Promise.all(children.map(async (x) => await (handlers as any)[x.type](x)))) targetElement.appendChild(child);
@ -661,7 +663,7 @@ export class MfmService {
}, },
}; };
await appendChildren(nodes, doc.body); await appendChildren(nodes, body);
if (quoteUri !== null) { if (quoteUri !== null) {
const a = doc.createElement('a'); const a = doc.createElement('a');
@ -675,9 +677,15 @@ export class MfmService {
quote.innerHTML += 'RE: '; quote.innerHTML += 'RE: ';
quote.appendChild(a); quote.appendChild(a);
doc.body.appendChild(quote); body.appendChild(quote);
} }
return inline ? doc.body.innerHTML : `<p>${doc.body.innerHTML}</p>`; let result = new XMLSerializer().serializeToString(body);
if (inline) {
result = result.replace(/^<p>/,'').replace(/<\/p>$/,'');
}
return result;
} }
} }