-
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
16 changed files
with
669 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @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 OPEN_PAREN = 40; | ||
const CLOSE_PAREN = 41; | ||
const SINGLE_QUOTE = 39; | ||
const DOUBLE_QUOTE = 34; | ||
const COLON = 58; | ||
const SEMICOLON = 59; | ||
|
||
export function parseStyleStringIntoObj(value: string): {[key: string]: any} { | ||
const styles: {[key: string]: any} = {}; | ||
|
||
let i = 0; | ||
let parenCount = 0; | ||
let currentQuote = 0; | ||
let valueStart = 0; | ||
let propStart = 0; | ||
let currentProp: string|null = null; | ||
let valueHasQuotes = false; | ||
while (i < value.length) { | ||
const token = value.charCodeAt(i++); | ||
switch (token) { | ||
case OPEN_PAREN: | ||
parenCount++; | ||
break; | ||
case CLOSE_PAREN: | ||
parenCount--; | ||
break; | ||
case SINGLE_QUOTE: | ||
// valueStart needs to be there since prop values don't | ||
// have quotes in CSS | ||
valueHasQuotes = valueHasQuotes || valueStart > 0; | ||
currentQuote = currentQuote == SINGLE_QUOTE ? 0 : SINGLE_QUOTE; | ||
break; | ||
case DOUBLE_QUOTE: | ||
// same logic as above | ||
valueHasQuotes = valueHasQuotes || valueStart > 0; | ||
currentQuote = currentQuote == DOUBLE_QUOTE ? 0 : DOUBLE_QUOTE; | ||
break; | ||
case COLON: | ||
if (!currentProp && !parenCount && !currentQuote) { | ||
currentProp = deCamelCase(value.substring(propStart, i - 1).trim()); | ||
valueStart = i; | ||
} | ||
break; | ||
case SEMICOLON: | ||
if (currentProp && valueStart && !parenCount && !currentQuote) { | ||
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; | ||
} | ||
|
||
function chompStartAndEndQuotes(value: string): string { | ||
const qS = value.charCodeAt(0); | ||
const qE = value.charCodeAt(value.length - 1); | ||
if (qS == qE && (qS == SINGLE_QUOTE || qS == DOUBLE_QUOTE)) { | ||
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; | ||
} | ||
|
||
function deCamelCase(value: string): string { | ||
return value.replace(/[a-z][A-Z]/, 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
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
Oops, something went wrong.