-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTML content transformer middleware (#5338)
* Add HTML content transformer * Add entry * Fix alt text * Apply to fenced code blocks only * Add breaking changes * Update entry * Add PR * Fix tests * Fix test * Add xmlns and remove HTML content provider related attributes --------- Co-authored-by: Eugene <[email protected]>
- Loading branch information
Showing
25 changed files
with
378 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,23 +23,23 @@ describe('renderMarkdown', () => { | |
const styleOptions = { markdownRespectCRLF: true }; | ||
|
||
expect(renderMarkdown('Same line.\nSame line. \n2nd line.', styleOptions, renderMarkdownOptions)).toBe( | ||
'<p>Same line.\nSame line.<br />\n2nd line.</p>' | ||
'<p xmlns="http://www.w3.org/1999/xhtml">Same line.\nSame line.<br />\n2nd line.</p>' | ||
); | ||
}); | ||
|
||
it('should respect CRLF', () => { | ||
const styleOptions = { markdownRespectCRLF: true }; | ||
|
||
expect(renderMarkdown('Same Line.\n\rSame Line.\r\n2nd line.', styleOptions, renderMarkdownOptions)).toBe( | ||
'<p>Same Line.\nSame Line.</p>\n<p>2nd line.</p>' | ||
'<p xmlns="http://www.w3.org/1999/xhtml">Same Line.\nSame Line.</p>\n<p xmlns="http://www.w3.org/1999/xhtml">2nd line.</p>' | ||
); | ||
}); | ||
|
||
it('should respect LFCR', () => { | ||
const styleOptions = { markdownRespectCRLF: false }; | ||
|
||
expect(renderMarkdown('Same Line.\r\nSame Line.\n\r2nd line.', styleOptions, renderMarkdownOptions)).toBe( | ||
'<p>Same Line.\nSame Line.</p>\n<p>2nd line.</p>' | ||
'<p xmlns="http://www.w3.org/1999/xhtml">Same Line.\nSame Line.</p>\n<p xmlns="http://www.w3.org/1999/xhtml">2nd line.</p>' | ||
); | ||
}); | ||
|
||
|
@@ -48,7 +48,9 @@ describe('renderMarkdown', () => { | |
|
||
expect( | ||
renderMarkdown('**Message with Markdown**\r\nShould see bold text.', styleOptions, renderMarkdownOptions) | ||
).toBe('<p><strong>Message with Markdown</strong></p>\n<p>Should see bold text.</p>'); | ||
).toBe( | ||
'<p xmlns="http://www.w3.org/1999/xhtml"><strong>Message with Markdown</strong></p>\n<p xmlns="http://www.w3.org/1999/xhtml">Should see bold text.</p>' | ||
); | ||
}); | ||
|
||
it('should render code correctly', () => { | ||
|
@@ -60,11 +62,7 @@ describe('renderMarkdown', () => { | |
styleOptions, | ||
renderMarkdownOptions | ||
) | ||
) | ||
.toBe(`<pre class="webchat__render-markdown__code-block"><webchat--code-block-copy-button class="webchat__code-block-copy-button" data-alt-copied="Copied" data-alt-copy="Copy" data-value="{ | ||
"hello": "World!" | ||
} | ||
"></webchat--code-block-copy-button><code>{ | ||
).toBe(`<pre xmlns="http://www.w3.org/1999/xhtml"><code>{ | ||
"hello": "World!" | ||
} | ||
</code></pre>`); | ||
|
@@ -74,7 +72,7 @@ describe('renderMarkdown', () => { | |
const styleOptions = { markdownRespectCRLF: true }; | ||
|
||
expect(renderMarkdown('[example](https://sample.com)', styleOptions, renderMarkdownOptions)).toBe( | ||
`<p>\u200B<a href="https://sample.com" aria-label="example " rel="noopener noreferrer" target="_blank">example<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="" class="webchat__render-markdown__external-link-icon" /></a>\u200B</p>` | ||
`<p xmlns="http://www.w3.org/1999/xhtml">\u200B<a href="https://sample.com" aria-label="example " rel="noopener noreferrer" target="_blank">example<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="" class="webchat__render-markdown__external-link-icon" /></a>\u200B</p>` | ||
); | ||
}); | ||
|
||
|
@@ -83,31 +81,31 @@ describe('renderMarkdown', () => { | |
const options = { externalLinkAlt: 'Opens in a new window, external.' }; | ||
|
||
expect(renderMarkdown('[example](https://sample.com)', styleOptions, options)).toBe( | ||
`<p>\u200B<a href="https://sample.com" aria-label="example Opens in a new window, external." rel="noopener noreferrer" target="_blank">example<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="" class="webchat__render-markdown__external-link-icon" title="Opens in a new window, external." /></a>\u200B</p>` | ||
`<p xmlns="http://www.w3.org/1999/xhtml">\u200B<a href="https://sample.com" aria-label="example Opens in a new window, external." rel="noopener noreferrer" target="_blank">example<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="" class="webchat__render-markdown__external-link-icon" title="Opens in a new window, external." /></a>\u200B</p>` | ||
); | ||
}); | ||
|
||
it('should render sip protocol links correctly', () => { | ||
const styleOptions = { markdownRespectCRLF: true }; | ||
|
||
expect(renderMarkdown(`[[email protected]](sip:[email protected])`, styleOptions, renderMarkdownOptions)).toBe( | ||
'<p>\u200B<a href="sip:[email protected]" rel="noopener noreferrer" target="_blank">[email protected]</a>\u200B</p>' | ||
'<p xmlns="http://www.w3.org/1999/xhtml">\u200B<a href="sip:[email protected]" rel="noopener noreferrer" target="_blank">[email protected]</a>\u200B</p>' | ||
); | ||
}); | ||
|
||
it('should render tel protocol links correctly', () => { | ||
const styleOptions = { markdownRespectCRLF: true }; | ||
|
||
expect(renderMarkdown(`[(505)503-4455](tel:505-503-4455)`, styleOptions, renderMarkdownOptions)).toBe( | ||
'<p>\u200B<a href="tel:505-503-4455" rel="noopener noreferrer" target="_blank">(505)503-4455</a>\u200B</p>' | ||
'<p xmlns="http://www.w3.org/1999/xhtml">\u200B<a href="tel:505-503-4455" rel="noopener noreferrer" target="_blank">(505)503-4455</a>\u200B</p>' | ||
); | ||
}); | ||
|
||
it('should render strikethrough text correctly', () => { | ||
const styleOptions = { markdownRespectCRLF: true }; | ||
|
||
expect(renderMarkdown(`~~strike text~~`, styleOptions, renderMarkdownOptions)).toBe( | ||
'<p><del>strike text</del></p>' | ||
'<p xmlns="http://www.w3.org/1999/xhtml"><del>strike text</del></p>' | ||
); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/bundle/src/markdown/createHTMLContentTransformMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { type HTMLContentTransformMiddleware } from 'botframework-webchat-component'; | ||
|
||
import createCodeBlockCopyButtonMiddleware from './middleware/createCodeBlockCopyButtonMiddleware'; | ||
import createSanitizeMiddleware from './middleware/createSanitizeMiddleware'; | ||
|
||
export default function createHTMLContentTransformMiddleware(): readonly HTMLContentTransformMiddleware[] { | ||
return Object.freeze([createCodeBlockCopyButtonMiddleware(), createSanitizeMiddleware()]); | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/bundle/src/markdown/middleware/createCodeBlockCopyButtonMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { type HTMLContentTransformMiddleware } from 'botframework-webchat-component'; | ||
|
||
import codeBlockCopyButtonDocumentMod from '../private/codeBlockCopyButtonDocumentMod'; | ||
|
||
export default function createCodeBlockCopyButtonMiddleware(): HTMLContentTransformMiddleware { | ||
return () => next => request => | ||
next( | ||
Object.freeze({ | ||
...request, | ||
documentFragment: codeBlockCopyButtonDocumentMod(request.documentFragment, { | ||
codeBlockCopyButtonAltCopied: request.codeBlockCopyButtonAltCopied, | ||
codeBlockCopyButtonAltCopy: request.codeBlockCopyButtonAltCopy, | ||
codeBlockCopyButtonClassName: request.codeBlockCopyButtonClassName, | ||
codeBlockCopyButtonTagName: request.codeBlockCopyButtonTagName | ||
}) | ||
}) | ||
); | ||
} |
109 changes: 109 additions & 0 deletions
109
packages/bundle/src/markdown/middleware/createSanitizeMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { | ||
parseDocumentFragmentFromString, | ||
serializeDocumentFragmentIntoString | ||
} from 'botframework-webchat-component/internal'; | ||
import sanitizeHTML from 'sanitize-html'; | ||
|
||
const BASE_SANITIZE_HTML_OPTIONS = Object.freeze({ | ||
allowedAttributes: { | ||
a: ['aria-label', 'class', 'href', 'name', 'rel', 'target'], | ||
button: ['aria-label', 'class', 'type', 'value'], | ||
img: ['alt', 'aria-label', 'class', 'src', 'title'], | ||
pre: ['class'], | ||
span: ['aria-label'] | ||
}, | ||
allowedSchemes: ['data', 'http', 'https', 'ftp', 'mailto', 'sip', 'tel'], | ||
allowedTags: [ | ||
'a', | ||
'b', | ||
'blockquote', | ||
'br', | ||
'button', | ||
'caption', | ||
'code', | ||
'del', | ||
'div', | ||
'em', | ||
'h1', | ||
'h2', | ||
'h3', | ||
'h4', | ||
'h5', | ||
'h6', | ||
'hr', | ||
'i', | ||
'img', | ||
'ins', | ||
'li', | ||
'nl', | ||
'ol', | ||
'p', | ||
'pre', | ||
's', | ||
'span', | ||
'strike', | ||
'strong', | ||
'table', | ||
'tbody', | ||
'td', | ||
'tfoot', | ||
'th', | ||
'thead', | ||
'tr', | ||
'ul', | ||
|
||
// Followings are for MathML elements, from https://developer.mozilla.org/en-US/docs/Web/MathML. | ||
'annotation-xml', | ||
'annotation', | ||
'math', | ||
'merror', | ||
'mfrac', | ||
'mi', | ||
'mmultiscripts', | ||
'mn', | ||
'mo', | ||
'mover', | ||
'mpadded', | ||
'mphantom', | ||
'mprescripts', | ||
'mroot', | ||
'mrow', | ||
'ms', | ||
'mspace', | ||
'msqrt', | ||
'mstyle', | ||
'msub', | ||
'msubsup', | ||
'msup', | ||
'mtable', | ||
'mtd', | ||
'mtext', | ||
'mtr', | ||
'munder', | ||
'munderover', | ||
'semantics' | ||
], | ||
// Bug of https://github.com/apostrophecms/sanitize-html/issues/633. | ||
// They should not remove `alt=""` even though it is empty. | ||
nonBooleanAttributes: [] | ||
}); | ||
|
||
export default function createSanitizeMiddleware() { | ||
return () => () => request => { | ||
const { codeBlockCopyButtonTagName, documentFragment } = request; | ||
const sanitizeHTMLOptions = { | ||
...BASE_SANITIZE_HTML_OPTIONS, | ||
allowedAttributes: { | ||
...BASE_SANITIZE_HTML_OPTIONS.allowedAttributes, | ||
[codeBlockCopyButtonTagName]: ['class', 'data-alt-copy', 'data-alt-copied', 'data-testid', 'data-value'] | ||
}, | ||
allowedTags: [...BASE_SANITIZE_HTML_OPTIONS.allowedTags, codeBlockCopyButtonTagName] | ||
}; | ||
|
||
const htmlAfterBetterLink = serializeDocumentFragmentIntoString(documentFragment); | ||
|
||
const htmlAfterSanitization = sanitizeHTML(htmlAfterBetterLink, sanitizeHTMLOptions); | ||
|
||
return parseDocumentFragmentFromString(htmlAfterSanitization); | ||
}; | ||
} |
Oops, something went wrong.