slab / parchment

Generalized state model for rich-text editors to interface with browser DOM
BSD 3-Clause "New" or "Revised" License
637 stars 146 forks source link

Support ClassAttributor if prefix == '' #149

Open StanlyShauro opened 3 months ago

StanlyShauro commented 3 months ago

Dear all,

Problem: There is a list of classes, e.g. class1, class2, class3. We have them rolled up into format my_class like this

import { ClassAttributor, Scope} from 'parchment';
const config = {
  scope: Scope.BLOCK,
  whitelist: ['class1', 'class2', 'class3']
};

const AlignClass = new ClassAttributor('align', '', config);

Draw attention, prefix is an empty string.

In this case I expect dom element classes like class1, class2, class3, but they are -class1, -class2 -class3.

Current code does not support empty prefix src/attributor/class.ts

function match(node: HTMLElement, prefix: string): string[] {
  const className = node.getAttribute('class') || '';
  return className
    .split(/\s+/)
    .filter((name) => name.indexOf(`${prefix}-`) === 0);
}