@@ -15,52 +15,54 @@ async function requestedGoogleFontUrl(html: string): Promise<URL> {
1515 return new URL ( requestedUrl ) ;
1616}
1717
18+ async function subsetTextFor ( html : string ) : Promise < string > {
19+ const url = await requestedGoogleFontUrl ( html ) ;
20+ return url . searchParams . get ( "text" ) ?? "" ;
21+ }
22+
1823describe ( "Google Fonts text subsetting" , ( ) => {
1924 it ( "sends the composition character set to the CSS API" , async ( ) => {
20- const url = await requestedGoogleFontUrl (
25+ const text = await subsetTextFor (
2126 `<!doctype html><html><head><style>
2227 h1 { font-family: "Noto Performance Test", sans-serif; }
2328 </style></head><body><h1>旅行ランキング</h1></body></html>` ,
2429 ) ;
2530
26- const text = url . searchParams . get ( "text" ) ?? "" ;
2731 for ( const character of new Set ( "旅行ランキング" ) ) {
2832 expect ( text ) . toContain ( character ) ;
2933 }
3034 } ) ;
3135
3236 it ( "includes decoded HTML entities from visible composition text" , async ( ) => {
33- const url = await requestedGoogleFontUrl (
37+ const text = await subsetTextFor (
3438 `<!doctype html><html><head><style>
3539 h1 { font-family: "Noto Performance Test", sans-serif; }
3640 </style></head><body><h1>旅行</h1></body></html>` ,
3741 ) ;
3842
39- expect ( url . searchParams . get ( " text" ) ) . toContain ( "旅行" ) ;
43+ expect ( text ) . toContain ( "旅行" ) ;
4044 } ) ;
4145
4246 it ( "includes case variants for transformed supplemental alias weights" , async ( ) => {
43- const url = await requestedGoogleFontUrl (
47+ const text = await subsetTextFor (
4448 `<!doctype html><html><head><style>
4549 h1 { font-family: "Inter", sans-serif; font-weight: 800; text-transform: uppercase; }
4650 </style></head><body><h1>Your Kidney Transplant:<br/>What Happens Next</h1></body></html>` ,
4751 ) ;
4852
49- const text = url . searchParams . get ( "text" ) ?? "" ;
5053 expect ( encodeURIComponent ( text ) . length ) . toBeLessThan ( 700 ) ;
5154 for ( const character of new Set ( "YOUR KIDNEY TRANSPLANT:WHAT HAPPENS NEXT" ) ) {
5255 expect ( text ) . toContain ( character ) ;
5356 }
5457 } ) ;
5558
5659 it ( "covers capitalized words through the same case closure" , async ( ) => {
57- const url = await requestedGoogleFontUrl (
60+ const text = await subsetTextFor (
5861 `<!doctype html><html><head><style>
5962 h1 { font-family: "Inter", sans-serif; font-weight: 800; text-transform: capitalize; }
6063 </style></head><body><h1>hello world</h1></body></html>` ,
6164 ) ;
6265
63- const text = url . searchParams . get ( "text" ) ?? "" ;
6466 expect ( text ) . toContain ( "H" ) ;
6567 expect ( text ) . toContain ( "W" ) ;
6668 } ) ;
@@ -83,115 +85,107 @@ describe("Google Fonts text subsetting", () => {
8385 } ) ;
8486
8587 it ( "includes Turkish İ and ı when lang=tr is present" , async ( ) => {
86- const url = await requestedGoogleFontUrl (
88+ const text = await subsetTextFor (
8789 `<!doctype html><html lang="tr"><head><style>
8890 h1 { font-family: "Inter", sans-serif; text-transform: uppercase; }
8991 </style></head><body><h1>istanbul</h1></body></html>` ,
9092 ) ;
9193
92- const text = url . searchParams . get ( "text" ) ?? "" ;
9394 expect ( text ) . toContain ( "İ" ) ;
9495 expect ( text ) . toContain ( "ı" ) ;
9596 } ) ;
9697
9798 it ( "does not include Turkish İ/ı without a Turkish lang attribute" , async ( ) => {
98- const url = await requestedGoogleFontUrl (
99+ const text = await subsetTextFor (
99100 `<!doctype html><html lang="en"><head><style>
100101 h1 { font-family: "Inter", sans-serif; text-transform: uppercase; }
101102 </style></head><body><h1>istanbul</h1></body></html>` ,
102103 ) ;
103104
104- const text = url . searchParams . get ( "text" ) ?? "" ;
105105 expect ( text ) . not . toContain ( "İ" ) ;
106106 expect ( text ) . not . toContain ( "ı" ) ;
107107 } ) ;
108108
109109 it ( "includes Azeri locale variants when lang=az is present" , async ( ) => {
110- const url = await requestedGoogleFontUrl (
110+ const text = await subsetTextFor (
111111 `<!doctype html><html lang="az"><head><style>
112112 p { font-family: "Inter", sans-serif; }
113113 </style></head><body><p>iyi</p></body></html>` ,
114114 ) ;
115115
116- const text = url . searchParams . get ( "text" ) ?? "" ;
117116 expect ( text ) . toContain ( "İ" ) ;
118117 expect ( text ) . toContain ( "ı" ) ;
119118 } ) ;
120119
121120 it ( "maps ASCII to fullwidth equivalents when full-width appears in the source" , async ( ) => {
122- const url = await requestedGoogleFontUrl (
121+ const text = await subsetTextFor (
123122 `<!doctype html><html><head><style>
124123 p { font-family: "Noto Performance Test", sans-serif; text-transform: full-width; }
125124 </style></head><body><p>ABC</p></body></html>` ,
126125 ) ;
127126
128- const text = url . searchParams . get ( "text" ) ?? "" ;
129127 expect ( text ) . toContain ( "A" ) ;
130128 expect ( text ) . toContain ( "B" ) ;
131129 expect ( text ) . toContain ( "C" ) ;
132130 } ) ;
133131
134132 it ( "does not add fullwidth variants without full-width in the source" , async ( ) => {
135- const url = await requestedGoogleFontUrl (
133+ const text = await subsetTextFor (
136134 `<!doctype html><html><head><style>
137135 p { font-family: "Noto Performance Test", sans-serif; }
138136 </style></head><body><p>ABC</p></body></html>` ,
139137 ) ;
140138
141- const text = url . searchParams . get ( "text" ) ?? "" ;
142139 expect ( text ) . not . toContain ( "A" ) ;
143140 } ) ;
144141
145142 it ( "maps small kana to full-size equivalents when full-size-kana appears in the source" , async ( ) => {
146- const url = await requestedGoogleFontUrl (
143+ const text = await subsetTextFor (
147144 `<!doctype html><html><head><style>
148145 p { font-family: "Noto Performance Test", sans-serif; text-transform: full-size-kana; }
149146 </style></head><body><p>ぁっょ</p></body></html>` ,
150147 ) ;
151148
152- const text = url . searchParams . get ( "text" ) ?? "" ;
153149 expect ( text ) . toContain ( "あ" ) ;
154150 expect ( text ) . toContain ( "つ" ) ;
155151 expect ( text ) . toContain ( "よ" ) ;
156152 } ) ;
157153
158154 it ( "maps small katakana to full-size when full-size-kana appears in the source" , async ( ) => {
159- const url = await requestedGoogleFontUrl (
155+ const text = await subsetTextFor (
160156 `<!doctype html><html><head><style>
161157 p { font-family: "Noto Performance Test", sans-serif; text-transform: full-size-kana; }
162158 </style></head><body><p>ァヵ</p></body></html>` ,
163159 ) ;
164160
165- const text = url . searchParams . get ( "text" ) ?? "" ;
166161 expect ( text ) . toContain ( "ア" ) ;
167162 expect ( text ) . toContain ( "カ" ) ;
168163 } ) ;
169164
170165 it ( "stays within the URL budget for a realistic mixed-script composition with all transforms" , async ( ) => {
171- const latin = "The Quick Brown Fox Jumps Over The Lazy Dog — Your Kidney Transplant: What Happens Next" ;
166+ const latin =
167+ "The Quick Brown Fox Jumps Over The Lazy Dog — Your Kidney Transplant: What Happens Next" ;
172168 const cjk = "旅行ランキング東京大阪京都名古屋福岡" ;
173169 const kana = "ぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ" ;
174170
175- const url = await requestedGoogleFontUrl (
171+ const text = await subsetTextFor (
176172 `<!doctype html><html lang="tr"><head><style>
177173 h1 { font-family: "Noto Performance Test", sans-serif; text-transform: full-width; }
178174 p { font-family: "Noto Performance Test", sans-serif; text-transform: full-size-kana; }
179175 </style></head><body><h1>${ latin } </h1><p>${ cjk } ${ kana } </p></body></html>` ,
180176 ) ;
181177
182- const text = url . searchParams . get ( "text" ) ?? "" ;
183178 expect ( text . length ) . toBeGreaterThan ( 0 ) ;
184179 expect ( encodeURIComponent ( text ) . length ) . toBeLessThanOrEqual ( 1700 ) ;
185180 } ) ;
186181
187182 it ( "collects lang from nested elements, not just the root" , async ( ) => {
188- const url = await requestedGoogleFontUrl (
183+ const text = await subsetTextFor (
189184 `<!doctype html><html lang="en"><head><style>
190185 p { font-family: "Inter", sans-serif; }
191186 </style></head><body><p>hello</p><p lang="tr">istanbul</p></body></html>` ,
192187 ) ;
193188
194- const text = url . searchParams . get ( "text" ) ?? "" ;
195189 expect ( text ) . toContain ( "İ" ) ;
196190 expect ( text ) . toContain ( "ı" ) ;
197191 } ) ;
0 commit comments