Skip to content

Commit 213fc46

Browse files
committed
Merge branch 'icon-path'
2 parents 843d67b + 5a12ce4 commit 213fc46

File tree

8 files changed

+34
-71
lines changed

8 files changed

+34
-71
lines changed

src/core/Canvas.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
isString,
66
isArrayHasData,
77
isSVG,
8-
isCssUrl,
98
extractCssUrl,
109
computeDegree
1110
} from './util';
@@ -66,15 +65,16 @@ const Canvas = {
6665
if (!isNil(strokeWidth) && ctx.lineWidth !== strokeWidth) {
6766
ctx.lineWidth = strokeWidth;
6867
}
69-
const strokeColor = style['linePatternFile'] || style['lineColor'] || DEFAULT_STROKE_COLOR;
68+
const strokePattern = style['linePatternFile'];
69+
const strokeColor = style['lineColor'] || DEFAULT_STROKE_COLOR;
7070
if (testing) {
7171
ctx.strokeStyle = '#000';
72-
} else if (isImageUrl(strokeColor) && resources) {
72+
} else if (strokePattern && resources) {
7373
let patternOffset;
7474
if (style['linePatternDx'] || style['linePatternDy']) {
7575
patternOffset = [style['linePatternDx'], style['linePatternDy']];
7676
}
77-
Canvas._setStrokePattern(ctx, strokeColor, strokeWidth, patternOffset, resources);
77+
Canvas._setStrokePattern(ctx, strokePattern, strokeWidth, patternOffset, resources);
7878
//line pattern will override stroke-dasharray
7979
style['lineDasharray'] = [];
8080
} else if (isGradient(strokeColor)) {
@@ -95,11 +95,12 @@ const Canvas = {
9595
if (ctx.setLineDash && isArrayHasData(style['lineDasharray'])) {
9696
ctx.setLineDash(style['lineDasharray']);
9797
}
98-
const fill = style['polygonPatternFile'] || style['polygonFill'] || DEFAULT_FILL_COLOR;
98+
const polygonPattern = style['polygonPatternFile'];
99+
const fill = style['polygonFill'] || DEFAULT_FILL_COLOR;
99100
if (testing) {
100101
ctx.fillStyle = '#000';
101-
} else if (isImageUrl(fill) && resources) {
102-
const fillImgUrl = extractImageUrl(fill);
102+
} else if (polygonPattern && resources) {
103+
const fillImgUrl = extractImageUrl(polygonPattern);
103104
let fillTexture = resources.getImage([fillImgUrl, null, null]);
104105
if (!fillTexture) {
105106
//if the linestring has a arrow and a linePatternFile, polygonPatternFile will be set with the linePatternFile.
@@ -938,9 +939,6 @@ function drawDashLine(ctx, startPoint, endPoint, dashArray) {
938939
}
939940

940941
const prefix = 'data:image/';
941-
function isImageUrl(url) {
942-
return url.length > prefix.length && url.substring(0, prefix.length) === prefix || isCssUrl(url);
943-
}
944942

945943
function extractImageUrl(url) {
946944
if (url.substring(0, prefix.length) === prefix) {

src/core/util/resource.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RESOURCE_PROPERTIES, RESOURCE_SIZE_PROPERTIES } from '../Constants';
22
import { IS_NODE } from './env';
33
import { extend, isNil, isNumber, isString } from './common';
4-
import { isURL, extractCssUrl, btoa } from './util';
4+
import { extractCssUrl, btoa } from './util';
55
import { isFunctionDefinition, getFunctionTypeResources } from '../mapbox';
66

77

@@ -202,46 +202,21 @@ export function convertResourceUrl(symbol) {
202202
if (!res) {
203203
continue;
204204
}
205-
s[props[ii]] = _convertUrlToAbsolute(res);
205+
s[props[ii]] = _convertUrl(res);
206206
}
207207
return s;
208208
}
209209

210-
function _convertUrlToAbsolute(res) {
210+
function _convertUrl(res) {
211211
if (isFunctionDefinition(res)) {
212212
const stops = res.stops;
213213
for (let i = 0; i < stops.length; i++) {
214-
stops[i][1] = _convertUrlToAbsolute(stops[i][1]);
214+
stops[i][1] = _convertUrl(stops[i][1]);
215215
}
216216
return res;
217217
}
218-
const embed = 'data:';
219218
if (res.slice(0, 4) === 'url(') {
220219
res = extractCssUrl(res);
221220
}
222-
if (!isURL(res) &&
223-
(res.length <= embed.length || res.substring(0, embed.length) !== embed)) {
224-
res = _absolute(location.href, res);
225-
}
226221
return res;
227222
}
228-
229-
function _absolute(base, relative) {
230-
const stack = base.split('/'),
231-
parts = relative.split('/');
232-
if (relative.slice(0, 1) === 0) {
233-
return stack.slice(0, 3).join('/') + relative;
234-
} else {
235-
stack.pop(); // remove current file name (or empty string)
236-
// (omit if "base" is the current folder without trailing slash)
237-
for (let i = 0; i < parts.length; i++) {
238-
if (parts[i] === '.')
239-
continue;
240-
if (parts[i] === '..')
241-
stack.pop();
242-
else
243-
stack.push(parts[i]);
244-
}
245-
return stack.join('/');
246-
}
247-
}

src/core/util/util.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,22 @@ export function isArrayHasData(obj) {
234234
return Array.isArray(obj) && obj.length > 0;
235235
}
236236

237+
238+
const urlPattern = /^([a-z][a-z\d+\-.]*:)?\/\//i;
237239
/**
238240
* Whether the input string is a valid url.
241+
* form: https://github.com/axios/axios/blob/master/lib/helpers/isAbsoluteURL.js
239242
* @param {String} url - url to check
240243
* @return {Boolean}
241244
* @memberOf Util
242245
* @private
243246
*/
244247
export function isURL(url) {
245-
if (!url) {
246-
return false;
247-
}
248-
const head = url.slice(0, 6);
249-
if (head === 'http:/' || head === 'https:' || head === 'file:/') {
250-
return true;
251-
}
252-
return false;
248+
// A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
249+
// RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
250+
// by any combination of letters, digits, plus, period, or hyphen.
251+
// eslint-disable-next-line no-useless-escape
252+
return urlPattern.test(url);
253253
}
254254

255255
//改原先的regex名字为xWithQuote;不带引号的regex,/^url\(([^\'\"].*[^\'\"])\)$/i,为xWithoutQuote。然后在is函数里||测试,extract函数里if...else处理。没引号的匹配后,取matches[1]
@@ -264,26 +264,21 @@ export function isCssUrl(str) {
264264
if (!isString(str)) {
265265
return 0;
266266
}
267-
const head = str.slice(0, 6);
268-
if (head === 'http:/' || head === 'https:') {
269-
return 3;
270-
}
271267
if (cssUrlRe.test(str)) {
272268
return 1;
273269
}
274270
if (cssUrlReWithQuote.test(str)) {
275271
return 2;
276272
}
277-
return 0;
273+
return 3;
278274
}
279275

280276
export function extractCssUrl(str) {
281277
const test = isCssUrl(str);
282278
let matches;
283279
if (test === 3) {
284280
return str;
285-
}
286-
if (test === 1) {
281+
} else if (test === 1) {
287282
matches = cssUrlRe.exec(str);
288283
return matches[1];
289284
} else if (test === 2) {

test/geometry/GeometryCollectionSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ describe('#GeometryCollection', function () {
260260
'markerHeight' : 50
261261
};
262262
var expected = {
263-
'markerFile' : 'http://localhost:9876/test',
263+
'markerFile' : 'test',
264264
'markerWidth' : 40,
265265
'markerHeight' : 50
266266
};

test/geometry/symbol/SymbolSpec.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ describe('SymbolSpec', function () {
6565
});
6666

6767
it('marker file', function () {
68-
var expected = location.href.substring(0, location.href.lastIndexOf('/')) + '/resources/x.svg';
6968
var marker = new maptalks.Marker([100, 0], {
7069
symbol:{
7170
'markerFile' : 'resources/x.svg',
@@ -75,7 +74,7 @@ describe('SymbolSpec', function () {
7574
});
7675
var res = marker._getExternalResources();
7776
expect(res).to.have.length(1);
78-
expect(res[0][0]).to.be.eql(expected);
77+
expect(res[0][0]).to.be.eql('resources/x.svg');
7978
expect(res[0][1]).to.be.eql(20);
8079
expect(res[0][2]).to.be.eql(30);
8180
});
@@ -184,27 +183,25 @@ describe('SymbolSpec', function () {
184183
});
185184

186185
it('line pattern file', function () {
187-
var expected = location.href.substring(0, location.href.lastIndexOf('/')) + '/resources/x.svg';
188186
var line = new maptalks.Polygon([[100, 0], [101, 1], [105, 10], [100, 0]], {
189187
symbol:{
190188
'linePatternFile' : 'resources/x.svg'
191189
}
192190
});
193191
var res = line._getExternalResources();
194192
expect(res).to.have.length(1);
195-
expect(res[0][0]).to.be.eql(expected);
193+
expect(res[0][0]).to.be.eql('resources/x.svg');
196194
});
197195

198196
it('polygon pattern file', function () {
199-
var expected = location.href.substring(0, location.href.lastIndexOf('/')) + '/resources/x.svg';
200197
var polygon = new maptalks.Polygon([[100, 0], [101, 1], [105, 10], [100, 0]], {
201198
symbol:{
202199
'polygonPatternFile' : 'resources/x.svg'
203200
}
204201
});
205202
var res = polygon._getExternalResources();
206203
expect(res).to.have.length(1);
207-
expect(res[0][0]).to.be.eql(expected);
204+
expect(res[0][0]).to.be.eql('resources/x.svg');
208205
});
209206

210207
it('with a non-exist svg icon', function (done) {
@@ -267,7 +264,6 @@ describe('SymbolSpec', function () {
267264
});
268265

269266
it('function type markerFile', function () {
270-
var expected = location.href.substring(0, location.href.lastIndexOf('/')) + '/resources/';
271267
var marker = new maptalks.Marker([100, 0], {
272268
symbol:{
273269
'markerFile' : {
@@ -285,9 +281,9 @@ describe('SymbolSpec', function () {
285281
});
286282
var res = marker._getExternalResources();
287283
expect(res).to.have.length(3);
288-
expect(res[0]).to.be.eql([expected + 'x.svg', 20, 30]);
289-
expect(res[1]).to.be.eql([expected + 'x1.svg', 20, 30]);
290-
expect(res[2]).to.be.eql([expected + 'x2.svg', 20, 30]);
284+
expect(res[0]).to.be.eql(['resources/x.svg', 20, 30]);
285+
expect(res[1]).to.be.eql(['resources/x1.svg', 20, 30]);
286+
expect(res[2]).to.be.eql(['resources/x2.svg', 20, 30]);
291287
});
292288

293289
it('function type markerPath', function () {

test/layer/ImageLayerSpec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ describe('Layer.ImageLayer', function () {
6969
});
7070
layer.on('layerload', function () {
7171
if (maptalks.Browser.ie) {
72-
expect(layer).to.be.painted(0, 1, [0, 0, 0, 58]);
72+
expect(layer).to.be.painted(1, 1, [0, 0, 0, 128]);
7373
} else {
74-
expect(layer).to.be.painted(0, 1, [0, 0, 0, 104]);
74+
expect(layer).to.be.painted(1, 1, [0, 0, 0, 56]);
7575
}
7676
done();
7777
});

test/layer/InfiniteHorizonSpec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ describe('Infinite Horizon Specs', function () {
7676
urlTemplate : '#'
7777
}).addTo(map);
7878
var tiles = tile.getTiles();
79-
expect(tiles.tileGrids.length).to.be.eql(2);
79+
expect(tiles.tileGrids.length).to.be.eql(1);
8080
expect(tiles.tileGrids[0].tiles.length).to.be.eql(36);
81-
expect(tiles.tileGrids[1].tiles.length).to.be.eql(16);
8281
});
8382

8483
it('cascade level 2 tiles', function () {
@@ -91,7 +90,7 @@ describe('Infinite Horizon Specs', function () {
9190
var tiles = tile.getTiles();
9291
expect(tiles.tileGrids.length).to.be.eql(3);
9392
expect(tiles.tileGrids[0].tiles.length).to.be.eql(30);
94-
expect(tiles.tileGrids[1].tiles.length).to.be.eql(5);
93+
expect(tiles.tileGrids[1].tiles.length).to.be.eql(2);
9594
expect(tiles.tileGrids[2].tiles.length).to.be.eql(22);
9695
});
9796
});

test/map/MapCameraSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ describe('Map.Camera', function () {
544544
it('should generate dom css matrix', function () {
545545
map.setPitch(75);
546546
map.setBearing(45);
547-
expect(maptalks.Util.join(map.domCssMatrix)).to.be.eql([31.819805153394643,-8.235571585149868,0.4403135308427733,0.6830127018922193,31.819805153394636,8.23557158514987,-0.4403135308427734,-0.6830127018922194,0,-43.466662183008076,-0.16685125662045044,-0.25881904510252074,0,0,28.501478623873567,45].join());
547+
expect(maptalks.Util.join(map.domCssMatrix)).to.be.eql([31.819805153394643,-8.235571585149868,0.8554549536393147,0.6830127018922193,31.819805153394636,8.23557158514987,-0.8554549536393148,-0.6830127018922194,0,-43.466662183008076,-0.32416386051937174,-0.25881904510252074,0,0,55.66501093030995,45].join());
548548
});
549549
});
550550

0 commit comments

Comments
 (0)