Skip to content

Commit bd012ef

Browse files
vicbchuckjaz
authored andcommitted
fix(ShadowCss): support [attr="value with space"]
fixes angular#6249
1 parent 2dd3996 commit bd012ef

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

modules/@angular/compiler/src/shadow_css.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,21 +417,35 @@ export class ShadowCss {
417417
return scopedP;
418418
};
419419

420-
const sep = /( |>|\+|~(?!=))\s*/g;
420+
const sep = /( |>|\+|~(?!=)|\[|\])\s*/g;
421421
const scopeAfter = selector.indexOf(_polyfillHostNoCombinator);
422422

423423
let scoped = '';
424424
let startIndex = 0;
425425
let res: RegExpExecArray;
426+
let inAttributeSelector: boolean = false;
426427

427428
while ((res = sep.exec(selector)) !== null) {
428429
const separator = res[1];
429-
const part = selector.slice(startIndex, res.index).trim();
430-
// if a selector appears before :host-context it should not be shimmed as it
431-
// matches on ancestor elements and not on elements in the host's shadow
432-
const scopedPart = startIndex >= scopeAfter ? _scopeSelectorPart(part) : part;
433-
scoped += `${scopedPart} ${separator} `;
434-
startIndex = sep.lastIndex;
430+
if (separator === '[') {
431+
inAttributeSelector = true;
432+
scoped += selector.slice(startIndex, res.index).trim() + '[';
433+
startIndex = sep.lastIndex;
434+
}
435+
if (!inAttributeSelector) {
436+
const part = selector.slice(startIndex, res.index).trim();
437+
// if a selector appears before :host-context it should not be shimmed as it
438+
// matches on ancestor elements and not on elements in the host's shadow
439+
const scopedPart = startIndex >= scopeAfter ? _scopeSelectorPart(part) : part;
440+
scoped += `${scopedPart} ${separator} `;
441+
startIndex = sep.lastIndex;
442+
} else if (separator === ']') {
443+
const part = selector.slice(startIndex, res.index).trim() + ']';
444+
const scopedPart = startIndex >= scopeAfter ? _scopeSelectorPart(part) : part;
445+
scoped += `${scopedPart} `;
446+
startIndex = sep.lastIndex;
447+
inAttributeSelector = false;
448+
}
435449
}
436450
return scoped + _scopeSelectorPart(selector.substring(startIndex));
437451
}

modules/@angular/compiler/test/shadow_css_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export function main() {
9191
expect(s('one[attr*="value"] {}', 'a')).toEqual('one[attr*="value"][a] {}');
9292
expect(s('one[attr|="value"] {}', 'a')).toEqual('one[attr|="value"][a] {}');
9393
expect(s('one[attr~="value"] {}', 'a')).toEqual('one[attr~="value"][a] {}');
94+
expect(s('one[attr="va lue"] {}', 'a')).toEqual('one[attr="va lue"][a] {}');
9495
expect(s('one[attr] {}', 'a')).toEqual('one[attr][a] {}');
9596
expect(s('[is="one"] {}', 'a')).toEqual('[is="one"][a] {}');
9697
});

0 commit comments

Comments
 (0)