Skip to content

Commit 044746f

Browse files
Brooksjdanyow
authored andcommitted
fix(StyleObserver): parse complex styles
fixes templating-resources/issues/248
1 parent 347ac4f commit 044746f

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/element-observation.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ export class StyleObserver {
7474
}
7575
}
7676
} else if (newValue.length) {
77-
let pairs = newValue.split(/(?:;|:(?!\/))\s*/);
78-
for (let i = 0, length = pairs.length; i < length; i++) {
79-
style = pairs[i].trim();
77+
let rx = /\s*([\w\-]+)\s*:\s*((?:(?:[\w\-]+\(\s*(?:"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|[\w\-]+\(\s*(?:^"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|[^\)]*)\),?|[^\)]*)\),?|"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|[^;]*),?\s*)+);?/g;
78+
let pair;
79+
while( (pair = rx.exec(newValue)) !== null )
80+
{
81+
style = pair[1];
8082
if ( !style ) { continue; }
8183

8284
styles[style] = version;
83-
84-
this.element.style[style] = pairs[++i];
85+
this.element.style[style] = pair[2];
8586
}
8687
}
8788
}

test/element-observation.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,34 @@ describe('element observation', () => {
168168
expect(el.style.backgroundColor).toBe('');
169169
expect(el.style.backgroundImage).toBe('');
170170

171+
observer.setValue(` width : 25px ; background: url("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]%@!$&'()*+,;=");`);
172+
expect(observer.getValue()).toBe(`width: 25px; background: url("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]%@!$&'()*+,;=");`);
173+
174+
observer.setValue('');
175+
expect(el.style.width).toBe('');
176+
expect(el.style.background).toBe('');
177+
178+
observer.setValue(` width : 25px ; background: url('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]%@!$&\\'()*+,;=');`);
179+
expect(observer.getValue()).toBe(`width: 25px; background: url("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]%@!$&'()*+,;=");`);
180+
181+
observer.setValue('');
182+
expect(el.style.width).toBe('');
183+
expect(el.style.background).toBe('');
184+
185+
observer.setValue(` color : rgb( 255 , 255 , 255 ) ; background: url(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]%@!$&*+,;=);`);
186+
expect(observer.getValue()).toBe(`color: rgb(255, 255, 255); background: url("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]%@!$&*+,;=");`);
187+
188+
observer.setValue('');
189+
expect(el.style.color).toBe('');
190+
expect(el.style.background).toBe('');
191+
192+
observer.setValue(`background: url(data:image/gif;base64,R0lGODh0o/XBs/fNl3/zy7//wA7);`);
193+
expect(observer.getValue()).toBe(`background: url("data:image/gif;base64,R0lGODh0o/XBs/fNl3/zy7//wA7");`);
194+
195+
observer.setValue('');
196+
expect(el.style.width).toBe('');
197+
expect(el.style.background).toBe('');
198+
171199
observer.setValue({ width: '50px', height: '40px', 'background-color': 'blue', 'background-image': 'url("http://aurelia.io/test2.png")' });
172200
expect(observer.getValue()).toBe('width: 50px; height: 40px; background-image: url("http://aurelia.io/test2.png"); background-color: blue;');
173201
expect(el.style.height).toBe('40px');

0 commit comments

Comments
 (0)