Skip to content

Commit

Permalink
Fixes #121 - Allow for w-preserve-attrs to enable preservation of att…
Browse files Browse the repository at this point in the history
…ributes
  • Loading branch information
patrick-steele-idem committed Jan 29, 2016
1 parent ad7fd0b commit f13575b
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ exports.renderer = function(input, out) {

# 5.x

## 5.3.x

### v5.3.0

- Fixes [#121](https://github.com/marko-js/marko-widgets/issues/121) - Allow for w-preserve-attrs to enable preservation of attributes:

```html
<div w-preserve-attrs="style,class"></div>
```

## 5.2.x

### v5.2.2
Expand Down
10 changes: 10 additions & 0 deletions lib/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ Widget.prototype = widgetProto = {
var id = fromEl.id;
var existingWidget;

var preservedAttrs = toEl.getAttribute('data-w-preserve-attrs');
if (preservedAttrs) {
preservedAttrs = preservedAttrs.split(/\s*[,]\s*/);
for (var i=0; i<preservedAttrs.length; i++) {
var preservedAttrName = preservedAttrs[i];
var preservedAttrValue = fromEl.getAttribute(preservedAttrName);
toEl.setAttribute(preservedAttrName, preservedAttrValue);
}
}

if (widgetsContext && id) {
if (widgetsContext.isPreservedEl(id)) {

Expand Down
4 changes: 4 additions & 0 deletions marko-taglib.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"w-preserve-body-if": {
"type": "expression",
"preserve-name": true
},
"w-preserve-attrs": {
"type": "string",
"preserve-name": true
}
},
"transformer": "./taglib/widgets-transformer.js"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@
"./lib/init-widgets.js": "./lib/init-widgets-browser.js",
"./lib/defineWidget.js": "./lib/defineWidget-browser.js"
},
"version": "5.2.3"
"version": "5.3.0"
}
24 changes: 24 additions & 0 deletions taglib/TransformHelper/handleWidgetPreserveAttrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2011 eBay Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

function handleWidgetEvents() {

var preserveAttrsExpression = this.node.getProperty('w-preserve-attrs');
this.node.removeProperty('w-preserve-attrs');
this.node.setAttribute('data-w-preserve-attrs', preserveAttrsExpression);
}

module.exports = handleWidgetEvents;
1 change: 1 addition & 0 deletions taglib/TransformHelper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ TransformHelper. prototype = {
getContainingWidgetNode: require('./getContainingWidgetNode'),
handleWidgetEvents: require('./handleWidgetEvents'),
handleWidgetPreserve: require('./handleWidgetPreserve'),
handleWidgetPreserveAttrs: require('./handleWidgetPreserveAttrs'),
handleWidgetBody: require('./handleWidgetBody'),
handleWidgetBind: require('./handleWidgetBind'),
handleWidgetExtend: require('./handleWidgetExtend'),
Expand Down
4 changes: 4 additions & 0 deletions taglib/widgets-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ exports.process =function (node, compiler, template) {
transformHelper.handleWidgetPreserve();
}

if (nodeProps['w-preserve-attrs'] != null) {
transformHelper.handleWidgetPreserveAttrs();
}

if (nodeProps['w-body'] != null) {
transformHelper.handleWidgetBody();
}
Expand Down
9 changes: 9 additions & 0 deletions test/client/spec-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,4 +718,13 @@ describe('widget' , function() {

widget.testStopPropagation();
});

it('should allow w-preserve-attrs', function() {
var widget = require('../fixtures/components/app-preserve-attrs')
.render({})
.appendTo(document.getElementById('target'))
.getWidget();

widget.testPreserveAttrs();
});
});
39 changes: 39 additions & 0 deletions test/fixtures/components/app-preserve-attrs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var expect = require('chai').expect;

module.exports = require('marko-widgets').defineComponent({
template: require('./template.marko'),
init: function() {
},
getInitialState: function() {
return {
name: 'Joe'
};
},

getTemplateData: function(state) {
return state;
},

testPreserveAttrs: function() {
var helloEl = this.getEl('hello');
expect(helloEl.innerHTML).to.equal('Hello Joe!');

this.el.style.border = "1px solid red";
this.el.className = "foo";

this.el.querySelector('a').href = "http://www.foo.com/";

var self = this;

require('marko-widgets').batchUpdate(function() {
self.setState('name', 'Frank');
});

expect(helloEl.innerHTML).to.equal('Hello Frank!');

// Make sure the preserved attributes did not change
expect(this.el.style.border).to.equal("1px solid red");
expect(this.el.className).to.equal("foo");
expect(this.el.querySelector('a').href).to.equal("http://www.foo.com/");
}
});
6 changes: 6 additions & 0 deletions test/fixtures/components/app-preserve-attrs/template.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div w-bind w-preserve-attrs="style,class">
<a href="http://www.ebay.com" w-preserve-attrs="href">
eBay
</a>
<span w-id="hello">Hello ${data.name}!</span>
</div>

0 comments on commit f13575b

Please sign in to comment.