Skip to content

Commit 4e56b50

Browse files
committed
Added options.appendClassNames to (very crudely) append to className instead of overwriting it, closes #15
1 parent 4801d3c commit 4e56b50

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,12 @@ Default: `false`
420420

421421
Leave the `handle` attribute intact. By default, the `handle` attribute will not be added to the created elements.
422422

423+
#### options.appendClassNames
424+
Type: `Boolean`
425+
Default: `false`
426+
427+
Append the contents of the `class` attribute value to the existing `className` property. This is useful when your web component sets its className in `createdCallback`.
428+
423429

424430
### Example
425431
```js

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ Compiler.prototype.setAttribute = function(elName, attr, value) {
211211

212212
for (var i = 0; i < attrs.length; i++) {
213213
var asProperty = asProperties[attrs[i].attr];
214-
if (asProperty) {
214+
if (attrs[i].attr === 'class' && this.options.appendClassNames) {
215+
this.pushStatement(elName+'.'+asProperty+' += '+this.makeVariableStatement(' '+attrs[i].value)+';');
216+
}
217+
else if (asProperty) {
215218
this.pushStatement(elName+'.'+asProperty+' = '+this.makeVariableStatement(attrs[i].value)+';');
216219
}
217220
else {

test/Options.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* jshint -W098 */
12
var expect = require('chai').expect;
23
var test = require('./lib/test.js');
34

@@ -38,5 +39,32 @@ describe('Options', function() {
3839
}
3940
});
4041
});
42+
43+
it('should append classNames when options.appendClassNames is true', function() {
44+
test({
45+
fixture: 'Custom Elements - With class property',
46+
done: function($, fixture, template, templateString) {
47+
expect(templateString).to.have.string('el0.className += " myNewClass";');
48+
},
49+
options: {
50+
appendClassNames: true
51+
}
52+
});
53+
});
54+
55+
it('should append classNames when options.appendClassNames is true with variables', function() {
56+
test({
57+
fixture: 'Custom Elements - With class property substitute',
58+
done: function($, fixture, template, templateString) {
59+
expect(templateString).to.have.string('el0.className += " "+data_0["className"];');
60+
},
61+
data: {
62+
className: 'myNewClass'
63+
},
64+
options: {
65+
appendClassNames: true
66+
}
67+
});
68+
});
4169
});
4270
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button is="custom-button" class="{{data.className}}">Hi</button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button is="custom-button" class="myNewClass">Hi</button>

0 commit comments

Comments
 (0)