From eda47822c002833db58f8daf779c74ee77bee882 Mon Sep 17 00:00:00 2001 From: Robert Messerle Date: Tue, 21 Jul 2015 13:42:08 -0700 Subject: [PATCH] fix(highlightText): changes to the template data will update the generated HTML Closes #3550 --- .../autocomplete/autocomplete.spec.js | 24 ++++++++++- .../autocomplete/js/highlightController.js | 43 ++++++++++++++----- .../autocomplete/js/highlightDirective.js | 7 ++- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/components/autocomplete/autocomplete.spec.js b/src/components/autocomplete/autocomplete.spec.js index 52b547c8264..cc5603830d9 100644 --- a/src/components/autocomplete/autocomplete.spec.js +++ b/src/components/autocomplete/autocomplete.spec.js @@ -11,7 +11,7 @@ describe('', function () { return container; } - function createScope (items) { + function createScope (items, obj) { var scope; items = items || [ 'foo', 'bar', 'baz' ].map(function (item) { return { display: item }; }); inject(function ($rootScope) { @@ -23,6 +23,7 @@ describe('', function () { }; scope.searchText = ''; scope.selectedItem = null; + for (var key in obj) scope[key] = obj[key]; }); return scope; } @@ -302,4 +303,25 @@ describe('', function () { expect(scope.selectedItem).toBe(null); })); }); + + describe('md-highlight-text', function () { + it('should update when content is modified', inject(function () { + var template = '
{{message}}
'; + var scope = createScope(null, { message: 'some text', query: 'some' }); + var element = compile(template, scope); + + expect(element.html()).toBe('some text'); + + scope.query = 'so'; + scope.$apply(); + + expect(element.html()).toBe('some text'); + + scope.message = 'some more text'; + scope.$apply(); + + expect(element.html()).toBe('some more text'); + })); + }); + }); diff --git a/src/components/autocomplete/js/highlightController.js b/src/components/autocomplete/js/highlightController.js index e66426f3098..2b4d6f56607 100644 --- a/src/components/autocomplete/js/highlightController.js +++ b/src/components/autocomplete/js/highlightController.js @@ -3,20 +3,43 @@ angular .controller('MdHighlightCtrl', MdHighlightCtrl); function MdHighlightCtrl ($scope, $element, $interpolate) { - this.init = init; + var ctrl = this; - return init(); + ctrl.term = null; + ctrl.template = null; + ctrl.watchers = []; + ctrl.init = init; - function init (term) { - var unsafeText = $interpolate($element.html())($scope), + function init (term, template) { + createWatchers(term, template); + $element.on('$destroy', cleanup); + } + + function createWatchers (term, template) { + ctrl.watchers.push($scope.$watch(term, function (term) { + ctrl.term = term; + updateHTML(term, ctrl.template); + })); + ctrl.watchers.push($scope.$watch(compileTemplate, function (template) { + ctrl.template = template; + updateHTML(ctrl.term, template); + })); + + function compileTemplate () { return $interpolate(template)($scope); } + } + + function cleanup () { + ctrl.watchers.forEach(function (watcher) { watcher(); }); + } + + function updateHTML () { + if (ctrl.term === null || ctrl.template === null) return; + var unsafeText = $interpolate(ctrl.template)($scope), text = angular.element('
').text(unsafeText).html(), flags = $element.attr('md-highlight-flags') || '', - watcher = $scope.$watch(term, function (term) { - var regex = getRegExp(term, flags), - html = text.replace(regex, '$&'); - $element.html(html); - }); - $element.on('$destroy', function () { watcher(); }); + regex = getRegExp(ctrl.term, flags), + html = text.replace(regex, '$&'); + $element.html(html); } function sanitize (term) { diff --git a/src/components/autocomplete/js/highlightDirective.js b/src/components/autocomplete/js/highlightDirective.js index 329fde32cee..f40ecf81d08 100644 --- a/src/components/autocomplete/js/highlightDirective.js +++ b/src/components/autocomplete/js/highlightDirective.js @@ -36,8 +36,11 @@ function MdHighlight () { terminal: true, scope: false, controller: 'MdHighlightCtrl', - link: function (scope, element, attr, ctrl) { - ctrl.init(attr.mdHighlightText); + compile: function (element, attr) { + var template = element.html(); + return function (scope, element, attr, ctrl) { + ctrl.init(attr.mdHighlightText, template); + }; } }; }