From 197e2c83777e8bb01f75566d8d78424c8e4eba2d Mon Sep 17 00:00:00 2001 From: tamaina Date: Sun, 9 Dec 2018 13:15:32 +0900 Subject: [PATCH] #2501 (#3567) * refactor mfm/html * fix * fix * https://github.com/syuilo/misskey/pull/3567#discussion_r240023301 --- src/mfm/html.ts | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/mfm/html.ts b/src/mfm/html.ts index 50d26e10e..66617adf9 100644 --- a/src/mfm/html.ts +++ b/src/mfm/html.ts @@ -14,44 +14,44 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser const doc = window.document; - function dive(nodes: Node[]): any[] { - return nodes.map(n => handlers[n.name](n)); + function appendChildren(children: Node[], targetElement: any): void { + for (const child of children.map(n => handlers[n.name](n))) targetElement.appendChild(child); } const handlers: { [key: string]: (token: Node) => any } = { bold(token) { const el = doc.createElement('b'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, big(token) { const el = doc.createElement('strong'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, small(token) { const el = doc.createElement('small'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, strike(token) { const el = doc.createElement('del'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, italic(token) { const el = doc.createElement('i'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, motion(token) { const el = doc.createElement('i'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, @@ -65,7 +65,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser center(token) { const el = doc.createElement('div'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, @@ -96,7 +96,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser link(token) { const a = doc.createElement('a'); a.href = token.props.url; - dive(token.children).forEach(child => a.appendChild(child)); + appendChildren(token.children, a); return a; }, @@ -111,13 +111,13 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser quote(token) { const el = doc.createElement('blockquote'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, title(token) { const el = doc.createElement('h1'); - dive(token.children).forEach(child => el.appendChild(child)); + appendChildren(token.children, el); return el; }, @@ -147,9 +147,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser } }; - dive(tokens).forEach(x => { - doc.body.appendChild(x); - }); + appendChildren(tokens, doc.body); return `

${doc.body.innerHTML}

`; };