From dc23f4037c6f6212a28b0b4b90326d08f23d9018 Mon Sep 17 00:00:00 2001 From: Juuso Backman Date: Tue, 2 Dec 2014 16:30:18 +0200 Subject: [PATCH] Fix common class: add a custom wrapper element with the defined class if commonClass is used. --- lib/app/js/app.js | 9 ++++++ lib/app/js/controllers/sections.js | 4 --- lib/app/views/partials/section.html | 7 ++--- test/angular/app.test.js | 46 +++++++++++++++++++++++++++-- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/lib/app/js/app.js b/lib/app/js/app.js index 1d7ddf7c..3398502d 100644 --- a/lib/app/js/app.js +++ b/lib/app/js/app.js @@ -91,6 +91,15 @@ angular.module('sgApp', [ } }; }) + .filter('addWrapper', ['Styleguide', function(Styleguide) { + return function(html) { + var cls = Styleguide.config.data.commonClass; + if (cls) { + return '' + html + ''; + } + return html; + }; + }]) // Trust modifier markup to be safe html .filter('unsafe', ['$sce', function($sce) { return function(val) { diff --git a/lib/app/js/controllers/sections.js b/lib/app/js/controllers/sections.js index 079b5241..f0de88f3 100644 --- a/lib/app/js/controllers/sections.js +++ b/lib/app/js/controllers/sections.js @@ -58,8 +58,4 @@ angular.module('sgApp') return re.test(section.reference); }; - $scope.getCommonClass = function() { - return Styleguide.config.data.commonClass; - }; - }); diff --git a/lib/app/views/partials/section.html b/lib/app/views/partials/section.html index 97ea9db1..bceab7ea 100644 --- a/lib/app/views/partials/section.html +++ b/lib/app/views/partials/section.html @@ -22,22 +22,19 @@

{{ section.reference }} {{
+ ng-bind-html="mod.wrappedMarkup | setModifierClass: mod.className | setVariables: variables | addWrapper | unsafe" sg-scope-user-styles>
-
-
+
diff --git a/test/angular/app.test.js b/test/angular/app.test.js index 63eded6b..b4f8f97a 100644 --- a/test/angular/app.test.js +++ b/test/angular/app.test.js @@ -2,14 +2,14 @@ describe('sgApp module', function() { - var module; + var appModule; beforeEach(function() { - module = angular.mock.module('sgApp'); + appModule = angular.mock.module('sgApp'); }); it('should be registered', function() { - expect(module).not.to.equal(null); + expect(appModule).not.to.equal(null); }); describe('setModifierClass filter', function() { @@ -75,4 +75,44 @@ describe('sgApp module', function() { expect(setVariables('test string', null)).to.eql('test string'); }); }); + + describe('addWrapper filter', function() { + + var addWrapper, + Styleguide; + + beforeEach(function() { + Styleguide = { + config: { + data: {} + } + }; + + module(function($provide) { + $provide.value('Styleguide', Styleguide); + }); + + inject(function($filter) { + addWrapper = $filter('addWrapper'); + }); + }); + + it('should be defined', function() { + expect(addWrapper).to.be.a('function'); + }); + + it('returns input as-is if Styleguide config does not have commonClass', function() { + var input = 'unchanged'; + expect(addWrapper(input)).to.eql(input); + }); + + it('returns input wrapped inside a tag with common class if Styleguide config has commonClass', function() { + Styleguide.config.data.commonClass = 'my-common-class'; + var input = 'wrapped', + expected = 'wrapped'; + expect(addWrapper(input)).to.eql(expected); + }); + + }); + });