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

Prevent step and interpolate expressions from having duplicate stops #5605

Merged
merged 1 commit into from
Nov 7, 2017
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: 1 addition & 1 deletion src/style-spec/expression/definitions/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Interpolate implements Expression {
return context.error('Input/output pairs for "interpolate" expressions must be defined using literal numeric values (not computed expressions) for the input values.', labelKey);
}

if (stops.length && stops[stops.length - 1][0] > label) {
if (stops.length && stops[stops.length - 1][0] >= label) {
return context.error('Input/output pairs for "interpolate" expressions must be arranged with input values in strictly ascending order.', labelKey);
}

Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/expression/definitions/step.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Step implements Expression {
return context.error('Input/output pairs for "step" expressions must be defined using literal numeric values (not computed expressions) for the input values.', labelKey);
}

if (stops.length && stops[stops.length - 1][0] > label) {
if (stops.length && stops[stops.length - 1][0] >= label) {
return context.error('Input/output pairs for "step" expressions must be arranged with input values in strictly ascending order.', labelKey);
}

Expand Down
5 changes: 5 additions & 0 deletions src/style-spec/function/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ function fixupDegenerateStepCurve(expression) {
}

function appendStopPair(curve, input, output, isStep) {
// Skip duplicate stop values. They were not validated for functions, but they are for expressions.
// https://github.com/mapbox/mapbox-gl-js/issues/4107
if (curve.length > 3 && input === curve[curve.length - 2]) {
return;
}
// step curves don't get the first input value, as it is redundant.
if (!(isStep && curve.length === 2)) {
curve.push(input);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"expression": ["interpolate", ["linear"], 0, 0, 1, 0, 2],
"inputs": [[{}, {}]],
"expected": {
"compiled": {
"result": "error",
"errors": [
{
"key": "[5]",
"error": "Input/output pairs for \"interpolate\" expressions must be arranged with input values in strictly ascending order."
}
]
}
}
}
15 changes: 15 additions & 0 deletions test/integration/expression-tests/step/duplicate_stops/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"expression": ["step", 0, "a", 0, "b", 0, "c"],
"inputs": [[{}, {}]],
"expected": {
"compiled": {
"result": "error",
"errors": [
{
"key": "[5]",
"error": "Input/output pairs for \"step\" expressions must be arranged with input values in strictly ascending order."
}
]
}
}
}
56 changes: 56 additions & 0 deletions test/unit/style-spec/convert_function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,61 @@ test('convertFunction', (t) => {
t.end();
});

t.test('duplicate step function stops', (t) => {
const functionValue = {
stops: [
[0, 'a'],
[1, 'b'],
[1, 'c'],
[2, 'd']
]
};

const expression = convertFunction(functionValue, {
type: 'string',
function: 'piecewise-constant'
}, 'text-field');
t.deepEqual(expression, [
'step',
['zoom'],
'a',
1,
'b',
2,
'd'
]);

t.end();
});

t.test('duplicate interpolate function stops', (t) => {
const functionValue = {
stops: [
[0, 'a'],
[1, 'b'],
[1, 'c'],
[2, 'd']
]
};

const expression = convertFunction(functionValue, {
type: 'number',
function: 'interpolated'
}, 'text-size');
t.deepEqual(expression, [
'interpolate',
['exponential', 1],
['zoom'],
0,
'a',
1,
'b',
2,
'd'
]);

t.end();
});

t.end();
});