Skip to content

fix(conversion): keep inline formatting when converting to a Tool with per-field sanitize rules - #3049

Merged
neSpecc merged 8 commits into
codex-team:nextfrom
playlikeboy:fix/2814-convert-sanitize-field-rules
Nov 30, 2026
Merged

neSpecc merged 8 commits into
codex-team:nextfrom
playlikeboy:fix/2814-convert-sanitize-field-rules

Conversation

@playlikeboy

@playlikeboy playlikeboy commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Converting a Block to a Tool that declares per-field sanitize rules 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:

static get sanitize() {
  return { text: { b: true }, caption: false };
}

BlockManager.convert() and the different-Tools branch of BlockManager.mergeBlocks() passed this config to clean() as the rules for the exported string. HTMLJanitor then reads text and caption as tag names, finds no rule for b, i or a, and strips them. Tools without a sanitize getter are not affected, because their config is the flat inline-tools config.

Changes

Related issue

#2814 (the conversion part; the note there about save not cleaning the data is not covered)

Testing

  • yarn lint (0 errors) and yarn build pass after merging next.

playlikeboy and others added 4 commits November 13, 2026 23:40
…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 neSpecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 next with 2.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.sanitizeConfig will then combine inline rules with the Tool's field rules via mergeSanitizerConfigs, and this PR will pick that up automatically.

Comment thread src/components/modules/blockManager.ts Outdated
Comment on lines +930 to +941
/**
* 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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
/**
* 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 {

Comment on lines +942 to +944
/**
* A Tool without any sanitizer rules allows no tags. sanitizeBlocks() skips an empty config, so clean the string itself
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is hard to follow, let's spell it out:

Suggested change
/**
* 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
*/

Comment on lines 863 to 865
/**
* 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
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simpler wording:

Suggested change
/**
* 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/components/modules/blockManager.ts Outdated
const cleanData = clean(blockToMergeDataStringified, targetBlock.tool.sanitizeConfig);

blockToMergeData = convertStringToBlockData(cleanData, targetBlock.tool.conversionConfig);
blockToMergeData = this.composeSanitizedBlockData(blockToMergeDataStringified, targetBlock.tool);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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: toolConfig isn't passed here, so an import function gets undefined as its config when merging (e.g. List's defaultStyle is lost). Could be fixed here by passing targetBlock.tool.settings.

@playlikeboy

Copy link
Copy Markdown
Contributor Author

Thanks @neSpecc — merged next (2.32.0-rc.5), resolved the docs/CHANGELOG.md conflict, bumped package.json to 2.32.0-rc.6, and refreshed the PR description. #3041 is also updated and rebased onto next.

@neSpecc neSpecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@playlikeboy comments are not resolved, please, take a look

@playlikeboy

Copy link
Copy Markdown
Contributor Author

Thanks @neSpecc for the detailed review! Addressed every comment:

  1. Renamed composeSanitizedBlockData -> convertStringToSanitizedBlockData in src/components/modules/blockManager.ts (and updated call sites at L515 and L866) with the requested JSDoc and the clarified empty-sanitizeConfig comment.
  2. Simplified the replacement comment above convert().
  3. Documented the ConversionConfig.import contract change (receives the raw exported string; resulting data is sanitized afterward against the target Tool's sanitizer config) in types/configs/conversion-config.ts and docs/CHANGELOG.md.
  4. Passed targetBlock.tool.settings to convertStringToSanitizedBlockData in mergeBlocks() so import functions receive tool user config during cross-tool merges.
  5. Added a cross-tool Backspace-merge Cypress test in test/cypress/tests/modules/BlockEvents/Backspace.cy.ts covering a target Tool with per-field sanitize rules (<b> preserved, <u> stripped).

Verified locally with eslint (0 errors on modified files) and vite build --mode test (local Cypress run skipped due to missing Xvfb; awaiting CI Cypress suite).

@neSpecc
neSpecc enabled auto-merge (squash) November 30, 2026 11:20
@neSpecc
neSpecc merged commit 5c6a3d4 into codex-team:next Nov 30, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants