@@ -137,8 +137,9 @@ export function setCookieFn(
137
137
if ( options . domain ) {
138
138
let domain = options . domain ;
139
139
if ( / ^ \/ .+ \/ / . test ( domain ) ) {
140
+ const baseURL = new URL ( document . baseURI ) ;
140
141
const reDomain = new RegExp ( domain . slice ( 1 , - 1 ) ) ;
141
- const match = reDomain . exec ( document . location . hostname ) ;
142
+ const match = reDomain . exec ( baseURL . hostname ) ;
142
143
domain = match ? match [ 0 ] : undefined ;
143
144
}
144
145
if ( domain ) {
@@ -369,23 +370,33 @@ export function removeCookie(
369
370
fn ( ) ;
370
371
} , ms ) ;
371
372
} ;
373
+ const baseURL = new URL ( document . baseURI ) ;
374
+ let targetDomain = extraArgs . domain ;
375
+ if ( targetDomain && / ^ \/ .+ \/ / . test ( targetDomain ) ) {
376
+ const reDomain = new RegExp ( targetDomain . slice ( 1 , - 1 ) ) ;
377
+ const match = reDomain . exec ( baseURL . hostname ) ;
378
+ targetDomain = match ? match [ 0 ] : undefined ;
379
+ }
372
380
const remove = ( ) => {
373
381
safe . String_split . call ( document . cookie , ';' ) . forEach ( cookieStr => {
374
382
const pos = cookieStr . indexOf ( '=' ) ;
375
383
if ( pos === - 1 ) { return ; }
376
384
const cookieName = cookieStr . slice ( 0 , pos ) . trim ( ) ;
377
385
if ( reName . test ( cookieName ) === false ) { return ; }
378
386
const part1 = cookieName + '=' ;
379
- const part2a = ' ; domain=' + document . location . hostname ;
380
- const part2b = ' ; domain=.' + document . location . hostname ;
387
+ const part2a = ` ; domain=${ baseURL . hostname } ` ;
388
+ const part2b = ` ; domain=.${ baseURL . hostname } ` ;
381
389
let part2c , part2d ;
382
- const domain = document . domain ;
383
- if ( domain ) {
384
- if ( domain !== document . location . hostname ) {
385
- part2c = '; domain=.' + domain ;
390
+ if ( targetDomain ) {
391
+ part2c = `; domain=${ targetDomain } ` ;
392
+ part2d = `; domain=.${ targetDomain } ` ;
393
+ } else if ( document . domain ) {
394
+ const domain = document . domain ;
395
+ if ( domain !== baseURL . hostname ) {
396
+ part2c = `; domain=.${ domain } ` ;
386
397
}
387
398
if ( domain . startsWith ( 'www.' ) ) {
388
- part2d = ' ; domain=' + domain . replace ( 'www' , '' ) ;
399
+ part2d = ` ; domain=${ domain . replace ( 'www' , '' ) } ` ;
389
400
}
390
401
}
391
402
const part3 = '; path=/' ;
0 commit comments