forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compiler): normalize style prop binding names (angular#50805)
Normalizes style property names in bindings by converting them to kebab-case and stripping off `!important` PR Close angular#50805
- Loading branch information
1 parent
c27d6b6
commit 03e6dc3
Showing
4 changed files
with
60 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/compiler/src/template/pipeline/src/phases/property_name_normalization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {hyphenate} from '../../../../render3/view/style_parser'; | ||
import {OpKind} from '../../ir'; | ||
import {ComponentCompilation} from '../compilation'; | ||
|
||
/** | ||
* Normalizes the property name for style properties. The following normalizations are performed: | ||
* - Convert style property names (other than CSS vars) to hyphenated (e.g. `backgroundColor` to | ||
* `background-color`) | ||
* - Strip `!important` from the end of class and style properties | ||
*/ | ||
export function phasePropertyNameNormalization(cpl: ComponentCompilation) { | ||
for (const [, view] of cpl.views) { | ||
for (const op of view.update) { | ||
if (op.kind === OpKind.StyleProp || op.kind === OpKind.InterpolateStyleProp) { | ||
op.name = normalizeStylePropName(op.name); | ||
} else if (op.kind === OpKind.ClassProp) { | ||
op.name = stripImportant(op.name); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Normalizes a style prop name by hyphenating it and stripping `!important`. | ||
*/ | ||
function normalizeStylePropName(name: string) { | ||
if (!name.startsWith('--')) { | ||
name = hyphenate(name); | ||
} | ||
return stripImportant(name); | ||
} | ||
|
||
/** | ||
* Strips `!important` out of the op name. | ||
*/ | ||
function stripImportant(name: string) { | ||
// TODO: should we be doing this? The information seems to just be discarded. | ||
// It also strips !important from cases like [style.!important] and | ||
// [style.color!important-other-stuff], which is kind of strange. | ||
const importantIndex = name.indexOf('!important'); | ||
if (importantIndex > -1) { | ||
return name.substring(0, importantIndex); | ||
} | ||
return name; | ||
} |