From 6b93315cc3d9080f0149311fd6123a3f9b38ec43 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 10 Feb 2014 13:09:16 -0500 Subject: [PATCH] Prevent browser errors for stream: searches. When we typed "stream:" into the search bar, the empty operand triggered an error in the Dict class for an undefined key, because we were using opts[0] as a "defensive" workaround to opts.operand, but opts.operand of '' is more correct than opts[0] being undefined. Now we only fall back to opts[0] whe opts.operand is undefined, and we emit a blueslip error when that happens. (imported from commit 88a196d3bc3d67689c36bc036f378da744c652f9) --- static/js/filter.js | 11 ++++++++--- zerver/tests/frontend/node/filter.js | 12 ++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/static/js/filter.js b/static/js/filter.js index 6d2be04731..2a499b7841 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -151,9 +151,14 @@ Filter.canonicalize_operator = function (operator) { }; Filter.canonicalize_term = function (opts) { - // Legacy code may still call use with a tuple of [operator, operand]. - var operator = opts.operator || opts[0]; - var operand = opts.operand || opts[1]; + var operator = opts.operator; + var operand = opts.operand; + + if (operator === undefined) { + blueslip.error("Old-style tuple operators are still in use for canonicalize_term"); + operator = opts[0]; + operand = opts[1]; + } operator = Filter.canonicalize_operator(operator); diff --git a/zerver/tests/frontend/node/filter.js b/zerver/tests/frontend/node/filter.js index e1474c8c73..d3a82c45b6 100644 --- a/zerver/tests/frontend/node/filter.js +++ b/zerver/tests/frontend/node/filter.js @@ -187,11 +187,19 @@ function get_predicate(operators) { assert(predicate({type: 'stream', stream: 'foo', subject: 'bar.d'})); // Try to get the MIT regex to explode for an empty stream. - predicate = new Filter([['stream', ''], ['topic', 'bar']]).predicate(); + var terms = [ + {operator: 'stream', operand: ''}, + {operator: 'topic', operand: 'bar'} + ]; + predicate = new Filter(terms).predicate(); assert(!predicate({type: 'stream', stream: 'foo', subject: 'bar'})); // Try to get the MIT regex to explode for an empty topic. - predicate = new Filter([['stream', 'foo'], ['topic', '']]).predicate(); + terms = [ + {operator: 'stream', operand: 'foo'}, + {operator: 'topic', operand: ''} + ]; + predicate = new Filter(terms).predicate(); assert(!predicate({type: 'stream', stream: 'foo', subject: 'bar'})); }());