From e31fcf0fcb06580064d1e6375dbedb69f1c95f25 Mon Sep 17 00:00:00 2001 From: Chris Chua Date: Sun, 5 Apr 2015 21:04:57 -0700 Subject: [PATCH] feat(tooltip): add tooltip-html directive This directive replaces tooltip-html-unsafe use cases. It requires safe HTML content from $sce. Closes #3496 --- src/tooltip/docs/demo.html | 2 +- src/tooltip/docs/demo.js | 4 +- src/tooltip/test/tooltip.spec.js | 54 +++++++++++++++++++++++- src/tooltip/tooltip.js | 26 +++++++++++- template/tooltip/tooltip-html-popup.html | 4 ++ 5 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 template/tooltip/tooltip-html-popup.html diff --git a/src/tooltip/docs/demo.html b/src/tooltip/docs/demo.html index 914dc91ccd..3072ec345c 100644 --- a/src/tooltip/docs/demo.html +++ b/src/tooltip/docs/demo.html @@ -25,7 +25,7 @@

- I can even contain HTML. Check me out! + I can even contain HTML. Check me out!

diff --git a/src/tooltip/docs/demo.js b/src/tooltip/docs/demo.js index bc7fd24b82..18c2794b3e 100644 --- a/src/tooltip/docs/demo.js +++ b/src/tooltip/docs/demo.js @@ -1,5 +1,5 @@ -angular.module('ui.bootstrap.demo').controller('TooltipDemoCtrl', function ($scope) { +angular.module('ui.bootstrap.demo').controller('TooltipDemoCtrl', function ($scope, $sce) { $scope.dynamicTooltip = 'Hello, World!'; $scope.dynamicTooltipText = 'dynamic'; - $scope.htmlTooltip = 'I\'ve been made bold!'; + $scope.htmlTooltip = $sce.trustAsHtml('I\'ve been made bold!'); }); diff --git a/src/tooltip/test/tooltip.spec.js b/src/tooltip/test/tooltip.spec.js index ec1a576f2d..23abe5d6ba 100644 --- a/src/tooltip/test/tooltip.spec.js +++ b/src/tooltip/test/tooltip.spec.js @@ -498,8 +498,54 @@ describe( 'tooltip positioning', function() { }); +describe( 'tooltipHtml', function() { + var elm, elmBody, elmScope, tooltipScope, scope; + + // load the tooltip code + beforeEach(module('ui.bootstrap.tooltip', function ( $tooltipProvider ) { + $tooltipProvider.options({ animation: false }); + })); + + // load the template + beforeEach(module('template/tooltip/tooltip-html-popup.html')); + + beforeEach(inject(function($rootScope, $compile, $sce) { + scope = $rootScope; + scope.html = 'I say: Hello!'; + scope.safeHtml = $sce.trustAsHtml(scope.html); + + elmBody = $compile( angular.element( + '

Selector Text
' + ))( scope ); + scope.$digest(); + elm = elmBody.find('span'); + elmScope = elm.scope(); + tooltipScope = elmScope.$$childTail; + })); + + it( 'should render html properly', inject( function () { + elm.trigger( 'mouseenter' ); + expect( elmBody.find('.tooltip-inner').html() ).toBe( scope.html ); + })); + + it( 'should show on mouseenter and hide on mouseleave', inject( function ($sce) { + expect( tooltipScope.isOpen ).toBe( false ); + + elm.trigger( 'mouseenter' ); + expect( tooltipScope.isOpen ).toBe( true ); + expect( elmBody.children().length ).toBe( 2 ); + + expect( $sce.getTrustedHtml(tooltipScope.contentExp()) ).toEqual( scope.html ); + + elm.trigger( 'mouseleave' ); + expect( tooltipScope.isOpen ).toBe( false ); + expect( elmBody.children().length ).toBe( 1 ); + })); +}); + describe( 'tooltipHtmlUnsafe', function() { var elm, elmBody, elmScope, tooltipScope, scope; + var logWarnSpy; // load the tooltip code beforeEach(module('ui.bootstrap.tooltip', function ( $tooltipProvider ) { @@ -509,10 +555,12 @@ describe( 'tooltipHtmlUnsafe', function() { // load the template beforeEach(module('template/tooltip/tooltip-html-unsafe-popup.html')); - beforeEach(inject(function($rootScope, $compile) { + beforeEach(inject(function($rootScope, $compile, $log) { scope = $rootScope; scope.html = 'I say: Hello!'; + logWarnSpy = spyOn($log, 'warn'); + elmBody = $compile( angular.element( '
Selector Text
' ))( scope ); @@ -522,6 +570,10 @@ describe( 'tooltipHtmlUnsafe', function() { tooltipScope = elmScope.$$childTail; })); + it( 'should warn that this is deprecated', function () { + expect(logWarnSpy).toHaveBeenCalledWith(jasmine.stringMatching('deprecated')); + }); + it( 'should render html properly', inject( function () { elm.trigger( 'mouseenter' ); expect( elmBody.find('.tooltip-inner').html() ).toBe( scope.html ); diff --git a/src/tooltip/tooltip.js b/src/tooltip/tooltip.js index f282a009aa..68bf8b196b 100644 --- a/src/tooltip/tooltip.js +++ b/src/tooltip/tooltip.js @@ -99,6 +99,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap '
+
+
+