@@ -757,7 +757,7 @@ function isLeafNode (node) {
757
757
</file>
758
758
</example>
759
759
*/
760
- function copy ( source , destination ) {
760
+ function copy ( source , destination , stackSource , stackDest ) {
761
761
if ( isWindow ( source ) || isScope ( source ) ) {
762
762
throw ngMinErr ( 'cpws' ,
763
763
"Can't copy! Making copies of Window or Scope instances is not supported." ) ;
@@ -767,33 +767,57 @@ function copy(source, destination) {
767
767
destination = source ;
768
768
if ( source ) {
769
769
if ( isArray ( source ) ) {
770
- destination = copy ( source , [ ] ) ;
770
+ destination = copy ( source , [ ] , stackSource , stackDest ) ;
771
771
} else if ( isDate ( source ) ) {
772
772
destination = new Date ( source . getTime ( ) ) ;
773
773
} else if ( isRegExp ( source ) ) {
774
774
destination = new RegExp ( source . source ) ;
775
775
} else if ( isObject ( source ) ) {
776
- destination = copy ( source , { } ) ;
776
+ destination = copy ( source , { } , stackSource , stackDest ) ;
777
777
}
778
778
}
779
779
} else {
780
780
if ( source === destination ) throw ngMinErr ( 'cpi' ,
781
781
"Can't copy! Source and destination are identical." ) ;
782
+
783
+ stackSource = stackSource || [ ] ;
784
+ stackDest = stackDest || [ ] ;
785
+
786
+ if ( isObject ( source ) ) {
787
+ var index = indexOf ( stackSource , source ) ;
788
+ if ( index !== - 1 ) return stackDest [ index ] ;
789
+
790
+ stackSource . push ( source ) ;
791
+ stackDest . push ( destination ) ;
792
+ }
793
+
794
+ var result ;
782
795
if ( isArray ( source ) ) {
783
796
destination . length = 0 ;
784
797
for ( var i = 0 ; i < source . length ; i ++ ) {
785
- destination . push ( copy ( source [ i ] ) ) ;
798
+ result = copy ( source [ i ] , null , stackSource , stackDest ) ;
799
+ if ( isObject ( source [ i ] ) ) {
800
+ stackSource . push ( source [ i ] ) ;
801
+ stackDest . push ( result ) ;
802
+ }
803
+ destination . push ( result ) ;
786
804
}
787
805
} else {
788
806
var h = destination . $$hashKey ;
789
807
forEach ( destination , function ( value , key ) {
790
808
delete destination [ key ] ;
791
809
} ) ;
792
810
for ( var key in source ) {
793
- destination [ key ] = copy ( source [ key ] ) ;
811
+ result = copy ( source [ key ] , null , stackSource , stackDest ) ;
812
+ if ( isObject ( source [ key ] ) ) {
813
+ stackSource . push ( source [ key ] ) ;
814
+ stackDest . push ( result ) ;
815
+ }
816
+ destination [ key ] = result ;
794
817
}
795
818
setHashKey ( destination , h ) ;
796
819
}
820
+
797
821
}
798
822
return destination ;
799
823
}
0 commit comments