Skip to content

Commit 4ea9eb3

Browse files
authored
Fix sanitize type to accept per-field SanitizerConfig (#2997)
* Fix sanitize type to accept per-field SanitizerConfig The sanitize property on BlockTool and BaseToolConstructable was typed as SanitizerConfig, which only allows tag-name keys with SanitizerRule values. In practice, Block Tools return an object mapping data field names to their own SanitizerConfig (as documented and used by official plugins like Paragraph and Quote). This caused TypeScript errors when using functions as sanitizer rules within per-field configs, since the type system tried to match the function against TagConfig's { [attr: string]: boolean | string }. Widen the type to SanitizerConfig | Record<string, SanitizerConfig> to match the runtime behavior already handled by cleanObject() in src/components/utils/sanitizer.ts. Fixes #2957 * Bump version to 2.31.6 and add changelog entry
1 parent 530ec56 commit 4ea9eb3

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 2.31.6
4+
5+
- `Fix` - Widen `sanitize` type on `BlockTool` and `BaseToolConstructable` to accept per-field `SanitizerConfig`
6+
37
### 2.31.5
48

59
- `Fix` - Handle __Ctrl + click__ on links with inline styles applied (e.g., bold, italic)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/editorjs",
3-
"version": "2.31.5",
3+
"version": "2.31.6",
44
"description": "Editor.js — open source block-style WYSIWYG editor with JSON output",
55
"main": "dist/editorjs.umd.js",
66
"module": "dist/editorjs.mjs",

types/tools/block-tool.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ import { MenuConfig } from './menu-config';
1313
*/
1414
export interface BlockTool extends BaseTool {
1515
/**
16-
* Sanitizer rules description
16+
* Sanitizer rules description.
17+
*
18+
* @example Flat config (tag-level rules applied to all output)
19+
* { b: true, a: { href: true } }
20+
*
21+
* @example Per-field config
22+
* { text: { br: true, b: true }, caption: { b: true, i: true } }
1723
*/
18-
sanitize?: SanitizerConfig;
24+
sanitize?: SanitizerConfig | Record<string, SanitizerConfig>;
1925

2026
/**
2127
* Process Tool's element in DOM and return raw data

types/tools/tool.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ export interface BaseToolConstructable {
3737
isInline?: boolean;
3838

3939
/**
40-
* Tool`s sanitizer configuration
40+
* Tool`s sanitizer configuration.
41+
*
42+
* For Block Tools, can be a Record mapping data field names to their SanitizerConfig.
43+
* For Inline Tools, should be a flat SanitizerConfig with tag names as keys.
4144
*/
42-
sanitize?: SanitizerConfig;
45+
sanitize?: SanitizerConfig | Record<string, SanitizerConfig>;
4346

4447
/**
4548
* Title of Inline Tool.

0 commit comments

Comments
 (0)