@@ -51,14 +51,12 @@ function ChangeSizeAbsolute(
5151 editor . changeSizeInternal ( newSize + "pt" , shouldSetDefaultRule ) ;
5252}
5353
54- function GetUserModifiedStyleSheet ( ) : any {
54+ function GetUserModifiedStyleSheet ( ) : CSSStyleSheet | undefined {
5555 for ( let i = 0 ; i < document . styleSheets . length ; i ++ ) {
56- if ( document . styleSheets [ i ] . title == "userModifiedStyles" )
56+ if ( document . styleSheets [ i ] . title === "userModifiedStyles" )
5757 return < CSSStyleSheet > document . styleSheets [ i ] ;
5858 }
59- // this is not a valid constructor
60- //return new CSSStyleSheet();
61- return { } ;
59+ return undefined ;
6260}
6361
6462function GetFooStyleRuleFontSize ( ) : number {
@@ -83,8 +81,8 @@ function ParseRuleForFontSize(ruleText: string): number {
8381}
8482
8583function GetRuleForFooStyle ( ) : CSSRule | null {
86- const x : CSSRuleList = ( < CSSStyleSheet > GetUserModifiedStyleSheet ( ) )
87- . cssRules ;
84+ const x = GetUserModifiedStyleSheet ( ) ?. cssRules ;
85+ if ( ! x ) return null ;
8886
8987 for ( let i = 0 ; i < x . length ; i ++ ) {
9088 if ( x [ i ] . cssText . indexOf ( "foo-style" ) > - 1 ) {
@@ -95,8 +93,7 @@ function GetRuleForFooStyle(): CSSRule | null {
9593}
9694
9795function GetRuleForNormalStyle ( ) : CSSRule | null {
98- const x : CSSRuleList = ( < CSSStyleSheet > GetUserModifiedStyleSheet ( ) )
99- . cssRules ;
96+ const x = GetUserModifiedStyleSheet ( ) ?. cssRules ;
10097 if ( ! x ) return null ;
10198
10299 for ( let i = 0 ; i < x . length ; i ++ ) {
@@ -108,8 +105,7 @@ function GetRuleForNormalStyle(): CSSRule | null {
108105}
109106
110107function GetRuleForCoverTitleStyle ( ) : CSSRule | null {
111- const x : CSSRuleList = ( < CSSStyleSheet > GetUserModifiedStyleSheet ( ) )
112- . cssRules ;
108+ const x = GetUserModifiedStyleSheet ( ) ?. cssRules ;
113109 if ( ! x ) return null ;
114110 for ( let i = 0 ; i < x . length ; i ++ ) {
115111 if ( x [ i ] . cssText . indexOf ( "Title-On-Cover-style" ) > - 1 ) {
@@ -128,8 +124,9 @@ function GetCalculatedFontSize(target: string): number {
128124}
129125
130126function GetRuleMatchingSelector ( selector : string ) : CSSRule | null {
131- const x = ( < CSSStyleSheet > GetUserModifiedStyleSheet ( ) ) . cssRules ;
132- const count = 0 ;
127+ const sheet = GetUserModifiedStyleSheet ( ) ;
128+ const x = sheet ?. cssRules ;
129+ if ( ! x ) return null ;
133130 for ( let i = 0 ; i < x . length ; i ++ ) {
134131 if ( x [ i ] . cssText . indexOf ( selector ) > - 1 ) {
135132 return x [ i ] ;
@@ -139,7 +136,9 @@ function GetRuleMatchingSelector(selector: string): CSSRule | null {
139136}
140137
141138function HasRuleMatchingThisSelector ( selector : string ) : boolean {
142- const x = ( < CSSStyleSheet > GetUserModifiedStyleSheet ( ) ) . cssRules ;
139+ const sheet = GetUserModifiedStyleSheet ( ) ;
140+ const x = sheet ?. cssRules ;
141+ if ( ! x ) return false ;
143142 let count = 0 ;
144143 for ( let i = 0 ; i < x . length ; i ++ ) {
145144 if ( x [ i ] . cssText . indexOf ( selector ) > - 1 ) {
@@ -150,8 +149,8 @@ function HasRuleMatchingThisSelector(selector: string): boolean {
150149}
151150
152151function countFooStyleRules ( ) : number {
153- const x : CSSRuleList = ( < CSSStyleSheet > GetUserModifiedStyleSheet ( ) )
154- . cssRules ;
152+ const x = GetUserModifiedStyleSheet ( ) ?. cssRules ;
153+ if ( ! x ) return 0 ;
155154
156155 let count = 0 ;
157156 for ( let i = 0 ; i < x . length ; i ++ ) {
@@ -320,6 +319,79 @@ describe("StyleEditor", () => {
320319 if ( rule != null ) expect ( ParseRuleForFontSize ( rule . cssText ) ) . toBe ( 20 ) ;
321320 } ) ;
322321
322+ it ( "putAudioHiliteRulesInDom stores audio highlight css variables" , ( ) => {
323+ const editor = new StyleEditor (
324+ "file://" + "C:/dev/Bloom/src/BloomBrowserUI/bookEdit" ,
325+ ) ;
326+
327+ const sentenceSelector = "foo-style span.ui-audioCurrent" ;
328+ const paddedSentenceSelector =
329+ "foo-style span.ui-audioCurrent > span.ui-enableHighlight" ;
330+ const paragraphSelector = "foo-style.ui-audioCurrent p" ;
331+
332+ // sanity check that the rules do not yet contain the variables
333+ expect ( HasRuleMatchingThisSelector ( sentenceSelector ) ) . toBeFalsy ( ) ;
334+ expect ( HasRuleMatchingThisSelector ( paddedSentenceSelector ) ) . toBeFalsy ( ) ;
335+ expect ( HasRuleMatchingThisSelector ( paragraphSelector ) ) . toBeFalsy ( ) ;
336+
337+ editor . putAudioHiliteRulesInDom (
338+ "foo-style" ,
339+ "rgb(1, 2, 3)" ,
340+ "rgb(4, 5, 6)" ,
341+ ) ;
342+
343+ const sentenceRule = GetRuleMatchingSelector ( sentenceSelector ) ;
344+ const paddedSentenceRule = GetRuleMatchingSelector (
345+ paddedSentenceSelector ,
346+ ) ;
347+ const paragraphRule = GetRuleMatchingSelector ( paragraphSelector ) ;
348+
349+ expect ( sentenceRule ?. cssText ) . toContain (
350+ "--bloom-audio-highlight-background: rgb(4, 5, 6)" ,
351+ ) ;
352+ expect ( sentenceRule ?. cssText ) . toContain (
353+ "--bloom-audio-highlight-text-color: rgb(1, 2, 3)" ,
354+ ) ;
355+ expect ( paddedSentenceRule ?. cssText ) . toContain (
356+ "--bloom-audio-highlight-background: rgb(4, 5, 6)" ,
357+ ) ;
358+ expect ( paragraphRule ?. cssText ) . toContain (
359+ "--bloom-audio-highlight-background: rgb(4, 5, 6)" ,
360+ ) ;
361+ expect ( sentenceRule ?. cssText ) . not . toMatch (
362+ / ( ^ | [ ; { \s ] ) b a c k g r o u n d - c o l o r \s * : / ,
363+ ) ;
364+ expect ( sentenceRule ?. cssText ) . not . toMatch ( / ( ^ | [ ; { \s ] ) c o l o r \s * : / ) ;
365+ expect ( paddedSentenceRule ?. cssText ) . not . toMatch (
366+ / ( ^ | [ ; { \s ] ) b a c k g r o u n d - c o l o r \s * : / ,
367+ ) ;
368+ expect ( paragraphRule ?. cssText ) . not . toMatch (
369+ / ( ^ | [ ; { \s ] ) b a c k g r o u n d - c o l o r \s * : / ,
370+ ) ;
371+ } ) ;
372+
373+ it ( "getAudioHiliteProps reads colors from audio highlight css variables" , ( ) => {
374+ const editor = new StyleEditor (
375+ "file://" + "C:/dev/Bloom/src/BloomBrowserUI/bookEdit" ,
376+ ) ;
377+
378+ const origProps = editor . getAudioHiliteProps ( "foo-style" ) ;
379+
380+ expect ( origProps ?. hiliteTextColor ) . not . toBe ( "rgb(1, 2, 3)" ) ;
381+ expect ( origProps ?. hiliteBgColor ) . not . toBe ( "rgb(4, 5, 6)" ) ;
382+
383+ editor . putAudioHiliteRulesInDom (
384+ "foo-style" ,
385+ "rgb(1, 2, 3)" ,
386+ "rgb(4, 5, 6)" ,
387+ ) ;
388+
389+ const props = editor . getAudioHiliteProps ( "foo-style" ) ;
390+
391+ expect ( props . hiliteTextColor ) . toBe ( "rgb(1, 2, 3)" ) ;
392+ expect ( props . hiliteBgColor ) . toBe ( "rgb(4, 5, 6)" ) ;
393+ } ) ;
394+
323395 // Skipped because currently we're running in jsdom. Making use of the existing rule depends on
324396 // getComputedStyle, which jsdom does not support. ChatGpt thinks it also depends on actual
325397 // dom element sizes, which jsdom also does not support. Attempts to polyfill proved difficult.
0 commit comments