Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/url.js

Issue 30013574: Issue 7296 - Implement lightweight URL parsing (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add tests for IPv6 Created Feb. 21, 2019, 12:24 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/url.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/url.js
===================================================================
--- a/lib/url.js
+++ b/lib/url.js
@@ -35,16 +35,87 @@
for (let key in publicSuffixes)
map.set(key, publicSuffixes[key]);
return map;
}
/**
+ * A <code>URLInfo</code> object represents information about a URL. It is
+ * returned by <code>{@link parseURL}</code>.
+ */
+class URLInfo
+{
+ /**
+ * @private
+ */
+ constructor(href, protocol = "", hostname = "")
+ {
+ this._href = href;
+ this._protocol = protocol;
+ this._hostname = hostname;
+ }
+
+ /**
+ * The entire URL.
+ * @type {string}
+ */
+ get href()
+ {
+ return this._href;
+ }
+
+ /**
+ * The protocol scheme of the URL, including the final <code>:</code>.
+ * @type {string}
+ */
+ get protocol()
+ {
+ return this._protocol;
+ }
+
+ /**
+ * The hostname of the URL.
+ * @type {string}
+ */
+ get hostname()
+ {
+ return this._hostname;
+ }
+
+ /**
+ * Returns the entire URL.
+ * @returns {string} The entire URL.
+ */
+ toString()
+ {
+ return this._href;
+ }
+}
+
+/**
+ * Parses a URL to extract the protocol and the hostname. This is a lightweight
+ * alternative to the native <code>URL</code> object. Unlike the
+ * <code>URL</code> object, this function is not robust and will give incorrect
+ * results for invalid URLs. <em>Use this function with valid, normalized,
+ * properly encoded (IDNA and percent-encoding) URLs only.</em>
+ *
+ * @param {string} url The URL to parse.
+ * @returns {URLInfo} Information about the URL.
+ */
+function parseURL(url)
+{
+ let match = /^([^:]+:)(?:\/\/(?:[^/]*@)?(\[[^\]]*\]|[^:/]+))?/.exec(url);
+ return match ? new URLInfo(url, match[1], match[2]) : new URLInfo(url);
+}
+
+exports.parseURL = parseURL;
+
+/**
* Normalizes a hostname.
* @param {string} hostname
* @returns {string}
*/
function normalizeHostname(hostname)
{
return (hostname[hostname.length - 1] == "." ?
hostname.replace(/\.+$/, "") : hostname).toLowerCase();
« no previous file with comments | « no previous file | test/url.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld