Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTMLLinkElement load emit timing #1227

Merged
merged 3 commits into from
Jun 25, 2019
Merged
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
87 changes: 48 additions & 39 deletions src/DOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ class Node extends EventTarget {
this.ownerDocument.removeEventListener('readystatechange', _readystatechange);

process.nextTick(() => {
this.dispatchEvent.apply(this, args);
const el = args[0].target;
el.dispatchEvent.apply(el, args);
});
}
};
Expand Down Expand Up @@ -1630,38 +1631,50 @@ class HTMLLinkElement extends HTMLLoadableElement {
this.stylesheet = null;

this.on('attribute', (name, value) => {
if (name === 'href' && this.isRunnable()) {
this.readyState = 'loading';

const url = value;
this.ownerDocument.defaultView.fetch(url)
.then(res => {
if (res.status >= 200 && res.status < 300) {
return res.text();
} else {
return Promise.reject(new Error('link href got invalid status code: ' + res.status + ' : ' + url));
}
})
.then(s => css.parse(s).stylesheet)
.then(stylesheet => {
this.stylesheet = stylesheet;
this.ownerDocument.defaultView[symbols.styleEpochSymbol]++;

this.readyState = 'complete';

this.dispatchEvent(new Event('load', {target: this}));
})
.catch(err => {
this.readyState = 'complete';

const e = new ErrorEvent('error', {target: this});
e.message = err.message;
e.stack = err.stack;
this.dispatchEvent(e);
});
if (this.isRunnable() && !this.readyState) {
this.loadRunNow();
}
});
}

loadRunNow() {
this.readyState = 'loading';

const url = _mapUrl(this.href, this.ownerDocument.defaultView);

return this.ownerDocument.resources.addResource((onprogress, cb) => {
this.ownerDocument.defaultView.fetch(url)
.then(res => {
if (res.status >= 200 && res.status < 300) {
return res.text();
} else {
return Promise.reject(new Error('link href got invalid status code: ' + res.status + ' : ' + url));
}
})
.then(s => css.parse(s).stylesheet)
.then(stylesheet => {
this.stylesheet = stylesheet;
this.ownerDocument.defaultView[symbols.styleEpochSymbol]++;

this.readyState = 'complete';

const e = new Event('load', {target: this});
this._dispatchEventOnDocumentReady(e);

cb();
})
.catch(err => {
this.readyState = 'complete';

const e = new ErrorEvent('error', {target: this});
e.message = err.message;
e.stack = err.stack;
this._dispatchEventOnDocumentReady(e);

cb(err);
});
});
}

get rel() {
return this.getAttribute('rel') || '';
Expand All @@ -1688,19 +1701,16 @@ class HTMLLinkElement extends HTMLLoadableElement {
}

isRunnable() {
return this.rel === 'stylesheet';
return this.rel === 'stylesheet' && !!this.href;
}

[symbols.runSymbol]() {
let running = false;
if (this.isRunnable() && !this.readyState) {
const hrefAttr = this.attributes.href;
if (hrefAttr) {
this._emit('attribute', 'href', hrefAttr.value);
running = true;
if (this.attributes.href) {
return this.loadRunNow();
}
}
return running ? _loadPromise(this) : Promise.resolve();
return Promise.resolve();
}
}
module.exports.HTMLLinkElement = HTMLLinkElement;
Expand Down Expand Up @@ -1870,8 +1880,7 @@ class HTMLScriptElement extends HTMLLoadableElement {

[symbols.runSymbol]() {
if (this.isRunnable() && !this.readyState) {
const srcAttr = this.attributes.src;
if (srcAttr) {
if (this.attributes.src) {
return this.loadRunNow();
} else if (this.childNodes.length > 0) {
return this.runNow();
Expand Down