Repository navigation
fix(iOS, Web): Unify HTML parsing and normalization flow #902
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
Open
szydlovsky
wants to merge
5
commits into
main
Choose a base branch
from
@szydlovsky/parsing-and-normalizing-adjustments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cdfae67
fix: add iOS parser try catch and remove dead code branches
szydlovsky 2a2dd8f
fix: keep a body tag removal safeguard on iOS
szydlovsky add1c1d
fix: adjust web normalizing/sanitizing flow to native platforms
szydlovsky d18fbec
chore: web tests
szydlovsky 596fbc7
chore: docs specification
szydlovsky 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import * as htmlNormalizer from '../normalization/htmlNormalizer'; | ||
| import { | ||
| isInternalHtml, | ||
| plainTextToHtml, | ||
| prepareIncomingHtml, | ||
| } from '../normalization/incomingHtml'; | ||
| import { preparePastedHtmlForTiptap } from '../normalization/tiptapHtmlNormalizer'; | ||
| import { prepareHtmlForWeb } from '../normalization/prepareHtmlForWeb'; | ||
|
|
||
| let normalizeSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| normalizeSpy = jest.spyOn(htmlNormalizer, 'normalizeHtml'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| normalizeSpy.mockRestore(); | ||
| }); | ||
|
|
||
| test.each([ | ||
| ['<html><p>a</p></html>', true], | ||
| ['\n<html><p>a</p></html>\n', true], | ||
| ['<html lang="en"><p>a</p></html>', false], | ||
| ['<p>a</p>', false], | ||
| ])('isInternalHtml(%j) is %s', (html, expected) => { | ||
| expect(isInternalHtml(html)).toBe(expected); | ||
| }); | ||
|
|
||
| test.each([ | ||
| ['', ''], | ||
| ['a\nb', '<p>a</p><p>b</p>'], | ||
| ['a\r\n\r\nb', '<p>a</p><br><p>b</p>'], | ||
| [' <b>x</b> & y', '<p> <b>x</b> & y</p>'], | ||
| ])('plainTextToHtml(%j) is %j', (text, expected) => { | ||
| expect(plainTextToHtml(text)).toBe(expected); | ||
| }); | ||
|
|
||
| describe('prepareIncomingHtml', () => { | ||
| test.each([true, false])( | ||
| 'uses the editor HTML as is (useHtmlNormalizer=%s)', | ||
| (useHtmlNormalizer) => { | ||
| expect( | ||
| prepareIncomingHtml('<html><p><b>a</b></p></html>', useHtmlNormalizer) | ||
| ).toBe('<p><b>a</b></p>'); | ||
| expect(normalizeSpy).not.toHaveBeenCalled(); | ||
| } | ||
| ); | ||
|
|
||
| test('normalizes other HTML with the normalizer on', () => { | ||
| expect(prepareIncomingHtml('<strong>a</strong>', true)).toBe('<b>a</b>'); | ||
| }); | ||
|
|
||
| test('shows other HTML as plain text with the normalizer off', () => { | ||
| expect(prepareIncomingHtml('<b>a</b>', false)).toBe( | ||
| '<p><b>a</b></p>' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('preparePastedHtmlForTiptap', () => { | ||
| test('normalizes <html>-wrapped clipboard HTML', () => { | ||
| preparePastedHtmlForTiptap( | ||
| '<html><body><strong>a</strong></body></html>', | ||
| true | ||
| ); | ||
| expect(normalizeSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('keeps clipboard HTML as HTML with the normalizer off', () => { | ||
| expect(preparePastedHtmlForTiptap('<b>a</b>', false)).toBe('<b>a</b>'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('prepareHtmlForWeb', () => { | ||
| test('drops layout whitespace of pretty-printed editor HTML', () => { | ||
| const html = | ||
| '<html>\n<p>a <b>b</b></p>\n<br>\n<ul>\n<li>x</li>\n</ul>\n' + | ||
| '<blockquote>\n<p>q</p>\n</blockquote>\n</html>'; | ||
| expect(prepareHtmlForWeb(html, true)).toBe( | ||
| '<p>a <b>b</b></p><br><ul><li><p>x</p></li></ul>' + | ||
| '<blockquote><p>q</p></blockquote>' | ||
| ); | ||
| }); | ||
|
|
||
| test('keeps whitespace in normalized HTML', () => { | ||
| expect(prepareHtmlForWeb('<b>a</b> <i>b</i>', true)).toBe( | ||
| '<b>a</b> <i>b</i>' | ||
| ); | ||
| }); | ||
| }); |
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,52 @@ | ||
| import type { EditorView } from '@tiptap/pm/view'; | ||
| import { handlePasteExternalHtmlAsText } from '../utils/pasteExternalHtmlAsText'; | ||
|
|
||
| function paste( | ||
| clipboard: Record<string, string>, | ||
| useHtmlNormalizer: boolean | ||
| ): { handled: boolean; pasteText: jest.Mock } { | ||
| const pasteText = jest.fn(); | ||
| const view = { pasteText } as unknown as EditorView; | ||
| const event = { | ||
| clipboardData: { getData: (type: string) => clipboard[type] ?? '' }, | ||
| } as unknown as ClipboardEvent; | ||
| const handled = handlePasteExternalHtmlAsText(view, event, useHtmlNormalizer); | ||
| return { handled, pasteText }; | ||
| } | ||
|
|
||
| describe('handlePasteExternalHtmlAsText', () => { | ||
| test('pastes external HTML as plain text with the normalizer off', () => { | ||
| const { handled, pasteText } = paste( | ||
| { 'text/html': '<b>a</b>', 'text/plain': 'a' }, | ||
| false | ||
| ); | ||
| expect(handled).toBe(true); | ||
| expect(pasteText).toHaveBeenCalledWith('a'); | ||
| }); | ||
|
|
||
| test('pastes nothing when there is no plain text', () => { | ||
| const { handled, pasteText } = paste({ 'text/html': '<b>a</b>' }, false); | ||
| expect(handled).toBe(true); | ||
| expect(pasteText).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test.each([ | ||
| ['the normalizer is on', { 'text/html': '<b>a</b>' }, true], | ||
| ['there is no HTML', { 'text/plain': 'a' }, false], | ||
| [ | ||
| 'the HTML is the editor HTML', | ||
| { 'text/html': '<html><b>a</b></html>' }, | ||
| false, | ||
| ], | ||
| [ | ||
| 'the HTML is from a TipTap editor', | ||
| { 'text/html': '<p data-pm-slice="1 1 []">a</p>' }, | ||
| false, | ||
| ], | ||
| ])( | ||
| 'leaves the paste to the editor when %s', | ||
| (_, clipboard, useHtmlNormalizer) => { | ||
| expect(paste(clipboard, useHtmlNormalizer).handled).toBe(false); | ||
| } | ||
| ); | ||
| }); |
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,45 @@ | ||
| import type { SanitizationConfig } from '../../types'; | ||
| import { sanitizeHtml } from '../sanitization/htmlSanitizer'; | ||
| import { normalizeHtml } from './htmlNormalizer'; | ||
|
|
||
| /** | ||
| * The editor's own HTML is wrapped in <html>...</html>; surrounding whitespace | ||
| * is ignored. | ||
| */ | ||
| export function isInternalHtml(html: string): boolean { | ||
| const trimmed = html.trim(); | ||
| return trimmed.startsWith('<html>') && trimmed.endsWith('</html>'); | ||
| } | ||
|
|
||
| /** | ||
| * Turns a value (defaultValue, setValue, children) into canonical HTML: | ||
| * - the editor's own HTML is used as is, | ||
| * - other HTML is normalized when the normalizer is on, | ||
| * - otherwise the value is shown as plain text. | ||
| */ | ||
| export function prepareIncomingHtml( | ||
| html: string, | ||
| useHtmlNormalizer: boolean | undefined, | ||
| sanitizationConfig?: SanitizationConfig | ||
| ): string { | ||
| // Checked before sanitization, which drops the <html> wrapper. | ||
| if (isInternalHtml(html)) return sanitizeHtml(html, sanitizationConfig); | ||
| if (!useHtmlNormalizer) return plainTextToHtml(html); | ||
| return normalizeHtml(sanitizeHtml(html, sanitizationConfig)); | ||
| } | ||
|
|
||
| /** A paragraph per line, an empty line as <br>, and any tags as literal text. */ | ||
| export function plainTextToHtml(text: string): string { | ||
| if (text === '') return ''; | ||
| return text | ||
| .split(/\r\n?|\n/) | ||
| .map((line) => (line === '' ? '<br>' : `<p>${escapeHtml(line)}</p>`)) | ||
| .join(''); | ||
| } | ||
|
|
||
| function escapeHtml(text: string): string { | ||
| return text | ||
| .replace(/&/g, '&') | ||
| .replace(/</g, '<') | ||
| .replace(/>/g, '>'); | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep document metadata outside the parsed body.
For
<html><head><title>Title</title></head><body><p>Hello</p></body></html>, these replacements retain the head content.getTextAndStylesFromHtml:then addsTitleto the visible text because unsupported tags do not suppress their contents. The previous body extraction discarded this content.Preserve body-content extraction when a body exists. Keep wrapper removal for editor HTML without a body.