File tree Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -399,10 +399,23 @@ const store = createStore(NS, {
399
399
/** @type {Element|null } */
400
400
let contextElement = null ;
401
401
if ( store . state . contextHTMLForUse ) {
402
- const walker = doc . createTreeWalker ( doc , NodeFilter . SHOW_ELEMENT ) ;
403
- while ( walker . nextNode ( ) ) {
404
- // @ts -expect-error It's an Element!
405
- contextElement = walker . currentNode ;
402
+ // An HTML document will always make HTML > HEAD + BODY.
403
+ // But that may not be the intended context.
404
+ // Guess the intended context in case the HEAD and BODY elements are empty.
405
+ if ( doc . body . hasChildNodes ( ) || doc . head . hasChildNodes ( ) ) {
406
+ const walker = doc . createTreeWalker ( doc , NodeFilter . SHOW_ELEMENT ) ;
407
+ while ( walker . nextNode ( ) ) {
408
+ // @ts -expect-error It's an Element!
409
+ contextElement = walker . currentNode ;
410
+ }
411
+ } else {
412
+ if ( / < b o d y \W / i. test ( store . state . contextHTMLForUse ) ) {
413
+ contextElement = doc . body ;
414
+ } else if ( / < h e a d \W / i. test ( store . state . contextHTMLForUse ) ) {
415
+ contextElement = doc . head ;
416
+ } else {
417
+ contextElement = doc . documentElement ;
418
+ }
406
419
}
407
420
if ( contextElement ) {
408
421
store . state . DOM . contextNode = contextElement . nodeName ;
You can’t perform that action at this time.
0 commit comments