@@ -28,10 +28,10 @@ interface IExtendedHtmlStyleElement extends HTMLStyleElement {
2828}
2929
3030interface IThemeState {
31- theme : ITheme ;
31+ theme : ITheme | undefined ;
3232 lastStyleElement : IExtendedHtmlStyleElement ;
3333 registeredStyles : IStyleRecord [ ] ;
34- loadStyles : ( processedStyles : string , rawStyles ?: string | ThemableArray ) => void ;
34+ loadStyles : ( ( processedStyles : string , rawStyles ?: string | ThemableArray ) => void ) | undefined ;
3535}
3636
3737interface IStyleRecord {
@@ -56,9 +56,8 @@ const _themeState: IThemeState = _root.__themeState__ = _root.__themeState__ ||
5656/**
5757 * Matches theming tokens. For example, "[theme: themeSlotName, default: #FFF]" (including the quotes).
5858 */
59- /* tslint:disable: max-line-length */
59+ // tslint:disable-next-line: max-line-length
6060const _themeTokenRegex : RegExp = / [ \' \" ] \[ t h e m e : \s * ( \w + ) \s * (?: \, \s * d e f a u l t : \s * ( [ \\ " \' ] ? [ \. \, \( \) \# \- \s \w ] * [ \. \, \( \) \# \- \w ] [ \" \' ] ? ) ) ? \s * \] [ \' \" ] / g;
61- /* tslint:enable: max-line-length */
6261
6362/** Maximum style text length, for supporting IE style restrictions. */
6463const MAX_STYLE_CONTENT_SIZE : number = 10000 ;
@@ -80,11 +79,11 @@ export function loadStyles(styles: string | ThemableArray): void {
8079
8180/**
8281 * Allows for customizable loadStyles logic. e.g. for server side rendering application
83- * @param {(processedStyles: string, rawStyles?: string | ThemableArray) => void }
82+ * @param {(processedStyles: string, rawStyles?: string | ThemableArray) => void }
8483 * a loadStyles callback that gets called when styles are loaded or reloaded
8584 */
8685export function configureLoadStyles (
87- loadStyles : ( processedStyles : string , rawStyles ?: string | ThemableArray ) => void
86+ loadStyles : ( ( processedStyles : string , rawStyles ?: string | ThemableArray ) => void ) | undefined
8887 ) : void {
8988 _themeState . loadStyles = loadStyles ;
9089}
@@ -110,7 +109,7 @@ function applyThemableStyles(stylesArray: ThemableArray, styleRecord?: IStyleRec
110109 * replaced.
111110 * @param {theme } theme JSON object of theme tokens to values.
112111 */
113- export function loadTheme ( theme : ITheme ) : void {
112+ export function loadTheme ( theme : ITheme | undefined ) : void {
114113 _themeState . theme = theme ;
115114
116115 // reload styles.
@@ -145,7 +144,7 @@ function reloadStyles(): void {
145144 * Find theme tokens and replaces them with provided theme values.
146145 * @param {string } styles Tokenized styles to fix.
147146 */
148- export function detokenize ( styles : string ) : string {
147+ export function detokenize ( styles : string | undefined ) : string | undefined {
149148 if ( styles ) {
150149 styles = resolveThemableArray ( splitStyles ( styles ) ) ;
151150 }
@@ -159,36 +158,29 @@ export function detokenize(styles: string): string {
159158 */
160159function resolveThemableArray ( splitStyleArray : ThemableArray ) : string {
161160 const { theme } : IThemeState = _themeState ;
162- let resolvedCss : string ;
163- if ( splitStyleArray ) {
164- // Resolve the array of theming instructions to an array of strings.
165- // Then join the array to produce the final CSS string.
166- const resolvedArray : string [ ] = splitStyleArray . map ( ( currentValue : IThemingInstruction ) => {
167- const themeSlot : string = currentValue . theme ;
168- if ( themeSlot ) {
169- // A theming annotation. Resolve it.
170- const themedValue : string = theme ? theme [ themeSlot ] : undefined ;
171- const defaultValue : string = currentValue . defaultValue ;
172-
173- // Warn to console if we hit an unthemed value even when themes are provided.
174- // Allow the themedValue to be undefined to explicitly request the default value.
175- if ( theme && ! themedValue && console && ! ( themeSlot in theme ) ) {
176- /* tslint:disable: max-line-length */
177- console . warn ( `Theming value not provided for "${ themeSlot } ". Falling back to "${ defaultValue || 'inherit' } ".` ) ;
178- /* tslint:enable: max-line-length */
179- }
180-
181- return themedValue || defaultValue || 'inherit' ;
182- } else {
183- // A non-themable string. Preserve it.
184- return currentValue . rawString ;
161+ // Resolve the array of theming instructions to an array of strings.
162+ // Then join the array to produce the final CSS string.
163+ const resolvedArray : ( string | undefined ) [ ] = ( splitStyleArray || [ ] ) . map ( ( currentValue : IThemingInstruction ) => {
164+ const themeSlot : string | undefined = currentValue . theme ;
165+ if ( themeSlot ) {
166+ // A theming annotation. Resolve it.
167+ const themedValue : string | undefined = theme ? theme [ themeSlot ] : undefined ;
168+ const defaultValue : string = currentValue . defaultValue || 'inherit' ;
169+
170+ // Warn to console if we hit an unthemed value even when themes are provided, unless "DEBUG" is false
171+ // Allow the themedValue to be undefined to explicitly request the default value.
172+ if ( theme && ! themedValue && console && ! ( themeSlot in theme ) && ( typeof DEBUG === 'undefined' || DEBUG ) ) {
173+ console . warn ( `Theming value not provided for "${ themeSlot } ". Falling back to "${ defaultValue } ".` ) ;
185174 }
186- } ) ;
187175
188- resolvedCss = resolvedArray . join ( '' ) ;
189- }
176+ return themedValue || defaultValue ;
177+ } else {
178+ // A non-themable string. Preserve it.
179+ return currentValue . rawString ;
180+ }
181+ } ) ;
190182
191- return resolvedCss ;
183+ return resolvedArray . join ( '' ) ;
192184}
193185
194186/**
@@ -199,7 +191,7 @@ export function splitStyles(styles: string): ThemableArray {
199191 const result : ThemableArray = [ ] ;
200192 if ( styles ) {
201193 let pos : number = 0 ; // Current position in styles.
202- let tokenMatch : RegExpExecArray ;
194+ let tokenMatch : RegExpExecArray | null ; // tslint:disable-line:no-null-keyword
203195 while ( tokenMatch = _themeTokenRegex . exec ( styles ) ) {
204196 const matchIndex : number = tokenMatch . index ;
205197 if ( matchIndex > pos ) {
@@ -262,9 +254,10 @@ function registerStyles(styleArray: ThemableArray, styleRecord?: IStyleRecord):
262254 */
263255function registerStylesIE ( styleArray : ThemableArray , styleRecord ?: IStyleRecord ) : void {
264256 const head : HTMLHeadElement = document . getElementsByTagName ( 'head' ) [ 0 ] ;
265- let { lastStyleElement, registeredStyles } : IThemeState = _themeState ;
257+ const registeredStyles : IStyleRecord [ ] = _themeState . registeredStyles ;
258+ let lastStyleElement : IExtendedHtmlStyleElement = _themeState . lastStyleElement ;
266259
267- const stylesheet : IStyleSheet = lastStyleElement ? lastStyleElement . styleSheet : undefined ;
260+ const stylesheet : IStyleSheet | undefined = lastStyleElement ? lastStyleElement . styleSheet : undefined ;
268261 const lastStyleContent : string = stylesheet ? stylesheet . cssText : '' ;
269262 let lastRegisteredStyle : IStyleRecord = registeredStyles [ registeredStyles . length - 1 ] ;
270263 const resolvedStyleText : string = resolveThemableArray ( styleArray ) ;
0 commit comments