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

Commit 02fa10f

Browse files
committed
allow the widget to change structure of the DOM and have the compiler follow the replaced element.
1 parent 076f37a commit 02fa10f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Compiler.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function Compiler(textMarkup, attrMarkup, directives, widgets){
7777
Compiler.prototype = {
7878
compile: function(rawElement) {
7979
rawElement = jqLite(rawElement);
80-
var template = this.templatize(rawElement) || new Template();
80+
var template = this.templatize(rawElement, 0, 0) || new Template();
8181
return function(element, parentScope){
8282
element = jqLite(element);
8383
var scope = parentScope && parentScope.$eval ?
@@ -95,7 +95,7 @@ Compiler.prototype = {
9595
};
9696
},
9797

98-
templatize: function(element, priority){
98+
templatize: function(element, elementIndex, priority){
9999
var self = this,
100100
widget,
101101
directiveFns = self.directives,
@@ -130,7 +130,11 @@ Compiler.prototype = {
130130
if (widget) {
131131
descend = false;
132132
directives = false;
133+
var parent = element.parent();
133134
template.addInit(widget.call(selfApi, element));
135+
if (parent) {
136+
element = jqLite(parent[0].childNodes[elementIndex]);
137+
}
134138
}
135139
if (descend){
136140
// process markup for text nodes only
@@ -156,7 +160,7 @@ Compiler.prototype = {
156160
// Process non text child nodes
157161
if (descend) {
158162
eachNode(element, function(child, i){
159-
template.addChild(i, self.templatize(child, priority));
163+
template.addChild(i, self.templatize(child, i, priority));
160164
});
161165
}
162166
return template.empty() ? null : template;

test/CompilerSpec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('compiler', function(){
108108

109109
it('should replace widgets', function(){
110110
widgets['NG:BUTTON'] = function(element) {
111-
element.replaceWith('<div>button</div>', element);
111+
element.replaceWith('<div>button</div>');
112112
return function(element) {
113113
log += 'init';
114114
};
@@ -118,4 +118,20 @@ describe('compiler', function(){
118118
expect(log).toEqual('init');
119119
});
120120

121+
it('should use the replaced element after calling widget', function(){
122+
widgets['H1'] = function(element) {
123+
var span = angular.element('<span>{{1+2}}</span>');
124+
element.replaceWith(span);
125+
this.descend(true);
126+
this.directives(true);
127+
return noop;
128+
};
129+
textMarkup.push(function(text, textNode, parent){
130+
if (text == '{{1+2}}')
131+
textNode.text('3');
132+
});
133+
var scope = compile('<div><h1>ignore me</h1></div>');
134+
expect(scope.$element.text()).toEqual('3');
135+
});
136+
121137
});

0 commit comments

Comments
 (0)