Skip to content

Commit

Permalink
Fix spec bug (kinda) in reinitializing a hyperlink's internal URL
Browse files Browse the repository at this point in the history
See whatwg/html#568; we do option 2 here since it avoids the performance hit of monitoring the attribute.
  • Loading branch information
domenic committed Jan 27, 2016
1 parent e268543 commit 0038dee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
43 changes: 17 additions & 26 deletions lib/jsdom/living/nodes/HTMLHyperlinkElementUtils-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get href() {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null) {
Expand All @@ -28,7 +28,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get origin() {
reinitializeURL(this);
setTheURL(this);

if (this.url === null) {
return "";
Expand All @@ -38,7 +38,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get protocol() {
reinitializeURL(this);
setTheURL(this);

if (this.url === null) {
return ":";
Expand All @@ -57,7 +57,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get username() {
reinitializeURL(this);
setTheURL(this);

if (this.url === null) {
return "";
Expand All @@ -78,7 +78,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get password() {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.password === null) {
Expand All @@ -100,7 +100,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get host() {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.host === null) {
Expand All @@ -115,7 +115,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

set host(v) {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.nonRelative) {
Expand All @@ -127,7 +127,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get hostname() {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.host === null) {
Expand All @@ -138,7 +138,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

set hostname(v) {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.nonRelative) {
Expand All @@ -150,7 +150,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get port() {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.port === null) {
Expand All @@ -161,7 +161,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

set port(v) {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.host === null || url.nonRelative || url.scheme === "file") {
Expand All @@ -173,7 +173,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get pathname() {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null) {
Expand All @@ -188,7 +188,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

set pathname(v) {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.nonRelative) {
Expand All @@ -200,7 +200,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get search() {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.query === null || url.query === "") {
Expand All @@ -211,7 +211,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

set search(v) {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null) {
Expand All @@ -229,7 +229,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

get hash() {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.fragment === null || url.fragment === "") {
Expand All @@ -240,7 +240,7 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}

set hash(v) {
reinitializeURL(this);
setTheURL(this);
const url = this.url;

if (url === null || url.scheme === "javascript") {
Expand All @@ -258,15 +258,6 @@ exports.implementation = class HTMLHyperlinkElementUtilsImpl {
}
};

function reinitializeURL(hheu) {
const url = hheu.url;
if (url !== null && url.nonRelative) {
return;
}

setTheURL(hheu);
}

function setTheURL(hheu) {
const href = hheu.getAttribute("href");
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<title>HTMLHyperlinkElementUtils href setter</title>
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<link rel="help" href="https://html.spec.whatwg.org/#dom-hyperlink-href">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
"use strict";

test(() => {

const a = document.createElement("a");
a.setAttribute("href", "mycustomprotocol:abc");

assert_equals(a.protocol, "mycustomprotocol:");

a.href = "otherprot:test.js";
assert_equals(a.protocol, "otherprot:");

}, "setting href IDL attribute also resets protocol on URLs with non-relative flag set");

test(() => {

const a = document.createElement("a");
a.setAttribute("href", "mycustomprotocol:abc");

assert_equals(a.protocol, "mycustomprotocol:");

a.setAttribute("href", "otherprot:test.js");
assert_equals(a.protocol, "otherprot:");

}, "setting href content attribute also resets protocol on URLs with non-relative flag set");

</script>

0 comments on commit 0038dee

Please sign in to comment.