@@ -36,13 +36,13 @@ class JSDOM {
3636 const mimeType = new MIMEType ( options . contentType === undefined ? "text/html" : options . contentType ) ;
3737 const { html, encoding } = normalizeHTML ( input , mimeType ) ;
3838
39- options = transformOptions ( options , encoding , mimeType ) ;
39+ const normalized = normalizeOptions ( options , encoding , mimeType ) ;
4040
41- this [ window ] = createWindow ( options . windowOptions ) ;
41+ this [ window ] = createWindow ( normalized . windowOptions ) ;
4242
4343 const documentImpl = this [ window ] . _document ;
4444
45- options . beforeParse ( this [ window ] . _globalProxy ) ;
45+ normalized . beforeParse ( this [ window ] . _globalProxy ) ;
4646
4747 parseIntoDocument ( html , documentImpl ) ;
4848
@@ -56,12 +56,11 @@ class JSDOM {
5656 }
5757
5858 get virtualConsole ( ) {
59- return this [ window ] . _virtualConsole ;
59+ return this [ window ] . _settings . virtualConsole ;
6060 }
6161
6262 get cookieJar ( ) {
63- // TODO NEWAPI move _cookieJar to window probably
64- return this [ window ] . _document . _cookieJar ;
63+ return this [ window ] . _settings . cookieJar ;
6564 }
6665
6766 serialize ( ) {
@@ -122,14 +121,14 @@ class JSDOM {
122121 // For the initial fetch, we default to "usable" instead of no resource loading, since fromURL() implicitly requests
123122 // fetching the initial resource. This does not impact further resource fetching, which uses options.resources.
124123 const resourcesForInitialFetch = options . resources !== undefined ? options . resources : "usable" ;
125- const { effectiveDispatcher } = extractResourcesOptions ( resourcesForInitialFetch , options . cookieJar ) ;
124+ const { dispatcher } = createResourceSettings ( resourcesForInitialFetch , options . cookieJar ) ;
126125
127126 const headers = { Accept : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" } ;
128127 if ( options . referrer ) {
129128 headers . Referer = options . referrer ;
130129 }
131130
132- const response = await fetchCollected ( effectiveDispatcher , {
131+ const response = await fetchCollected ( dispatcher , {
133132 url,
134133 headers
135134 } ) ;
@@ -138,14 +137,11 @@ class JSDOM {
138137 throw new Error ( `Resource was not loaded. Status: ${ response . status } ` ) ;
139138 }
140139
141- options = Object . assign ( options , {
140+ return new JSDOM ( response . body , {
141+ ...options ,
142142 url : response . url ,
143- contentType : response . headers [ "content-type" ] || undefined ,
144- referrer : options . referrer ,
145- resources : options . resources
143+ contentType : response . headers [ "content-type" ] || undefined
146144 } ) ;
147-
148- return new JSDOM ( response . body , options ) ;
149145 }
150146
151147 static async fromFile ( filename , options = { } ) {
@@ -165,8 +161,7 @@ function normalizeFromURLOptions(options) {
165161 throw new TypeError ( "Cannot supply a contentType option when using fromURL" ) ;
166162 }
167163
168- // Normalization of options which must be done before the rest of the fromURL code can use them, because they are
169- // given to request()
164+ // Normalize fields needed for the initial request. The constructor handles the remaining options after fetching.
170165 const normalized = { ...options } ;
171166
172167 if ( options . referrer !== undefined ) {
@@ -178,54 +173,6 @@ function normalizeFromURLOptions(options) {
178173 }
179174
180175 return normalized ;
181-
182- // All other options don't need to be processed yet, and can be taken care of in the normal course of things when
183- // `fromURL` calls `new JSDOM(html, options)`.
184- }
185-
186- function extractResourcesOptions ( resources , cookieJar ) {
187- // loadSubresources controls whether PerDocumentResourceLoader fetches scripts, stylesheets, etc.
188- // XHR always works regardless of this flag.
189- let userAgent , baseDispatcher , userInterceptors , loadSubresources ;
190-
191- if ( resources === undefined ) {
192- // resources: undefined means no automatic subresource fetching, but XHR still works
193- userAgent = DEFAULT_USER_AGENT ;
194- baseDispatcher = getGlobalDispatcher ( ) ;
195- userInterceptors = [ ] ;
196- loadSubresources = false ;
197- } else if ( resources === "usable" ) {
198- // resources: "usable" means use all defaults
199- userAgent = DEFAULT_USER_AGENT ;
200- baseDispatcher = getGlobalDispatcher ( ) ;
201- userInterceptors = [ ] ;
202- loadSubresources = true ;
203- } else if ( typeof resources === "object" && resources !== null ) {
204- // resources: { userAgent?, dispatcher?, interceptors? }
205- userAgent = resources . userAgent !== undefined ? resources . userAgent : DEFAULT_USER_AGENT ;
206- baseDispatcher = resources . dispatcher !== undefined ? resources . dispatcher : getGlobalDispatcher ( ) ;
207- userInterceptors = resources . interceptors !== undefined ? resources . interceptors : [ ] ;
208- loadSubresources = true ;
209- } else {
210- throw new TypeError ( `resources must be undefined, "usable", or an object` ) ;
211- }
212-
213- // User interceptors come first (outermost), then decompress interceptor
214- const allUserInterceptors = [
215- ...userInterceptors ,
216- createDecompressInterceptor ( )
217- ] ;
218-
219- return {
220- userAgent,
221- effectiveDispatcher : new JSDOMDispatcher ( {
222- baseDispatcher,
223- cookieJar,
224- userAgent,
225- userInterceptors : allUserInterceptors
226- } ) ,
227- loadSubresources
228- } ;
229176}
230177
231178function normalizeFromFileOptions ( filename , options ) {
@@ -245,97 +192,85 @@ function normalizeFromFileOptions(filename, options) {
245192 return normalized ;
246193}
247194
248- function transformOptions ( options , encoding , mimeType ) {
249- const transformed = {
250- windowOptions : {
251- // Defaults
252- url : "about:blank" ,
253- referrer : "" ,
254- contentType : "text/html" ,
255- parsingMode : "html" ,
256- parseOptions : {
257- sourceCodeLocationInfo : false
258- } ,
259- runScripts : undefined ,
260- encoding,
261- pretendToBeVisual : false ,
262- storageQuota : 5000000 ,
263-
264- // Defaults filled in later
265- dispatcher : undefined ,
266- loadSubresources : undefined ,
267- userAgent : undefined ,
268- virtualConsole : undefined ,
269- cookieJar : undefined
270- } ,
271-
272- // Defaults
273- beforeParse ( ) { }
274- } ;
275-
276- // options.contentType was parsed into mimeType by the caller.
195+ function normalizeOptions ( options , encoding , mimeType ) {
196+ // `options.contentType` was parsed into `mimeType` by the caller.
277197 if ( ! mimeType . isHTML ( ) && ! mimeType . isXML ( ) ) {
278198 throw new RangeError ( `The given content type of "${ options . contentType } " was not a HTML or XML content type` ) ;
279199 }
280200
281- transformed . windowOptions . contentType = mimeType . essence ;
282- transformed . windowOptions . parsingMode = mimeType . isHTML ( ) ? "html" : "xml" ;
283-
284- if ( options . url !== undefined ) {
285- transformed . windowOptions . url = ( new URL ( options . url ) ) . href ;
286- }
287-
288- if ( options . referrer !== undefined ) {
289- transformed . windowOptions . referrer = ( new URL ( options . referrer ) ) . href ;
201+ const parsingMode = mimeType . isHTML ( ) ? "html" : "xml" ;
202+ const url = options . url === undefined ? "about:blank" : ( new URL ( options . url ) ) . href ;
203+ const referrer = options . referrer === undefined ? "" : ( new URL ( options . referrer ) ) . href ;
204+ const sourceCodeLocationInfo = Boolean ( options . includeNodeLocations ) ;
205+ if ( sourceCodeLocationInfo && parsingMode === "xml" ) {
206+ throw new TypeError ( "Cannot set includeNodeLocations to true with an XML content type" ) ;
290207 }
291208
292- if ( options . includeNodeLocations ) {
293- if ( transformed . windowOptions . parsingMode === "xml" ) {
294- throw new TypeError ( "Cannot set includeNodeLocations to true with an XML content type" ) ;
295- }
296-
297- transformed . windowOptions . parseOptions . sourceCodeLocationInfo = true ;
298- }
299-
300- transformed . windowOptions . cookieJar = options . cookieJar === undefined ?
301- new CookieJar ( ) :
302- options . cookieJar ;
303-
304- transformed . windowOptions . virtualConsole = options . virtualConsole === undefined ?
305- ( new VirtualConsole ( ) ) . forwardTo ( console ) :
306- options . virtualConsole ;
307-
308- if ( ! ( transformed . windowOptions . virtualConsole instanceof VirtualConsole ) ) {
209+ const cookieJar = options . cookieJar === undefined ? new CookieJar ( ) : options . cookieJar ;
210+ const virtualConsole = options . virtualConsole === undefined ?
211+ ( new VirtualConsole ( ) ) . forwardTo ( console ) :
212+ options . virtualConsole ;
213+ if ( ! ( virtualConsole instanceof VirtualConsole ) ) {
309214 throw new TypeError ( "virtualConsole must be an instance of VirtualConsole" ) ;
310215 }
311216
312- const { userAgent, effectiveDispatcher, loadSubresources } =
313- extractResourcesOptions ( options . resources , transformed . windowOptions . cookieJar ) ;
314- transformed . windowOptions . userAgent = userAgent ;
315- transformed . windowOptions . dispatcher = effectiveDispatcher ;
316- transformed . windowOptions . loadSubresources = loadSubresources ;
317-
318- if ( options . runScripts !== undefined ) {
319- transformed . windowOptions . runScripts = String ( options . runScripts ) ;
320- if ( transformed . windowOptions . runScripts !== "dangerously" &&
321- transformed . windowOptions . runScripts !== "outside-only" ) {
322- throw new RangeError ( `runScripts must be undefined, "dangerously", or "outside-only"` ) ;
323- }
217+ const resourceSettings = createResourceSettings ( options . resources , cookieJar ) ;
218+ const runScripts = options . runScripts === undefined ? undefined : String ( options . runScripts ) ;
219+ if ( runScripts !== undefined && runScripts !== "dangerously" && runScripts !== "outside-only" ) {
220+ throw new RangeError ( `runScripts must be undefined, "dangerously", or "outside-only"` ) ;
324221 }
325222
326- if ( options . beforeParse !== undefined ) {
327- transformed . beforeParse = options . beforeParse ;
328- }
223+ const { beforeParse = ( ) => { } } = options ;
224+ const pretendToBeVisual = Boolean ( options . pretendToBeVisual ) ;
225+ const storageQuota = options . storageQuota === undefined ? 5000000 : Number ( options . storageQuota ) ;
329226
330- if ( options . pretendToBeVisual !== undefined ) {
331- transformed . windowOptions . pretendToBeVisual = Boolean ( options . pretendToBeVisual ) ;
332- }
227+ return {
228+ windowOptions : {
229+ settings : {
230+ runScripts,
231+ pretendToBeVisual,
232+ storageQuota,
233+ cookieJar,
234+ virtualConsole,
235+ ...resourceSettings
236+ } ,
237+ documentOptions : {
238+ url,
239+ referrer,
240+ contentType : mimeType . essence ,
241+ parsingMode,
242+ encoding,
243+ parseOptions : { sourceCodeLocationInfo }
244+ }
245+ } ,
246+ beforeParse
247+ } ;
248+ }
333249
334- if ( options . storageQuota !== undefined ) {
335- transformed . windowOptions . storageQuota = Number ( options . storageQuota ) ;
250+ function createResourceSettings ( resources , cookieJar ) {
251+ const resourceOptions = resources === undefined || resources === "usable" ? { } : resources ;
252+ if ( typeof resourceOptions !== "object" || resourceOptions === null ) {
253+ throw new TypeError ( `resources must be undefined, "usable", or an object` ) ;
336254 }
337255
338- return transformed ;
256+ const {
257+ userAgent = DEFAULT_USER_AGENT ,
258+ dispatcher = getGlobalDispatcher ( ) ,
259+ interceptors = [ ]
260+ } = resourceOptions ;
261+
262+ return {
263+ userAgent,
264+ dispatcher : new JSDOMDispatcher ( {
265+ baseDispatcher : dispatcher ,
266+ cookieJar,
267+ userAgent,
268+ // User interceptors come first (outermost), then the decompression interceptor.
269+ userInterceptors : [ ...interceptors , createDecompressInterceptor ( ) ]
270+ } ) ,
271+ // This controls automatic subresource fetching; XHR works regardless.
272+ loadSubresources : resources !== undefined
273+ } ;
339274}
340275
341276function normalizeHTML ( html , mimeType ) {
0 commit comments