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

Side by Side Diff: lib/url.js

Issue 30013574: Issue 7296 - Implement lightweight URL parsing (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add toString() Created Feb. 21, 2019, 12:11 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/url.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 22 matching lines...) Expand all
33 { 33 {
34 let map = new Map(); 34 let map = new Map();
35 35
36 for (let key in publicSuffixes) 36 for (let key in publicSuffixes)
37 map.set(key, publicSuffixes[key]); 37 map.set(key, publicSuffixes[key]);
38 38
39 return map; 39 return map;
40 } 40 }
41 41
42 /** 42 /**
43 * A <code>URLInfo</code> object represents information about a URL. It is
44 * returned by <code>{@link parseURL}</code>.
45 */
46 class URLInfo
47 {
48 /**
49 * @private
50 */
51 constructor(href, protocol = "", hostname = "")
52 {
53 this._href = href;
54 this._protocol = protocol;
55 this._hostname = hostname;
56 }
57
58 /**
59 * The entire URL.
60 * @type {string}
61 */
62 get href()
63 {
64 return this._href;
65 }
66
67 /**
68 * The protocol scheme of the URL, including the final <code>:</code>.
69 * @type {string}
70 */
71 get protocol()
72 {
73 return this._protocol;
74 }
75
76 /**
77 * The hostname of the URL.
78 * @type {string}
79 */
80 get hostname()
81 {
82 return this._hostname;
83 }
84
85 /**
86 * Returns the entire URL.
87 * @returns {string} The entire URL.
88 */
89 toString()
90 {
91 return this._href;
92 }
93 }
94
95 /**
96 * Parses a URL to extract the protocol and the hostname. This is a lightweight
97 * alternative to the native <code>URL</code> object. Unlike the
98 * <code>URL</code> object, this function is not robust and will give incorrect
99 * results for invalid URLs. <em>Use this function with valid, normalized,
100 * properly encoded (IDNA and percent-encoding) URLs only.</em>
101 *
102 * @param {string} url The URL to parse.
103 * @returns {URLInfo} Information about the URL.
104 */
105 function parseURL(url)
106 {
107 let match = /^([^:]+:)(?:\/\/(?:[^/]*@)?(\[[^\]]*\]|[^:/]+))?/.exec(url);
108 return match ? new URLInfo(url, match[1], match[2]) : new URLInfo(url);
109 }
110
111 exports.parseURL = parseURL;
112
113 /**
43 * Normalizes a hostname. 114 * Normalizes a hostname.
44 * @param {string} hostname 115 * @param {string} hostname
45 * @returns {string} 116 * @returns {string}
46 */ 117 */
47 function normalizeHostname(hostname) 118 function normalizeHostname(hostname)
48 { 119 {
49 return (hostname[hostname.length - 1] == "." ? 120 return (hostname[hostname.length - 1] == "." ?
50 hostname.replace(/\.+$/, "") : hostname).toLowerCase(); 121 hostname.replace(/\.+$/, "") : hostname).toLowerCase();
51 } 122 }
52 123
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 if (requestHostname == documentHostname) 224 if (requestHostname == documentHostname)
154 return false; 225 return false;
155 226
156 if (!isDomain(requestHostname) || !isDomain(documentHostname)) 227 if (!isDomain(requestHostname) || !isDomain(documentHostname))
157 return true; 228 return true;
158 229
159 return getBaseDomain(requestHostname) != getBaseDomain(documentHostname); 230 return getBaseDomain(requestHostname) != getBaseDomain(documentHostname);
160 } 231 }
161 232
162 exports.isThirdParty = isThirdParty; 233 exports.isThirdParty = isThirdParty;
OLDNEW
« 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