-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ivy): properly apply style="", [style], [style.foo] and [attr.st…
…yle] bindings
- Loading branch information
Showing
18 changed files
with
788 additions
and
127 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
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,108 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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 | ||
*/ | ||
|
||
const enum CharCode { | ||
OpenParen = 40, | ||
CloseParen = 41, | ||
SingleQuote = 39, | ||
DoubleQuote = 34, | ||
Colon = 58, | ||
Semicolon = 59, | ||
BackSlash = 92, | ||
} | ||
|
||
const enum QuoteMode { | ||
None = 0, | ||
Single = 1, | ||
Double = 2, | ||
} | ||
|
||
export function parseStyle(value: string): {[key: string]: any} { | ||
const styles: {[key: string]: any} = {}; | ||
|
||
let i = 0; | ||
let parenDepth = 0; | ||
let quote: QuoteMode = 0; | ||
let valueStart = 0; | ||
let propStart = 0; | ||
let currentProp: string|null = null; | ||
let valueHasQuotes = false; | ||
while (i < value.length) { | ||
const token = value.charCodeAt(i++) as CharCode; | ||
switch (token) { | ||
case CharCode.OpenParen: | ||
parenDepth++; | ||
break; | ||
case CharCode.CloseParen: | ||
parenDepth--; | ||
break; | ||
case CharCode.SingleQuote: | ||
// valueStart needs to be there since prop values don't | ||
// have quotes in CSS | ||
valueHasQuotes = valueHasQuotes || valueStart > 0; | ||
if (quote === QuoteMode.None) { | ||
quote = QuoteMode.Single; | ||
} else if (quote === QuoteMode.Single && value.charCodeAt(i - 1) !== CharCode.BackSlash) { | ||
quote = QuoteMode.None; | ||
} | ||
break; | ||
case CharCode.DoubleQuote: | ||
// same logic as above | ||
valueHasQuotes = valueHasQuotes || valueStart > 0; | ||
if (quote === QuoteMode.None) { | ||
quote = QuoteMode.Double; | ||
} else if (quote === QuoteMode.Double && value.charCodeAt(i - 1) !== CharCode.BackSlash) { | ||
quote = QuoteMode.None; | ||
} | ||
break; | ||
case CharCode.Colon: | ||
if (!currentProp && parenDepth === 0 && quote === QuoteMode.None) { | ||
currentProp = hyphenate(value.substring(propStart, i - 1).trim()); | ||
valueStart = i; | ||
} | ||
break; | ||
case CharCode.Semicolon: | ||
if (currentProp && valueStart > 0 && parenDepth === 0 && quote === QuoteMode.None) { | ||
const styleVal = value.substring(valueStart, i - 1).trim(); | ||
styles[currentProp] = valueHasQuotes ? chompStartAndEndQuotes(styleVal) : styleVal; | ||
propStart = i; | ||
valueStart = 0; | ||
currentProp = null; | ||
valueHasQuotes = false; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
if (currentProp && valueStart) { | ||
const styleVal = value.substr(valueStart).trim(); | ||
styles[currentProp] = valueHasQuotes ? chompStartAndEndQuotes(styleVal) : styleVal; | ||
} | ||
|
||
return styles; | ||
} | ||
|
||
export function chompStartAndEndQuotes(value: string): string { | ||
const qS = value.charCodeAt(0); | ||
const qE = value.charCodeAt(value.length - 1); | ||
if (qS == qE && (qS == CharCode.SingleQuote || qS == CharCode.DoubleQuote)) { | ||
const tempValue = value.substring(1, value.length - 1); | ||
// special case to avoid using a multi-quoted string that was just chomped | ||
// (e.g. `font-family: "Verdana", "sans-serif"`) | ||
if (tempValue.indexOf('\'') == -1 && tempValue.indexOf('"') == -1) { | ||
value = tempValue; | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
export function hyphenate(value: string): string { | ||
return value.replace(/[a-z][A-Z]/g, v => { | ||
return v.charAt(0) + '-' + v.charAt(1); | ||
}).toLowerCase(); | ||
} |
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
Oops, something went wrong.