Skip to content

Commit f6e77a4

Browse files
committed
【fix】优化判断同域的方法
1 parent 3ddcf8b commit f6e77a4

File tree

4 files changed

+45
-63
lines changed

4 files changed

+45
-63
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: 30 additions & 60 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 可以是空,或者以下浏览器:<br>
@@ -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,51 +668,20 @@ 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
/**
713683
* @memberOf CommonUtil
714-
* @description 计算 SuperMap iServer 服务的 REST 图层的显示分辨率,需要从 SuperMap iServer 的 REST 图层表述中获取 viewBounds、viewer、scale、coordUnit、datumAxis 五个参数,来进行计算。
684+
* @description 计算 iServer 服务的 REST 图层的显示分辨率,需要从 iServer 的 REST 图层表述中获取 viewBounds、viewer、scale、coordUnit、datumAxis 五个参数,来进行计算。
715685
* @param {Bounds} viewBounds - 地图的参照可视范围,即地图初始化时默认的地图显示范围。
716686
* @param {Size} viewer - 地图初始化时默认的地图图片的尺寸。
717687
* @param {number} scale - 地图初始化时默认的显示比例尺。
@@ -720,7 +690,7 @@ const Util = {
720690
* @returns {number} 图层显示分辨率。
721691
*/
722692
calculateDpi: function (viewBounds, viewer, scale, coordUnit, datumAxis) {
723-
//10000 是 0.1 毫米与米的转换。DPI 的计算公式:Viewer / DPI * 0.0254 * 10000 = ViewBounds * scale ,公式中的10000是为了提高计算结果的精度,以下出现的ratio皆为如此。
693+
//10000 是 0.1毫米与米的转换。DPI的计算公式:Viewer / DPI * 0.0254 * 10000 = ViewBounds * scale ,公式中的10000是为了提高计算结果的精度,以下出现的ratio皆为如此。
724694
if (!viewBounds || !viewer || !scale) {
725695
return;
726696
}
@@ -1098,10 +1068,10 @@ const Util = {
10981068
},
10991069
/**
11001070
* @memberOf CommonUtil
1101-
* @description 获取转换后的 path 路径
1102-
* @param {string} path - 待转换的 path,包含`{param}`。
1103-
* @param {Object} pathParams - path 中待替换的参数
1104-
* @returns {string} 转换后的 path 路径
1071+
* @description 获取转换后的path路径
1072+
* @param {string} path - 待转换的path,包含`{param}`。
1073+
* @param {Object} pathParams - path中待替换的参数
1074+
* @returns {string} 转换后的path路径
11051075
*/
11061076
convertPath: function (path, pathParams) {
11071077
if (!pathParams) {

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy