cocooned 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/app/assets/javascripts/cocooned.js +86 -58
- data/lib/cocooned/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a59c32459a7aee2edc8eb770c60764f1de44003cceed283335b969c81541aa2
|
4
|
+
data.tar.gz: 1be857f2d25e18a1cc91087d5f8cc2c68ce1b3b3abdb468b6dd52751724aa5af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16645d6101fa1b8010e20ea13282d098a31e129bc23e8ee14fd6510278a6f4dc40421e68e486921143e2305fc183de38315479fda24a40779394fa1797477676
|
7
|
+
data.tar.gz: a4cc58468cde6ec46dd12defb9bbc0845aba6d846058425082c28a3ac81dfc9171e4c0fd9df7e9f942c1915cbc548287aa34fcf5eaaeec1d5859eb0e293c03f0
|
data/CHANGELOG.md
CHANGED
@@ -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,
|
252
|
+
constructor (documentFragment, replacements) {
|
301
253
|
this.#documentFragment = documentFragment;
|
302
|
-
this.#
|
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(
|
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(
|
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) {
|
data/lib/cocooned/version.rb
CHANGED