editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

sanitize method for Block Tool seems to be typed wrong

Open adaptedbee opened this issue 4 months ago • 6 comments

Case 1:

In Block Tool types, sanitize should return SanitizerConfig (which is object with tag names as keys). Examples in SanitizerConfig are also written for fields with tag names as keys.

Case 2:

In documentation, it returns object with 'url' and 'caption' keys. Same behavior in other official plugins, such as paragraph, quote etc. And unfortunately, in that situation I can't use function as value for tag name, because tag name here becomes typed as 'attr'.

Editor.js version: 2.30.8

I can't understand which of that cases is right or wrong. Is there any correct way to use function for sanitizing tag?

adaptedbee avatar Sep 30 '25 15:09 adaptedbee

I'm not sure I've got the problem. SanitizerRule can be a function in our types:

export type SanitizerRule = TagConfig | ((el: Element) => TagConfig)

export interface SanitizerConfig {
  /**
   * Tag name and params not to be stripped off
   * @see {@link https://github.com/guardian/html-janitor}
   *
   * @example Save P tags
   * p: true
   *
   * @example Save A tags and do not strip HREF attribute
   * a: {
   *   href: true
   * }
   *
   * @example Save A tags with TARGET="_blank" attribute
   * a: function (aTag) {
   *   return aTag.target === '_black';
   * }
   *
   * @example Save U tags that are not empty
   * u: function(el){
   *   return el.textContent !== '';
   * }
   *
   * @example For blockquote with class 'indent' save CLASS and STYLE attributes
   *          Otherwise strip all attributes
   * blockquote: function(el) {
   *   if (el.classList.contains('indent')) {
   *     return { 'class': true, 'style': true };
   *   } else {
   *     return {};
   *   }
   * }
   */
  [key: string]: SanitizerRule;
}

neSpecc avatar Oct 13 '25 16:10 neSpecc

@neSpecc

I'm trying to do that

Image

and receiving TypeScript error TS2322: Type '(el: any) => boolean' is not assignable to type 'string | boolean'

adaptedbee avatar Oct 13 '25 17:10 adaptedbee

Yes, probably we have some problems in types. Could you try to fix it?

neSpecc avatar Oct 13 '25 17:10 neSpecc

I think I could try.

But also, I tried to find in that repo, how sanitize field is actually used, and I didn't find anything. So it seems to me that this field is not actual at all (please correct me if I'm wrong).

adaptedbee avatar Oct 13 '25 17:10 adaptedbee

It is used on paste and on save.

neSpecc avatar Oct 13 '25 17:10 neSpecc

Can you show any example?

adaptedbee avatar Oct 13 '25 17:10 adaptedbee