Sha256: 7a863ea8c2497c7c2419e600423a1a4720e537e96c9a46106abe45bf4d9e4e5b

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

import Attributor from './attributor';

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

class ClassAttributor extends Attributor {
  static keys(node: HTMLElement): string[] {
    return (node.getAttribute('class') || '').split(/\s+/).map(function(name) {
      return name
        .split('-')
        .slice(0, -1)
        .join('-');
    });
  }

  add(node: HTMLElement, value: string): boolean {
    if (!this.canAdd(node, value)) return false;
    this.remove(node);
    node.classList.add(`${this.keyName}-${value}`);
    return true;
  }

  remove(node: HTMLElement): void {
    let matches = match(node, this.keyName);
    matches.forEach(function(name) {
      node.classList.remove(name);
    });
    if (node.classList.length === 0) {
      node.removeAttribute('class');
    }
  }

  value(node: HTMLElement): string {
    let result = match(node, this.keyName)[0] || '';
    let value = result.slice(this.keyName.length + 1); // +1 for hyphen
    return this.canAdd(node, value) ? value : '';
  }
}

export default ClassAttributor;

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_modular_admin-1.0.0 app/assets/node_modules/parchment/src/attributor/class.ts
rails_modular_admin-0.4.0 app/assets/node_modules/parchment/src/attributor/class.ts