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

feat(paste): support filter functions in tag-based paste config

Open costajohnt opened this issue 8 months ago • 1 comments

Extend the paste processing logic to check filter functions when matching pasted elements to tools.

Previously, filter functions in pasteConfig.tags were only used during sanitization (via HTMLJanitor) but ignored during paste processing, causing all elements with a matching tag name to be treated as substitutable regardless of the filter.

Changes

  • Add a filter field to TagSubstitute interface
  • Store filter in getTagsConfig when the sanitization config is a function
  • Add isTagSubstitutable helper that checks both tag name and filter
  • Update processHTML, processElementNode, and containsAnotherToolTags to use the helper

Example

A tool can now use a filter function in pasteConfig.tags to match only specific elements:

static get pasteConfig() {
  return {
    tags: {
      SPAN: (el) => parseInt(el.style.fontWeight, 10) > 400
    }
  }
}

Previously, this filter was only checked during sanitization. Now it's also checked during paste processing, so only <span> elements that pass the filter will be assigned to the tool.

Closes #2959

costajohnt avatar Mar 18 '26 14:03 costajohnt

Bumping this in case it got lost in the queue. The change is opt-in (filter functions are only used when configured), so existing paste configs are unaffected. Happy to address any feedback.

costajohnt avatar May 07 '26 00:05 costajohnt