Repository navigation
fix(conversion): keep inline formatting when converting to a Tool with per-field sanitize rules - #3049
Conversation
…ules
A Block Tool's sanitize config describes its data fields, for example
{ text: { b: true }, caption: false }. On conversion and on merging
Blocks of different Tools, this config was passed to HTMLJanitor as the
rules for the exported string, so the field names were read as tag names
and every real tag was removed: bold, italic and links were lost when
converting to any Tool with per-field sanitize rules.
The exported string is now imported into the target Tool's data first,
and the data is sanitized with sanitizeBlocks, the same way as on saving.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ules sanitizeBlocks() leaves data untouched when the tool sanitize config is empty, so a trailing <br> added by Firefox survived the conversion. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Bump the version to 2.32.0-rc.5, as next is now at rc.4. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
neSpecc
left a comment
There was a problem hiding this comment.
Thanks for the fix and the detailed root-cause analysis!
A few general notes:
- The branch needs a rebase: #3048 has already been merged into
nextwith2.32.0-rc.5, so please re-bump the version and resolve the CHANGELOG conflict. - The PR description is stale (it says the version was bumped to
rc.4, and #3048 has already landed). - I'd merge this after #3041:
tool.sanitizeConfigwill then combine inline rules with the Tool's field rules viamergeSanitizerConfigs, and this PR will pick that up automatically.
| /** | ||
| * Composes data of the passed Tool from a string exported by another Block and sanitizes it. | ||
| * | ||
| * The Tool's sanitizer config describes its data fields ({ text: { b: true }, caption: false }), | ||
| * so it is applied to the composed data, as on saving. Applied to the exported string, | ||
| * it would treat the field names as tag names and remove every real tag. | ||
| * | ||
| * @param exportedString - string got with the Conversion Config "export" | ||
| * @param tool - Tool to compose data for | ||
| * @param toolConfig - user config of the Tool, passed to the Conversion Config "import" function | ||
| */ | ||
| private composeSanitizedBlockData(exportedString: string, tool: BlockToolAdapter, toolConfig?: ToolConfig): BlockToolData { |
There was a problem hiding this comment.
Let's rename it so it's clear that the input is a string, consistent with convertStringToBlockData / convertBlockDataToString (don't forget the calls at L515 and L866). The sentence about the old behavior belongs in the PR description rather than in JSDoc, and toolConfig deserves a clearer description:
| /** | |
| * Composes data of the passed Tool from a string exported by another Block and sanitizes it. | |
| * | |
| * The Tool's sanitizer config describes its data fields ({ text: { b: true }, caption: false }), | |
| * so it is applied to the composed data, as on saving. Applied to the exported string, | |
| * it would treat the field names as tag names and remove every real tag. | |
| * | |
| * @param exportedString - string got with the Conversion Config "export" | |
| * @param tool - Tool to compose data for | |
| * @param toolConfig - user config of the Tool, passed to the Conversion Config "import" function | |
| */ | |
| private composeSanitizedBlockData(exportedString: string, tool: BlockToolAdapter, toolConfig?: ToolConfig): BlockToolData { | |
| /** | |
| * Converts a string exported from another Block to the passed Tool's data | |
| * and sanitizes this data with the Tool's sanitizer config, the same way as on saving | |
| * | |
| * @param exportedString - string got from another Block via its Conversion Config "export" | |
| * @param tool - Tool to convert the string to | |
| * @param toolConfig - Tool's user config (`tools.<name>.config`), passed as the 2nd argument to the Conversion Config "import" function | |
| */ | |
| private convertStringToSanitizedBlockData(exportedString: string, tool: BlockToolAdapter, toolConfig?: ToolConfig): BlockToolData { |
| /** | ||
| * A Tool without any sanitizer rules allows no tags. sanitizeBlocks() skips an empty config, so clean the string itself | ||
| */ |
There was a problem hiding this comment.
This case is hard to follow, let's spell it out:
| /** | |
| * A Tool without any sanitizer rules allows no tags. sanitizeBlocks() skips an empty config, so clean the string itself | |
| */ | |
| /** | |
| * The Tool has no sanitizer rules (no own `sanitize` and no Inline Tools or Tunes with rules). | |
| * sanitizeBlocks() leaves data untouched in this case, but the converted string must not bring | |
| * any markup to such a Tool, so strip all tags before importing | |
| */ |
| /** | ||
| * Clean exported data with replacing sanitizer config | ||
| * Now using Conversion Config "import" we compose a new Block data, cleaned with the replacing Tool's sanitizer config | ||
| */ |
There was a problem hiding this comment.
Simpler wording:
| /** | |
| * Clean exported data with replacing sanitizer config | |
| * Now using Conversion Config "import" we compose a new Block data, cleaned with the replacing Tool's sanitizer config | |
| */ | |
| /** | |
| * Convert the exported string to the replacing Tool's data and sanitize it | |
| */ |
| return convertStringToBlockData(clean(exportedString), tool.conversionConfig, toolConfig); | ||
| } | ||
|
|
||
| const data = convertStringToBlockData(exportedString, tool.conversionConfig, toolConfig); |
There was a problem hiding this comment.
Note that the Conversion Config import function now receives the raw, unsanitized string (before, it got an already cleaned one), and sanitization happens on the resulting data. This is a contract change for third-party Tools. Please either document it (CHANGELOG + JSDoc of ConversionConfig.import), or, for a string import, pre-clean the string with that field's rule before importing.
| const cleanData = clean(blockToMergeDataStringified, targetBlock.tool.sanitizeConfig); | ||
|
|
||
| blockToMergeData = convertStringToBlockData(cleanData, targetBlock.tool.conversionConfig); | ||
| blockToMergeData = this.composeSanitizedBlockData(blockToMergeDataStringified, targetBlock.tool); |
There was a problem hiding this comment.
- Please add a Cypress test for this path too: Backspace-merging blocks of different Tools into a Tool with per-field
sanitize(the case from your table in the description). - Optional, pre-existing:
toolConfigisn't passed here, so animportfunction getsundefinedas its config when merging (e.g. List'sdefaultStyleis lost). Could be fixed here by passingtargetBlock.tool.settings.
neSpecc
left a comment
There was a problem hiding this comment.
@playlikeboy comments are not resolved, please, take a look
|
Thanks @neSpecc for the detailed review! Addressed every comment:
Verified locally with |
Summary
Converting a Block to a Tool that declares per-field
sanitizerules removes all inline formatting from the text: bold, italic and links are lost. The same happens when two Blocks of different Tools are merged with Backspace. This is the "Convert to" part of #2814.Root cause
A Block Tool's sanitize config describes its data fields:
BlockManager.convert()and the different-Tools branch ofBlockManager.mergeBlocks()passed this config toclean()as the rules for the exported string. HTMLJanitor then readstextandcaptionas tag names, finds no rule forb,iora, and strips them. Tools without asanitizegetter are not affected, because their config is the flat inline-tools config.Changes
src/components/modules/blockManager.ts: the exported string is first imported into the target Tool's data withconvertStringToBlockData(), and that data is sanitized withsanitizeBlocks(), the same way as on saving. Bothconvert()andmergeBlocks()use one private helper for this. Tags the target field does not allow are still removed.test/cypress/tests/api/blocks.cy.ts: test thatblocks.convert()keeps<b>and removes<u>for a Tool withsanitize: { text: { b: true } }.next(2.32.0-rc.5from fix(inline-toolbar): make tool shortcuts work with several Editors on a page #3048/fix(selection): focus contenteditable when setting cursor #3057/fix(dev): Enable Image tool tunes on dev page #3060), resolveddocs/CHANGELOG.md, and bumpedpackage.jsonto2.32.0-rc.6.Related issue
#2814 (the conversion part; the note there about
savenot cleaning the data is not covered)Testing
yarn lint(0 errors) andyarn buildpass after mergingnext.