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

Commit 496e6bf

Browse files
committed
refactored quickClone to cloneNode and exposed it on jQuery
1 parent 23b255a commit 496e6bf

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

docs/angular.element.ngdoc

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ raw DOM references.
4040
- [text()](http://api.jquery.com/text/)
4141
- [trigger()](http://api.jquery.com/trigger/)
4242

43-
## Additionally these methods are available in both jQuery and jQuery lite version.
43+
## Additionally these methods extend the jQuery and are available in both jQuery and jQuery lite
44+
version:
4445

4546
- `scope()` - retrieves the current angular scope of the element.
47+
- `cloneNode()` - Clones the current node, ensuring identical structure. This is important since
48+
the `clone()` method implemented by jQuery under some circumstances changes the DOM
49+
structure, which then prevents proper application of compiled template to the cloned node.
50+
__Always use `cloneNode()` when cloning previously compiled templates.__
4651

4752
@param {string|DOMElement} element HTML string or DOMElement to be wrapped into jQuery.
4853
@returns {Object} jQuery object.

src/Angular.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,6 @@ if (msie) {
453453
};
454454
}
455455

456-
function quickClone(element) {
457-
return jqLite(element[0].cloneNode(true));
458-
}
459-
460456
function isVisible(element) {
461457
var rect = element[0].getBoundingClientRect(),
462458
width = (rect.width || (rect.right||0 - rect.left||0)),
@@ -1034,9 +1030,23 @@ function bindJQuery(){
10341030
// reset to jQuery or default to us.
10351031
if (window.jQuery) {
10361032
jqLite = window.jQuery;
1037-
jqLite.fn.scope = JQLite.prototype.scope;
1033+
extend(jqLite.fn, {
1034+
scope: JQLite.prototype.scope,
1035+
cloneNode: cloneNode
1036+
});
10381037
} else {
10391038
jqLite = jqLiteWrap;
10401039
}
10411040
angular.element = jqLite;
10421041
}
1042+
1043+
/**
1044+
* throw error of the argument is falsy.
1045+
*/
1046+
function assertArg(arg, name) {
1047+
if (!arg) {
1048+
var error = new Error("Argument '" + name + "' is required");
1049+
if (window.console) window.console.log(error.stack);
1050+
throw error;
1051+
}
1052+
};

src/jqLite.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,10 @@ JQLite.prototype = {
251251
return jqLite(this[0].parentNode);
252252
},
253253

254-
clone: function() { return jqLite(this[0].cloneNode(true)); }
254+
clone: cloneNode,
255+
cloneNode: cloneNode
255256
};
257+
function cloneNode() { return jqLite(this[0].cloneNode(true)); }
256258

257259
if (msie) {
258260
extend(JQLite.prototype, {

src/widgets.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ var ngSwitch = angularWidget('ng:switch', function (element){
790790
forEach(cases, function(switchCase){
791791
if (!found && switchCase.when(childScope, value)) {
792792
found = true;
793-
var caseElement = quickClone(switchCase.element);
793+
var caseElement = switchCase.element.cloneNode();
794794
element.append(caseElement);
795795
childScope.$tryEval(switchCase.change, element);
796796
switchCase.template(caseElement, childScope);
@@ -943,8 +943,7 @@ angularWidget("@ng:repeat", function(expression, element){
943943
childScope.$position = index == 0 ?
944944
'first' :
945945
(index == collectionLength - 1 ? 'last' : 'middle');
946-
cloneElement = quickClone(element);
947-
lastElement.after(cloneElement);
946+
lastElement.after(cloneElement = element.cloneNode());
948947
cloneElement.attr('ng:repeat-index', index);
949948
linker(cloneElement, childScope);
950949
children.push(childScope);

0 commit comments

Comments
 (0)