Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `Fix` - `readOnly.toggle()` now restores editor to consistent state if save fails
- `Fix` - Inline formatting is no longer removed when a Block is converted to, or merged into, a Tool with per-field `sanitize` rules (`ConversionConfig.import` now receives the unsanitized exported string and sanitization runs on the returned data)
- `Fix` - Data of a Block merged into a Block of the same Tool is sanitized with the Tool's sanitize config
- `Fix` - Sanitize rules for the same tag declared by several Inline Tools, Block Tool field rules, and paste configs are merged instead of overwriting each other

### 2.31.7

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/editorjs",
"version": "2.32.0-rc.6",
"version": "2.32.0-rc.7",
"description": "Editor.js — open source block-style WYSIWYG editor with JSON output",
"main": "dist/editorjs.umd.js",
"module": "dist/editorjs.mjs",
Expand Down
9 changes: 6 additions & 3 deletions src/components/modules/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from '../../../types';
import type Block from '../block';
import type { SavedData } from '../../../types/data-formats';
import { clean, sanitizeBlocks } from '../utils/sanitizer';
import { clean, mergeSanitizerConfigs, sanitizeBlocks } from '../utils/sanitizer';
import type BlockToolAdapter from '../tools/block';

/**
Expand Down Expand Up @@ -219,7 +219,10 @@ export default class Paste extends Module {
return result;
}, {});

const customConfig = Object.assign({}, toolsTags, Tools.getAllInlineToolsSanitizeConfig(), { br: {} });
const customConfig = {
...mergeSanitizerConfigs(toolsTags, Tools.getAllInlineToolsSanitizeConfig()),
br: {},
};
const cleanData = clean(htmlData, customConfig);

/** If there is no HTML or HTML string is equal to plain one, process it as plain text */
Expand Down Expand Up @@ -704,7 +707,7 @@ export default class Paste extends Module {
return result;
}, {});

const customConfig = Object.assign({}, toolTags, tool.baseSanitizeConfig);
const customConfig = mergeSanitizerConfigs(toolTags, tool.baseSanitizeConfig);

/**
* A workaround for the HTMLJanitor bug with Tables (incorrect sanitizing of table.innerHTML)
Expand Down
12 changes: 4 additions & 8 deletions src/components/modules/tools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Paragraph from '@editorjs/paragraph';
import Module from '../__module';
import * as _ from '../utils';
import { mergeSanitizerConfigs } from '../utils/sanitizer';
import type { SanitizerConfig, ToolConfig, ToolConstructable, ToolSettings } from '../../../types';
import BoldInlineTool from '../inline-tools/inline-tool-bold';
import ItalicInlineTool from '../inline-tools/inline-tool-italic';
Expand Down Expand Up @@ -150,14 +151,9 @@ export default class Tools extends Module {
*/
@_.cacheable
public getAllInlineToolsSanitizeConfig(): SanitizerConfig {
const config: SanitizerConfig = {} as SanitizerConfig;

Array.from(this.inlineTools.values())
.forEach(inlineTool => {
Object.assign(config, inlineTool.sanitizeConfig);
});

return config;
return mergeSanitizerConfigs(
...Array.from(this.inlineTools.values()).map(inlineTool => inlineTool.sanitizeConfig)
);
}

/**
Expand Down
18 changes: 6 additions & 12 deletions src/components/tools/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ToolboxConfigEntry
} from '@/types';
import * as _ from '../utils';
import { mergeSanitizerConfigs } from '../utils/sanitizer';
import type InlineToolAdapter from './inline';
import type BlockTuneAdapter from './tune';
import ToolsCollection from './collection';
Expand Down Expand Up @@ -187,7 +188,7 @@ export default class BlockToolAdapter extends BaseToolAdapter<ToolType.Block, IB
* Otherwise pass as it is
*/
if (_.isObject(rule)) {
toolConfig[fieldName] = Object.assign({}, baseConfig, rule);
toolConfig[fieldName] = mergeSanitizerConfigs(baseConfig, rule as SanitizerConfig);
} else {
toolConfig[fieldName] = rule;
}
Expand All @@ -202,16 +203,9 @@ export default class BlockToolAdapter extends BaseToolAdapter<ToolType.Block, IB
*/
@_.cacheable
public get baseSanitizeConfig(): SanitizerConfig {
const baseConfig = {};

Array
.from(this.inlineTools.values())
.forEach(tool => Object.assign(baseConfig, tool.sanitizeConfig));

Array
.from(this.tunes.values())
.forEach(tune => Object.assign(baseConfig, tune.sanitizeConfig));

return baseConfig;
return mergeSanitizerConfigs(

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.

The same problem remains a few lines above, in sanitizeConfig (line 191): Object.assign({}, baseConfig, rule) lets the Block Tool's own field rule overwrite the merged inline rules for the same tag (e.g. text: { span: { class: true } } drops inline tools' span attributes). Let's use mergeSanitizerConfigs(baseConfig, rule) there as well, ideally with a test.

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.

also, mention this in changelog

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in fd56dec6: BlockToolAdapter.sanitizeConfig now calls mergeSanitizerConfigs(baseConfig, rule) (with test coverage in sanitisation.cy.ts) and docs/CHANGELOG.md documents Inline Tools, Block Tool field rules, and paste configs.

...Array.from(this.inlineTools.values()).map(tool => tool.sanitizeConfig),
...Array.from(this.tunes.values()).map(tune => tune.sanitizeConfig)
);
}
}
152 changes: 151 additions & 1 deletion src/components/utils/sanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,159 @@ import * as _ from '../utils';
*/

import HTMLJanitor from 'html-janitor';
import type { BlockToolData, SanitizerConfig } from '../../../types';
import type { BlockToolData, SanitizerConfig, SanitizerRule } from '../../../types';
import type { TagConfig } from '../../../types/configs/sanitizer-config';
import type { SavedData } from '../../../types/data-formats';

/**
* Value of a single attribute inside a tag rule object
*
* html-janitor accepts:
* - true — keep the attribute with any value
* - false — strip the attribute
* - string — keep the attribute only if its value equals the string
* - function — keep the attribute only if the function returns true
*/
type AttributeRule = boolean | string | ((value: string, node: Element) => boolean);

/**
* Merges several sanitizer configs into a single one, combining rules per tag
* instead of overwriting them
*
* Needed when several Inline Tools (or Block Tunes) declare rules for the same tag,
* e.g. two tools that both wrap text with <span> but allow different attributes.
* With a plain Object.assign the last tool's rule would replace the first one.
*
* Merge semantics (a rule that allows something always wins over one that does not):
* - tag: true + anything → true (all attributes allowed)
* - tag: false (or missing) + X → X
* - tag: object + object → object with the union of attributes
* - tag: function + object/function → function returning the merged result of both for the element
* - attribute: true + anything → true
* - attribute: false (or missing) + X → X
* - attribute: equal strings → that string
* - attribute: any other mix of string/function → function that passes when either side passes
*
* Passed configs are not mutated.
*
* @param configs - sanitizer configs to merge
* @returns merged sanitizer config
*/
export function mergeSanitizerConfigs(...configs: Array<SanitizerConfig | undefined>): SanitizerConfig {
const result = {} as SanitizerConfig;

configs.forEach((config) => {
if (!_.isObject(config)) {
return;
}

Object.keys(config).forEach((tag) => {
result[tag] = mergeTagRules(result[tag], config[tag]);
});
});

return result;
}

/**
* Merges two rules for the same tag
*
* @param a - first rule
* @param b - second rule
*/
function mergeTagRules(a: TagConfig | undefined, b: TagConfig | undefined): TagConfig;
/**
* Merges two rules for the same tag
*
* @param a - first rule
* @param b - second rule
*/
function mergeTagRules(a: SanitizerRule | undefined, b: SanitizerRule | undefined): SanitizerRule;
/**
* Merges two rules for the same tag
*
* @param a - first rule
* @param b - second rule
*/
function mergeTagRules(a: SanitizerRule | undefined, b: SanitizerRule | undefined): SanitizerRule {
if (a === undefined || a === null || a === false) {
return b;
}

if (b === undefined || b === null || b === false) {
return a;
}

if (a === true || b === true) {
return true;
}

if (_.isFunction(a) || _.isFunction(b)) {
return (el: Element): TagConfig => {
const resultA = _.isFunction(a) ? a(el) : a;
const resultB = _.isFunction(b) ? b(el) : b;

return mergeTagRules(resultA, resultB);
};
}

const merged = { ...a };

Object.keys(b).forEach((attr) => {
merged[attr] = mergeAttributeRules(merged[attr], b[attr]);
});

return merged;
}

/**
* Merges two rules for the same attribute of a tag
*
* @param a - first rule
* @param b - second rule
*/
function mergeAttributeRules(a: AttributeRule | undefined, b: AttributeRule | undefined): AttributeRule {
if (a === undefined || a === null || a === false) {
return b;
}

if (b === undefined || b === null || b === false) {
return a;
}

if (a === true || b === true) {
return true;
}

if (a === b) {
return a;
}

return (value: string, node: Element): boolean => {
return checkAttributeRule(a, value, node) || checkAttributeRule(b, value, node);
};
}

/**
* Checks whether an attribute value passes a string or function rule
*
* @param rule - string (exact value) or function rule
* @param value - attribute value
* @param node - element that owns the attribute
*/
function checkAttributeRule(rule: AttributeRule, value: string, node: Element): boolean {
if (_.isFunction(rule)) {
/**
* html-janitor treats any truthy predicate result as "allowed"
* (for example `value => value.match(/^font-/)` returns an array),
* so the merged rule must do the same
*/
return Boolean(rule(value, node));
}

return rule === value;
}

/**
* Sanitize Blocks
*
Expand Down
Loading
Loading