From 3812378816089b438040ba3844cb16e141c19c01 Mon Sep 17 00:00:00 2001 From: Mikhail Troshev Date: Fri, 21 Aug 2015 13:37:17 +0300 Subject: [PATCH] fix bug with `ctx.js()` (fix #160) --- lib/bh.js | 10 ++++------ test/test.js.js | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/bh.js b/lib/bh.js index cc85b81..0fb968f 100644 --- a/lib/bh.js +++ b/lib/bh.js @@ -435,12 +435,10 @@ function BH() { * @returns {Boolean|Object|Ctx} */ js: function(js, force) { - if (js === undefined) { - return this.ctx.js; - } - this.ctx.js = force ? - (js === true ? {} : js) : - js ? this.extend(this.ctx.js, js) : this.ctx.js; + var ctx = this.ctx; + if (js === undefined) return ctx.js; + if (force || ctx.js === undefined) ctx.js = js; + else if (ctx.js !== false) this.extend(ctx.js, js); return this; }, /** diff --git a/test/test.js.js b/test/test.js.js index 3bcf2b3..a7a2cc4 100644 --- a/test/test.js.js +++ b/test/test.js.js @@ -22,12 +22,19 @@ describe('ctx.js()', function() { }); it('should not set js', function() { - bh.match('button', function(ctx) { + bh.match('button', function(ctx, json) { ctx.js(false); }); bh.apply({ block: 'button' }).should.equal('
'); }); + it('should not override user declarations', function() { + bh.match('button', function(ctx) { + ctx.js(true); + }); + bh.apply({ block: 'button', js: false }).should.equal('
'); + }); + it('should extend user js', function() { bh.match('button', function(ctx) { ctx.js({ a: 2 }); @@ -36,7 +43,7 @@ describe('ctx.js()', function() { .should.equal('
'); }); - it('should not override later declarations', function() { + it('should not override later declarations #1', function() { bh.match('button', function(ctx) { ctx.js(false); }); @@ -46,6 +53,16 @@ describe('ctx.js()', function() { bh.apply({ block: 'button' }).should.equal('
'); }); + it('should not override later declarations #2', function() { + bh.match('button', function(ctx) { + ctx.js(true); + }); + bh.match('button', function(ctx) { + ctx.js(false); + }); + bh.apply({ block: 'button' }).should.equal('
'); + }); + it('should override later declarations with force flag', function() { bh.match('button', function(ctx) { ctx.js(true, true);