Skip to content

Commit

Permalink
Introduce a tag argument to restrict matches
Browse files Browse the repository at this point in the history
  • Loading branch information
gael-ian committed May 19, 2024
1 parent 08153ee commit 59648ac
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
12 changes: 6 additions & 6 deletions npm/__tests__/unit/cocooned/plugins/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ describe('coreMixin', () => {
describe('replacements', () => {
it('returns default replacements', () => {
expect(given.extended.replacements).toEqual(expect.arrayContaining([
{ attribute: 'for', delimiters: ['_'] },
{ attribute: 'id', delimiters: ['_'] },
{ attribute: 'name', delimiters: ['[', ']'] }
{ tag: 'label', attribute: 'for', delimiters: ['_'] },
{ tag: '*', attribute: 'id', delimiters: ['_'] },
{ tag: '*', attribute: 'name', delimiters: ['[', ']'] }
]))
})

it('returns replacements for Trix compatibility', () => {
expect(given.extended.replacements).toEqual(expect.arrayContaining([
{ attribute: 'input', delimiters: ['_'] }
{ tag: 'trix-editor', attribute: 'input', delimiters: ['_'] }
]))
})

describe('when extended', () => {
beforeEach(() => {
given.extended.registerReplacement(given.attribute, given.delimiter)
given.extended.registerReplacement({ attribute: given.attribute, delimiters: [given.delimiter] })
})

given('attribute', () => faker.lorem.word())
given('delimiter', () => faker.string.fromCharacters('_.-/'))

it('returns registered additional replacement', () => {
expect(given.extended.replacements).toEqual(expect.arrayContaining([
{ attribute: given.attribute, delimiters: [given.delimiter] }
{ attribute: given.attribute, delimiters: [given.delimiter], tag: '*' }
]))
})
})
Expand Down
16 changes: 7 additions & 9 deletions npm/src/cocooned/plugins/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import { Replacement } from './core/triggers/add/replacement.js'
import { clickHandler, itemDelegatedClickHandler } from '../events/handlers.js'

const coreMixin = (Base) => class extends Base {
static registerReplacement (attribute, ...delimiters) {
this.__replacements.push({ attribute, delimiters })
static registerReplacement ({ tag = '*', attribute, delimiters }) {
this.__replacements.push({ tag, attribute, delimiters })
}

static get replacements () {
return this.__replacements;
}

static replacementsFor (association) {
return this.replacements.map(r => {
return new Replacement(r.attribute, association, ...r.delimiters)
})
return this.replacements.map(r => new Replacement({ association, ...r }))
}

static get selectors () {
Expand Down Expand Up @@ -54,12 +52,12 @@ const coreMixin = (Base) => class extends Base {
/* Protected and private attributes and methods */
static __replacements = [
// Default attributes
{ attribute: 'for', delimiters: ['_'] },
{ attribute: 'id', delimiters: ['_'] },
{ attribute: 'name', delimiters: ['[', ']'] },
{ tag: 'label', attribute: 'for', delimiters: ['_'] },
{ tag: '*', attribute: 'id', delimiters: ['_'] },
{ tag: '*', attribute: 'name', delimiters: ['[', ']'] },

// Compatibility with Trix. See #65 on Github.
{ attribute: 'input', delimiters: ['_'] },
{ tag: 'trix-editor', attribute: 'input', delimiters: ['_'] },
];
}

Expand Down
4 changes: 3 additions & 1 deletion npm/src/cocooned/plugins/core/triggers/add/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Builder {

#applyReplacements (node, id) {
this.#replacements.forEach(replacement => {
node.querySelectorAll(`*[${replacement.attribute}]`).forEach(node => replacement.apply(node, id))
node.querySelectorAll(`${replacement.tag}[${replacement.attribute}]`).forEach(node => {
return replacement.apply(node, id)
})
})

node.querySelectorAll('template').forEach(template => {
Expand Down
8 changes: 5 additions & 3 deletions npm/src/cocooned/plugins/core/triggers/add/replacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ const reHasRegExpChar = RegExp(reRegExpChar.source)

class Replacement {
attribute
tag

constructor (attribute, association, startDelimiter, endDelimiter = null) {
constructor ({ tag = '*', attribute, association, delimiters }) {
this.attribute = attribute
this.tag = tag

this.#association = association
this.#startDelimiter = startDelimiter
this.#endDelimiter = endDelimiter || startDelimiter
this.#startDelimiter = delimiters[0]
this.#endDelimiter = delimiters[delimiters.length - 1]
}

apply (node, id) {
Expand Down

0 comments on commit 59648ac

Please sign in to comment.