From 6fe07ff092843e5a2c0b74ecae630fae008167fb Mon Sep 17 00:00:00 2001 From: Muir Adams Date: Mon, 4 Feb 2019 14:27:42 -0800 Subject: [PATCH 1/3] Fix issue where the attributes of a component are combined with the attributes of any of its nested components and included in both. Now components and each of their nested components have their own attributes object. --- src/adapter.js | 86 +++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/src/adapter.js b/src/adapter.js index 3b90e22..7b25c41 100644 --- a/src/adapter.js +++ b/src/adapter.js @@ -48,6 +48,48 @@ class TwigAdapter extends Fractal.Adapter { const render = Twig.Template.prototype.render; Twig.Template.prototype.render = function(context, params) { + let attributes = new AttributesObject(); + + function AttributesObject() { + let self = this; + this.classes = ''; + this.attr = []; + + this.addClass = function(...str) { + // console.log(str); + self.classes = _.flatten(str).join(' '); + + return self; + }; + + this.removeClass = function(...str) { + // todo implement + // self.classes = str.join(' '); + + return self; + }; + + this.setAttribute = function(attribute, value) { + let str = `${attribute}="${value}"`; + + self.attr.push(str); + self.attr = _.uniq(self.attr); + + return self; + }; + } + + AttributesObject.prototype.toString = function toString() { + let attrList = [ + this.classes ? `class="${this.classes}"` : '', + this.attr ? this.attr.join(' ') : '', + ]; + + return attrList.join(' '); + }; + + context['attributes'] = attributes; + if (!self._config.pristine && this.id) { let handle = null; @@ -177,51 +219,10 @@ class TwigAdapter extends Fractal.Adapter { } render(path, str, context, meta) { - let attributes = new AttributesObject(); let self = this; meta = meta || {}; - function AttributesObject() { - let self = this; - this.classes = ''; - this.attr = []; - - this.addClass = function(...str) { - // console.log(str); - self.classes = _.flatten(str).join(' '); - - return self; - }; - - this.removeClass = function(...str) { - // todo implement - // self.classes = str.join(' '); - - return self; - }; - - this.setAttribute = function(attribute, value) { - let str = `${attribute}="${value}"`; - - self.attr.push(str); - self.attr = _.uniq(self.attr); - - return self; - }; - } - - AttributesObject.prototype.toString = function toString() { - let attrList = [ - this.classes ? `class="${this.classes}"` : '', - this.attr ? this.attr.join(' ') : '', - ]; - - return attrList.join(' '); - }; - - - if (!this._config.pristine) { setEnv('_self', meta.self, context); setEnv('_target', meta.target, context); @@ -229,7 +230,6 @@ class TwigAdapter extends Fractal.Adapter { setEnv('_config', this._app.config(), context); setEnv('title_prefix', '', context); setEnv('title_suffix', '', context); - setEnv('attributes', attributes, context); } return new Promise(function(resolve, reject){ @@ -244,7 +244,7 @@ class TwigAdapter extends Fractal.Adapter { name: meta.self ? `${self._config.handlePrefix}${meta.self.handle}` : tplPath, precompiled: str }); - resolve(template.render(context)); + resolve(template.render(context, 'test')); } catch (e) { reject(new Error(e)); } From d2301d4eccffaf154aff90c89bfc87c87cdfc7bd Mon Sep 17 00:00:00 2001 From: Muir Adams Date: Mon, 4 Feb 2019 14:29:55 -0800 Subject: [PATCH 2/3] Remove function argument used for testing. --- src/adapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapter.js b/src/adapter.js index 7b25c41..408d7fc 100644 --- a/src/adapter.js +++ b/src/adapter.js @@ -244,7 +244,7 @@ class TwigAdapter extends Fractal.Adapter { name: meta.self ? `${self._config.handlePrefix}${meta.self.handle}` : tplPath, precompiled: str }); - resolve(template.render(context, 'test')); + resolve(template.render(context)); } catch (e) { reject(new Error(e)); } From 578d619d205fd37f0a02db9ffb1870627d3830f4 Mon Sep 17 00:00:00 2001 From: Muir Adams Date: Mon, 4 Feb 2019 14:51:23 -0800 Subject: [PATCH 3/3] Do not add attributes if config is set to pristine --- src/adapter.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/adapter.js b/src/adapter.js index 408d7fc..2e750e9 100644 --- a/src/adapter.js +++ b/src/adapter.js @@ -88,7 +88,9 @@ class TwigAdapter extends Fractal.Adapter { return attrList.join(' '); }; - context['attributes'] = attributes; + if (!config.pristine) { + context['attributes'] = attributes; + } if (!self._config.pristine && this.id) {