Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions cjs/interface/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ const {Range} = require('./range.js');
const {Text} = require('./text.js');
const {TreeWalker} = require('./tree-walker.js');

const boundGlobals = new WeakMap;
const boundGlobal = fn => {
if (!boundGlobals.has(fn))
boundGlobals.set(fn, fn.bind(globalThis));
return boundGlobals.get(fn);
};

const query = (method, ownerDocument, selectors) => {
let {[NEXT]: next, [END]: end} = ownerDocument;
return method.call({ownerDocument, [NEXT]: next, [END]: end}, selectors);
Expand Down Expand Up @@ -130,9 +137,14 @@ class Document extends NonElementParentNode {
this[MUTATION_OBSERVER] = new MutationObserverClass(this);
return this[MUTATION_OBSERVER].class;
}
return (this[GLOBALS] && this[GLOBALS][name]) ||
globalExports[name] ||
globalThis[name];
const value = (this[GLOBALS] && this[GLOBALS][name]) ||
globalExports[name] ||
globalThis[name];
// a native function called through the proxy runs with the proxy
// as receiver, which browsers reject ("Illegal invocation"):
// bind it to the global, once, and leave constructors as they are
return typeof value === 'function' && !('prototype' in value) && value === globalThis[name] ?
boundGlobal(value) : value;
}
}));
return window.get(this);
Expand Down
18 changes: 15 additions & 3 deletions esm/interface/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ import {Range} from './range.js';
import {Text} from './text.js';
import {TreeWalker} from './tree-walker.js';

const boundGlobals = new WeakMap;
const boundGlobal = fn => {
if (!boundGlobals.has(fn))
boundGlobals.set(fn, fn.bind(globalThis));
return boundGlobals.get(fn);
};

const query = (method, ownerDocument, selectors) => {
let {[NEXT]: next, [END]: end} = ownerDocument;
return method.call({ownerDocument, [NEXT]: next, [END]: end}, selectors);
Expand Down Expand Up @@ -130,9 +137,14 @@ export class Document extends NonElementParentNode {
this[MUTATION_OBSERVER] = new MutationObserverClass(this);
return this[MUTATION_OBSERVER].class;
}
return (this[GLOBALS] && this[GLOBALS][name]) ||
globalExports[name] ||
globalThis[name];
const value = (this[GLOBALS] && this[GLOBALS][name]) ||
globalExports[name] ||
globalThis[name];
// a native function called through the proxy runs with the proxy
// as receiver, which browsers reject ("Illegal invocation"):
// bind it to the global, once, and leave constructors as they are
return typeof value === 'function' && !('prototype' in value) && value === globalThis[name] ?
boundGlobal(value) : value;
}
}));
return window.get(this);
Expand Down
7 changes: 7 additions & 0 deletions test/html/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ assert(window.addEventListener, null);

assert(typeof window.performance.now(), 'number');

// https://github.com/WebReflection/linkedom/issues/345
globalThis.receiver = {receiver() { return this; }}.receiver;
assert(window.receiver() === globalThis, true, 'a native function runs with the global as receiver');
assert(window.receiver === window.receiver, true, 'bound once');
assert(window.URL === URL, true, 'constructors keep their identity');
delete globalThis.receiver;

assert(parseHTML('<html><body><div>asdf</div></body></html>').document.body.outerHTML, '<body><div>asdf</div></body>');

const location = {href: 'http://ok'};
Expand Down