cocooned 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec1afe38e7db15aee028d1890edcb0cb661bf62c696b2a79aa5102c573a8a1fe
4
- data.tar.gz: df9f13885a634db0e7b3f1f1348573b0aa6bfa1f73a488de918dbaf3cf07dc15
3
+ metadata.gz: 4a59c32459a7aee2edc8eb770c60764f1de44003cceed283335b969c81541aa2
4
+ data.tar.gz: 1be857f2d25e18a1cc91087d5f8cc2c68ce1b3b3abdb468b6dd52751724aa5af
5
5
  SHA512:
6
- metadata.gz: 8562d9fe1b0a704991ad1ba284e3e219a62e9da606b8791aa5ffbca23647f80abed6d3826f730533d7bb2dbb12c3b3c9ecfd2d670d17f6929ad0c72ca027f37e
7
- data.tar.gz: 4e3157dbe296abd6b18162e34e7f98d8cba90ad14c7f449cb9cc56ad66bca9a8cee31c6a04d3ed8cbbf35832e420c2b05adccb1e9ea0ad1f5c82ac058c5bd2d7
6
+ metadata.gz: 16645d6101fa1b8010e20ea13282d098a31e129bc23e8ee14fd6510278a6f4dc40421e68e486921143e2305fc183de38315479fda24a40779394fa1797477676
7
+ data.tar.gz: a4cc58468cde6ec46dd12defb9bbc0845aba6d846058425082c28a3ac81dfc9171e4c0fd9df7e9f942c1915cbc548287aa34fcf5eaaeec1d5859eb0e293c03f0
data/CHANGELOG.md CHANGED
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## Version 2.2.1 (2024-05-20)
10
+
11
+ ## Fixed
12
+
13
+ * Add UMD build of Coconned missing in 2.2.0
14
+
9
15
  ## Version 2.2.0 (2024-05-20)
10
16
 
11
17
  ## Added
@@ -248,63 +248,10 @@
248
248
  }
249
249
  }
250
250
 
251
- /**
252
- * Borrowed from Lodash
253
- * See https://lodash.com/docs/#escapeRegExp
254
- */
255
- const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
256
- const reHasRegExpChar = RegExp(reRegExpChar.source);
257
-
258
- class Replacement {
259
- attribute
260
-
261
- constructor (attribute, name, startDelimiter, endDelimiter = null) {
262
- this.attribute = attribute;
263
-
264
- this.#name = name;
265
- this.#startDelimiter = startDelimiter;
266
- this.#endDelimiter = endDelimiter || startDelimiter;
267
- }
268
-
269
- apply (node, id) {
270
- const value = node.getAttribute(this.attribute);
271
- if (!this.#regexp.test(value)) {
272
- return
273
- }
274
-
275
- node.setAttribute(this.attribute, value.replace(this.#regexp, this.#replacement(id)));
276
- }
277
-
278
- /* Protected and private attributes and methods */
279
- #name
280
- #startDelimiter
281
- #endDelimiter
282
-
283
- #replacement (id) {
284
- return `${this.#startDelimiter}${id}${this.#endDelimiter}$1`
285
- }
286
-
287
- get #regexp () {
288
- const escaped = this.#escape(`${this.#startDelimiter}${this.#name}${this.#endDelimiter}`);
289
- return new RegExp(`${escaped}(.*?)`, 'g')
290
- }
291
-
292
- #escape (string) {
293
- return (string && reHasRegExpChar.test(string))
294
- ? string.replace(reRegExpChar, '\\$&')
295
- : (string || '')
296
- }
297
- }
298
-
299
251
  class Builder {
300
- constructor (documentFragment, association) {
252
+ constructor (documentFragment, replacements) {
301
253
  this.#documentFragment = documentFragment;
302
- this.#association = association;
303
- this.#replacements = [
304
- new Replacement('for', association, '_'),
305
- new Replacement('id', association, '_'),
306
- new Replacement('name', association, '[', ']')
307
- ];
254
+ this.#replacements = replacements;
308
255
  }
309
256
 
310
257
  build (id) {
@@ -314,13 +261,14 @@
314
261
  }
315
262
 
316
263
  /* Protected and private attributes and methods */
317
- #association
318
264
  #documentFragment
319
265
  #replacements
320
266
 
321
267
  #applyReplacements (node, id) {
322
268
  this.#replacements.forEach(replacement => {
323
- node.querySelectorAll(`*[${replacement.attribute}]`).forEach(node => replacement.apply(node, id));
269
+ node.querySelectorAll(`${replacement.tag}[${replacement.attribute}]`).forEach(node => {
270
+ return replacement.apply(node, id)
271
+ });
324
272
  });
325
273
 
326
274
  node.querySelectorAll('template').forEach(template => {
@@ -478,7 +426,10 @@
478
426
  return null
479
427
  }
480
428
 
481
- return new Builder(template.content, `new_${this.#dataset.association}`)
429
+ return new Builder(
430
+ template.content,
431
+ this.#cocooned.replacementsFor(`new_${this.#dataset.association}`)
432
+ )
482
433
  }
483
434
 
484
435
  _extractCount () {
@@ -675,6 +626,56 @@
675
626
  }
676
627
  }
677
628
 
629
+ /**
630
+ * Borrowed from Lodash
631
+ * See https://lodash.com/docs/#escapeRegExp
632
+ */
633
+ const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
634
+ const reHasRegExpChar = RegExp(reRegExpChar.source);
635
+
636
+ class Replacement {
637
+ attribute
638
+ tag
639
+
640
+ constructor ({ tag = '*', attribute, association, delimiters }) {
641
+ this.attribute = attribute;
642
+ this.tag = tag;
643
+
644
+ this.#association = association;
645
+ this.#startDelimiter = delimiters[0];
646
+ this.#endDelimiter = delimiters[delimiters.length - 1];
647
+ }
648
+
649
+ apply (node, id) {
650
+ const value = node.getAttribute(this.attribute);
651
+ if (!this.#regexp.test(value)) {
652
+ return
653
+ }
654
+
655
+ node.setAttribute(this.attribute, value.replace(this.#regexp, this.#replacement(id)));
656
+ }
657
+
658
+ /* Protected and private attributes and methods */
659
+ #association
660
+ #startDelimiter
661
+ #endDelimiter
662
+
663
+ #replacement (id) {
664
+ return `${this.#startDelimiter}${id}${this.#endDelimiter}$1`
665
+ }
666
+
667
+ get #regexp () {
668
+ const escaped = this.#escape(`${this.#startDelimiter}${this.#association}${this.#endDelimiter}`);
669
+ return new RegExp(`${escaped}(.*?)`, 'g')
670
+ }
671
+
672
+ #escape (string) {
673
+ return (string && reHasRegExpChar.test(string))
674
+ ? string.replace(reRegExpChar, '\\$&')
675
+ : (string || '')
676
+ }
677
+ }
678
+
678
679
  function clickHandler$1 (callback) {
679
680
  return e => {
680
681
  e.preventDefault();
@@ -708,6 +709,18 @@
708
709
  }
709
710
 
710
711
  const coreMixin = (Base) => class extends Base {
712
+ static registerReplacement ({ tag = '*', attribute, delimiters }) {
713
+ this.__replacements.push({ tag, attribute, delimiters });
714
+ }
715
+
716
+ static get replacements () {
717
+ return this.__replacements
718
+ }
719
+
720
+ static replacementsFor (association) {
721
+ return this.replacements.map(r => new Replacement({ association, ...r }))
722
+ }
723
+
711
724
  static get selectors () {
712
725
  return {
713
726
  ...super.selectors,
@@ -736,6 +749,21 @@
736
749
  })
737
750
  );
738
751
  }
752
+
753
+ replacementsFor (association) {
754
+ return this.constructor.replacementsFor(association)
755
+ }
756
+
757
+ /* Protected and private attributes and methods */
758
+ static __replacements = [
759
+ // Default attributes
760
+ { tag: 'label', attribute: 'for', delimiters: ['_'] },
761
+ { tag: '*', attribute: 'id', delimiters: ['_'] },
762
+ { tag: '*', attribute: 'name', delimiters: ['[', ']'] },
763
+
764
+ // Compatibility with Trix. See #65 on Github.
765
+ { tag: 'trix-editor', attribute: 'input', delimiters: ['_'] }
766
+ ]
739
767
  };
740
768
 
741
769
  let Cocooned$1 = class Cocooned extends coreMixin(Base) {
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cocooned
4
- VERSION = '2.2.0'
4
+ VERSION = '2.2.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocooned
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gaël-Ian Havard