refactor(core): replace some `for` statements with map/reduce

This commit is contained in:
Zhicheng Wang 2016-08-13 07:08:37 +08:00 committed by Vikram Subramanian
parent 05d1312306
commit 398bbb6aa9
1 changed files with 18 additions and 18 deletions

View File

@ -323,10 +323,8 @@ export class ShadowCss {
* by replacing with space. * by replacing with space.
*/ */
private _convertShadowDOMSelectors(cssText: string): string { private _convertShadowDOMSelectors(cssText: string): string {
for (let i = 0; i < _shadowDOMSelectorsRe.length; i++) { return _shadowDOMSelectorsRe.reduce(
cssText = StringWrapper.replaceAll(cssText, _shadowDOMSelectorsRe[i], ' '); (result, pattern) => { return StringWrapper.replaceAll(result, pattern, ' '); }, cssText);
}
return cssText;
} }
// change a selector like 'div' to 'name div' // change a selector like 'div' to 'name div'
@ -346,20 +344,22 @@ export class ShadowCss {
private _scopeSelector( private _scopeSelector(
selector: string, scopeSelector: string, hostSelector: string, strict: boolean): string { selector: string, scopeSelector: string, hostSelector: string, strict: boolean): string {
const r: any[] /** TODO #9100 */ = [], parts = selector.split(','); return selector.split(',')
for (let i = 0; i < parts.length; i++) { .map((part) => { return StringWrapper.split(part.trim(), _shadowDeepSelectors); })
const p = parts[i].trim(); .map((deepParts) => {
const deepParts = StringWrapper.split(p, _shadowDeepSelectors); const [shallowPart, ...otherParts] = deepParts;
const shallowPart = deepParts[0]; const applyScope = (shallowPart: string) => {
if (this._selectorNeedsScoping(shallowPart, scopeSelector)) { if (this._selectorNeedsScoping(shallowPart, scopeSelector)) {
deepParts[0] = strict && !StringWrapper.contains(shallowPart, _polyfillHostNoCombinator) ? return strict && !StringWrapper.contains(shallowPart, _polyfillHostNoCombinator) ?
this._applyStrictSelectorScope(shallowPart, scopeSelector) : this._applyStrictSelectorScope(shallowPart, scopeSelector) :
this._applySelectorScope(shallowPart, scopeSelector, hostSelector); this._applySelectorScope(shallowPart, scopeSelector, hostSelector);
} else {
return shallowPart;
} }
// replace /deep/ with a space for child selectors };
r.push(deepParts.join(' ')); return [applyScope(shallowPart), ...otherParts].join(' ');
} })
return r.join(', '); .join(', ');
} }
private _selectorNeedsScoping(selector: string, scopeSelector: string): boolean { private _selectorNeedsScoping(selector: string, scopeSelector: string): boolean {