chromium/third_party/blink/web_tests/fast/dom/HTMLAnchorElement/set-href-attribute-pathname.html

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>
description('Test setting the pathname attribute of the URL in HTMLAnchorElement.');

var a = document.createElement('a');

debug("Set pathname that starts with slash");
a.href = "https://www.mydomain.com/path/testurl.html?key=value";
a.pathname = "/path name";
shouldBe("a.href", "'https://www.mydomain.com/path%20name?key=value'");

// IE8 throws an "Invalid URL" exception.
try {
debug("Set pathname that does not start with slash and contains '?'");
a.href = "https://www.mydomain.com/path/testurl.html?key=value";
a.pathname = "pa?th";
shouldBe("a.href", "'https://www.mydomain.com/pa%3Fth?key=value'");
} catch(e) {
debug("Exception: " + e.description);
}

// IE8 throws an "Invalid URL" exception.
try {
debug("Set pathname that starts with double slash and contains '#'");
a.href = "https://www.mydomain.com/path?key=value";
a.pathname = "//path#name";
shouldBe("a.href", "'https://www.mydomain.com//path%23name?key=value'");
} catch(e) {
debug("Exception: " + e.description);
}

debug("Set a pathname containing .. in it");
a.href = "https://www.mydomain.com/path/testurl.html?key=value";
a.pathname = "/it/../path";
shouldBe("a.href", "'https://www.mydomain.com/path?key=value'");

debug("Set pathname to null");
a.href = "https://www.mydomain.com/path/testurl.html?key=value";
a.pathname = null;
shouldBe("a.href", "'https://www.mydomain.com/null?key=value'");

debug("Set pathname to empty string");
a.href = "https://www.mydomain.com/?key=value";
a.pathname = "";
shouldBe("a.href", "'https://www.mydomain.com/?key=value'");

// IE8 considers this URL as valid, but we consider it invalid.
debug("Set pathname that includes illegal characters to URL that contains illegal characters.");
a.href = "https://www.my|d[]()omain.com/path/testurl.html?key=value";
a.pathname = "p$a|th"; // should be no-op since original URL is invalid
shouldBe("a.href", "'https://www.my|d[]()omain.com/path/testurl.html?key=value'");

debug("Set pathname to URL that contains '@' in host");
a.href = "http://w@#ww";
a.pathname = "path"; // should be no-op since original URL is invalid
shouldBe("a.href", "'http://w@#ww'");

// IE8 allows setting the pathname, for non-hierarchial URL.
// It is not supposed to allow that per
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname
// In particular this URL's "cannot-be-a-base-URL" is true.
debug("Set pathname to a URL with non-hierarchical protocol");
a.href = "tel:+1800-555-1212";
a.pathname = "the-path";
shouldBe("a.href", "'tel:+1800-555-1212'");
</script>
</body>
</html>