Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Polyline/Polygon not overwrite the source array #1439

Merged
merged 4 commits into from
Feb 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions spec/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
<!-- /layer/vector/ -->
<script type="text/javascript" src="suites/layer/vector/CircleSpec.js"></script>
<script type="text/javascript" src="suites/layer/vector/CircleMarkerSpec.js"></script>
<script type="text/javascript" src="suites/layer/vector/PolygonSpec.js"></script>
<script type="text/javascript" src="suites/layer/vector/PolylineSpec.js"></script>
<script type="text/javascript" src="suites/layer/vector/PolylineGeometrySpec.js"></script>

<!-- /map -->
Expand Down
55 changes: 55 additions & 0 deletions spec/suites/layer/vector/PolygonSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
describe('Polygon', function() {

var c = document.createElement('div');
c.style.width = '400px';
c.style.height = '400px';
var map = new L.Map(c);
map.setView(new L.LatLng(55.8, 37.6), 6);

describe("#initialize", function() {
it("doesn't overwrite the given latlng array", function () {
var originalLatLngs = [
[1, 2],
[3, 4]
];
var sourceLatLngs = originalLatLngs.slice();

var polygon = new L.Polygon(sourceLatLngs);

expect(sourceLatLngs).toEqual(originalLatLngs);
expect(polygon._latlngs).toNotEqual(sourceLatLngs);
});
});

describe("#setLatLngs", function () {
it("doesn't overwrite the given latlng array", function () {
var originalLatLngs = [
[1, 2],
[3, 4]
];
var sourceLatLngs = originalLatLngs.slice();

var polygon = new L.Polygon(sourceLatLngs);

polygon.setLatLngs(sourceLatLngs);

expect(sourceLatLngs).toEqual(originalLatLngs);
});
});

describe("#spliceLatLngs", function () {
it("splices the internal latLngs", function () {
var latLngs = [
[1, 2],
[3, 4],
[5, 6]
];

var polygon = new L.Polygon(latLngs);

polygon.spliceLatLngs(1, 1, [7, 8]);

expect(polygon._latlngs).toEqual([L.latLng([1, 2]), L.latLng([7, 8]), L.latLng([5, 6])]);
});
});
});
55 changes: 55 additions & 0 deletions spec/suites/layer/vector/PolylineSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
describe('Polyline', function() {

var c = document.createElement('div');
c.style.width = '400px';
c.style.height = '400px';
var map = new L.Map(c);
map.setView(new L.LatLng(55.8, 37.6), 6);

describe("#initialize", function() {
it("doesn't overwrite the given latlng array", function () {
var originalLatLngs = [
[1, 2],
[3, 4]
];
var sourceLatLngs = originalLatLngs.slice();

var polyline = new L.Polyline(sourceLatLngs);

expect(sourceLatLngs).toEqual(originalLatLngs);
expect(polyline._latlngs).toNotEqual(sourceLatLngs);
});
});

describe("#setLatLngs", function () {
it("doesn't overwrite the given latlng array", function () {
var originalLatLngs = [
[1, 2],
[3, 4]
];
var sourceLatLngs = originalLatLngs.slice();

var polyline = new L.Polyline(sourceLatLngs);

polyline.setLatLngs(sourceLatLngs);

expect(sourceLatLngs).toEqual(originalLatLngs);
});
});

describe("#spliceLatLngs", function () {
it("splices the internal latLngs", function () {
var latLngs = [
[1, 2],
[3, 4],
[5, 6]
];

var polyline = new L.Polyline(latLngs);

polyline.spliceLatLngs(1, 1, [7, 8]);

expect(polyline._latlngs).toEqual([L.latLng([1, 2]), L.latLng([7, 8]), L.latLng([5, 6])]);
});
});
});
13 changes: 7 additions & 6 deletions src/layer/vector/Polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ L.Polyline = L.Path.extend({

spliceLatLngs: function () { // (Number index, Number howMany)
var removed = [].splice.apply(this._latlngs, arguments);
this._convertLatLngs(this._latlngs);
this._convertLatLngs(this._latlngs, true);
this.redraw();
return removed;
},
Expand Down Expand Up @@ -85,15 +85,16 @@ L.Polyline = L.Path.extend({
return bounds;
},

_convertLatLngs: function (latlngs) {
var i, len;
_convertLatLngs: function (latlngs, overwrite) {
var i, len, target = overwrite ? latlngs : [];

for (i = 0, len = latlngs.length; i < len; i++) {
if (L.Util.isArray(latlngs[i]) && typeof latlngs[i][0] !== 'number') {
if (L.Util.isArray(latlngs[i]) && typeof latlngs[i][0] !== 'number') {
return;
}
latlngs[i] = L.latLng(latlngs[i]);
target[i] = L.latLng(latlngs[i]);
}
return latlngs;
return target;
},

_initEvents: function () {
Expand Down