Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 4a051ef

Browse files
committed
feat($compile): support compiling text nodes by wrapping them in <span>
1 parent 1752c8c commit 4a051ef

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/jqLite.js

+9
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,15 @@ forEach({
627627
}
628628
},
629629

630+
wrap: function(element, wrapNode) {
631+
wrapNode = jqLite(wrapNode)[0];
632+
var parent = element.parentNode;
633+
if (parent) {
634+
parent.replaceChild(wrapNode, element);
635+
}
636+
wrapNode.appendChild(element);
637+
},
638+
630639
remove: function(element) {
631640
JQLiteDealoc(element);
632641
var parent = element.parentNode;

src/service/compiler.js

+7
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ function $CompileProvider($provide) {
180180

181181
return function(templateElement) {
182182
templateElement = jqLite(templateElement);
183+
// We can not compile top level text elements since text nodes can be merged and we will
184+
// not be able to attach scope data to them, so we will wrap them in <span>
185+
forEach(templateElement, function(node, index){
186+
if (node.nodeType == 3 /* text node */) {
187+
templateElement[index] = jqLite(node).wrap('<span>').parent()[0];
188+
}
189+
});
183190
var linkingFn = compileNodes(templateElement, templateElement);
184191
return function(scope, cloneConnectFn){
185192
assertArg(scope, 'scope');

test/jqLiteSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,26 @@ describe('jqLite', function() {
802802
});
803803
});
804804

805+
806+
describe('wrap', function() {
807+
it('should wrap text node', function() {
808+
var root = jqLite('<div>A&lt;a&gt;B&lt;/a&gt;C</div>');
809+
var text = root.contents();
810+
expect(text.wrap("<span>")[0]).toBe(text[0]);
811+
expect(root.find('span').text()).toEqual('A<a>B</a>C');
812+
});
813+
it('should wrap free text node', function() {
814+
var root = jqLite('<div>A&lt;a&gt;B&lt;/a&gt;C</div>');
815+
var text = root.contents();
816+
text.remove();
817+
expect(root.text()).toBe('');
818+
819+
text.wrap("<span>");
820+
expect(text.parent().text()).toEqual('A<a>B</a>C');
821+
});
822+
});
823+
824+
805825
describe('prepend', function() {
806826
it('should prepend to empty', function() {
807827
var root = jqLite('<div>');

test/service/compilerSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ describe('$compile', function() {
9696

9797
describe('compile phase', function() {
9898

99+
it('should wrap root text nodes in spans', inject(function($compile, $rootScope) {
100+
element = jqLite('<div>A&lt;a&gt;B&lt;/a&gt;C</div>');
101+
var text = element.contents();
102+
expect(text[0].nodeName).toEqual('#text');
103+
text = $compile(text)($rootScope);
104+
expect(lowercase(text[0].nodeName)).toEqual('span');
105+
expect(element.find('span').text()).toEqual('A<a>B</a>C');
106+
}));
107+
99108
describe('multiple directives per element', function() {
100109
it('should allow multiple directives per element', inject(function($compile, $rootScope, log){
101110
element = $compile(

0 commit comments

Comments
 (0)