Skip to content

Commit fc8b637

Browse files
committed
【fix】优化判断同域的方法
1 parent ddc8f2b commit fc8b637

File tree

4 files changed

+39
-57
lines changed

4 files changed

+39
-57
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
"promise-polyfill": "8.2.3",
161161
"rbush": "^2.0.2",
162162
"three": "0.150.1",
163+
"urijs": "^1.19.11",
163164
"util": "^0.12.4",
164165
"xlsx": "https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz"
165166
},

src/common/commontypes/Util.js

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
44
import { StringExt } from './BaseTypes';
55
import { Geometry } from './Geometry';
6+
import URI from 'urijs';
67

78
/**
89
* @description 浏览器名称,依赖于 userAgent 属性,BROWSER_NAME 可以是空,或者以下浏览器:
@@ -129,28 +130,28 @@ const DOTS_PER_INCH = 96;
129130
const Util = {
130131

131132
/**
132-
* @memberOf CommonUtil
133-
* @description 对象拷贝赋值。
134-
* @param {Object} dest - 目标对象。
135-
* @param {Object} arguments - 待拷贝的对象。
136-
* @returns {Object} 赋值后的目标对象。
137-
*/
133+
* @memberOf CommonUtil
134+
* @description 对象拷贝赋值。
135+
* @param {Object} dest - 目标对象。
136+
* @param {Object} arguments - 待拷贝的对象。
137+
* @returns {Object} 赋值后的目标对象。
138+
*/
138139
assign(dest) {
139140
for (var index = 0; index < Object.getOwnPropertyNames(arguments).length; index++) {
140-
var arg = Object.getOwnPropertyNames(arguments)[index];
141+
var arg = Object.getOwnPropertyNames(arguments)[index];
141142
if (arg == "caller" || arg == "callee" || arg == "length" || arg == "arguments") {
142-
continue;
143-
}
144-
var obj = arguments[arg];
145-
if (obj) {
146-
for (var j = 0; j < Object.getOwnPropertyNames(obj).length; j++) {
147-
var key = Object.getOwnPropertyNames(obj)[j];
143+
continue;
144+
}
145+
var obj = arguments[arg];
146+
if (obj) {
147+
for (var j = 0; j < Object.getOwnPropertyNames(obj).length; j++) {
148+
var key = Object.getOwnPropertyNames(obj)[j];
148149
if (arg == "caller" || arg == "callee" || arg == "length" || arg == "arguments") {
149-
continue;
150-
}
151-
dest[key] = obj[key];
152-
}
150+
continue;
151+
}
152+
dest[key] = obj[key];
153153
}
154+
}
154155
}
155156
return dest;
156157
},
@@ -667,46 +668,15 @@ const Util = {
667668
if (!url) {
668669
return true;
669670
}
670-
var index = url.indexOf('//');
671-
var documentUrl = document.location.toString();
672-
var documentIndex = documentUrl.indexOf('//');
671+
const index = url.indexOf('//');
673672
if (index === -1) {
674673
return true;
675-
} else {
676-
var protocol;
677-
var substring = (protocol = url.substring(0, index));
678-
var documentSubString = documentUrl.substring(documentIndex + 2);
679-
documentIndex = documentSubString.indexOf('/');
680-
var documentPortIndex = documentSubString.indexOf(':');
681-
var documentDomainWithPort = documentSubString.substring(0, documentIndex);
682-
//var documentPort;
683-
684-
var documentprotocol = document.location.protocol;
685-
if (documentPortIndex !== -1) {
686-
// documentPort = +documentSubString.substring(documentPortIndex, documentIndex);
687-
} else {
688-
documentDomainWithPort += ':' + (documentprotocol.toLowerCase() === 'http:' ? 80 : 443);
689-
}
690-
if (documentprotocol.toLowerCase() !== substring.toLowerCase()) {
691-
return false;
692-
}
693-
substring = url.substring(index + 2);
694-
var portIndex = substring.indexOf(':');
695-
index = substring.indexOf('/');
696-
var domainWithPort = substring.substring(0, index);
697-
var domain;
698-
if (portIndex !== -1) {
699-
domain = substring.substring(0, portIndex);
700-
} else {
701-
domain = substring.substring(0, index);
702-
domainWithPort += ':' + (protocol.toLowerCase() === 'http:' ? 80 : 443);
703-
}
704-
var documentDomain = document.domain;
705-
if (domain === documentDomain && domainWithPort === documentDomainWithPort) {
706-
return true;
707-
}
708674
}
709-
return false;
675+
return Util.isSameDomain(url, document.location.toString());
676+
},
677+
678+
isSameDomain(url, otherUrl) {
679+
return new URI(url).normalize().origin() === new URI(otherUrl).normalize().origin();
710680
},
711681

712682
/**

src/common/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"mapv": "2.0.62",
2626
"flatgeobuf": "3.23.1",
2727
"rbush": "^2.0.2",
28+
"urijs": "^1.19.11",
2829
"xlsx": "https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz"
2930
}
3031
}

test/common/commontypes/UtilSpec.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,21 @@ describe('Util', () => {
209209
it('isInTheSameDomain', () => {
210210
var Url = "https://iclient.supermap.io/examples/leaflet/editor.html#addressMatchService";
211211
var correct = Util.isInTheSameDomain(Url);
212-
expect(correct).toBeFalsy(false);
213-
212+
expect(correct).toBeFalsy();
214213
var errorUrl = "httttttp:wwwwwww.bbbb";
215214
var error = Util.isInTheSameDomain(errorUrl);
216-
expect(error).toBeTruthy(true);
215+
expect(error).toBeTruthy();
216+
217+
});
218+
it('isSameDomain', () => {
219+
expect(Util.isSameDomain('https://test.com/b',"httttttp:wwwwwww.bbbb")).toBeFalsy();
220+
expect(Util.isSameDomain('https://test.com/b','https://test.com')).toBeTruthy();
221+
expect(Util.isSameDomain('https://test.com:443/b','https://test.com')).toBeTruthy();
222+
expect(Util.isSameDomain('http://test.com/b','https://test.com')).toBeFalsy();
223+
expect(Util.isSameDomain('http://test.com/b','http://test.com')).toBeTruthy();
224+
expect(Util.isSameDomain('http://test.com:80/b','http://test.com')).toBeTruthy();
225+
expect(Util.isSameDomain('https://test.com/b?epsg:4326','https://test.com')).toBeTruthy();
226+
217227
});
218228

219229
it("calculateDpi", () => {

0 commit comments

Comments
 (0)