-
Notifications
You must be signed in to change notification settings - Fork 183
Remove comments <!-- and --> from styles and re apply fix for Word Desktop Pasting
#3024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2f47bd9
Update dependencies and enhance paste functionality by cleaning HTML …
BryanValverdeU 7f0a278
Reapply "Refactor getStyleMetadata to not rely on DomCreator and only…
BryanValverdeU 830c213
Enhance cleanHtmlComments to handle both HTML comment formats in styl…
BryanValverdeU a685fae
Set original DOMPurify
BryanValverdeU 38760c5
Update packages/roosterjs-content-model-plugins/lib/paste/WordDesktop…
BryanValverdeU 7054b72
Ensure headEndIndex is valid
BryanValverdeU 5c6499f
Merge branch 'u/bvalverde/removeComments' of https://github.com/micro…
BryanValverdeU 2c6b9d1
address comment
BryanValverdeU f724c94
Address comments
BryanValverdeU 5731a1e
Merge branch 'master' into u/bvalverde/removeComments
BryanValverdeU 6b70da8
Merge branch 'master' into u/bvalverde/removeComments
JiuqingSong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
packages/roosterjs-content-model-core/lib/command/paste/cleanHtmlComments.ts
This file contains hidden or 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,54 @@ | ||
| const HtmlCommentStart = '\x3C!--'; | ||
| const HtmlCommentStart2 = '<!--'; | ||
| const HtmlCommentEnd = '-->'; | ||
| const styleTag = '<style'; | ||
| const styleClosingTag = '</style>'; | ||
| const nonWordCharacterRegex = /\W/; | ||
|
|
||
| /** | ||
| * @internal | ||
| * Exported only for unit test | ||
| */ | ||
| export function cleanHtmlComments(html: string) { | ||
| let { styleIndex, styleEndIndex } = extractHtmlIndexes(html); | ||
|
|
||
| while (styleIndex > -1) { | ||
| html = removeCommentsFromHtml(html, HtmlCommentStart, styleEndIndex, styleIndex); | ||
| html = removeCommentsFromHtml(html, HtmlCommentStart2, styleEndIndex, styleIndex); | ||
| html = removeCommentsFromHtml(html, HtmlCommentEnd, styleEndIndex, styleIndex); | ||
|
|
||
| ({ styleIndex, styleEndIndex } = extractHtmlIndexes(html, styleEndIndex + 1)); | ||
| } | ||
|
|
||
| return html; | ||
| } | ||
|
|
||
| function extractHtmlIndexes(html: string, startIndex: number = 0) { | ||
| const htmlLowercase = html.toLowerCase(); | ||
| let styleIndex = htmlLowercase.indexOf(styleTag, startIndex); | ||
| let currentIndex = styleIndex + styleTag.length; | ||
| let nextChar = html.substring(currentIndex, currentIndex + 1); | ||
|
|
||
| while (!nonWordCharacterRegex.test(nextChar) && styleIndex > -1) { | ||
| styleIndex = htmlLowercase.indexOf(styleTag, styleIndex + 1); | ||
| currentIndex = styleIndex + styleTag.length; | ||
| nextChar = html.substring(currentIndex, currentIndex + 1); | ||
| } | ||
|
|
||
| const styleEndIndex = htmlLowercase.indexOf(styleClosingTag, startIndex); | ||
| return { styleIndex, styleEndIndex }; | ||
| } | ||
|
|
||
| function removeCommentsFromHtml( | ||
| html: string, | ||
| marker: string, | ||
| endId: number, | ||
| startId: number | ||
| ): string { | ||
| let id = html.indexOf(marker, startId); | ||
| while (id > -1 && id < endId) { | ||
| html = html.substring(0, id) + html.substring(id + marker.length); | ||
| id = html.indexOf(marker, id + 1); | ||
| } | ||
| return html; | ||
| } | ||
This file contains hidden or 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 hidden or 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
90 changes: 90 additions & 0 deletions
90
packages/roosterjs-content-model-core/test/command/paste/cleanHtmlCommentsTest.ts
This file contains hidden or 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,90 @@ | ||
| import { cleanHtmlComments } from '../../../lib/command/paste/cleanHtmlComments'; | ||
|
|
||
| describe('cleanHtmlComments', () => { | ||
| it('removes HTML comments within style tags', () => { | ||
| const input = `<head><style>/* Some CSS */<!-- This is a comment -->body { color: red; }<!-- Another comment --></style></head>`; | ||
| const expected = | ||
| '<head><style>/* Some CSS */ This is a comment body { color: red; } Another comment </style></head>'; | ||
|
|
||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('removes HTML comments within style tags \x3C!--', () => { | ||
| const input = `<head><style>/* Some CSS */\x3C!-- This is a comment -->body { color: red; }<!-- Another comment --></style></head>`; | ||
| const expected = | ||
| '<head><style>/* Some CSS */ This is a comment body { color: red; } Another comment </style></head>'; | ||
|
|
||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('does not remove comments outside style tags', () => { | ||
| const input = `<head><!-- This is a comment --><style>body { color: red; }</style><!-- Another comment --></head>`; | ||
| const expected = | ||
| '<head><!-- This is a comment --><style>body { color: red; }</style><!-- Another comment --></head>'; | ||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('handles multiple style tags', () => { | ||
| const input = `<head><style><!-- Comment 1 -->body { color: red; }</style><style><!-- Comment 2 -->p { font-size: 16px; }</style></head>`; | ||
| const expected = | ||
| '<head><style> Comment 1 body { color: red; }</style><style> Comment 2 p { font-size: 16px; }</style></head>'; | ||
|
|
||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('handles no style tags gracefully', () => { | ||
| const input = ` | ||
| <head> | ||
| <!-- This is a comment --> | ||
| </head> | ||
| `; | ||
| const expected = ` | ||
| <head> | ||
| <!-- This is a comment --> | ||
| </head> | ||
| `; | ||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('handles style tags', () => { | ||
| const input = '<head><style><!-- This is a comment --></style></head>'; | ||
| const expected = '<head><style> This is a comment </style></head>'; | ||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('handles style tags', () => { | ||
| const input = '<head><style>\x3C!-- This is a comment --></style></head>'; | ||
| const expected = '<head><style> This is a comment </style></head>'; | ||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('handle different style tags', () => { | ||
| const input = | ||
| '<head><style111><!--some text--></style111>some other text<style><!--... --></style></head>'; | ||
| const expected = | ||
| '<head><style111><!--some text--></style111>some other text<style>... </style></head>'; | ||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('handle different style tags in body', () => { | ||
| const input = | ||
| '<body><style111><!--some text--></style111>some other text<style><!--... --></style></body>'; | ||
| const expected = | ||
| '<body><style111><!--some text--></style111>some other text<style>... </style></body>'; | ||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('handle different style tags and attributes in body', () => { | ||
| const input = | ||
| '<body><style111 style=""><!--some text--></style111>some other text<style style=""><!--... --></style></body>'; | ||
| const expected = | ||
| '<body><style111 style=""><!--some text--></style111>some other text<style style="">... </style></body>'; | ||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('handles empty input', () => { | ||
| const input = ''; | ||
| const expected = ''; | ||
| expect(cleanHtmlComments(input)).toBe(expected); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
6 changes: 1 addition & 5 deletions
6
packages/roosterjs-content-model-plugins/test/paste/utils/getStyleMetadataTest.ts
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.