2011-07-17 08:05:43 +00:00
'use strict' ;
2011-11-29 20:11:32 +00:00
describe ( '$compile' , function ( ) {
2012-06-06 20:58:10 +00:00
var element , directive , $compile , $rootScope ;
2010-03-18 19:20:06 +00:00
2011-11-29 20:11:32 +00:00
beforeEach ( module ( provideLog , function ( $provide , $compileProvider ) {
element = null ;
2012-06-04 19:08:27 +00:00
directive = $compileProvider . directive ;
2011-03-23 16:33:29 +00:00
2012-06-04 19:08:27 +00:00
directive ( 'log' , function ( log ) {
2011-11-29 20:11:32 +00:00
return {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2011-11-29 20:11:32 +00:00
priority : 0 ,
compile : valueFn ( function ( scope , element , attrs ) {
log ( attrs . log || 'LOG' ) ;
} )
} ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'highLog' , function ( log ) {
2012-03-08 06:47:01 +00:00
return { restrict : 'CAM' , priority : 3 , compile : valueFn ( function ( scope , element , attrs ) {
2011-11-29 20:11:32 +00:00
log ( attrs . highLog || 'HIGH' ) ;
} ) } ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'mediumLog' , function ( log ) {
2012-03-08 06:47:01 +00:00
return { restrict : 'CAM' , priority : 2 , compile : valueFn ( function ( scope , element , attrs ) {
2011-11-29 20:11:32 +00:00
log ( attrs . mediumLog || 'MEDIUM' ) ;
} ) } ;
} ) ;
2011-05-06 20:29:51 +00:00
2012-06-04 19:08:27 +00:00
directive ( 'greet' , function ( ) {
2012-03-08 06:47:01 +00:00
return { restrict : 'CAM' , priority : 10 , compile : valueFn ( function ( scope , element , attrs ) {
2011-11-29 20:11:32 +00:00
element . text ( "Hello " + attrs . greet ) ;
} ) } ;
} ) ;
2011-03-23 16:33:29 +00:00
2012-06-04 19:08:27 +00:00
directive ( 'set' , function ( ) {
2011-11-29 20:11:32 +00:00
return function ( scope , element , attrs ) {
element . text ( attrs . set ) ;
2010-03-18 19:20:06 +00:00
} ;
2011-11-29 20:11:32 +00:00
} ) ;
2010-03-18 19:20:06 +00:00
2012-06-04 19:08:27 +00:00
directive ( 'mediumStop' , valueFn ( {
2011-11-29 20:11:32 +00:00
priority : 2 ,
terminal : true
} ) ) ;
2011-03-23 16:33:29 +00:00
2012-06-04 19:08:27 +00:00
directive ( 'stop' , valueFn ( {
2011-11-29 20:11:32 +00:00
terminal : true
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'negativeStop' , valueFn ( {
2011-11-29 20:11:32 +00:00
priority : - 100 , // even with negative priority we still should be able to stop descend
terminal : true
} ) ) ;
2012-06-06 20:58:10 +00:00
return function ( _$compile _ , _$rootScope _ ) {
$rootScope = _$rootScope _ ;
$compile = _$compile _ ;
} ;
2011-10-26 05:21:21 +00:00
} ) ) ;
2010-03-18 19:20:06 +00:00
2012-06-06 20:58:10 +00:00
function compile ( html ) {
element = angular . element ( html ) ;
$compile ( element ) ( $rootScope ) ;
}
2011-03-23 16:33:29 +00:00
2011-11-29 20:11:32 +00:00
afterEach ( function ( ) {
dealoc ( element ) ;
} ) ;
2010-03-18 19:20:06 +00:00
2011-03-23 16:33:29 +00:00
2011-11-29 20:11:32 +00:00
describe ( 'configuration' , function ( ) {
it ( 'should register a directive' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'div' , function ( log ) {
2012-03-08 06:47:01 +00:00
return {
restrict : 'ECA' ,
link : function ( scope , element ) {
log ( 'OK' ) ;
element . text ( 'SUCCESS' ) ;
}
2011-11-29 20:11:32 +00:00
} ;
} )
} ) ;
inject ( function ( $compile , $rootScope , log ) {
element = $compile ( '<div></div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'SUCCESS' ) ;
expect ( log ) . toEqual ( 'OK' ) ;
} )
} ) ;
2010-04-12 23:24:28 +00:00
2011-11-29 20:11:32 +00:00
it ( 'should allow registration of multiple directives with same name' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'div' , function ( log ) {
2012-03-08 06:47:01 +00:00
return {
restrict : 'ECA' ,
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
link : {
pre : log . fn ( 'pre1' ) ,
post : log . fn ( 'post1' )
}
2012-03-08 06:47:01 +00:00
} ;
2011-11-29 20:11:32 +00:00
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'div' , function ( log ) {
2012-03-08 06:47:01 +00:00
return {
restrict : 'ECA' ,
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
link : {
pre : log . fn ( 'pre2' ) ,
post : log . fn ( 'post2' )
}
2012-03-08 06:47:01 +00:00
} ;
2011-11-29 20:11:32 +00:00
} ) ;
} ) ;
inject ( function ( $compile , $rootScope , log ) {
element = $compile ( '<div></div>' ) ( $rootScope ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'pre1; pre2; post2; post1' ) ;
2011-11-29 20:11:32 +00:00
} ) ;
} ) ;
2013-10-05 09:49:09 +00:00
it ( 'should throw an exception if a directive is called "hasOwnProperty"' , function ( ) {
module ( function ( ) {
expect ( function ( ) {
directive ( 'hasOwnProperty' , function ( ) { } ) ;
} ) . toThrowMinErr ( 'ng' , 'badname' , "hasOwnProperty is not a valid directive name" ) ;
} ) ;
inject ( function ( $compile ) { } ) ;
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
describe ( 'compile phase' , function ( ) {
2013-02-18 12:05:16 +00:00
it ( 'should attach scope to the document node when it is compiled explicitly' , inject ( function ( $document ) {
$compile ( $document ) ( $rootScope ) ;
expect ( $document . scope ( ) ) . toBe ( $rootScope ) ;
} ) ) ;
2012-02-04 00:20:24 +00:00
it ( 'should wrap root text nodes in spans' , inject ( function ( $compile , $rootScope ) {
element = jqLite ( '<div>A<a>B</a>C</div>' ) ;
var text = element . contents ( ) ;
expect ( text [ 0 ] . nodeName ) . toEqual ( '#text' ) ;
text = $compile ( text ) ( $rootScope ) ;
2013-02-18 12:05:16 +00:00
expect ( text [ 0 ] . nodeName ) . toEqual ( 'SPAN' ) ;
2012-02-04 00:20:24 +00:00
expect ( element . find ( 'span' ) . text ( ) ) . toEqual ( 'A<a>B</a>C' ) ;
} ) ) ;
2013-01-09 22:23:50 +00:00
it ( 'should not wrap root whitespace text nodes in spans' , function ( ) {
element = jqLite (
'<div> <div>A</div>\n ' + // The spaces and newlines here should not get wrapped
'<div>B</div>C\t\n ' + // The "C", tabs and spaces here will be wrapped
'</div>' ) ;
$compile ( element . contents ( ) ) ( $rootScope ) ;
var spans = element . find ( 'span' ) ;
expect ( spans . length ) . toEqual ( 1 ) ;
expect ( spans . text ( ) . indexOf ( 'C' ) ) . toEqual ( 0 ) ;
} ) ;
2013-02-18 12:05:16 +00:00
it ( 'should not leak memory when there are top level empty text nodes' , function ( ) {
var calcCacheSize = function ( ) {
var size = 0 ;
forEach ( jqLite . cache , function ( item , key ) { size ++ ; } ) ;
return size ;
} ;
// We compile the contents of element (i.e. not element itself)
// Then delete these contents and check the cache has been reset to zero
// First with only elements at the top level
element = jqLite ( '<div><div></div></div>' ) ;
$compile ( element . contents ( ) ) ( $rootScope ) ;
element . html ( '' ) ;
expect ( calcCacheSize ( ) ) . toEqual ( 0 ) ;
// Next with non-empty text nodes at the top level
// (in this case the compiler will wrap them in a <span>)
element = jqLite ( '<div>xxx</div>' ) ;
$compile ( element . contents ( ) ) ( $rootScope ) ;
element . html ( '' ) ;
expect ( calcCacheSize ( ) ) . toEqual ( 0 ) ;
// Next with comment nodes at the top level
element = jqLite ( '<div><!-- comment --></div>' ) ;
$compile ( element . contents ( ) ) ( $rootScope ) ;
element . html ( '' ) ;
expect ( calcCacheSize ( ) ) . toEqual ( 0 ) ;
// Finally with empty text nodes at the top level
element = jqLite ( '<div> \n<div></div> </div>' ) ;
$compile ( element . contents ( ) ) ( $rootScope ) ;
element . html ( '' ) ;
expect ( calcCacheSize ( ) ) . toEqual ( 0 ) ;
} ) ;
2013-01-09 22:23:50 +00:00
2013-02-24 06:54:35 +00:00
it ( 'should not blow up when elements with no childNodes property are compiled' , inject (
function ( $compile , $rootScope ) {
// it turns out that when a browser plugin is bound to an DOM element (typically <object>),
// the plugin's context rather than the usual DOM apis are exposed on this element, so
// childNodes might not exist.
if ( msie < 9 ) return ;
element = jqLite ( '<div>{{1+2}}</div>' ) ;
element [ 0 ] . childNodes [ 1 ] = { nodeType : 3 , nodeName : 'OBJECT' , textContent : 'fake node' } ;
if ( ! element [ 0 ] . childNodes [ 1 ] ) return ; //browser doesn't support this kind of mocking
expect ( element [ 0 ] . childNodes [ 1 ] . textContent ) . toBe ( 'fake node' ) ;
$compile ( element ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
// object's children can't be compiled in this case, so we expect them to be raw
expect ( element . html ( ) ) . toBe ( "3" ) ;
} ) ) ;
2011-11-29 20:11:32 +00:00
describe ( 'multiple directives per element' , function ( ) {
it ( 'should allow multiple directives per element' , inject ( function ( $compile , $rootScope , log ) {
element = $compile (
'<span greet="angular" log="L" x-high-log="H" data-medium-log="M"></span>' )
( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'Hello angular' ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'L; M; H' ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
it ( 'should recurse to children' , inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div>0<a set="hello">1</a>2<b set="angular">3</b>4</div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( '0hello2angular4' ) ;
} ) ) ;
2011-03-23 16:33:29 +00:00
2011-11-29 20:11:32 +00:00
it ( 'should allow directives in classes' , inject ( function ( $compile , $rootScope , log ) {
element = $compile ( '<div class="greet: angular; log:123;"></div>' ) ( $rootScope ) ;
expect ( element . html ( ) ) . toEqual ( 'Hello angular' ) ;
expect ( log ) . toEqual ( '123' ) ;
} ) ) ;
2012-03-12 23:49:28 +00:00
it ( 'should ignore not set CSS classes on SVG elements' , inject ( function ( $compile , $rootScope , log ) {
if ( ! window . SVGElement ) return ;
// According to spec SVG element className property is readonly, but only FF
// implements it this way which causes compile exceptions.
element = $compile ( '<svg><text>{{1}}</text></svg>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toEqual ( '1' ) ;
} ) ) ;
2011-11-29 20:11:32 +00:00
it ( 'should allow directives in comments' , inject (
function ( $compile , $rootScope , log ) {
element = $compile ( '<div>0<!-- directive: log angular -->1</div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( 'angular' ) ;
}
) ) ;
it ( 'should receive scope, element, and attributes' , function ( ) {
var injector ;
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'log' , function ( $injector , $rootScope ) {
2011-11-29 20:11:32 +00:00
injector = $injector ;
return {
2012-03-08 06:47:01 +00:00
restrict : 'CA' ,
2011-11-29 20:11:32 +00:00
compile : function ( element , templateAttr ) {
expect ( typeof templateAttr . $normalize ) . toBe ( 'function' ) ;
expect ( typeof templateAttr . $set ) . toBe ( 'function' ) ;
2012-03-28 23:03:59 +00:00
expect ( isElement ( templateAttr . $$element ) ) . toBeTruthy ( ) ;
2011-11-29 20:11:32 +00:00
expect ( element . text ( ) ) . toEqual ( 'unlinked' ) ;
expect ( templateAttr . exp ) . toEqual ( 'abc' ) ;
expect ( templateAttr . aa ) . toEqual ( 'A' ) ;
expect ( templateAttr . bb ) . toEqual ( 'B' ) ;
expect ( templateAttr . cc ) . toEqual ( 'C' ) ;
return function ( scope , element , attr ) {
expect ( element . text ( ) ) . toEqual ( 'unlinked' ) ;
expect ( attr ) . toBe ( templateAttr ) ;
expect ( scope ) . toEqual ( $rootScope ) ;
element . text ( 'worked' ) ;
}
}
} ;
} ) ;
2010-03-18 21:43:49 +00:00
} ) ;
2011-11-29 20:11:32 +00:00
inject ( function ( $rootScope , $compile , $injector ) {
element = $compile (
'<div class="log" exp="abc" aa="A" x-Bb="B" daTa-cC="C">unlinked</div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'worked' ) ;
expect ( injector ) . toBe ( $injector ) ; // verify that directive is injectable
} ) ;
} ) ;
} ) ;
describe ( 'error handling' , function ( ) {
it ( 'should handle exceptions' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( $exceptionHandlerProvider ) {
2011-11-29 20:11:32 +00:00
$exceptionHandlerProvider . mode ( 'log' ) ;
2012-06-04 19:08:27 +00:00
directive ( 'factoryError' , function ( ) { throw 'FactoryError' ; } ) ;
directive ( 'templateError' ,
2011-11-29 20:11:32 +00:00
valueFn ( { compile : function ( ) { throw 'TemplateError' ; } } ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'linkingError' ,
2011-11-29 20:11:32 +00:00
valueFn ( function ( ) { throw 'LinkingError' ; } ) ) ;
} ) ;
inject ( function ( $rootScope , $compile , $exceptionHandler ) {
element = $compile ( '<div factory-error template-error linking-error></div>' ) ( $rootScope ) ;
expect ( $exceptionHandler . errors [ 0 ] ) . toEqual ( 'FactoryError' ) ;
expect ( $exceptionHandler . errors [ 1 ] [ 0 ] ) . toEqual ( 'TemplateError' ) ;
expect ( ie ( $exceptionHandler . errors [ 1 ] [ 1 ] ) ) .
toEqual ( '<div factory-error linking-error template-error>' ) ;
expect ( $exceptionHandler . errors [ 2 ] [ 0 ] ) . toEqual ( 'LinkingError' ) ;
expect ( ie ( $exceptionHandler . errors [ 2 ] [ 1 ] ) ) .
2012-02-11 05:35:02 +00:00
toEqual ( '<div class="ng-scope" factory-error linking-error template-error>' ) ;
2011-11-29 20:11:32 +00:00
// crazy stuff to make IE happy
function ie ( text ) {
var list = [ ] ,
2012-02-11 05:35:02 +00:00
parts , elementName ;
2011-11-29 20:11:32 +00:00
parts = lowercase ( text ) .
replace ( '<' , '' ) .
replace ( '>' , '' ) .
split ( ' ' ) ;
2012-02-11 05:35:02 +00:00
elementName = parts . shift ( ) ;
2011-11-29 20:11:32 +00:00
parts . sort ( ) ;
2012-02-11 05:35:02 +00:00
parts . unshift ( elementName ) ;
2011-11-29 20:11:32 +00:00
forEach ( parts , function ( value , key ) {
if ( value . substring ( 0 , 3 ) == 'ng-' ) {
} else {
2012-01-28 00:18:16 +00:00
value = value . replace ( '=""' , '' ) ;
var match = value . match ( /=(.*)/ ) ;
if ( match && match [ 1 ] . charAt ( 0 ) != '"' ) {
value = value . replace ( /=(.*)/ , '="$1"' ) ;
}
list . push ( value ) ;
2011-11-29 20:11:32 +00:00
}
} ) ;
return '<' + list . join ( ' ' ) + '>' ;
}
} ) ;
} ) ;
2012-04-03 21:15:42 +00:00
it ( 'should allow changing the template structure after the current node' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'after' , valueFn ( {
2012-04-03 21:15:42 +00:00
compile : function ( element ) {
element . after ( '<span log>B</span>' ) ;
}
} ) ) ;
} ) ;
inject ( function ( $compile , $rootScope , log ) {
element = jqLite ( "<div><div after>A</div></div>" ) ;
$compile ( element ) ( $rootScope ) ;
expect ( element . text ( ) ) . toBe ( 'AB' ) ;
expect ( log ) . toEqual ( 'LOG' ) ;
} ) ;
} ) ;
it ( 'should allow changing the template structure after the current node inside ngRepeat' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'after' , valueFn ( {
2012-04-03 21:15:42 +00:00
compile : function ( element ) {
element . after ( '<span log>B</span>' ) ;
}
} ) ) ;
} ) ;
inject ( function ( $compile , $rootScope , log ) {
element = jqLite ( '<div><div ng-repeat="i in [1,2]"><div after>A</div></div></div>' ) ;
$compile ( element ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toBe ( 'ABAB' ) ;
expect ( log ) . toEqual ( 'LOG; LOG' ) ;
} ) ;
} ) ;
2013-01-08 19:11:51 +00:00
it ( 'should allow modifying the DOM structure in post link fn' , function ( ) {
module ( function ( ) {
directive ( 'removeNode' , valueFn ( {
link : function ( $scope , $element ) {
$element . remove ( ) ;
}
} ) ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
element = jqLite ( '<div><div remove-node></div><div>{{test}}</div></div>' ) ;
$rootScope . test = 'Hello' ;
$compile ( element ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . children ( ) . length ) . toBe ( 1 ) ;
expect ( element . text ( ) ) . toBe ( 'Hello' ) ;
} ) ;
} )
2011-11-29 20:11:32 +00:00
} ) ;
describe ( 'compiler control' , function ( ) {
describe ( 'priority' , function ( ) {
it ( 'should honor priority' , inject ( function ( $compile , $rootScope , log ) {
element = $compile (
'<span log="L" x-high-log="H" data-medium-log="M"></span>' )
( $rootScope ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'L; M; H' ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
} ) ;
describe ( 'terminal' , function ( ) {
it ( 'should prevent further directives from running' , inject ( function ( $rootScope , $compile ) {
element = $compile ( '<div negative-stop><a set="FAIL">OK</a></div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'OK' ) ;
}
) ) ;
it ( 'should prevent further directives from running, but finish current priority level' ,
inject ( function ( $rootScope , $compile , log ) {
// class is processed after attrs, so putting log in class will put it after
// the stop in the current level. This proves that the log runs after stop
element = $compile (
'<div high-log medium-stop log class="medium-log"><a set="FAIL">OK</a></div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'OK' ) ;
expect ( log . toArray ( ) . sort ( ) ) . toEqual ( [ 'HIGH' , 'MEDIUM' ] ) ;
} )
) ;
} ) ;
describe ( 'restrict' , function ( ) {
it ( 'should allow restriction of attributes' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
2011-11-29 20:11:32 +00:00
forEach ( { div : 'E' , attr : 'A' , clazz : 'C' , all : 'EAC' } , function ( restrict , name ) {
2012-06-04 19:08:27 +00:00
directive ( name , function ( log ) {
2011-11-29 20:11:32 +00:00
return {
restrict : restrict ,
compile : valueFn ( function ( scope , element , attr ) {
log ( name ) ;
} )
} ;
} ) ;
} ) ;
} ) ;
inject ( function ( $rootScope , $compile , log ) {
dealoc ( $compile ( '<span div class="div"></span>' ) ( $rootScope ) ) ;
expect ( log ) . toEqual ( '' ) ;
log . reset ( ) ;
dealoc ( $compile ( '<div></div>' ) ( $rootScope ) ) ;
expect ( log ) . toEqual ( 'div' ) ;
log . reset ( ) ;
dealoc ( $compile ( '<attr class=""attr"></attr>' ) ( $rootScope ) ) ;
expect ( log ) . toEqual ( '' ) ;
log . reset ( ) ;
dealoc ( $compile ( '<span attr></span>' ) ( $rootScope ) ) ;
expect ( log ) . toEqual ( 'attr' ) ;
log . reset ( ) ;
dealoc ( $compile ( '<clazz clazz></clazz>' ) ( $rootScope ) ) ;
expect ( log ) . toEqual ( '' ) ;
log . reset ( ) ;
dealoc ( $compile ( '<span class="clazz"></span>' ) ( $rootScope ) ) ;
expect ( log ) . toEqual ( 'clazz' ) ;
log . reset ( ) ;
dealoc ( $compile ( '<all class="all" all></all>' ) ( $rootScope ) ) ;
expect ( log ) . toEqual ( 'all; all; all' ) ;
} ) ;
} ) ;
} ) ;
describe ( 'template' , function ( ) {
2012-06-04 19:08:27 +00:00
beforeEach ( module ( function ( ) {
directive ( 'replace' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2011-11-29 20:11:32 +00:00
replace : true ,
2012-05-02 23:34:54 +00:00
template : '<div class="log" style="width: 10px" high-log>Replace!</div>' ,
2011-11-29 20:11:32 +00:00
compile : function ( element , attr ) {
attr . $set ( 'compiled' , 'COMPILED' ) ;
2012-03-28 23:03:59 +00:00
expect ( element ) . toBe ( attr . $$element ) ;
2011-11-29 20:11:32 +00:00
}
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'append' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2012-05-02 23:34:54 +00:00
template : '<div class="log" style="width: 10px" high-log>Append!</div>' ,
2011-11-29 20:11:32 +00:00
compile : function ( element , attr ) {
attr . $set ( 'compiled' , 'COMPILED' ) ;
2012-03-28 23:03:59 +00:00
expect ( element ) . toBe ( attr . $$element ) ;
2011-11-29 20:11:32 +00:00
}
} ) ) ;
2012-06-06 14:23:07 +00:00
directive ( 'replaceWithInterpolatedClass' , valueFn ( {
replace : true ,
template : '<div class="class_{{1+1}}">Replace with interpolated class!</div>' ,
compile : function ( element , attr ) {
attr . $set ( 'compiled' , 'COMPILED' ) ;
expect ( element ) . toBe ( attr . $$element ) ;
2012-08-11 06:46:42 +00:00
}
2012-06-06 14:23:07 +00:00
} ) ) ;
2013-11-12 23:53:06 +00:00
directive ( 'replaceWithInterpolatedStyle' , valueFn ( {
replace : true ,
template : '<div style="width:{{1+1}}px">Replace with interpolated style!</div>' ,
compile : function ( element , attr ) {
attr . $set ( 'compiled' , 'COMPILED' ) ;
expect ( element ) . toBe ( attr . $$element ) ;
}
} ) ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
it ( 'should replace element with template' , inject ( function ( $compile , $rootScope ) {
2012-05-02 23:34:54 +00:00
element = $compile ( '<div><div replace>ignore</div><div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'Replace!' ) ;
2011-11-29 20:11:32 +00:00
expect ( element . find ( 'div' ) . attr ( 'compiled' ) ) . toEqual ( 'COMPILED' ) ;
} ) ) ;
it ( 'should append element with template' , inject ( function ( $compile , $rootScope ) {
2012-05-02 23:34:54 +00:00
element = $compile ( '<div><div append>ignore</div><div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'Append!' ) ;
2011-11-29 20:11:32 +00:00
expect ( element . find ( 'div' ) . attr ( 'compiled' ) ) . toEqual ( 'COMPILED' ) ;
} ) ) ;
2012-05-02 23:34:54 +00:00
it ( 'should compile template when replacing' , inject ( function ( $compile , $rootScope , log ) {
element = $compile ( '<div><div replace medium-log>ignore</div><div>' )
2011-11-29 20:11:32 +00:00
( $rootScope ) ;
$rootScope . $digest ( ) ;
2012-05-02 23:34:54 +00:00
expect ( element . text ( ) ) . toEqual ( 'Replace!' ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'LOG; HIGH; MEDIUM' ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
2012-05-02 23:34:54 +00:00
it ( 'should compile template when appending' , inject ( function ( $compile , $rootScope , log ) {
element = $compile ( '<div><div append medium-log>ignore</div><div>' )
2011-11-29 20:11:32 +00:00
( $rootScope ) ;
$rootScope . $digest ( ) ;
2012-05-02 23:34:54 +00:00
expect ( element . text ( ) ) . toEqual ( 'Append!' ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'LOG; HIGH; MEDIUM' ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
2012-03-20 00:26:05 +00:00
it ( 'should merge attributes including style attr' , inject ( function ( $compile , $rootScope ) {
2011-11-29 20:11:32 +00:00
element = $compile (
'<div><div replace class="medium-log" style="height: 20px" ></div><div>' )
( $rootScope ) ;
var div = element . find ( 'div' ) ;
expect ( div . hasClass ( 'medium-log' ) ) . toBe ( true ) ;
expect ( div . hasClass ( 'log' ) ) . toBe ( true ) ;
expect ( div . css ( 'width' ) ) . toBe ( '10px' ) ;
expect ( div . css ( 'height' ) ) . toBe ( '20px' ) ;
2013-07-03 00:15:24 +00:00
expect ( div . attr ( 'replace' ) ) . toEqual ( '' ) ;
2011-11-29 20:11:32 +00:00
expect ( div . attr ( 'high-log' ) ) . toEqual ( '' ) ;
} ) ) ;
it ( 'should prevent multiple templates per element' , inject ( function ( $compile ) {
try {
2013-04-23 04:00:15 +00:00
$compile ( '<div><span replace class="replace"></span></div>' ) ;
this . fail ( new Error ( 'should have thrown Multiple directives error' ) ) ;
2011-11-29 20:11:32 +00:00
} catch ( e ) {
expect ( e . message ) . toMatch ( /Multiple directives .* asking for template/ ) ;
}
} ) ) ;
2012-05-02 23:34:54 +00:00
it ( 'should play nice with repeater when replacing' , inject ( function ( $compile , $rootScope ) {
2011-11-29 20:11:32 +00:00
element = $compile (
'<div>' +
2012-05-02 23:34:54 +00:00
'<div ng-repeat="i in [1,2]" replace></div>' +
2011-11-29 20:11:32 +00:00
'</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
2012-05-02 23:34:54 +00:00
expect ( element . text ( ) ) . toEqual ( 'Replace!Replace!' ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
2012-05-02 23:34:54 +00:00
it ( 'should play nice with repeater when appending' , inject ( function ( $compile , $rootScope ) {
2011-11-29 20:11:32 +00:00
element = $compile (
'<div>' +
2012-05-02 23:34:54 +00:00
'<div ng-repeat="i in [1,2]" append></div>' +
2011-11-29 20:11:32 +00:00
'</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
2012-05-02 23:34:54 +00:00
expect ( element . text ( ) ) . toEqual ( 'Append!Append!' ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
2012-03-20 00:26:05 +00:00
2013-11-12 23:53:06 +00:00
it ( 'should handle interpolated css class from replacing directive' , inject (
2012-06-06 14:23:07 +00:00
function ( $compile , $rootScope ) {
element = $compile ( '<div replace-with-interpolated-class></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element ) . toHaveClass ( 'class_2' ) ;
} ) ) ;
2013-11-21 22:30:50 +00:00
if ( ! msie || msie > 11 ) {
// style interpolation not working on IE (including IE11).
2013-11-12 23:53:06 +00:00
it ( 'should handle interpolated css style from replacing directive' , inject (
function ( $compile , $rootScope ) {
element = $compile ( '<div replace-with-interpolated-style></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . css ( 'width' ) ) . toBe ( '2px' ) ;
} ) ) ;
}
2012-06-06 14:23:07 +00:00
2012-03-20 00:26:05 +00:00
it ( 'should merge interpolated css class' , inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div class="one {{cls}} three" replace></div>' ) ( $rootScope ) ;
$rootScope . $apply ( function ( ) {
$rootScope . cls = 'two' ;
} ) ;
expect ( element ) . toHaveClass ( 'one' ) ;
expect ( element ) . toHaveClass ( 'two' ) ; // interpolated
expect ( element ) . toHaveClass ( 'three' ) ;
expect ( element ) . toHaveClass ( 'log' ) ; // merged from replace directive template
} ) ) ;
2012-04-09 18:20:55 +00:00
it ( 'should merge interpolated css class with ngRepeat' ,
2012-03-20 00:26:05 +00:00
inject ( function ( $compile , $rootScope ) {
element = $compile (
'<div>' +
'<div ng-repeat="i in [1]" class="one {{cls}} three" replace></div>' +
'</div>' ) ( $rootScope ) ;
$rootScope . $apply ( function ( ) {
$rootScope . cls = 'two' ;
} ) ;
var child = element . find ( 'div' ) . eq ( 0 ) ;
expect ( child ) . toHaveClass ( 'one' ) ;
expect ( child ) . toHaveClass ( 'two' ) ; // interpolated
expect ( child ) . toHaveClass ( 'three' ) ;
expect ( child ) . toHaveClass ( 'log' ) ; // merged from replace directive template
} ) ) ;
2012-05-04 08:24:46 +00:00
it ( "should fail if replacing and template doesn't have a single root element" , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'noRootElem' , function ( ) {
2012-05-04 08:24:46 +00:00
return {
replace : true ,
template : 'dada'
}
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'multiRootElem' , function ( ) {
2012-05-04 08:24:46 +00:00
return {
replace : true ,
template : '<div></div><div></div>'
}
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'singleRootWithWhiteSpace' , function ( ) {
2012-05-04 08:24:46 +00:00
return {
replace : true ,
template : ' <div></div> \n'
}
} ) ;
} ) ;
inject ( function ( $compile ) {
expect ( function ( ) {
$compile ( '<p no-root-elem></p>' ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr ( "$compile" , "tplrt" , "Template for directive 'noRootElem' must have exactly one root element. " ) ;
2012-05-04 08:24:46 +00:00
expect ( function ( ) {
$compile ( '<p multi-root-elem></p>' ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr ( "$compile" , "tplrt" , "Template for directive 'multiRootElem' must have exactly one root element. " ) ;
2012-05-04 08:24:46 +00:00
// ws is ok
expect ( function ( ) {
$compile ( '<p single-root-with-white-space></p>' ) ;
} ) . not . toThrow ( ) ;
} ) ;
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
2013-02-21 20:56:40 +00:00
describe ( 'template as function' , function ( ) {
beforeEach ( module ( function ( ) {
directive ( 'myDirective' , valueFn ( {
replace : true ,
template : function ( $element , $attrs ) {
expect ( $element . text ( ) ) . toBe ( 'original content' ) ;
expect ( $attrs . myDirective ) . toBe ( 'some value' ) ;
return '<div id="templateContent">template content</div>' ;
} ,
compile : function ( $element , $attrs ) {
expect ( $element . text ( ) ) . toBe ( 'template content' ) ;
expect ( $attrs . id ) . toBe ( 'templateContent' ) ;
}
} ) ) ;
} ) ) ;
it ( 'should evaluate `template` when defined as fn and use returned string as template' , inject (
function ( $compile , $rootScope ) {
element = $compile ( '<div my-directive="some value">original content<div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'template content' ) ;
} ) ) ;
} ) ;
2012-05-04 08:24:46 +00:00
describe ( 'templateUrl' , function ( ) {
2011-11-29 20:11:32 +00:00
beforeEach ( module (
2012-06-04 19:08:27 +00:00
function ( ) {
directive ( 'hello' , valueFn ( {
2012-05-02 23:34:54 +00:00
restrict : 'CAM' , templateUrl : 'hello.html' , transclude : true
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'cau' , valueFn ( {
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
restrict : 'CAM' , templateUrl : 'cau.html'
2012-05-02 23:34:54 +00:00
} ) ) ;
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
directive ( 'crossDomainTemplate' , valueFn ( {
restrict : 'CAM' , templateUrl : 'http://example.com/should-not-load.html'
} ) ) ;
directive ( 'trustedTemplate' , function ( $sce ) { return {
restrict : 'CAM' ,
templateUrl : function ( ) {
return $sce . trustAsResourceUrl ( 'http://example.com/trusted-template.html' ) ;
} } ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'cError' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2011-11-29 20:11:32 +00:00
templateUrl : 'error.html' ,
compile : function ( ) {
throw Error ( 'cError' ) ;
}
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'lError' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2011-11-29 20:11:32 +00:00
templateUrl : 'error.html' ,
compile : function ( ) {
throw Error ( 'lError' ) ;
}
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'iHello' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2011-11-29 20:11:32 +00:00
replace : true ,
templateUrl : 'hello.html'
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'iCau' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2011-11-29 20:11:32 +00:00
replace : true ,
templateUrl : 'cau.html'
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'iCError' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2011-11-29 20:11:32 +00:00
replace : true ,
templateUrl : 'error.html' ,
compile : function ( ) {
throw Error ( 'cError' ) ;
}
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'iLError' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'CAM' ,
2011-11-29 20:11:32 +00:00
replace : true ,
templateUrl : 'error.html' ,
compile : function ( ) {
throw Error ( 'lError' ) ;
}
} ) ) ;
2013-01-22 15:59:09 +00:00
directive ( 'replace' , valueFn ( {
replace : true ,
template : '<span>Hello, {{name}}!</span>'
} ) ) ;
2011-11-29 20:11:32 +00:00
}
) ) ;
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
it ( 'should not load cross domain templates by default' , inject (
function ( $compile , $rootScope , $templateCache , $sce ) {
expect ( function ( ) {
$templateCache . put ( 'http://example.com/should-not-load.html' , 'Should not load even if in cache.' ) ;
$compile ( '<div class="crossDomainTemplate"></div>' ) ( $rootScope ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr ( '$sce' , 'insecurl' , 'Blocked loading resource from url not allowed by $sceDelegate policy. URL: http://example.com/should-not-load.html' ) ;
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
} ) ) ;
it ( 'should load cross domain templates when trusted' , inject (
function ( $compile , $httpBackend , $rootScope , $sce ) {
$httpBackend . expect ( 'GET' , 'http://example.com/trusted-template.html' ) . respond ( '<span>example.com/trusted_template_contents</span>' ) ;
element = $compile ( '<div class="trustedTemplate"></div>' ) ( $rootScope ) ;
expect ( sortedHtml ( element ) ) .
toEqual ( '<div class="trustedTemplate"></div>' ) ;
$httpBackend . flush ( ) ;
expect ( sortedHtml ( element ) ) .
toEqual ( '<div class="trustedTemplate"><span>example.com/trusted_template_contents</span></div>' ) ;
} ) ) ;
2011-11-29 20:11:32 +00:00
it ( 'should append template via $http and cache it in $templateCache' , inject (
function ( $compile , $httpBackend , $templateCache , $rootScope , $browser ) {
$httpBackend . expect ( 'GET' , 'hello.html' ) . respond ( '<span>Hello!</span> World!' ) ;
$templateCache . put ( 'cau.html' , '<span>Cau!</span>' ) ;
element = $compile ( '<div><b class="hello">ignore</b><b class="cau">ignore</b></div>' ) ( $rootScope ) ;
expect ( sortedHtml ( element ) ) .
toEqual ( '<div><b class="hello"></b><b class="cau"></b></div>' ) ;
$rootScope . $digest ( ) ;
expect ( sortedHtml ( element ) ) .
toEqual ( '<div><b class="hello"></b><b class="cau"><span>Cau!</span></b></div>' ) ;
$httpBackend . flush ( ) ;
expect ( sortedHtml ( element ) ) . toEqual (
'<div>' +
'<b class="hello"><span>Hello!</span> World!</b>' +
'<b class="cau"><span>Cau!</span></b>' +
'</div>' ) ;
}
) ) ;
it ( 'should inline template via $http and cache it in $templateCache' , inject (
function ( $compile , $httpBackend , $templateCache , $rootScope ) {
$httpBackend . expect ( 'GET' , 'hello.html' ) . respond ( '<span>Hello!</span>' ) ;
$templateCache . put ( 'cau.html' , '<span>Cau!</span>' ) ;
element = $compile ( '<div><b class=i-hello>ignore</b><b class=i-cau>ignore</b></div>' ) ( $rootScope ) ;
expect ( sortedHtml ( element ) ) .
toEqual ( '<div><b class="i-hello"></b><b class="i-cau"></b></div>' ) ;
$rootScope . $digest ( ) ;
2013-02-26 04:43:27 +00:00
expect ( sortedHtml ( element ) ) . toBeOneOf (
'<div><b class="i-hello"></b><span class="i-cau">Cau!</span></div>' ,
'<div><b class="i-hello"></b><span class="i-cau" i-cau="">Cau!</span></div>' //ie8
) ;
2011-11-29 20:11:32 +00:00
$httpBackend . flush ( ) ;
2013-02-26 04:43:27 +00:00
expect ( sortedHtml ( element ) ) . toBeOneOf (
'<div><span class="i-hello">Hello!</span><span class="i-cau">Cau!</span></div>' ,
'<div><span class="i-hello" i-hello="">Hello!</span><span class="i-cau" i-cau="">Cau!</span></div>' //ie8
) ;
2011-11-29 20:11:32 +00:00
}
) ) ;
it ( 'should compile, link and flush the template append' , inject (
function ( $compile , $templateCache , $rootScope , $browser ) {
$templateCache . put ( 'hello.html' , '<span>Hello, {{name}}!</span>' ) ;
$rootScope . name = 'Elvis' ;
element = $compile ( '<div><b class="hello"></b></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( sortedHtml ( element ) ) .
toEqual ( '<div><b class="hello"><span>Hello, Elvis!</span></b></div>' ) ;
}
) ) ;
it ( 'should compile, link and flush the template inline' , inject (
function ( $compile , $templateCache , $rootScope ) {
$templateCache . put ( 'hello.html' , '<span>Hello, {{name}}!</span>' ) ;
$rootScope . name = 'Elvis' ;
element = $compile ( '<div><b class=i-hello></b></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
2013-02-26 04:43:27 +00:00
expect ( sortedHtml ( element ) ) . toBeOneOf (
'<div><span class="i-hello">Hello, Elvis!</span></div>' ,
'<div><span class="i-hello" i-hello="">Hello, Elvis!</span></div>' //ie8
) ;
2011-11-29 20:11:32 +00:00
}
) ) ;
it ( 'should compile, flush and link the template append' , inject (
function ( $compile , $templateCache , $rootScope ) {
$templateCache . put ( 'hello.html' , '<span>Hello, {{name}}!</span>' ) ;
$rootScope . name = 'Elvis' ;
var template = $compile ( '<div><b class="hello"></b></div>' ) ;
element = template ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( sortedHtml ( element ) ) .
toEqual ( '<div><b class="hello"><span>Hello, Elvis!</span></b></div>' ) ;
}
) ) ;
it ( 'should compile, flush and link the template inline' , inject (
function ( $compile , $templateCache , $rootScope ) {
$templateCache . put ( 'hello.html' , '<span>Hello, {{name}}!</span>' ) ;
$rootScope . name = 'Elvis' ;
var template = $compile ( '<div><b class=i-hello></b></div>' ) ;
element = template ( $rootScope ) ;
$rootScope . $digest ( ) ;
2013-02-26 04:43:27 +00:00
expect ( sortedHtml ( element ) ) . toBeOneOf (
'<div><span class="i-hello">Hello, Elvis!</span></div>' ,
'<div><span class="i-hello" i-hello="">Hello, Elvis!</span></div>' //ie8
) ;
2011-11-29 20:11:32 +00:00
}
) ) ;
2013-01-22 15:59:09 +00:00
it ( 'should compile template when replacing element in another template' ,
inject ( function ( $compile , $templateCache , $rootScope ) {
$templateCache . put ( 'hello.html' , '<div replace></div>' ) ;
$rootScope . name = 'Elvis' ;
element = $compile ( '<div><b class="hello"></b></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( sortedHtml ( element ) ) .
2013-07-03 00:15:24 +00:00
toEqual ( '<div><b class="hello"><span replace="">Hello, Elvis!</span></b></div>' ) ;
2013-01-22 15:59:09 +00:00
} ) ) ;
it ( 'should compile template when replacing root element' ,
inject ( function ( $compile , $templateCache , $rootScope ) {
$rootScope . name = 'Elvis' ;
element = $compile ( '<div replace></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( sortedHtml ( element ) ) .
2013-07-03 00:15:24 +00:00
toEqual ( '<span replace="">Hello, Elvis!</span>' ) ;
2013-01-22 15:59:09 +00:00
} ) ) ;
2011-11-29 20:11:32 +00:00
it ( 'should resolve widgets after cloning in append mode' , function ( ) {
module ( function ( $exceptionHandlerProvider ) {
$exceptionHandlerProvider . mode ( 'log' ) ;
} ) ;
inject ( function ( $compile , $templateCache , $rootScope , $httpBackend , $browser ,
$exceptionHandler ) {
$httpBackend . expect ( 'GET' , 'hello.html' ) . respond ( '<span>{{greeting}} </span>' ) ;
$httpBackend . expect ( 'GET' , 'error.html' ) . respond ( '<div></div>' ) ;
$templateCache . put ( 'cau.html' , '<span>{{name}}</span>' ) ;
$rootScope . greeting = 'Hello' ;
$rootScope . name = 'Elvis' ;
var template = $compile (
'<div>' +
'<b class="hello"></b>' +
'<b class="cau"></b>' +
'<b class=c-error></b>' +
'<b class=l-error></b>' +
2011-10-07 21:11:32 +00:00
'</div>' ) ;
2011-11-29 20:11:32 +00:00
var e1 ;
var e2 ;
e1 = template ( $rootScope . $new ( ) , noop ) ; // clone
expect ( e1 . text ( ) ) . toEqual ( '' ) ;
$httpBackend . flush ( ) ;
e2 = template ( $rootScope . $new ( ) , noop ) ; // clone
$rootScope . $digest ( ) ;
expect ( e1 . text ( ) ) . toEqual ( 'Hello Elvis' ) ;
expect ( e2 . text ( ) ) . toEqual ( 'Hello Elvis' ) ;
expect ( $exceptionHandler . errors . length ) . toEqual ( 2 ) ;
expect ( $exceptionHandler . errors [ 0 ] [ 0 ] . message ) . toEqual ( 'cError' ) ;
expect ( $exceptionHandler . errors [ 1 ] [ 0 ] . message ) . toEqual ( 'lError' ) ;
dealoc ( e1 ) ;
dealoc ( e2 ) ;
} ) ;
} ) ;
it ( 'should resolve widgets after cloning in inline mode' , function ( ) {
module ( function ( $exceptionHandlerProvider ) {
$exceptionHandlerProvider . mode ( 'log' ) ;
} ) ;
inject ( function ( $compile , $templateCache , $rootScope , $httpBackend , $browser ,
$exceptionHandler ) {
$httpBackend . expect ( 'GET' , 'hello.html' ) . respond ( '<span>{{greeting}} </span>' ) ;
$httpBackend . expect ( 'GET' , 'error.html' ) . respond ( '<div></div>' ) ;
$templateCache . put ( 'cau.html' , '<span>{{name}}</span>' ) ;
$rootScope . greeting = 'Hello' ;
$rootScope . name = 'Elvis' ;
var template = $compile (
'<div>' +
'<b class=i-hello></b>' +
'<b class=i-cau></b>' +
'<b class=i-c-error></b>' +
'<b class=i-l-error></b>' +
'</div>' ) ;
var e1 ;
var e2 ;
e1 = template ( $rootScope . $new ( ) , noop ) ; // clone
expect ( e1 . text ( ) ) . toEqual ( '' ) ;
$httpBackend . flush ( ) ;
e2 = template ( $rootScope . $new ( ) , noop ) ; // clone
$rootScope . $digest ( ) ;
expect ( e1 . text ( ) ) . toEqual ( 'Hello Elvis' ) ;
expect ( e2 . text ( ) ) . toEqual ( 'Hello Elvis' ) ;
expect ( $exceptionHandler . errors . length ) . toEqual ( 2 ) ;
expect ( $exceptionHandler . errors [ 0 ] [ 0 ] . message ) . toEqual ( 'cError' ) ;
expect ( $exceptionHandler . errors [ 1 ] [ 0 ] . message ) . toEqual ( 'lError' ) ;
dealoc ( e1 ) ;
dealoc ( e2 ) ;
} ) ;
} ) ;
it ( 'should be implicitly terminal and not compile placeholder content in append' , inject (
function ( $compile , $templateCache , $rootScope , log ) {
// we can't compile the contents because that would result in a memory leak
$templateCache . put ( 'hello.html' , 'Hello!' ) ;
element = $compile ( '<div><b class="hello"><div log></div></b></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( '' ) ;
}
) ) ;
it ( 'should be implicitly terminal and not compile placeholder content in inline' , inject (
function ( $compile , $templateCache , $rootScope , log ) {
// we can't compile the contents because that would result in a memory leak
$templateCache . put ( 'hello.html' , 'Hello!' ) ;
element = $compile ( '<div><b class=i-hello><div log></div></b></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( '' ) ;
}
) ) ;
it ( 'should throw an error and clear element content if the template fails to load' , inject (
function ( $compile , $httpBackend , $rootScope ) {
$httpBackend . expect ( 'GET' , 'hello.html' ) . respond ( 404 , 'Not Found!' ) ;
element = $compile ( '<div><b class="hello">content</b></div>' ) ( $rootScope ) ;
expect ( function ( ) {
$httpBackend . flush ( ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr ( '$compile' , 'tpload' , 'Failed to load template: hello.html' ) ;
2011-11-29 20:11:32 +00:00
expect ( sortedHtml ( element ) ) . toBe ( '<div><b class="hello"></b></div>' ) ;
}
) ) ;
it ( 'should prevent multiple templates per element' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'sync' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'C' ,
2011-11-29 20:11:32 +00:00
template : '<span></span>'
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'async' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'C' ,
2011-11-29 20:11:32 +00:00
templateUrl : 'template.html'
} ) ) ;
} ) ;
2013-10-11 04:35:54 +00:00
inject ( function ( $compile , $httpBackend ) {
$httpBackend . whenGET ( 'template.html' ) . respond ( '<p>template.html</p>' ) ;
2011-11-29 20:11:32 +00:00
expect ( function ( ) {
$compile ( '<div><div class="sync async"></div></div>' ) ;
2013-10-11 04:35:54 +00:00
$httpBackend . flush ( ) ;
} ) . toThrowMinErr ( '$compile' , 'multidir' , 'Multiple directives [async, sync] asking for template on: ' +
2012-06-08 18:53:17 +00:00
'<div class="sync async">' ) ;
2011-11-29 20:11:32 +00:00
} ) ;
} ) ;
describe ( 'delay compile / linking functions until after template is resolved' , function ( ) {
var template ;
2012-06-04 19:08:27 +00:00
beforeEach ( module ( function ( ) {
function logDirective ( name , priority , options ) {
directive ( name , function ( log ) {
2011-11-29 20:11:32 +00:00
return ( extend ( {
priority : priority ,
compile : function ( ) {
log ( name + '-C' ) ;
fix($compile): link parents before traversing
How did compiling a templateUrl (async) directive with `replace:true` work before this commit?
1/ apply all directives with higher priority than the templateUrl directive
2/ partially apply the templateUrl directive (create `beforeTemplateNodeLinkFn`)
3/ fetch the template
4/ apply second part of the templateUrl directive on the fetched template
(`afterTemplateNodeLinkFn`)
That is, the templateUrl directive is basically split into two parts (two `nodeLinkFn` functions),
which has to be both applied.
Normally we compose linking functions (`nodeLinkFn`) using continuation - calling the linking
function of a parent element, passing the linking function of the child elements as an argument. The
parent linking function then does:
1/ execute its pre-link functions
2/ call the child elements linking function (traverse)
3/ execute its post-link functions
Now, we have two linking functions for the same DOM element level (because the templateUrl directive
has been split).
There has been multiple issues because of the order of these two linking functions (creating
controller before setting up scope locals, running linking functions before instantiating
controller, etc.). It is easy to fix one use case, but it breaks some other use case. It is hard to
decide what is the "correct" order of these two linking functions as they are essentially on the
same level.
Running them side-by-side screws up pre/post linking functions for the high priority directives
(those executed before the templateUrl directive). It runs post-linking functions before traversing:
```js
beforeTemplateNodeLinkFn(null); // do not travers
afterTemplateNodeLinkFn(afterTemplateChildLinkFn);
```
Composing them (in any order) screws up the order of post-linking functions. We could fix this by
having post-linking functions to execute in reverse order (from the lowest priority to the highest)
which might actually make a sense.
**My solution is to remove this splitting.** This commit removes the `beforeTemplateNodeLinkFn`. The
first run (before we have the template) only schedules fetching the template. The rest (creating
scope locals, instantiating a controller, linking functions, etc) is done when processing the
directive again (in the context of the already fetched template; this is the cloned
`derivedSyncDirective`).
We still need to pass-through the linking functions of the higher priority directives (those
executed before the templateUrl directive), that's why I added `preLinkFns` and `postLinkFns`
arguments to `applyDirectivesToNode`.
This also changes the "$compile transclude should make the result of a transclusion available to the
parent directive in post- linking phase (templateUrl)" unit test. It was testing that a parent
directive can see the content of transclusion in its pre-link function. That is IMHO wrong (as the
`ngTransclude` directive inserts the translusion in its linking function). This test was only passing because of
c173ca412878d537b18df01f39e400ea48a4b398, which changed the behavior of the compiler to traverse
before executing the parent linking function. That was wrong and also caused the #3792 issue, which
this change fixes.
Closes #3792
Closes #3923
Closes #3935
Closes #3927
2013-09-08 17:05:25 +00:00
return {
pre : function ( ) { log ( name + '-PreL' ) ; } ,
post : function ( ) { log ( name + '-PostL' ) ; }
}
2011-11-29 20:11:32 +00:00
}
} , options || { } ) ) ;
} ) ;
}
2012-06-04 19:08:27 +00:00
logDirective ( 'first' , 10 ) ;
logDirective ( 'second' , 5 , { templateUrl : 'second.html' } ) ;
logDirective ( 'third' , 3 ) ;
logDirective ( 'last' , 0 ) ;
2011-11-29 20:11:32 +00:00
2012-06-04 19:08:27 +00:00
logDirective ( 'iFirst' , 10 , { replace : true } ) ;
logDirective ( 'iSecond' , 5 , { replace : true , templateUrl : 'second.html' } ) ;
logDirective ( 'iThird' , 3 , { replace : true } ) ;
logDirective ( 'iLast' , 0 , { replace : true } ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
it ( 'should flush after link append' , inject (
function ( $compile , $rootScope , $httpBackend , log ) {
$httpBackend . expect ( 'GET' , 'second.html' ) . respond ( '<div third>{{1+2}}</div>' ) ;
template = $compile ( '<div><span first second last></span></div>' ) ;
element = template ( $rootScope ) ;
expect ( log ) . toEqual ( 'first-C' ) ;
log ( 'FLUSH' ) ;
$httpBackend . flush ( ) ;
$rootScope . $digest ( ) ;
expect ( log ) . toEqual (
'first-C; FLUSH; second-C; last-C; third-C; ' +
fix($compile): link parents before traversing
How did compiling a templateUrl (async) directive with `replace:true` work before this commit?
1/ apply all directives with higher priority than the templateUrl directive
2/ partially apply the templateUrl directive (create `beforeTemplateNodeLinkFn`)
3/ fetch the template
4/ apply second part of the templateUrl directive on the fetched template
(`afterTemplateNodeLinkFn`)
That is, the templateUrl directive is basically split into two parts (two `nodeLinkFn` functions),
which has to be both applied.
Normally we compose linking functions (`nodeLinkFn`) using continuation - calling the linking
function of a parent element, passing the linking function of the child elements as an argument. The
parent linking function then does:
1/ execute its pre-link functions
2/ call the child elements linking function (traverse)
3/ execute its post-link functions
Now, we have two linking functions for the same DOM element level (because the templateUrl directive
has been split).
There has been multiple issues because of the order of these two linking functions (creating
controller before setting up scope locals, running linking functions before instantiating
controller, etc.). It is easy to fix one use case, but it breaks some other use case. It is hard to
decide what is the "correct" order of these two linking functions as they are essentially on the
same level.
Running them side-by-side screws up pre/post linking functions for the high priority directives
(those executed before the templateUrl directive). It runs post-linking functions before traversing:
```js
beforeTemplateNodeLinkFn(null); // do not travers
afterTemplateNodeLinkFn(afterTemplateChildLinkFn);
```
Composing them (in any order) screws up the order of post-linking functions. We could fix this by
having post-linking functions to execute in reverse order (from the lowest priority to the highest)
which might actually make a sense.
**My solution is to remove this splitting.** This commit removes the `beforeTemplateNodeLinkFn`. The
first run (before we have the template) only schedules fetching the template. The rest (creating
scope locals, instantiating a controller, linking functions, etc) is done when processing the
directive again (in the context of the already fetched template; this is the cloned
`derivedSyncDirective`).
We still need to pass-through the linking functions of the higher priority directives (those
executed before the templateUrl directive), that's why I added `preLinkFns` and `postLinkFns`
arguments to `applyDirectivesToNode`.
This also changes the "$compile transclude should make the result of a transclusion available to the
parent directive in post- linking phase (templateUrl)" unit test. It was testing that a parent
directive can see the content of transclusion in its pre-link function. That is IMHO wrong (as the
`ngTransclude` directive inserts the translusion in its linking function). This test was only passing because of
c173ca412878d537b18df01f39e400ea48a4b398, which changed the behavior of the compiler to traverse
before executing the parent linking function. That was wrong and also caused the #3792 issue, which
this change fixes.
Closes #3792
Closes #3923
Closes #3935
Closes #3927
2013-09-08 17:05:25 +00:00
'first-PreL; second-PreL; last-PreL; third-PreL; ' +
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
'third-PostL; last-PostL; second-PostL; first-PostL' ) ;
2011-11-29 20:11:32 +00:00
var span = element . find ( 'span' ) ;
expect ( span . attr ( 'first' ) ) . toEqual ( '' ) ;
expect ( span . attr ( 'second' ) ) . toEqual ( '' ) ;
expect ( span . find ( 'div' ) . attr ( 'third' ) ) . toEqual ( '' ) ;
expect ( span . attr ( 'last' ) ) . toEqual ( '' ) ;
expect ( span . text ( ) ) . toEqual ( '3' ) ;
} ) ) ;
it ( 'should flush after link inline' , inject (
function ( $compile , $rootScope , $httpBackend , log ) {
$httpBackend . expect ( 'GET' , 'second.html' ) . respond ( '<div i-third>{{1+2}}</div>' ) ;
template = $compile ( '<div><span i-first i-second i-last></span></div>' ) ;
element = template ( $rootScope ) ;
expect ( log ) . toEqual ( 'iFirst-C' ) ;
log ( 'FLUSH' ) ;
$httpBackend . flush ( ) ;
$rootScope . $digest ( ) ;
expect ( log ) . toEqual (
'iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C; ' +
fix($compile): link parents before traversing
How did compiling a templateUrl (async) directive with `replace:true` work before this commit?
1/ apply all directives with higher priority than the templateUrl directive
2/ partially apply the templateUrl directive (create `beforeTemplateNodeLinkFn`)
3/ fetch the template
4/ apply second part of the templateUrl directive on the fetched template
(`afterTemplateNodeLinkFn`)
That is, the templateUrl directive is basically split into two parts (two `nodeLinkFn` functions),
which has to be both applied.
Normally we compose linking functions (`nodeLinkFn`) using continuation - calling the linking
function of a parent element, passing the linking function of the child elements as an argument. The
parent linking function then does:
1/ execute its pre-link functions
2/ call the child elements linking function (traverse)
3/ execute its post-link functions
Now, we have two linking functions for the same DOM element level (because the templateUrl directive
has been split).
There has been multiple issues because of the order of these two linking functions (creating
controller before setting up scope locals, running linking functions before instantiating
controller, etc.). It is easy to fix one use case, but it breaks some other use case. It is hard to
decide what is the "correct" order of these two linking functions as they are essentially on the
same level.
Running them side-by-side screws up pre/post linking functions for the high priority directives
(those executed before the templateUrl directive). It runs post-linking functions before traversing:
```js
beforeTemplateNodeLinkFn(null); // do not travers
afterTemplateNodeLinkFn(afterTemplateChildLinkFn);
```
Composing them (in any order) screws up the order of post-linking functions. We could fix this by
having post-linking functions to execute in reverse order (from the lowest priority to the highest)
which might actually make a sense.
**My solution is to remove this splitting.** This commit removes the `beforeTemplateNodeLinkFn`. The
first run (before we have the template) only schedules fetching the template. The rest (creating
scope locals, instantiating a controller, linking functions, etc) is done when processing the
directive again (in the context of the already fetched template; this is the cloned
`derivedSyncDirective`).
We still need to pass-through the linking functions of the higher priority directives (those
executed before the templateUrl directive), that's why I added `preLinkFns` and `postLinkFns`
arguments to `applyDirectivesToNode`.
This also changes the "$compile transclude should make the result of a transclusion available to the
parent directive in post- linking phase (templateUrl)" unit test. It was testing that a parent
directive can see the content of transclusion in its pre-link function. That is IMHO wrong (as the
`ngTransclude` directive inserts the translusion in its linking function). This test was only passing because of
c173ca412878d537b18df01f39e400ea48a4b398, which changed the behavior of the compiler to traverse
before executing the parent linking function. That was wrong and also caused the #3792 issue, which
this change fixes.
Closes #3792
Closes #3923
Closes #3935
Closes #3927
2013-09-08 17:05:25 +00:00
'iFirst-PreL; iSecond-PreL; iThird-PreL; iLast-PreL; ' +
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
'iLast-PostL; iThird-PostL; iSecond-PostL; iFirst-PostL' ) ;
2011-11-29 20:11:32 +00:00
var div = element . find ( 'div' ) ;
expect ( div . attr ( 'i-first' ) ) . toEqual ( '' ) ;
2013-07-03 00:15:24 +00:00
expect ( div . attr ( 'i-second' ) ) . toEqual ( '' ) ;
2011-11-29 20:11:32 +00:00
expect ( div . attr ( 'i-third' ) ) . toEqual ( '' ) ;
expect ( div . attr ( 'i-last' ) ) . toEqual ( '' ) ;
expect ( div . text ( ) ) . toEqual ( '3' ) ;
} ) ) ;
it ( 'should flush before link append' , inject (
function ( $compile , $rootScope , $httpBackend , log ) {
$httpBackend . expect ( 'GET' , 'second.html' ) . respond ( '<div third>{{1+2}}</div>' ) ;
template = $compile ( '<div><span first second last></span></div>' ) ;
expect ( log ) . toEqual ( 'first-C' ) ;
log ( 'FLUSH' ) ;
$httpBackend . flush ( ) ;
expect ( log ) . toEqual ( 'first-C; FLUSH; second-C; last-C; third-C' ) ;
element = template ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( log ) . toEqual (
'first-C; FLUSH; second-C; last-C; third-C; ' +
fix($compile): link parents before traversing
How did compiling a templateUrl (async) directive with `replace:true` work before this commit?
1/ apply all directives with higher priority than the templateUrl directive
2/ partially apply the templateUrl directive (create `beforeTemplateNodeLinkFn`)
3/ fetch the template
4/ apply second part of the templateUrl directive on the fetched template
(`afterTemplateNodeLinkFn`)
That is, the templateUrl directive is basically split into two parts (two `nodeLinkFn` functions),
which has to be both applied.
Normally we compose linking functions (`nodeLinkFn`) using continuation - calling the linking
function of a parent element, passing the linking function of the child elements as an argument. The
parent linking function then does:
1/ execute its pre-link functions
2/ call the child elements linking function (traverse)
3/ execute its post-link functions
Now, we have two linking functions for the same DOM element level (because the templateUrl directive
has been split).
There has been multiple issues because of the order of these two linking functions (creating
controller before setting up scope locals, running linking functions before instantiating
controller, etc.). It is easy to fix one use case, but it breaks some other use case. It is hard to
decide what is the "correct" order of these two linking functions as they are essentially on the
same level.
Running them side-by-side screws up pre/post linking functions for the high priority directives
(those executed before the templateUrl directive). It runs post-linking functions before traversing:
```js
beforeTemplateNodeLinkFn(null); // do not travers
afterTemplateNodeLinkFn(afterTemplateChildLinkFn);
```
Composing them (in any order) screws up the order of post-linking functions. We could fix this by
having post-linking functions to execute in reverse order (from the lowest priority to the highest)
which might actually make a sense.
**My solution is to remove this splitting.** This commit removes the `beforeTemplateNodeLinkFn`. The
first run (before we have the template) only schedules fetching the template. The rest (creating
scope locals, instantiating a controller, linking functions, etc) is done when processing the
directive again (in the context of the already fetched template; this is the cloned
`derivedSyncDirective`).
We still need to pass-through the linking functions of the higher priority directives (those
executed before the templateUrl directive), that's why I added `preLinkFns` and `postLinkFns`
arguments to `applyDirectivesToNode`.
This also changes the "$compile transclude should make the result of a transclusion available to the
parent directive in post- linking phase (templateUrl)" unit test. It was testing that a parent
directive can see the content of transclusion in its pre-link function. That is IMHO wrong (as the
`ngTransclude` directive inserts the translusion in its linking function). This test was only passing because of
c173ca412878d537b18df01f39e400ea48a4b398, which changed the behavior of the compiler to traverse
before executing the parent linking function. That was wrong and also caused the #3792 issue, which
this change fixes.
Closes #3792
Closes #3923
Closes #3935
Closes #3927
2013-09-08 17:05:25 +00:00
'first-PreL; second-PreL; last-PreL; third-PreL; ' +
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
'third-PostL; last-PostL; second-PostL; first-PostL' ) ;
2011-11-29 20:11:32 +00:00
var span = element . find ( 'span' ) ;
expect ( span . attr ( 'first' ) ) . toEqual ( '' ) ;
expect ( span . attr ( 'second' ) ) . toEqual ( '' ) ;
expect ( span . find ( 'div' ) . attr ( 'third' ) ) . toEqual ( '' ) ;
expect ( span . attr ( 'last' ) ) . toEqual ( '' ) ;
expect ( span . text ( ) ) . toEqual ( '3' ) ;
} ) ) ;
it ( 'should flush before link inline' , inject (
function ( $compile , $rootScope , $httpBackend , log ) {
$httpBackend . expect ( 'GET' , 'second.html' ) . respond ( '<div i-third>{{1+2}}</div>' ) ;
template = $compile ( '<div><span i-first i-second i-last></span></div>' ) ;
expect ( log ) . toEqual ( 'iFirst-C' ) ;
log ( 'FLUSH' ) ;
$httpBackend . flush ( ) ;
expect ( log ) . toEqual ( 'iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C' ) ;
element = template ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( log ) . toEqual (
'iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C; ' +
fix($compile): link parents before traversing
How did compiling a templateUrl (async) directive with `replace:true` work before this commit?
1/ apply all directives with higher priority than the templateUrl directive
2/ partially apply the templateUrl directive (create `beforeTemplateNodeLinkFn`)
3/ fetch the template
4/ apply second part of the templateUrl directive on the fetched template
(`afterTemplateNodeLinkFn`)
That is, the templateUrl directive is basically split into two parts (two `nodeLinkFn` functions),
which has to be both applied.
Normally we compose linking functions (`nodeLinkFn`) using continuation - calling the linking
function of a parent element, passing the linking function of the child elements as an argument. The
parent linking function then does:
1/ execute its pre-link functions
2/ call the child elements linking function (traverse)
3/ execute its post-link functions
Now, we have two linking functions for the same DOM element level (because the templateUrl directive
has been split).
There has been multiple issues because of the order of these two linking functions (creating
controller before setting up scope locals, running linking functions before instantiating
controller, etc.). It is easy to fix one use case, but it breaks some other use case. It is hard to
decide what is the "correct" order of these two linking functions as they are essentially on the
same level.
Running them side-by-side screws up pre/post linking functions for the high priority directives
(those executed before the templateUrl directive). It runs post-linking functions before traversing:
```js
beforeTemplateNodeLinkFn(null); // do not travers
afterTemplateNodeLinkFn(afterTemplateChildLinkFn);
```
Composing them (in any order) screws up the order of post-linking functions. We could fix this by
having post-linking functions to execute in reverse order (from the lowest priority to the highest)
which might actually make a sense.
**My solution is to remove this splitting.** This commit removes the `beforeTemplateNodeLinkFn`. The
first run (before we have the template) only schedules fetching the template. The rest (creating
scope locals, instantiating a controller, linking functions, etc) is done when processing the
directive again (in the context of the already fetched template; this is the cloned
`derivedSyncDirective`).
We still need to pass-through the linking functions of the higher priority directives (those
executed before the templateUrl directive), that's why I added `preLinkFns` and `postLinkFns`
arguments to `applyDirectivesToNode`.
This also changes the "$compile transclude should make the result of a transclusion available to the
parent directive in post- linking phase (templateUrl)" unit test. It was testing that a parent
directive can see the content of transclusion in its pre-link function. That is IMHO wrong (as the
`ngTransclude` directive inserts the translusion in its linking function). This test was only passing because of
c173ca412878d537b18df01f39e400ea48a4b398, which changed the behavior of the compiler to traverse
before executing the parent linking function. That was wrong and also caused the #3792 issue, which
this change fixes.
Closes #3792
Closes #3923
Closes #3935
Closes #3927
2013-09-08 17:05:25 +00:00
'iFirst-PreL; iSecond-PreL; iThird-PreL; iLast-PreL; ' +
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
'iLast-PostL; iThird-PostL; iSecond-PostL; iFirst-PostL' ) ;
2011-11-29 20:11:32 +00:00
var div = element . find ( 'div' ) ;
expect ( div . attr ( 'i-first' ) ) . toEqual ( '' ) ;
2013-07-03 00:15:24 +00:00
expect ( div . attr ( 'i-second' ) ) . toEqual ( '' ) ;
2011-11-29 20:11:32 +00:00
expect ( div . attr ( 'i-third' ) ) . toEqual ( '' ) ;
expect ( div . attr ( 'i-last' ) ) . toEqual ( '' ) ;
expect ( div . text ( ) ) . toEqual ( '3' ) ;
} ) ) ;
} ) ;
it ( 'should allow multiple elements in template' , inject ( function ( $compile , $httpBackend ) {
$httpBackend . expect ( 'GET' , 'hello.html' ) . respond ( 'before <b>mid</b> after' ) ;
element = jqLite ( '<div hello></div>' ) ;
$compile ( element ) ;
$httpBackend . flush ( ) ;
expect ( element . text ( ) ) . toEqual ( 'before mid after' ) ;
} ) ) ;
2012-05-02 23:34:54 +00:00
it ( 'should work when directive is on the root element' , inject (
2011-11-29 20:11:32 +00:00
function ( $compile , $httpBackend , $rootScope ) {
2012-05-02 23:34:54 +00:00
$httpBackend . expect ( 'GET' , 'hello.html' ) .
respond ( '<span>3==<span ng-transclude></span></span>' ) ;
2011-11-29 20:11:32 +00:00
element = jqLite ( '<b class="hello">{{1+2}}</b>' ) ;
$compile ( element ) ( $rootScope ) ;
$httpBackend . flush ( ) ;
expect ( element . text ( ) ) . toEqual ( '3==3' ) ;
}
) ) ;
2013-10-11 04:35:54 +00:00
it ( 'should work when directive is in a repeater' , inject (
2011-11-29 20:11:32 +00:00
function ( $compile , $httpBackend , $rootScope ) {
2012-05-02 23:34:54 +00:00
$httpBackend . expect ( 'GET' , 'hello.html' ) .
respond ( '<span>i=<span ng-transclude></span>;</span>' ) ;
2011-11-29 20:11:32 +00:00
element = jqLite ( '<div><b class=hello ng-repeat="i in [1,2]">{{i}}</b></div>' ) ;
$compile ( element ) ( $rootScope ) ;
$httpBackend . flush ( ) ;
expect ( element . text ( ) ) . toEqual ( 'i=1;i=2;' ) ;
}
) ) ;
2012-05-04 08:24:46 +00:00
it ( "should fail if replacing and template doesn't have a single root element" , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( $exceptionHandlerProvider ) {
2012-05-04 08:24:46 +00:00
$exceptionHandlerProvider . mode ( 'log' ) ;
2012-06-04 19:08:27 +00:00
directive ( 'template' , function ( ) {
2012-05-04 08:24:46 +00:00
return {
replace : true ,
templateUrl : 'template.html'
}
} ) ;
} ) ;
inject ( function ( $compile , $templateCache , $rootScope , $exceptionHandler ) {
// no root element
$templateCache . put ( 'template.html' , 'dada' ) ;
$compile ( '<p template></p>' ) ;
$rootScope . $digest ( ) ;
expect ( $exceptionHandler . errors . pop ( ) . message ) .
2013-08-13 22:30:52 +00:00
toMatch ( /\[\$compile:tplrt\] Template for directive 'template' must have exactly one root element\. template\.html/ ) ;
2012-05-04 08:24:46 +00:00
// multi root
$templateCache . put ( 'template.html' , '<div></div><div></div>' ) ;
$compile ( '<p template></p>' ) ;
$rootScope . $digest ( ) ;
expect ( $exceptionHandler . errors . pop ( ) . message ) .
2013-08-13 22:30:52 +00:00
toMatch ( /\[\$compile:tplrt\] Template for directive 'template' must have exactly one root element\. template\.html/ ) ;
2012-05-04 08:24:46 +00:00
// ws is ok
$templateCache . put ( 'template.html' , ' <div></div> \n' ) ;
$compile ( '<p template></p>' ) ;
$rootScope . $apply ( ) ;
expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
} ) ;
} ) ;
2012-10-25 07:33:36 +00:00
it ( 'should resume delayed compilation without duplicates when in a repeater' , function ( ) {
// this is a test for a regression
// scope creation, isolate watcher setup, controller instantiation, etc should happen
// only once even if we are dealing with delayed compilation of a node due to templateUrl
// and the template node is in a repeater
var controllerSpy = jasmine . createSpy ( 'controller' ) ;
module ( function ( $compileProvider ) {
$compileProvider . directive ( 'delayed' , valueFn ( {
controller : controllerSpy ,
templateUrl : 'delayed.html' ,
scope : {
title : '@'
}
} ) ) ;
} ) ;
inject ( function ( $templateCache , $compile , $rootScope ) {
$rootScope . coolTitle = 'boom!' ;
$templateCache . put ( 'delayed.html' , '<div>{{title}}</div>' ) ;
element = $compile (
'<div><div ng-repeat="i in [1,2]"><div delayed title="{{coolTitle + i}}"></div>|</div></div>'
) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( controllerSpy . callCount ) . toBe ( 2 ) ;
expect ( element . text ( ) ) . toBe ( 'boom!1|boom!2|' ) ;
} ) ;
} ) ;
fix($compile): link parents before traversing
How did compiling a templateUrl (async) directive with `replace:true` work before this commit?
1/ apply all directives with higher priority than the templateUrl directive
2/ partially apply the templateUrl directive (create `beforeTemplateNodeLinkFn`)
3/ fetch the template
4/ apply second part of the templateUrl directive on the fetched template
(`afterTemplateNodeLinkFn`)
That is, the templateUrl directive is basically split into two parts (two `nodeLinkFn` functions),
which has to be both applied.
Normally we compose linking functions (`nodeLinkFn`) using continuation - calling the linking
function of a parent element, passing the linking function of the child elements as an argument. The
parent linking function then does:
1/ execute its pre-link functions
2/ call the child elements linking function (traverse)
3/ execute its post-link functions
Now, we have two linking functions for the same DOM element level (because the templateUrl directive
has been split).
There has been multiple issues because of the order of these two linking functions (creating
controller before setting up scope locals, running linking functions before instantiating
controller, etc.). It is easy to fix one use case, but it breaks some other use case. It is hard to
decide what is the "correct" order of these two linking functions as they are essentially on the
same level.
Running them side-by-side screws up pre/post linking functions for the high priority directives
(those executed before the templateUrl directive). It runs post-linking functions before traversing:
```js
beforeTemplateNodeLinkFn(null); // do not travers
afterTemplateNodeLinkFn(afterTemplateChildLinkFn);
```
Composing them (in any order) screws up the order of post-linking functions. We could fix this by
having post-linking functions to execute in reverse order (from the lowest priority to the highest)
which might actually make a sense.
**My solution is to remove this splitting.** This commit removes the `beforeTemplateNodeLinkFn`. The
first run (before we have the template) only schedules fetching the template. The rest (creating
scope locals, instantiating a controller, linking functions, etc) is done when processing the
directive again (in the context of the already fetched template; this is the cloned
`derivedSyncDirective`).
We still need to pass-through the linking functions of the higher priority directives (those
executed before the templateUrl directive), that's why I added `preLinkFns` and `postLinkFns`
arguments to `applyDirectivesToNode`.
This also changes the "$compile transclude should make the result of a transclusion available to the
parent directive in post- linking phase (templateUrl)" unit test. It was testing that a parent
directive can see the content of transclusion in its pre-link function. That is IMHO wrong (as the
`ngTransclude` directive inserts the translusion in its linking function). This test was only passing because of
c173ca412878d537b18df01f39e400ea48a4b398, which changed the behavior of the compiler to traverse
before executing the parent linking function. That was wrong and also caused the #3792 issue, which
this change fixes.
Closes #3792
Closes #3923
Closes #3935
Closes #3927
2013-09-08 17:05:25 +00:00
it ( 'should support templateUrl with replace' , function ( ) {
// a regression https://github.com/angular/angular.js/issues/3792
module ( function ( $compileProvider ) {
$compileProvider . directive ( 'simple' , function ( ) {
return {
templateUrl : '/some.html' ,
replace : true
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , $rootScope , $compile ) {
$templateCache . put ( '/some.html' ,
'<div ng-switch="i">' +
'<div ng-switch-when="1">i = 1</div>' +
'<div ng-switch-default>I dont know what `i` is.</div>' +
'</div>' ) ;
element = $compile ( '<div simple></div>' ) ( $rootScope ) ;
$rootScope . $apply ( function ( ) {
$rootScope . i = 1 ;
} ) ;
expect ( element . html ( ) ) . toContain ( 'i = 1' ) ;
} ) ;
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
2013-10-11 04:35:54 +00:00
describe ( 'templateUrl as function' , function ( ) {
2013-02-21 20:56:40 +00:00
beforeEach ( module ( function ( ) {
directive ( 'myDirective' , valueFn ( {
replace : true ,
templateUrl : function ( $element , $attrs ) {
expect ( $element . text ( ) ) . toBe ( 'original content' ) ;
expect ( $attrs . myDirective ) . toBe ( 'some value' ) ;
return 'my-directive.html' ;
} ,
compile : function ( $element , $attrs ) {
expect ( $element . text ( ) ) . toBe ( 'template content' ) ;
expect ( $attrs . id ) . toBe ( 'templateContent' ) ;
}
} ) ) ;
} ) ) ;
it ( 'should evaluate `templateUrl` when defined as fn and use returned value as url' , inject (
function ( $compile , $rootScope , $templateCache ) {
$templateCache . put ( 'my-directive.html' , '<div id="templateContent">template content</span>' ) ;
element = $compile ( '<div my-directive="some value">original content<div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( '' ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toEqual ( 'template content' ) ;
} ) ) ;
} ) ;
2011-11-29 20:11:32 +00:00
describe ( 'scope' , function ( ) {
2012-01-28 00:18:16 +00:00
var iscope ;
2011-11-29 20:11:32 +00:00
2012-06-04 19:08:27 +00:00
beforeEach ( module ( function ( ) {
2011-11-29 20:11:32 +00:00
forEach ( [ '' , 'a' , 'b' ] , function ( name ) {
2012-06-04 19:08:27 +00:00
directive ( 'scope' + uppercase ( name ) , function ( log ) {
2011-11-29 20:11:32 +00:00
return {
scope : true ,
2012-03-08 06:47:01 +00:00
restrict : 'CA' ,
2011-11-29 20:11:32 +00:00
compile : function ( ) {
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
return { pre : function ( scope , element ) {
2011-11-29 20:11:32 +00:00
log ( scope . $id ) ;
expect ( element . data ( '$scope' ) ) . toBe ( scope ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} } ;
2011-11-29 20:11:32 +00:00
}
} ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'iscope' + uppercase ( name ) , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
scope : { } ,
2012-03-08 06:47:01 +00:00
restrict : 'CA' ,
2012-01-28 00:18:16 +00:00
compile : function ( ) {
return function ( scope , element ) {
iscope = scope ;
log ( scope . $id ) ;
2013-11-08 00:19:03 +00:00
expect ( element . data ( '$isolateScopeNoTemplate' ) ) . toBe ( scope ) ;
2012-01-28 00:18:16 +00:00
} ;
}
} ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'tscope' + uppercase ( name ) , function ( log ) {
2012-05-03 04:08:02 +00:00
return {
scope : true ,
restrict : 'CA' ,
templateUrl : 'tscope.html' ,
compile : function ( ) {
return function ( scope , element ) {
log ( scope . $id ) ;
expect ( element . data ( '$scope' ) ) . toBe ( scope ) ;
} ;
}
} ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'trscope' + uppercase ( name ) , function ( log ) {
2012-05-03 04:08:02 +00:00
return {
scope : true ,
replace : true ,
restrict : 'CA' ,
templateUrl : 'trscope.html' ,
compile : function ( ) {
return function ( scope , element ) {
log ( scope . $id ) ;
expect ( element . data ( '$scope' ) ) . toBe ( scope ) ;
} ;
}
} ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'tiscope' + uppercase ( name ) , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
scope : { } ,
2012-03-08 06:47:01 +00:00
restrict : 'CA' ,
2012-01-28 00:18:16 +00:00
templateUrl : 'tiscope.html' ,
compile : function ( ) {
return function ( scope , element ) {
iscope = scope ;
log ( scope . $id ) ;
2013-11-05 20:31:20 +00:00
expect ( element . data ( '$isolateScope' ) ) . toBe ( scope ) ;
2012-01-28 00:18:16 +00:00
} ;
}
} ;
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'log' , function ( log ) {
2012-03-08 06:47:01 +00:00
return {
restrict : 'CA' ,
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
link : { pre : function ( scope ) {
2013-11-05 20:27:56 +00:00
log ( 'log-' + scope . $id + '-' + ( scope . $parent && scope . $parent . $id || 'no-parent' ) ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} }
2011-11-29 20:11:32 +00:00
} ;
} ) ;
} ) ) ;
it ( 'should allow creation of new scopes' , inject ( function ( $rootScope , $compile , log ) {
element = $compile ( '<div><span scope><a log></a></span></div>' ) ( $rootScope ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( '002; log-002-001; LOG' ) ;
2012-02-11 05:35:02 +00:00
expect ( element . find ( 'span' ) . hasClass ( 'ng-scope' ) ) . toBe ( true ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
2010-03-18 22:50:14 +00:00
2012-03-08 00:18:41 +00:00
it ( 'should allow creation of new isolated scopes for directives' , inject (
function ( $rootScope , $compile , log ) {
2012-01-28 00:18:16 +00:00
element = $compile ( '<div><span iscope><a log></a></span></div>' ) ( $rootScope ) ;
2013-11-05 20:27:56 +00:00
expect ( log ) . toEqual ( 'log-001-no-parent; LOG; 002' ) ;
2012-01-28 00:18:16 +00:00
$rootScope . name = 'abc' ;
expect ( iscope . $parent ) . toBe ( $rootScope ) ;
expect ( iscope . name ) . toBeUndefined ( ) ;
} ) ) ;
2012-05-03 04:08:02 +00:00
it ( 'should allow creation of new scopes for directives with templates' , inject (
function ( $rootScope , $compile , log , $httpBackend ) {
$httpBackend . expect ( 'GET' , 'tscope.html' ) . respond ( '<a log>{{name}}; scopeId: {{$id}}</a>' ) ;
element = $compile ( '<div><span tscope></span></div>' ) ( $rootScope ) ;
$httpBackend . flush ( ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'log-002-001; LOG; 002' ) ;
2012-05-03 04:08:02 +00:00
$rootScope . name = 'Jozo' ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toBe ( 'Jozo; scopeId: 002' ) ;
expect ( element . find ( 'span' ) . scope ( ) . $id ) . toBe ( '002' ) ;
} ) ) ;
it ( 'should allow creation of new scopes for replace directives with templates' , inject (
function ( $rootScope , $compile , log , $httpBackend ) {
$httpBackend . expect ( 'GET' , 'trscope.html' ) .
respond ( '<p><a log>{{name}}; scopeId: {{$id}}</a></p>' ) ;
element = $compile ( '<div><span trscope></span></div>' ) ( $rootScope ) ;
$httpBackend . flush ( ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'log-002-001; LOG; 002' ) ;
2012-05-03 04:08:02 +00:00
$rootScope . name = 'Jozo' ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toBe ( 'Jozo; scopeId: 002' ) ;
expect ( element . find ( 'a' ) . scope ( ) . $id ) . toBe ( '002' ) ;
} ) ) ;
2012-05-03 07:15:07 +00:00
it ( 'should allow creation of new scopes for replace directives with templates in a repeater' ,
inject ( function ( $rootScope , $compile , log , $httpBackend ) {
$httpBackend . expect ( 'GET' , 'trscope.html' ) .
respond ( '<p><a log>{{name}}; scopeId: {{$id}} |</a></p>' ) ;
element = $compile ( '<div><span ng-repeat="i in [1,2,3]" trscope></span></div>' ) ( $rootScope ) ;
$httpBackend . flush ( ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'log-003-002; LOG; 003; log-005-004; LOG; 005; log-007-006; LOG; 007' ) ;
2012-05-03 07:15:07 +00:00
$rootScope . name = 'Jozo' ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toBe ( 'Jozo; scopeId: 003 |Jozo; scopeId: 005 |Jozo; scopeId: 007 |' ) ;
expect ( element . find ( 'p' ) . scope ( ) . $id ) . toBe ( '003' ) ;
expect ( element . find ( 'a' ) . scope ( ) . $id ) . toBe ( '003' ) ;
} ) ) ;
2012-03-08 00:18:41 +00:00
it ( 'should allow creation of new isolated scopes for directives with templates' , inject (
2012-01-28 00:18:16 +00:00
function ( $rootScope , $compile , log , $httpBackend ) {
$httpBackend . expect ( 'GET' , 'tiscope.html' ) . respond ( '<a log></a>' ) ;
element = $compile ( '<div><span tiscope></span></div>' ) ( $rootScope ) ;
$httpBackend . flush ( ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'log-002-001; LOG; 002' ) ;
2012-01-28 00:18:16 +00:00
$rootScope . name = 'abc' ;
expect ( iscope . $parent ) . toBe ( $rootScope ) ;
expect ( iscope . name ) . toBeUndefined ( ) ;
2011-11-29 20:11:32 +00:00
} ) ) ;
2011-03-23 16:33:29 +00:00
2011-11-29 20:11:32 +00:00
2012-03-08 00:18:41 +00:00
it ( 'should correctly create the scope hierachy' , inject (
2012-01-28 00:18:16 +00:00
function ( $rootScope , $compile , log ) {
element = $compile (
'<div>' + //1
'<b class=scope>' + //2
'<b class=scope><b class=log></b></b>' + //3
'<b class=log></b>' +
'</b>' +
'<b class=scope>' + //4
'<b class=log></b>' +
'</b>' +
'</div>'
) ( $rootScope ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( '002; 003; log-003-002; LOG; log-002-001; LOG; 004; log-004-001; LOG' ) ;
2012-01-28 00:18:16 +00:00
} )
) ;
2013-11-08 00:19:03 +00:00
it ( 'should allow more than one new scope directives per element, but directives should share' +
2012-03-23 18:46:54 +00:00
'the scope' , inject (
2012-01-28 00:18:16 +00:00
function ( $rootScope , $compile , log ) {
2012-03-23 18:46:54 +00:00
element = $compile ( '<div class="scope-a; scope-b"></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( '002; 002' ) ;
2012-01-28 00:18:16 +00:00
} )
) ;
it ( 'should not allow more then one isolate scope creation per element' , inject (
function ( $rootScope , $compile ) {
expect ( function ( ) {
$compile ( '<div class="iscope-a; scope-b"></div>' ) ;
fix($compile): link parents before traversing
How did compiling a templateUrl (async) directive with `replace:true` work before this commit?
1/ apply all directives with higher priority than the templateUrl directive
2/ partially apply the templateUrl directive (create `beforeTemplateNodeLinkFn`)
3/ fetch the template
4/ apply second part of the templateUrl directive on the fetched template
(`afterTemplateNodeLinkFn`)
That is, the templateUrl directive is basically split into two parts (two `nodeLinkFn` functions),
which has to be both applied.
Normally we compose linking functions (`nodeLinkFn`) using continuation - calling the linking
function of a parent element, passing the linking function of the child elements as an argument. The
parent linking function then does:
1/ execute its pre-link functions
2/ call the child elements linking function (traverse)
3/ execute its post-link functions
Now, we have two linking functions for the same DOM element level (because the templateUrl directive
has been split).
There has been multiple issues because of the order of these two linking functions (creating
controller before setting up scope locals, running linking functions before instantiating
controller, etc.). It is easy to fix one use case, but it breaks some other use case. It is hard to
decide what is the "correct" order of these two linking functions as they are essentially on the
same level.
Running them side-by-side screws up pre/post linking functions for the high priority directives
(those executed before the templateUrl directive). It runs post-linking functions before traversing:
```js
beforeTemplateNodeLinkFn(null); // do not travers
afterTemplateNodeLinkFn(afterTemplateChildLinkFn);
```
Composing them (in any order) screws up the order of post-linking functions. We could fix this by
having post-linking functions to execute in reverse order (from the lowest priority to the highest)
which might actually make a sense.
**My solution is to remove this splitting.** This commit removes the `beforeTemplateNodeLinkFn`. The
first run (before we have the template) only schedules fetching the template. The rest (creating
scope locals, instantiating a controller, linking functions, etc) is done when processing the
directive again (in the context of the already fetched template; this is the cloned
`derivedSyncDirective`).
We still need to pass-through the linking functions of the higher priority directives (those
executed before the templateUrl directive), that's why I added `preLinkFns` and `postLinkFns`
arguments to `applyDirectivesToNode`.
This also changes the "$compile transclude should make the result of a transclusion available to the
parent directive in post- linking phase (templateUrl)" unit test. It was testing that a parent
directive can see the content of transclusion in its pre-link function. That is IMHO wrong (as the
`ngTransclude` directive inserts the translusion in its linking function). This test was only passing because of
c173ca412878d537b18df01f39e400ea48a4b398, which changed the behavior of the compiler to traverse
before executing the parent linking function. That was wrong and also caused the #3792 issue, which
this change fixes.
Closes #3792
Closes #3923
Closes #3935
Closes #3927
2013-09-08 17:05:25 +00:00
} ) . toThrowMinErr ( '$compile' , 'multidir' , 'Multiple directives [iscopeA, scopeB] asking for new/isolated scope on: ' +
2013-11-05 20:31:20 +00:00
'<div class="iscope-a; scope-b">' ) ;
2012-01-28 00:18:16 +00:00
} )
) ;
2012-03-23 18:46:54 +00:00
it ( 'should create new scope even at the root of the template' , inject (
2011-11-29 20:11:32 +00:00
function ( $rootScope , $compile , log ) {
element = $compile ( '<div scope-a></div>' ) ( $rootScope ) ;
2012-03-23 18:46:54 +00:00
expect ( log ) . toEqual ( '002' ) ;
} )
) ;
it ( 'should create isolate scope even at the root of the template' , inject (
function ( $rootScope , $compile , log ) {
element = $compile ( '<div iscope></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( '002' ) ;
2012-01-28 00:18:16 +00:00
} )
) ;
2013-11-08 00:19:03 +00:00
describe ( 'scope()/isolate() scope getters' , function ( ) {
describe ( 'with no directives' , function ( ) {
it ( 'should return the scope of the parent node' , inject (
function ( $rootScope , $compile ) {
element = $compile ( '<div></div>' ) ( $rootScope ) ;
expect ( element . scope ( ) ) . toBe ( $rootScope ) ;
} )
) ;
} ) ;
describe ( 'with new scope directives' , function ( ) {
it ( 'should return the new scope at the directive element' , inject (
function ( $rootScope , $compile ) {
element = $compile ( '<div scope></div>' ) ( $rootScope ) ;
expect ( element . scope ( ) . $parent ) . toBe ( $rootScope ) ;
} )
) ;
it ( 'should return the new scope for children in the original template' , inject (
function ( $rootScope , $compile ) {
element = $compile ( '<div scope><a></a></div>' ) ( $rootScope ) ;
expect ( element . find ( 'a' ) . scope ( ) . $parent ) . toBe ( $rootScope ) ;
} )
) ;
it ( 'should return the new scope for children in the directive template' , inject (
function ( $rootScope , $compile , $httpBackend ) {
$httpBackend . expect ( 'GET' , 'tscope.html' ) . respond ( '<a></a>' ) ;
element = $compile ( '<div tscope></div>' ) ( $rootScope ) ;
$httpBackend . flush ( ) ;
expect ( element . find ( 'a' ) . scope ( ) . $parent ) . toBe ( $rootScope ) ;
} )
) ;
} ) ;
describe ( 'with isolate scope directives' , function ( ) {
it ( 'should return the root scope for directives at the root element' , inject (
function ( $rootScope , $compile ) {
element = $compile ( '<div iscope></div>' ) ( $rootScope ) ;
expect ( element . scope ( ) ) . toBe ( $rootScope ) ;
} )
) ;
it ( 'should return the non-isolate scope at the directive element' , inject (
function ( $rootScope , $compile ) {
var directiveElement ;
element = $compile ( '<div><div iscope></div></div>' ) ( $rootScope ) ;
directiveElement = element . children ( ) ;
expect ( directiveElement . scope ( ) ) . toBe ( $rootScope ) ;
expect ( directiveElement . isolateScope ( ) . $parent ) . toBe ( $rootScope ) ;
} )
) ;
it ( 'should return the isolate scope for children in the original template' , inject (
function ( $rootScope , $compile ) {
element = $compile ( '<div iscope><a></a></div>' ) ( $rootScope ) ;
expect ( element . find ( 'a' ) . scope ( ) ) . toBe ( $rootScope ) ; //xx
} )
) ;
it ( 'should return the isolate scope for children in directive template' , inject (
function ( $rootScope , $compile , $httpBackend ) {
$httpBackend . expect ( 'GET' , 'tiscope.html' ) . respond ( '<a></a>' ) ;
element = $compile ( '<div tiscope></div>' ) ( $rootScope ) ;
expect ( element . isolateScope ( ) ) . toBeUndefined ( ) ; // this is the current behavior, not desired feature
$httpBackend . flush ( ) ;
expect ( element . find ( 'a' ) . scope ( ) ) . toBe ( element . isolateScope ( ) ) ;
expect ( element . isolateScope ( ) ) . not . toBe ( $rootScope ) ;
} )
) ;
} ) ;
describe ( 'with isolate scope directives and directives that manually create a new scope' , function ( ) {
it ( 'should return the new scope at the directive element' , inject (
function ( $rootScope , $compile ) {
var directiveElement ;
element = $compile ( '<div><a ng-if="true" iscope></a></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
directiveElement = element . find ( 'a' ) ;
expect ( directiveElement . scope ( ) . $parent ) . toBe ( $rootScope ) ;
expect ( directiveElement . scope ( ) ) . not . toBe ( directiveElement . isolateScope ( ) ) ;
} )
) ;
it ( 'should return the isolate scope for child elements' , inject (
function ( $rootScope , $compile , $httpBackend ) {
var directiveElement , child ;
$httpBackend . expect ( 'GET' , 'tiscope.html' ) . respond ( '<span></span>' ) ;
element = $compile ( '<div><a ng-if="true" tiscope></a></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
$httpBackend . flush ( ) ;
directiveElement = element . find ( 'a' ) ;
child = directiveElement . find ( 'span' ) ;
expect ( child . scope ( ) ) . toBe ( directiveElement . isolateScope ( ) ) ;
} )
) ;
} ) ;
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
2010-03-18 22:50:14 +00:00
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
2010-03-19 00:12:38 +00:00
2011-03-23 16:33:29 +00:00
2011-11-29 20:11:32 +00:00
describe ( 'interpolation' , function ( ) {
2012-06-04 22:06:02 +00:00
var observeSpy , directiveAttrs ;
2012-02-15 19:34:56 +00:00
2012-06-04 19:08:27 +00:00
beforeEach ( module ( function ( ) {
directive ( 'observer' , function ( ) {
2012-02-15 19:34:56 +00:00
return function ( scope , elm , attr ) {
2012-06-04 22:06:02 +00:00
directiveAttrs = attr ;
2012-02-15 19:34:56 +00:00
observeSpy = jasmine . createSpy ( '$observe attr' ) ;
2012-05-03 23:30:36 +00:00
expect ( attr . $observe ( 'someAttr' , observeSpy ) ) . toBe ( observeSpy ) ;
2012-02-15 19:34:56 +00:00
} ;
} ) ;
2013-02-26 10:14:27 +00:00
directive ( 'replaceSomeAttr' , valueFn ( {
compile : function ( element , attr ) {
attr . $set ( 'someAttr' , 'bar-{{1+1}}' ) ;
expect ( element ) . toBe ( attr . $$element ) ;
}
} ) ) ;
2012-02-15 19:34:56 +00:00
} ) ) ;
2010-03-19 23:41:02 +00:00
2011-11-29 20:11:32 +00:00
it ( 'should compile and link both attribute and text bindings' , inject (
function ( $rootScope , $compile ) {
$rootScope . name = 'angular' ;
element = $compile ( '<div name="attr: {{name}}">text: {{name}}</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toEqual ( 'text: angular' ) ;
expect ( element . attr ( 'name' ) ) . toEqual ( 'attr: angular' ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} )
) ;
2013-10-24 19:09:41 +00:00
it ( 'should process attribute interpolation in pre-linking phase at priority 100' , function ( ) {
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
module ( function ( ) {
directive ( 'attrLog' , function ( log ) {
return {
compile : function ( $element , $attrs ) {
log ( 'compile=' + $attrs . myName ) ;
return {
pre : function ( $scope , $element , $attrs ) {
2013-10-24 19:09:41 +00:00
log ( 'preLinkP0=' + $attrs . myName ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} ,
2013-10-24 19:09:41 +00:00
post : function ( $scope , $element , $attrs ) {
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
log ( 'postLink=' + $attrs . myName ) ;
}
}
}
}
2013-10-24 19:09:41 +00:00
} ) ;
} ) ;
module ( function ( ) {
directive ( 'attrLogHighPriority' , function ( log ) {
return {
priority : 101 ,
compile : function ( ) {
return {
pre : function ( $scope , $element , $attrs ) {
log ( 'preLinkP101=' + $attrs . myName ) ;
}
} ;
}
}
} ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} ) ;
inject ( function ( $rootScope , $compile , log ) {
2013-10-24 19:09:41 +00:00
element = $compile ( '<div attr-log-high-priority attr-log my-name="{{name}}"></div>' ) ( $rootScope ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
$rootScope . name = 'angular' ;
$rootScope . $apply ( ) ;
log ( 'digest=' + element . attr ( 'my-name' ) ) ;
2013-10-24 19:09:41 +00:00
expect ( log ) . toEqual ( 'compile={{name}}; preLinkP101={{name}}; preLinkP0=; postLink=; digest=angular' ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} ) ;
} ) ;
2011-03-23 16:33:29 +00:00
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
describe ( 'SCE values' , function ( ) {
it ( 'should resolve compile and link both attribute and text bindings' , inject (
function ( $rootScope , $compile , $sce ) {
$rootScope . name = $sce . trustAsHtml ( 'angular' ) ;
element = $compile ( '<div name="attr: {{name}}">text: {{name}}</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toEqual ( 'text: angular' ) ;
expect ( element . attr ( 'name' ) ) . toEqual ( 'attr: angular' ) ;
} ) ) ;
} ) ;
2010-04-26 18:57:33 +00:00
2011-11-29 20:11:32 +00:00
it ( 'should decorate the binding with ng-binding and interpolation function' , inject (
function ( $compile , $rootScope ) {
element = $compile ( '<div>{{1+2}}</div>' ) ( $rootScope ) ;
expect ( element . hasClass ( 'ng-binding' ) ) . toBe ( true ) ;
expect ( element . data ( '$binding' ) [ 0 ] . exp ) . toEqual ( '{{1+2}}' ) ;
} ) ) ;
2012-02-15 19:34:56 +00:00
it ( 'should observe interpolated attrs' , inject ( function ( $rootScope , $compile ) {
$compile ( '<div some-attr="{{value}}" observer></div>' ) ( $rootScope ) ;
// should be async
expect ( observeSpy ) . not . toHaveBeenCalled ( ) ;
$rootScope . $apply ( function ( ) {
$rootScope . value = 'bound-value' ;
} ) ;
expect ( observeSpy ) . toHaveBeenCalledOnceWith ( 'bound-value' ) ;
} ) ) ;
2012-11-19 10:01:53 +00:00
it ( 'should set interpolated attrs to initial interpolation value' , inject ( function ( $rootScope , $compile ) {
$rootScope . whatever = 'test value' ;
2012-02-15 19:34:56 +00:00
$compile ( '<div some-attr="{{whatever}}" observer></div>' ) ( $rootScope ) ;
2012-11-19 10:01:53 +00:00
expect ( directiveAttrs . someAttr ) . toBe ( $rootScope . whatever ) ;
2012-02-15 19:34:56 +00:00
} ) ) ;
2013-02-26 10:14:27 +00:00
it ( 'should allow directive to replace interpolated attributes before attr interpolation compilation' , inject (
function ( $compile , $rootScope ) {
element = $compile ( '<div some-attr="foo-{{1+1}}" replace-some-attr></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . attr ( 'some-attr' ) ) . toEqual ( 'bar-2' ) ;
} ) ) ;
2012-06-04 22:06:02 +00:00
it ( 'should call observer of non-interpolated attr through $evalAsync' ,
inject ( function ( $rootScope , $compile ) {
$compile ( '<div some-attr="nonBound" observer></div>' ) ( $rootScope ) ;
expect ( directiveAttrs . someAttr ) . toBe ( 'nonBound' ) ;
2012-02-15 19:34:56 +00:00
2012-06-04 22:06:02 +00:00
expect ( observeSpy ) . not . toHaveBeenCalled ( ) ;
$rootScope . $digest ( ) ;
expect ( observeSpy ) . toHaveBeenCalled ( ) ;
} )
) ;
2012-02-15 19:34:56 +00:00
it ( 'should delegate exceptions to $exceptionHandler' , function ( ) {
observeSpy = jasmine . createSpy ( '$observe attr' ) . andThrow ( 'ERROR' ) ;
2012-06-04 19:08:27 +00:00
module ( function ( $exceptionHandlerProvider ) {
2012-02-15 19:34:56 +00:00
$exceptionHandlerProvider . mode ( 'log' ) ;
2012-06-04 19:08:27 +00:00
directive ( 'error' , function ( ) {
2012-02-15 19:34:56 +00:00
return function ( scope , elm , attr ) {
attr . $observe ( 'someAttr' , observeSpy ) ;
attr . $observe ( 'someAttr' , observeSpy ) ;
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope , $exceptionHandler ) {
$compile ( '<div some-attr="{{value}}" error></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( observeSpy ) . toHaveBeenCalled ( ) ;
expect ( observeSpy . callCount ) . toBe ( 2 ) ;
expect ( $exceptionHandler . errors ) . toEqual ( [ 'ERROR' , 'ERROR' ] ) ;
} ) ;
2012-03-08 23:00:38 +00:00
} ) ;
it ( 'should translate {{}} in terminal nodes' , inject ( function ( $rootScope , $compile ) {
element = $compile ( '<select ng:model="x"><option value="">Greet {{name}}!</option></select>' ) ( $rootScope )
$rootScope . $digest ( ) ;
expect ( sortedHtml ( element ) . replace ( ' selected="true"' , '' ) ) .
toEqual ( '<select ng:model="x">' +
2013-02-26 04:43:27 +00:00
'<option value="">Greet !</option>' +
2012-03-08 23:00:38 +00:00
'</select>' ) ;
$rootScope . name = 'Misko' ;
$rootScope . $digest ( ) ;
expect ( sortedHtml ( element ) . replace ( ' selected="true"' , '' ) ) .
toEqual ( '<select ng:model="x">' +
2013-02-26 04:43:27 +00:00
'<option value="">Greet Misko!</option>' +
2012-03-08 23:00:38 +00:00
'</select>' ) ;
} ) ) ;
2012-08-11 07:13:10 +00:00
it ( 'should support custom start/end interpolation symbols in template and directive template' ,
function ( ) {
module ( function ( $interpolateProvider , $compileProvider ) {
$interpolateProvider . startSymbol ( '##' ) . endSymbol ( ']]' ) ;
$compileProvider . directive ( 'myDirective' , function ( ) {
return {
template : '<span>{{hello}}|{{hello|uppercase}}</span>'
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div>##hello|uppercase]]|<div my-directive></div></div>' ) ( $rootScope ) ;
$rootScope . hello = 'ahoj' ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toBe ( 'AHOJ|ahoj|AHOJ' ) ;
} ) ;
} ) ;
it ( 'should support custom start/end interpolation symbols in async directive template' ,
function ( ) {
module ( function ( $interpolateProvider , $compileProvider ) {
$interpolateProvider . startSymbol ( '##' ) . endSymbol ( ']]' ) ;
$compileProvider . directive ( 'myDirective' , function ( ) {
return {
templateUrl : 'myDirective.html'
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope , $templateCache ) {
$templateCache . put ( 'myDirective.html' , '<span>{{hello}}|{{hello|uppercase}}</span>' ) ;
element = $compile ( '<div>##hello|uppercase]]|<div my-directive></div></div>' ) ( $rootScope ) ;
$rootScope . hello = 'ahoj' ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toBe ( 'AHOJ|ahoj|AHOJ' ) ;
} ) ;
} ) ;
2013-10-24 19:09:41 +00:00
it ( 'should make attributes observable for terminal directives' , function ( ) {
module ( function ( ) {
directive ( 'myAttr' , function ( log ) {
return {
terminal : true ,
link : function ( scope , element , attrs ) {
attrs . $observe ( 'myAttr' , function ( val ) {
log ( val ) ;
} ) ;
}
}
} ) ;
} ) ;
inject ( function ( $compile , $rootScope , log ) {
element = $compile ( '<div my-attr="{{myVal}}"></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( [ ] ) ;
$rootScope . myVal = 'carrot' ;
$rootScope . $digest ( ) ;
expect ( log ) . toEqual ( [ 'carrot' ] ) ;
} ) ;
} )
2011-11-29 20:11:32 +00:00
} ) ;
2011-03-23 16:33:29 +00:00
2011-02-22 23:19:22 +00:00
2011-11-29 20:11:32 +00:00
describe ( 'link phase' , function ( ) {
2011-03-23 16:33:29 +00:00
2012-06-04 19:08:27 +00:00
beforeEach ( module ( function ( ) {
2011-11-29 20:11:32 +00:00
forEach ( [ 'a' , 'b' , 'c' ] , function ( name ) {
2012-06-04 19:08:27 +00:00
directive ( name , function ( log ) {
2011-11-29 20:11:32 +00:00
return {
2012-03-08 06:47:01 +00:00
restrict : 'ECA' ,
2011-11-29 20:11:32 +00:00
compile : function ( ) {
log ( 't' + uppercase ( name ) )
return {
pre : function ( ) {
log ( 'pre' + uppercase ( name ) ) ;
} ,
post : function linkFn ( ) {
log ( 'post' + uppercase ( name ) ) ;
}
} ;
}
} ;
} ) ;
} ) ;
} ) ) ;
it ( 'should not store linkingFns for noop branches' , inject ( function ( $rootScope , $compile ) {
element = jqLite ( '<div name="{{a}}"><span>ignore</span></div>' ) ;
var linkingFn = $compile ( element ) ;
// Now prune the branches with no directives
element . find ( 'span' ) . remove ( ) ;
expect ( element . find ( 'span' ) . length ) . toBe ( 0 ) ;
// and we should still be able to compile without errors
linkingFn ( $rootScope ) ;
} ) ) ;
it ( 'should compile from top to bottom but link from bottom up' , inject (
function ( $compile , $rootScope , log ) {
element = $compile ( '<a b><c></c></a>' ) ( $rootScope ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'tA; tB; tC; preA; preB; preC; postC; postB; postA' ) ;
2011-11-29 20:11:32 +00:00
}
) ) ;
it ( 'should support link function on directive object' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'abc' , valueFn ( {
2011-11-29 20:11:32 +00:00
link : function ( scope , element , attrs ) {
element . text ( attrs . abc ) ;
}
} ) ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div abc="WORKS">FAIL</div>' ) ( $rootScope ) ;
expect ( element . text ( ) ) . toEqual ( 'WORKS' ) ;
} ) ;
} ) ;
2013-02-04 19:38:52 +00:00
it ( 'should support $observe inside link function on directive object' , function ( ) {
module ( function ( ) {
directive ( 'testLink' , valueFn ( {
templateUrl : 'test-link.html' ,
link : function ( scope , element , attrs ) {
attrs . $observe ( 'testLink' , function ( val ) {
scope . testAttr = val ;
} ) ;
}
} ) ) ;
} ) ;
inject ( function ( $compile , $rootScope , $templateCache ) {
$templateCache . put ( 'test-link.html' , '{{testAttr}}' ) ;
element = $compile ( '<div test-link="{{1+2}}"></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toBe ( '3' ) ;
} ) ;
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
describe ( 'attrs' , function ( ) {
it ( 'should allow setting of attributes' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( {
2011-11-29 20:11:32 +00:00
setter : valueFn ( function ( scope , element , attr ) {
attr . $set ( 'name' , 'abc' ) ;
attr . $set ( 'disabled' , true ) ;
expect ( attr . name ) . toBe ( 'abc' ) ;
expect ( attr . disabled ) . toBe ( true ) ;
} )
} ) ;
} ) ;
inject ( function ( $rootScope , $compile ) {
element = $compile ( '<div setter></div>' ) ( $rootScope ) ;
expect ( element . attr ( 'name' ) ) . toEqual ( 'abc' ) ;
expect ( element . attr ( 'disabled' ) ) . toEqual ( 'disabled' ) ;
} ) ;
} ) ;
2012-03-13 21:42:26 +00:00
it ( 'should read boolean attributes as boolean only on control elements' , function ( ) {
var value ;
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( {
2012-03-13 21:42:26 +00:00
input : valueFn ( {
restrict : 'ECA' ,
link : function ( scope , element , attr ) {
value = attr . required ;
}
} )
} ) ;
} ) ;
inject ( function ( $rootScope , $compile ) {
element = $compile ( '<input required></input>' ) ( $rootScope ) ;
expect ( value ) . toEqual ( true ) ;
} ) ;
} ) ;
it ( 'should read boolean attributes as text on non-controll elements' , function ( ) {
var value ;
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( {
2012-03-08 06:47:01 +00:00
div : valueFn ( {
restrict : 'ECA' ,
link : function ( scope , element , attr ) {
2012-03-13 21:42:26 +00:00
value = attr . required ;
2012-03-08 06:47:01 +00:00
}
2011-11-29 20:11:32 +00:00
} )
} ) ;
} ) ;
inject ( function ( $rootScope , $compile ) {
2012-03-13 21:42:26 +00:00
element = $compile ( '<div required="some text"></div>' ) ( $rootScope ) ;
expect ( value ) . toEqual ( 'some text' ) ;
2011-11-29 20:11:32 +00:00
} ) ;
} ) ;
it ( 'should create new instance of attr for each template stamping' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( $provide ) {
2011-11-29 20:11:32 +00:00
var state = { first : [ ] , second : [ ] } ;
$provide . value ( 'state' , state ) ;
2012-06-04 19:08:27 +00:00
directive ( {
2011-11-29 20:11:32 +00:00
first : valueFn ( {
priority : 1 ,
compile : function ( templateElement , templateAttr ) {
return function ( scope , element , attr ) {
state . first . push ( {
template : { element : templateElement , attr : templateAttr } ,
link : { element : element , attr : attr }
} ) ;
}
}
} ) ,
second : valueFn ( {
priority : 2 ,
compile : function ( templateElement , templateAttr ) {
return function ( scope , element , attr ) {
state . second . push ( {
template : { element : templateElement , attr : templateAttr } ,
link : { element : element , attr : attr }
} ) ;
}
}
} )
} ) ;
} ) ;
inject ( function ( $rootScope , $compile , state ) {
var template = $compile ( '<div first second>' ) ;
dealoc ( template ( $rootScope . $new ( ) , noop ) ) ;
dealoc ( template ( $rootScope . $new ( ) , noop ) ) ;
// instance between directives should be shared
expect ( state . first [ 0 ] . template . element ) . toBe ( state . second [ 0 ] . template . element ) ;
expect ( state . first [ 0 ] . template . attr ) . toBe ( state . second [ 0 ] . template . attr ) ;
// the template and the link can not be the same instance
expect ( state . first [ 0 ] . template . element ) . not . toBe ( state . first [ 0 ] . link . element ) ;
expect ( state . first [ 0 ] . template . attr ) . not . toBe ( state . first [ 0 ] . link . attr ) ;
// each new template needs to be new instance
expect ( state . first [ 0 ] . link . element ) . not . toBe ( state . first [ 1 ] . link . element ) ;
expect ( state . first [ 0 ] . link . attr ) . not . toBe ( state . first [ 1 ] . link . attr ) ;
expect ( state . second [ 0 ] . link . element ) . not . toBe ( state . second [ 1 ] . link . element ) ;
expect ( state . second [ 0 ] . link . attr ) . not . toBe ( state . second [ 1 ] . link . attr ) ;
} ) ;
} ) ;
2012-03-28 20:47:57 +00:00
it ( 'should properly $observe inside ng-repeat' , function ( ) {
var spies = [ ] ;
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'observer' , function ( ) {
2012-03-28 20:47:57 +00:00
return function ( scope , elm , attr ) {
spies . push ( jasmine . createSpy ( 'observer ' + spies . length ) ) ;
attr . $observe ( 'some' , spies [ spies . length - 1 ] ) ;
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div><div ng-repeat="i in items">' +
'<span some="id_{{i.id}}" observer></span>' +
'</div></div>' ) ( $rootScope ) ;
$rootScope . $apply ( function ( ) {
$rootScope . items = [ { id : 1 } , { id : 2 } ] ;
} ) ;
expect ( spies [ 0 ] ) . toHaveBeenCalledOnceWith ( 'id_1' ) ;
expect ( spies [ 1 ] ) . toHaveBeenCalledOnceWith ( 'id_2' ) ;
spies [ 0 ] . reset ( ) ;
spies [ 1 ] . reset ( ) ;
$rootScope . $apply ( function ( ) {
$rootScope . items [ 0 ] . id = 5 ;
} ) ;
expect ( spies [ 0 ] ) . toHaveBeenCalledOnceWith ( 'id_5' ) ;
} ) ;
} ) ;
2011-11-29 20:11:32 +00:00
describe ( '$set' , function ( ) {
var attr ;
beforeEach ( function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'input' , valueFn ( {
2012-03-08 06:47:01 +00:00
restrict : 'ECA' ,
link : function ( scope , element , attr ) {
scope . attr = attr ;
}
2011-11-29 20:11:32 +00:00
} ) ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
2012-03-13 21:42:26 +00:00
element = $compile ( '<input></input>' ) ( $rootScope ) ;
2011-11-29 20:11:32 +00:00
attr = $rootScope . attr ;
expect ( attr ) . toBeDefined ( ) ;
} ) ;
} ) ;
it ( 'should set attributes' , function ( ) {
attr . $set ( 'ngMyAttr' , 'value' ) ;
expect ( element . attr ( 'ng-my-attr' ) ) . toEqual ( 'value' ) ;
expect ( attr . ngMyAttr ) . toEqual ( 'value' ) ;
} ) ;
it ( 'should allow overriding of attribute name and remember the name' , function ( ) {
2012-03-23 20:04:52 +00:00
attr . $set ( 'ngOther' , '123' , true , 'other' ) ;
2011-11-29 20:11:32 +00:00
expect ( element . attr ( 'other' ) ) . toEqual ( '123' ) ;
expect ( attr . ngOther ) . toEqual ( '123' ) ;
attr . $set ( 'ngOther' , '246' ) ;
expect ( element . attr ( 'other' ) ) . toEqual ( '246' ) ;
expect ( attr . ngOther ) . toEqual ( '246' ) ;
} ) ;
it ( 'should remove attribute' , function ( ) {
attr . $set ( 'ngMyAttr' , 'value' ) ;
expect ( element . attr ( 'ng-my-attr' ) ) . toEqual ( 'value' ) ;
attr . $set ( 'ngMyAttr' , undefined ) ;
expect ( element . attr ( 'ng-my-attr' ) ) . toBe ( undefined ) ;
attr . $set ( 'ngMyAttr' , 'value' ) ;
attr . $set ( 'ngMyAttr' , null ) ;
expect ( element . attr ( 'ng-my-attr' ) ) . toBe ( undefined ) ;
2012-03-23 20:04:52 +00:00
} ) ;
it ( 'should not set DOM element attr if writeAttr false' , function ( ) {
attr . $set ( 'test' , 'value' , false ) ;
expect ( element . attr ( 'test' ) ) . toBeUndefined ( ) ;
expect ( attr . test ) . toBe ( 'value' ) ;
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
} ) ;
2012-01-28 00:18:16 +00:00
2012-06-06 20:58:10 +00:00
describe ( 'isolated locals' , function ( ) {
2013-11-05 20:31:20 +00:00
var componentScope , regularScope ;
2012-06-06 20:58:10 +00:00
beforeEach ( module ( function ( ) {
directive ( 'myComponent' , function ( ) {
return {
scope : {
attr : '@' ,
attrAlias : '@attr' ,
ref : '=' ,
refAlias : '= ref' ,
2012-09-13 09:40:00 +00:00
reference : '=' ,
2013-01-26 19:15:06 +00:00
optref : '=?' ,
optrefAlias : '=? optref' ,
optreference : '=?' ,
2012-06-06 20:58:10 +00:00
expr : '&' ,
exprAlias : '&expr'
} ,
link : function ( scope ) {
componentScope = scope ;
}
} ;
2012-01-28 00:18:16 +00:00
} ) ;
2012-06-06 20:58:10 +00:00
directive ( 'badDeclaration' , function ( ) {
return {
scope : { attr : 'xxx' }
} ;
} ) ;
2013-11-05 20:31:20 +00:00
directive ( 'storeScope' , function ( ) {
return {
link : function ( scope ) {
regularScope = scope ;
}
}
} ) ;
} ) ) ;
2013-11-08 00:19:03 +00:00
2013-11-05 20:31:20 +00:00
it ( 'should give other directives the parent scope' , inject ( function ( $rootScope ) {
compile ( '<div><input type="text" my-component store-scope ng-model="value"></div>' ) ;
$rootScope . $apply ( function ( ) {
$rootScope . value = 'from-parent' ;
} ) ;
expect ( element . find ( 'input' ) . val ( ) ) . toBe ( 'from-parent' ) ;
expect ( componentScope ) . not . toBe ( regularScope ) ;
expect ( componentScope . $parent ) . toBe ( regularScope )
2012-06-06 20:58:10 +00:00
} ) ) ;
2013-11-08 00:19:03 +00:00
2013-11-05 20:27:56 +00:00
it ( 'should not give the isolate scope to other directive template' , function ( ) {
module ( function ( ) {
directive ( 'otherTplDir' , function ( ) {
return {
template : 'value: {{value}}'
} ;
} ) ;
} ) ;
inject ( function ( $rootScope ) {
compile ( '<div my-component other-tpl-dir>' ) ;
$rootScope . $apply ( function ( ) {
$rootScope . value = 'from-parent' ;
} ) ;
expect ( element . html ( ) ) . toBe ( 'value: from-parent' ) ;
} ) ;
} ) ;
it ( 'should not give the isolate scope to other directive template (with templateUrl)' , function ( ) {
module ( function ( ) {
directive ( 'otherTplDir' , function ( ) {
return {
templateUrl : 'other.html'
} ;
} ) ;
} ) ;
inject ( function ( $rootScope , $templateCache ) {
$templateCache . put ( 'other.html' , 'value: {{value}}' )
compile ( '<div my-component other-tpl-dir>' ) ;
$rootScope . $apply ( function ( ) {
$rootScope . value = 'from-parent' ;
} ) ;
expect ( element . html ( ) ) . toBe ( 'value: from-parent' ) ;
} ) ;
} ) ;
it ( 'should not give the isolate scope to regular child elements' , function ( ) {
inject ( function ( $rootScope ) {
compile ( '<div my-component>value: {{value}}</div>' ) ;
$rootScope . $apply ( function ( ) {
$rootScope . value = 'from-parent' ;
} ) ;
expect ( element . html ( ) ) . toBe ( 'value: from-parent' ) ;
} ) ;
} ) ;
2012-06-06 20:58:10 +00:00
describe ( 'attribute' , function ( ) {
it ( 'should copy simple attribute' , inject ( function ( ) {
compile ( '<div><span my-component attr="some text">' ) ;
expect ( componentScope . attr ) . toEqual ( 'some text' ) ;
expect ( componentScope . attrAlias ) . toEqual ( 'some text' ) ;
expect ( componentScope . attrAlias ) . toEqual ( componentScope . attr ) ;
} ) ) ;
2012-10-24 10:06:36 +00:00
it ( 'should set up the interpolation before it reaches the link function' , inject ( function ( ) {
2012-06-06 20:58:10 +00:00
$rootScope . name = 'misko' ;
2012-10-24 10:06:36 +00:00
compile ( '<div><span my-component attr="hello {{name}}">' ) ;
2012-06-06 20:58:10 +00:00
expect ( componentScope . attr ) . toEqual ( 'hello misko' ) ;
expect ( componentScope . attrAlias ) . toEqual ( 'hello misko' ) ;
2012-10-24 10:06:36 +00:00
} ) ) ;
it ( 'should update when interpolated attribute updates' , inject ( function ( ) {
compile ( '<div><span my-component attr="hello {{name}}">' ) ;
2012-06-06 20:58:10 +00:00
$rootScope . name = 'igor' ;
$rootScope . $apply ( ) ;
expect ( componentScope . attr ) . toEqual ( 'hello igor' ) ;
expect ( componentScope . attrAlias ) . toEqual ( 'hello igor' ) ;
} ) ) ;
2012-01-28 00:18:16 +00:00
} ) ;
2012-06-06 20:58:10 +00:00
describe ( 'object reference' , function ( ) {
it ( 'should update local when origin changes' , inject ( function ( ) {
compile ( '<div><span my-component ref="name">' ) ;
expect ( componentScope . ref ) . toBe ( undefined ) ;
expect ( componentScope . refAlias ) . toBe ( componentScope . ref ) ;
$rootScope . name = 'misko' ;
$rootScope . $apply ( ) ;
expect ( $rootScope . name ) . toBe ( 'misko' ) ;
expect ( componentScope . ref ) . toBe ( 'misko' ) ;
2013-08-20 21:41:27 +00:00
expect ( componentScope . refAlias ) . toBe ( 'misko' ) ;
2012-06-06 20:58:10 +00:00
2013-08-20 21:41:27 +00:00
$rootScope . name = { } ;
2012-06-06 20:58:10 +00:00
$rootScope . $apply ( ) ;
2013-08-20 21:41:27 +00:00
expect ( componentScope . ref ) . toBe ( $rootScope . name ) ;
expect ( componentScope . refAlias ) . toBe ( $rootScope . name ) ;
2012-06-06 20:58:10 +00:00
} ) ) ;
it ( 'should update local when both change' , inject ( function ( ) {
compile ( '<div><span my-component ref="name">' ) ;
$rootScope . name = { mark : 123 } ;
componentScope . ref = 'misko' ;
$rootScope . $apply ( ) ;
expect ( $rootScope . name ) . toEqual ( { mark : 123 } )
expect ( componentScope . ref ) . toBe ( $rootScope . name ) ;
expect ( componentScope . refAlias ) . toBe ( $rootScope . name ) ;
$rootScope . name = 'igor' ;
componentScope . ref = { } ;
$rootScope . $apply ( ) ;
expect ( $rootScope . name ) . toEqual ( 'igor' )
expect ( componentScope . ref ) . toBe ( $rootScope . name ) ;
expect ( componentScope . refAlias ) . toBe ( $rootScope . name ) ;
} ) ) ;
it ( 'should complain on non assignable changes' , inject ( function ( ) {
compile ( '<div><span my-component ref="\'hello \' + name">' ) ;
$rootScope . name = 'world' ;
$rootScope . $apply ( ) ;
expect ( componentScope . ref ) . toBe ( 'hello world' ) ;
componentScope . ref = 'ignore me' ;
expect ( $rootScope . $apply ) .
2013-08-13 22:30:52 +00:00
toThrowMinErr ( "$compile" , "nonassign" , "Expression ''hello ' + name' used with directive 'myComponent' is non-assignable!" ) ;
2012-06-06 20:58:10 +00:00
expect ( componentScope . ref ) . toBe ( 'hello world' ) ;
// reset since the exception was rethrown which prevented phase clearing
$rootScope . $$phase = null ;
$rootScope . name = 'misko' ;
$rootScope . $apply ( ) ;
expect ( componentScope . ref ) . toBe ( 'hello misko' ) ;
} ) ) ;
2012-09-13 09:40:00 +00:00
// regression
it ( 'should stabilize model' , inject ( function ( ) {
compile ( '<div><span my-component reference="name">' ) ;
var lastRefValueInParent ;
$rootScope . $watch ( 'name' , function ( ref ) {
lastRefValueInParent = ref ;
} ) ;
$rootScope . name = 'aaa' ;
$rootScope . $apply ( ) ;
componentScope . reference = 'new' ;
$rootScope . $apply ( ) ;
expect ( lastRefValueInParent ) . toBe ( 'new' ) ;
} ) ) ;
2012-06-06 20:58:10 +00:00
} ) ;
2013-01-26 19:15:06 +00:00
describe ( 'optional object reference' , function ( ) {
it ( 'should update local when origin changes' , inject ( function ( ) {
compile ( '<div><span my-component optref="name">' ) ;
expect ( componentScope . optRef ) . toBe ( undefined ) ;
expect ( componentScope . optRefAlias ) . toBe ( componentScope . optRef ) ;
$rootScope . name = 'misko' ;
$rootScope . $apply ( ) ;
expect ( componentScope . optref ) . toBe ( $rootScope . name ) ;
expect ( componentScope . optrefAlias ) . toBe ( $rootScope . name ) ;
$rootScope . name = { } ;
$rootScope . $apply ( ) ;
expect ( componentScope . optref ) . toBe ( $rootScope . name ) ;
expect ( componentScope . optrefAlias ) . toBe ( $rootScope . name ) ;
} ) ) ;
it ( 'should not throw exception when reference does not exist' , inject ( function ( ) {
compile ( '<div><span my-component>' ) ;
expect ( componentScope . optref ) . toBe ( undefined ) ;
expect ( componentScope . optrefAlias ) . toBe ( undefined ) ;
expect ( componentScope . optreference ) . toBe ( undefined ) ;
} ) ) ;
} ) ;
2012-06-06 20:58:10 +00:00
describe ( 'executable expression' , function ( ) {
it ( 'should allow expression execution with locals' , inject ( function ( ) {
compile ( '<div><span my-component expr="count = count + offset">' ) ;
$rootScope . count = 2 ;
expect ( typeof componentScope . expr ) . toBe ( 'function' ) ;
expect ( typeof componentScope . exprAlias ) . toBe ( 'function' ) ;
expect ( componentScope . expr ( { offset : 1 } ) ) . toEqual ( 3 ) ;
expect ( $rootScope . count ) . toEqual ( 3 ) ;
expect ( componentScope . exprAlias ( { offset : 10 } ) ) . toEqual ( 13 ) ;
expect ( $rootScope . count ) . toEqual ( 13 ) ;
} ) ) ;
2012-01-28 00:18:16 +00:00
} ) ;
2012-06-06 20:58:10 +00:00
it ( 'should throw on unknown definition' , inject ( function ( ) {
expect ( function ( ) {
compile ( '<div><span bad-declaration>' ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr ( "$compile" , "iscp" , "Invalid isolate scope definition for directive 'badDeclaration'. Definition: {... attr: 'xxx' ...}" ) ;
2012-06-06 20:58:10 +00:00
} ) ) ;
2013-01-23 05:01:13 +00:00
it ( 'should expose a $$isolateBindings property onto the scope' , inject ( function ( ) {
compile ( '<div><span my-component>' ) ;
expect ( typeof componentScope . $$isolateBindings ) . toBe ( 'object' ) ;
expect ( componentScope . $$isolateBindings . attr ) . toBe ( '@attr' ) ;
expect ( componentScope . $$isolateBindings . attrAlias ) . toBe ( '@attr' ) ;
expect ( componentScope . $$isolateBindings . ref ) . toBe ( '=ref' ) ;
expect ( componentScope . $$isolateBindings . refAlias ) . toBe ( '=ref' ) ;
expect ( componentScope . $$isolateBindings . reference ) . toBe ( '=reference' ) ;
expect ( componentScope . $$isolateBindings . expr ) . toBe ( '&expr' ) ;
expect ( componentScope . $$isolateBindings . exprAlias ) . toBe ( '&expr' ) ;
} ) ) ;
2012-06-06 20:58:10 +00:00
} ) ;
2012-01-28 00:18:16 +00:00
2012-06-06 20:58:10 +00:00
describe ( 'controller' , function ( ) {
2012-01-28 00:18:16 +00:00
it ( 'should get required controller' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'main' , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
priority : 2 ,
controller : function ( ) {
this . name = 'main' ;
} ,
link : function ( scope , element , attrs , controller ) {
log ( controller . name ) ;
}
} ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'dep' , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
priority : 1 ,
require : 'main' ,
link : function ( scope , element , attrs , controller ) {
log ( 'dep:' + controller . name ) ;
}
} ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'other' , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
link : function ( scope , element , attrs , controller ) {
log ( ! ! controller ) ; // should be false
}
} ;
} ) ;
} ) ;
inject ( function ( log , $compile , $rootScope ) {
element = $compile ( '<div main dep other></div>' ) ( $rootScope ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
expect ( log ) . toEqual ( 'false; dep:main; main' ) ;
2012-01-28 00:18:16 +00:00
} ) ;
} ) ;
2013-10-22 00:38:43 +00:00
it ( 'should get required controller via linkingFn (template)' , function ( ) {
module ( function ( ) {
directive ( 'dirA' , function ( ) {
return {
controller : function ( ) {
this . name = 'dirA' ;
}
} ;
} ) ;
directive ( 'dirB' , function ( log ) {
return {
require : 'dirA' ,
template : '<p>dirB</p>' ,
link : function ( scope , element , attrs , dirAController ) {
log ( 'dirAController.name: ' + dirAController . name ) ;
}
} ;
} ) ;
} ) ;
inject ( function ( log , $compile , $rootScope ) {
element = $compile ( '<div dir-a dir-b></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( 'dirAController.name: dirA' ) ;
} ) ;
} ) ;
it ( 'should get required controller via linkingFn (templateUrl)' , function ( ) {
module ( function ( ) {
directive ( 'dirA' , function ( ) {
return {
controller : function ( ) {
this . name = 'dirA' ;
}
} ;
} ) ;
directive ( 'dirB' , function ( log ) {
return {
require : 'dirA' ,
templateUrl : 'dirB.html' ,
link : function ( scope , element , attrs , dirAController ) {
log ( 'dirAController.name: ' + dirAController . name ) ;
}
} ;
} ) ;
} ) ;
inject ( function ( log , $compile , $rootScope , $templateCache ) {
$templateCache . put ( 'dirB.html' , '<p>dirB</p>' ) ;
element = $compile ( '<div dir-a dir-b></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( log ) . toEqual ( 'dirAController.name: dirA' ) ;
} ) ;
} ) ;
2013-11-05 20:31:20 +00:00
it ( 'should require controller of an isolate directive from a non-isolate directive on the ' +
2013-11-05 23:50:53 +00:00
'same element' , function ( ) {
2013-11-05 20:31:20 +00:00
var IsolateController = function ( ) { } ;
var isolateDirControllerInNonIsolateDirective ;
module ( function ( ) {
directive ( 'isolate' , function ( ) {
return {
scope : { } ,
controller : IsolateController
} ;
} ) ;
directive ( 'nonIsolate' , function ( ) {
return {
require : 'isolate' ,
link : function ( _ , _ _ , _ _ _ , isolateDirController ) {
isolateDirControllerInNonIsolateDirective = isolateDirController ;
}
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div isolate non-isolate></div>' ) ( $rootScope ) ;
expect ( isolateDirControllerInNonIsolateDirective ) . toBeDefined ( ) ;
expect ( isolateDirControllerInNonIsolateDirective instanceof IsolateController ) . toBe ( true ) ;
} ) ;
} ) ;
2013-11-07 08:24:07 +00:00
it ( 'should give the isolate scope to the controller of another replaced directives in the template' , function ( ) {
module ( function ( ) {
directive ( 'testDirective' , function ( ) {
return {
replace : true ,
restrict : 'E' ,
scope : { } ,
template : '<input type="checkbox" ng-model="model">'
} ;
} ) ;
} ) ;
inject ( function ( $rootScope ) {
compile ( '<div><test-directive></test-directive></div>' ) ;
element = element . children ( ) . eq ( 0 ) ;
expect ( element [ 0 ] . checked ) . toBe ( false ) ;
element . isolateScope ( ) . model = true ;
$rootScope . $digest ( ) ;
expect ( element [ 0 ] . checked ) . toBe ( true ) ;
} ) ;
} ) ;
2013-11-08 00:18:06 +00:00
it ( 'should share isolate scope with replaced directives (template)' , function ( ) {
2013-11-05 23:50:53 +00:00
var normalScope ;
var isolateScope ;
module ( function ( ) {
directive ( 'isolate' , function ( ) {
return {
replace : true ,
scope : { } ,
template : '<span ng-init="name=\'WORKS\'">{{name}}</span>' ,
link : function ( s ) {
isolateScope = s ;
}
} ;
} ) ;
directive ( 'nonIsolate' , function ( ) {
return {
link : function ( s ) {
normalScope = s ;
}
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div isolate non-isolate></div>' ) ( $rootScope ) ;
expect ( normalScope ) . toBe ( $rootScope ) ;
expect ( normalScope . name ) . toEqual ( undefined ) ;
expect ( isolateScope . name ) . toEqual ( 'WORKS' ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toEqual ( 'WORKS' ) ;
} ) ;
} ) ;
2013-11-08 00:18:06 +00:00
it ( 'should share isolate scope with replaced directives (templateUrl)' , function ( ) {
2013-11-05 23:50:53 +00:00
var normalScope ;
var isolateScope ;
module ( function ( ) {
directive ( 'isolate' , function ( ) {
return {
replace : true ,
scope : { } ,
templateUrl : 'main.html' ,
link : function ( s ) {
isolateScope = s ;
}
} ;
} ) ;
directive ( 'nonIsolate' , function ( ) {
return {
link : function ( s ) {
normalScope = s ;
}
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope , $templateCache ) {
$templateCache . put ( 'main.html' , '<span ng-init="name=\'WORKS\'">{{name}}</span>' ) ;
element = $compile ( '<div isolate non-isolate></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( normalScope ) . toBe ( $rootScope ) ;
expect ( normalScope . name ) . toEqual ( undefined ) ;
expect ( isolateScope . name ) . toEqual ( 'WORKS' ) ;
expect ( element . text ( ) ) . toEqual ( 'WORKS' ) ;
} ) ;
} ) ;
2013-11-08 00:18:06 +00:00
it ( 'should not get confused about where to use isolate scope when a replaced directive is used multiple times' ,
function ( ) {
module ( function ( ) {
directive ( 'isolate' , function ( ) {
return {
replace : true ,
scope : { } ,
template : '<span scope-tester="replaced"><span scope-tester="inside"></span></span>'
} ;
} ) ;
directive ( 'scopeTester' , function ( log ) {
return {
link : function ( $scope , $element ) {
log ( $element . attr ( 'scope-tester' ) + '=' + ( $scope . $root === $scope ? 'non-isolate' : 'isolate' ) ) ;
}
}
} ) ;
} ) ;
inject ( function ( $compile , $rootScope , log ) {
element = $compile ( '<div>' +
'<div isolate scope-tester="outside"></div>' +
'<span scope-tester="sibling"></span>' +
'</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( log ) . toEqual ( 'inside=isolate; ' +
'outside replaced=non-isolate; ' + // outside
'outside replaced=isolate; ' + // replaced
'sibling=non-isolate' )
} ) ;
} ) ;
2013-11-05 20:31:20 +00:00
it ( 'should require controller of a non-isolate directive from an isolate directive on the ' +
'same element' , function ( ) {
var NonIsolateController = function ( ) { } ;
var nonIsolateDirControllerInIsolateDirective ;
module ( function ( ) {
directive ( 'isolate' , function ( ) {
return {
scope : { } ,
require : 'nonIsolate' ,
link : function ( _ , _ _ , _ _ _ , nonIsolateDirController ) {
nonIsolateDirControllerInIsolateDirective = nonIsolateDirController ;
}
} ;
} ) ;
directive ( 'nonIsolate' , function ( ) {
return {
controller : NonIsolateController
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div isolate non-isolate></div>' ) ( $rootScope ) ;
expect ( nonIsolateDirControllerInIsolateDirective ) . toBeDefined ( ) ;
expect ( nonIsolateDirControllerInIsolateDirective instanceof NonIsolateController ) . toBe ( true ) ;
} ) ;
} ) ;
2013-05-25 00:18:51 +00:00
it ( 'should support controllerAs' , function ( ) {
2013-05-25 00:18:51 +00:00
module ( function ( ) {
directive ( 'main' , function ( ) {
return {
templateUrl : 'main.html' ,
transclude : true ,
scope : { } ,
controller : function ( ) {
this . name = 'lucas' ;
} ,
controllerAs : 'mainCtrl'
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , $compile , $rootScope ) {
$templateCache . put ( 'main.html' , '<span>template:{{mainCtrl.name}} <div ng-transclude></div></span>' ) ;
element = $compile ( '<div main>transclude:{{mainCtrl.name}}</div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toBe ( 'template:lucas transclude:' ) ;
} ) ;
} ) ;
it ( 'should support controller alias' , function ( ) {
module ( function ( $controllerProvider ) {
$controllerProvider . register ( 'MainCtrl' , function ( ) {
this . name = 'lucas' ;
} ) ;
directive ( 'main' , function ( ) {
return {
templateUrl : 'main.html' ,
scope : { } ,
controller : 'MainCtrl as mainCtrl'
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , $compile , $rootScope ) {
$templateCache . put ( 'main.html' , '<span>{{mainCtrl.name}}</span>' ) ;
element = $compile ( '<div main></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toBe ( 'lucas' ) ;
} ) ;
} ) ;
2012-01-28 00:18:16 +00:00
it ( 'should require controller on parent element' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'main' , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
controller : function ( ) {
this . name = 'main' ;
}
} ;
} ) ;
2012-06-04 19:08:27 +00:00
directive ( 'dep' , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
require : '^main' ,
link : function ( scope , element , attrs , controller ) {
log ( 'dep:' + controller . name ) ;
}
} ;
} ) ;
} ) ;
inject ( function ( log , $compile , $rootScope ) {
element = $compile ( '<div main><div dep></div></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( 'dep:main' ) ;
} ) ;
} ) ;
2013-05-24 18:00:14 +00:00
it ( "should throw an error if required controller can't be found" , function ( ) {
module ( function ( ) {
directive ( 'dep' , function ( log ) {
return {
require : '^main' ,
link : function ( scope , element , attrs , controller ) {
log ( 'dep:' + controller . name ) ;
}
} ;
} ) ;
} ) ;
inject ( function ( log , $compile , $rootScope ) {
expect ( function ( ) {
$compile ( '<div main><div dep></div></div>' ) ( $rootScope ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr ( "$compile" , "ctreq" , "Controller 'main', required by directive 'dep', can't be found!" ) ;
2013-05-24 18:00:14 +00:00
} ) ;
} ) ;
2012-01-28 00:18:16 +00:00
it ( 'should have optional controller on current element' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'dep' , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
require : '?main' ,
link : function ( scope , element , attrs , controller ) {
log ( 'dep:' + ! ! controller ) ;
}
} ;
} ) ;
} ) ;
inject ( function ( log , $compile , $rootScope ) {
element = $compile ( '<div main><div dep></div></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( 'dep:false' ) ;
} ) ;
} ) ;
it ( 'should support multiple controllers' , function ( ) {
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'c1' , valueFn ( {
2012-01-28 00:18:16 +00:00
controller : function ( ) { this . name = 'c1' ; }
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'c2' , valueFn ( {
2012-01-28 00:18:16 +00:00
controller : function ( ) { this . name = 'c2' ; }
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'dep' , function ( log ) {
2012-01-28 00:18:16 +00:00
return {
require : [ '^c1' , '^c2' ] ,
link : function ( scope , element , attrs , controller ) {
log ( 'dep:' + controller [ 0 ] . name + '-' + controller [ 1 ] . name ) ;
}
} ;
} ) ;
} ) ;
inject ( function ( log , $compile , $rootScope ) {
element = $compile ( '<div c1 c2><div dep></div></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( 'dep:c1-c2' ) ;
} ) ;
2012-05-02 23:04:11 +00:00
} ) ;
2012-01-28 00:18:16 +00:00
2012-05-02 23:04:11 +00:00
it ( 'should instantiate the controller just once when template/templateUrl' , function ( ) {
var syncCtrlSpy = jasmine . createSpy ( 'sync controller' ) ,
asyncCtrlSpy = jasmine . createSpy ( 'async controller' ) ;
2012-06-04 19:08:27 +00:00
module ( function ( ) {
directive ( 'myDirectiveSync' , valueFn ( {
2012-05-02 23:04:11 +00:00
template : '<div>Hello!</div>' ,
controller : syncCtrlSpy
} ) ) ;
2012-06-04 19:08:27 +00:00
directive ( 'myDirectiveAsync' , valueFn ( {
2012-05-02 23:04:11 +00:00
templateUrl : 'myDirectiveAsync.html' ,
controller : asyncCtrlSpy ,
compile : function ( ) {
return function ( ) {
}
}
} ) ) ;
} ) ;
inject ( function ( $templateCache , $compile , $rootScope ) {
expect ( syncCtrlSpy ) . not . toHaveBeenCalled ( ) ;
expect ( asyncCtrlSpy ) . not . toHaveBeenCalled ( ) ;
$templateCache . put ( 'myDirectiveAsync.html' , '<div>Hello!</div>' ) ;
element = $compile ( '<div>' +
'<span xmy-directive-sync></span>' +
'<span my-directive-async></span>' +
'</div>' ) ( $rootScope ) ;
expect ( syncCtrlSpy ) . not . toHaveBeenCalled ( ) ;
expect ( asyncCtrlSpy ) . not . toHaveBeenCalled ( ) ;
$rootScope . $apply ( ) ;
//expect(syncCtrlSpy).toHaveBeenCalledOnce();
expect ( asyncCtrlSpy ) . toHaveBeenCalledOnce ( ) ;
} ) ;
2012-01-28 00:18:16 +00:00
} ) ;
2013-07-18 18:30:32 +00:00
it ( 'should instantiate controllers in the parent->child order when transluction, templateUrl and replacement ' +
'are in the mix' , function ( ) {
// When a child controller is in the transclusion that replaces the parent element that has a directive with
// a controller, we should ensure that we first instantiate the parent and only then stuff that comes from the
// transclusion.
//
// The transclusion moves the child controller onto the same element as parent controller so both controllers are
// on the same level.
module ( function ( ) {
directive ( 'parentDirective' , function ( ) {
return {
transclude : true ,
replace : true ,
templateUrl : 'parentDirective.html' ,
controller : function ( log ) { log ( 'parentController' ) ; }
} ;
} ) ;
directive ( 'childDirective' , function ( ) {
return {
require : '^parentDirective' ,
templateUrl : 'childDirective.html' ,
controller : function ( log ) { log ( 'childController' ) ; }
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , log , $compile , $rootScope ) {
$templateCache . put ( 'parentDirective.html' , '<div ng-transclude>parentTemplateText;</div>' ) ;
$templateCache . put ( 'childDirective.html' , '<span>childTemplateText;</span>' ) ;
element = $compile ( '<div parent-directive><div child-directive></div>childContentText;</div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'parentController; childController' ) ;
2013-08-20 23:31:09 +00:00
expect ( element . text ( ) ) . toBe ( 'childTemplateText;childContentText;' )
2013-07-18 18:30:32 +00:00
} ) ;
} ) ;
2013-08-10 02:56:10 +00:00
it ( 'should instantiate the controller after the isolate scope bindings are initialized (with template)' , function ( ) {
module ( function ( ) {
var Ctrl = function ( $scope , log ) {
log ( 'myFoo=' + $scope . myFoo ) ;
} ;
directive ( 'myDirective' , function ( ) {
return {
scope : {
myFoo : "="
} ,
template : '<p>Hello</p>' ,
controller : Ctrl
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , $compile , $rootScope , log ) {
$rootScope . foo = "bar" ;
element = $compile ( '<div my-directive my-foo="foo"></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'myFoo=bar' ) ;
} ) ;
} ) ;
it ( 'should instantiate the controller after the isolate scope bindings are initialized (with templateUrl)' , function ( ) {
module ( function ( ) {
var Ctrl = function ( $scope , log ) {
log ( 'myFoo=' + $scope . myFoo ) ;
} ;
directive ( 'myDirective' , function ( ) {
return {
scope : {
myFoo : "="
} ,
templateUrl : 'hello.html' ,
controller : Ctrl
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , $compile , $rootScope , log ) {
$templateCache . put ( 'hello.html' , '<p>Hello</p>' ) ;
$rootScope . foo = "bar" ;
element = $compile ( '<div my-directive my-foo="foo"></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'myFoo=bar' ) ;
} ) ;
} ) ;
2013-07-18 18:30:32 +00:00
it ( 'should instantiate controllers in the parent->child->baby order when nested transluction, templateUrl and ' +
'replacement are in the mix' , function ( ) {
// similar to the test above, except that we have one more layer of nesting and nested transclusion
module ( function ( ) {
directive ( 'parentDirective' , function ( ) {
return {
transclude : true ,
replace : true ,
templateUrl : 'parentDirective.html' ,
controller : function ( log ) { log ( 'parentController' ) ; }
} ;
} ) ;
directive ( 'childDirective' , function ( ) {
return {
require : '^parentDirective' ,
transclude : true ,
replace : true ,
templateUrl : 'childDirective.html' ,
controller : function ( log ) { log ( 'childController' ) ; }
} ;
} ) ;
directive ( 'babyDirective' , function ( ) {
return {
require : '^childDirective' ,
templateUrl : 'babyDirective.html' ,
controller : function ( log ) { log ( 'babyController' ) ; }
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , log , $compile , $rootScope ) {
$templateCache . put ( 'parentDirective.html' , '<div ng-transclude>parentTemplateText;</div>' ) ;
$templateCache . put ( 'childDirective.html' , '<span ng-transclude>childTemplateText;</span>' ) ;
$templateCache . put ( 'babyDirective.html' , '<span>babyTemplateText;</span>' ) ;
element = $compile ( '<div parent-directive>' +
'<div child-directive>' +
'childContentText;' +
'<div baby-directive>babyContent;</div>' +
'</div>' +
'</div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'parentController; childController; babyController' ) ;
2013-08-20 23:31:09 +00:00
expect ( element . text ( ) ) . toBe ( 'childContentText;babyTemplateText;' )
2013-07-18 18:30:32 +00:00
} ) ;
} ) ;
2013-08-08 23:04:11 +00:00
it ( 'should allow controller usage in pre-link directive functions with templateUrl' , function ( ) {
module ( function ( ) {
var Ctrl = function ( log ) {
log ( 'instance' ) ;
} ;
directive ( 'myDirective' , function ( ) {
return {
scope : true ,
templateUrl : 'hello.html' ,
controller : Ctrl ,
compile : function ( ) {
return {
pre : function ( scope , template , attr , ctrl ) { } ,
post : function ( ) { }
} ;
}
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , $compile , $rootScope , log ) {
$templateCache . put ( 'hello.html' , '<p>Hello</p>' ) ;
element = $compile ( '<div my-directive></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'instance' ) ;
expect ( element . text ( ) ) . toBe ( 'Hello' ) ;
} ) ;
} ) ;
it ( 'should allow controller usage in pre-link directive functions with a template' , function ( ) {
module ( function ( ) {
var Ctrl = function ( log ) {
log ( 'instance' ) ;
} ;
directive ( 'myDirective' , function ( ) {
return {
scope : true ,
template : '<p>Hello</p>' ,
controller : Ctrl ,
compile : function ( ) {
return {
pre : function ( scope , template , attr , ctrl ) { } ,
post : function ( ) { }
} ;
}
} ;
} ) ;
} ) ;
inject ( function ( $templateCache , $compile , $rootScope , log ) {
element = $compile ( '<div my-directive></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'instance' ) ;
expect ( element . text ( ) ) . toBe ( 'Hello' ) ;
} ) ;
} ) ;
2012-01-28 00:18:16 +00:00
} ) ;
describe ( 'transclude' , function ( ) {
2013-10-26 03:56:51 +00:00
describe ( 'content transclusion' , function ( ) {
it ( 'should support transclude directive' , function ( ) {
module ( function ( ) {
directive ( 'trans' , function ( ) {
return {
transclude : 'content' ,
replace : true ,
scope : true ,
template : '<ul><li>W:{{$parent.$id}}-{{$id}};</li><li ng-transclude></li></ul>'
2012-01-28 00:18:16 +00:00
}
2013-10-26 03:56:51 +00:00
} ) ;
} ) ;
inject ( function ( log , $rootScope , $compile ) {
element = $compile ( '<div><div trans>T:{{$parent.$id}}-{{$id}}<span>;</span></div></div>' )
( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toEqual ( 'W:001-002;T:001-003;' ) ;
expect ( jqLite ( element . find ( 'span' ) [ 0 ] ) . text ( ) ) . toEqual ( 'T:001-003' ) ;
expect ( jqLite ( element . find ( 'span' ) [ 1 ] ) . text ( ) ) . toEqual ( ';' ) ;
2012-01-28 00:18:16 +00:00
} ) ;
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should transclude transcluded content' , function ( ) {
module ( function ( ) {
directive ( 'book' , valueFn ( {
2012-01-28 00:18:16 +00:00
transclude : 'content' ,
2013-10-26 03:56:51 +00:00
template : '<div>book-<div chapter>(<div ng-transclude></div>)</div></div>'
} ) ) ;
directive ( 'chapter' , valueFn ( {
transclude : 'content' ,
templateUrl : 'chapter.html'
} ) ) ;
directive ( 'section' , valueFn ( {
transclude : 'content' ,
template : '<div>section-!<div ng-transclude></div>!</div></div>'
} ) ) ;
return function ( $httpBackend ) {
$httpBackend .
expect ( 'GET' , 'chapter.html' ) .
respond ( '<div>chapter-<div section>[<div ng-transclude></div>]</div></div>' ) ;
2012-01-28 00:18:16 +00:00
}
} ) ;
2013-10-26 03:56:51 +00:00
inject ( function ( log , $rootScope , $compile , $httpBackend ) {
element = $compile ( '<div><div book>paragraph</div></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
2012-01-28 00:18:16 +00:00
2013-10-26 03:56:51 +00:00
expect ( element . text ( ) ) . toEqual ( 'book-' ) ;
2012-01-28 00:18:16 +00:00
2013-10-26 03:56:51 +00:00
$httpBackend . flush ( ) ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toEqual ( 'book-chapter-section-![(paragraph)]!' ) ;
} ) ;
2012-01-28 00:18:16 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should only allow one content transclusion per element' , function ( ) {
module ( function ( ) {
directive ( 'first' , valueFn ( {
transclude : true
} ) ) ;
directive ( 'second' , valueFn ( {
transclude : true
} ) ) ;
} ) ;
inject ( function ( $compile ) {
expect ( function ( ) {
$compile ( '<div first="" second=""></div>' ) ;
} ) . toThrowMinErr ( '$compile' , 'multidir' , /Multiple directives \[first, second\] asking for transclusion on: <div .+/ ) ;
} ) ;
2012-01-28 00:18:16 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should remove transclusion scope, when the DOM is destroyed' , function ( ) {
module ( function ( ) {
directive ( 'box' , valueFn ( {
transclude : true ,
scope : { name : '=' , show : '=' } ,
template : '<div><h1>Hello: {{name}}!</h1><div ng-transclude></div></div>' ,
link : function ( scope , element ) {
scope . $watch (
'show' ,
function ( show ) {
if ( ! show ) {
element . find ( 'div' ) . find ( 'div' ) . remove ( ) ;
}
}
) ;
}
} ) ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
$rootScope . username = 'Misko' ;
$rootScope . select = true ;
element = $compile (
'<div><div box name="username" show="select">user: {{username}}</div></div>' )
( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toEqual ( 'Hello: Misko!user: Misko' ) ;
2013-10-11 04:35:54 +00:00
2013-10-26 03:56:51 +00:00
var widgetScope = $rootScope . $$childHead ;
var transcludeScope = widgetScope . $$nextSibling ;
expect ( widgetScope . name ) . toEqual ( 'Misko' ) ;
expect ( widgetScope . $parent ) . toEqual ( $rootScope ) ;
expect ( transcludeScope . $parent ) . toEqual ( $rootScope ) ;
2013-10-11 04:35:54 +00:00
2013-10-26 03:56:51 +00:00
$rootScope . select = false ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toEqual ( 'Hello: Misko!' ) ;
expect ( widgetScope . $$nextSibling ) . toEqual ( null ) ;
} ) ;
2013-10-11 04:35:54 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should add a $$transcluded property onto the transcluded scope' , function ( ) {
module ( function ( ) {
directive ( 'trans' , function ( ) {
return {
transclude : true ,
replace : true ,
scope : true ,
template : '<div><span>I:{{$$transcluded}}</span><div ng-transclude></div></div>'
} ;
} ) ;
} ) ;
inject ( function ( log , $rootScope , $compile ) {
element = $compile ( '<div><div trans>T:{{$$transcluded}}</div></div>' )
( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( jqLite ( element . find ( 'span' ) [ 0 ] ) . text ( ) ) . toEqual ( 'I:' ) ;
expect ( jqLite ( element . find ( 'span' ) [ 1 ] ) . text ( ) ) . toEqual ( 'T:true' ) ;
} ) ;
2013-10-11 04:35:54 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should clear contents of the ng-translude element before appending transcluded content' , function ( ) {
module ( function ( ) {
directive ( 'trans' , function ( ) {
return {
transclude : true ,
template : '<div ng-transclude>old stuff! </div>'
} ;
} ) ;
} ) ;
inject ( function ( log , $rootScope , $compile ) {
element = $compile ( '<div trans>unicorn!</div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude=""><span>unicorn!</span></div>' ) ;
} ) ;
2012-01-28 00:18:16 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should throw on an ng-translude element inside no transclusion directive' , function ( ) {
inject ( function ( $rootScope , $compile ) {
// we need to do this because different browsers print empty attributres differently
try {
$compile ( '<div><div ng-transclude></div></div>' ) ( $rootScope ) ;
} catch ( e ) {
expect ( e . message ) . toMatch ( new RegExp (
'^\\\[ngTransclude:orphan\\\] ' +
'Illegal use of ngTransclude directive in the template! ' +
'No parent directive that requires a transclusion found\. ' +
'Element: <div ng-transclude.+' ) ) ;
2012-01-28 00:18:16 +00:00
}
2013-10-26 03:56:51 +00:00
} ) ;
2012-01-28 00:18:16 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should make the result of a transclusion available to the parent directive in post-linking phase' +
'(template)' , function ( ) {
module ( function ( ) {
directive ( 'trans' , function ( log ) {
return {
transclude : true ,
template : '<div ng-transclude></div>' ,
link : {
pre : function ( $scope , $element ) {
log ( 'pre(' + $element . text ( ) + ')' ) ;
} ,
post : function ( $scope , $element ) {
log ( 'post(' + $element . text ( ) + ')' ) ;
}
}
2012-03-16 05:18:06 +00:00
} ;
2013-10-26 03:56:51 +00:00
} ) ;
} ) ;
inject ( function ( log , $rootScope , $compile ) {
element = $compile ( '<div trans><span>unicorn!</span></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'pre(); post(unicorn!)' ) ;
} ) ;
2012-03-16 05:18:06 +00:00
} ) ;
2013-01-10 06:07:33 +00:00
2013-01-23 05:01:13 +00:00
2013-10-26 03:56:51 +00:00
it ( 'should make the result of a transclusion available to the parent directive in post-linking phase' +
'(templateUrl)' , function ( ) {
// when compiling an async directive the transclusion is always processed before the directive
// this is different compared to sync directive. delaying the transclusion makes little sense.
2013-01-23 05:01:13 +00:00
2013-10-26 03:56:51 +00:00
module ( function ( ) {
directive ( 'trans' , function ( log ) {
return {
transclude : true ,
templateUrl : 'trans.html' ,
link : {
pre : function ( $scope , $element ) {
log ( 'pre(' + $element . text ( ) + ')' ) ;
} ,
post : function ( $scope , $element ) {
log ( 'post(' + $element . text ( ) + ')' ) ;
}
}
} ;
} ) ;
2013-01-23 05:01:13 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
inject ( function ( log , $rootScope , $compile , $templateCache ) {
$templateCache . put ( 'trans.html' , '<div ng-transclude></div>' ) ;
2013-08-20 23:08:24 +00:00
2013-10-26 03:56:51 +00:00
element = $compile ( '<div trans><span>unicorn!</span></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'pre(); post(unicorn!)' ) ;
2013-08-20 23:31:09 +00:00
} ) ;
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should make the result of a transclusion available to the parent *replace* directive in post-linking phase' +
'(template)' , function ( ) {
module ( function ( ) {
directive ( 'replacedTrans' , function ( log ) {
return {
transclude : true ,
replace : true ,
template : '<div ng-transclude></div>' ,
link : {
pre : function ( $scope , $element ) {
log ( 'pre(' + $element . text ( ) + ')' ) ;
} ,
post : function ( $scope , $element ) {
log ( 'post(' + $element . text ( ) + ')' ) ;
}
}
} ;
} ) ;
} ) ;
inject ( function ( log , $rootScope , $compile ) {
element = $compile ( '<div replaced-trans><span>unicorn!</span></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'pre(); post(unicorn!)' ) ;
} ) ;
2013-09-25 23:37:59 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should make the result of a transclusion available to the parent *replace* directive in post-linking phase' +
' (templateUrl)' , function ( ) {
module ( function ( ) {
directive ( 'replacedTrans' , function ( log ) {
return {
transclude : true ,
replace : true ,
templateUrl : 'trans.html' ,
link : {
pre : function ( $scope , $element ) {
log ( 'pre(' + $element . text ( ) + ')' ) ;
} ,
post : function ( $scope , $element ) {
log ( 'post(' + $element . text ( ) + ')' ) ;
}
2013-08-20 23:08:24 +00:00
}
2013-10-26 03:56:51 +00:00
} ;
} ) ;
} ) ;
inject ( function ( log , $rootScope , $compile , $templateCache ) {
$templateCache . put ( 'trans.html' , '<div ng-transclude></div>' ) ;
element = $compile ( '<div replaced-trans><span>unicorn!</span></div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'pre(); post(unicorn!)' ) ;
2013-08-20 23:08:24 +00:00
} ) ;
} ) ;
2013-11-14 21:50:36 +00:00
it ( 'should copy the directive controller to all clones' , function ( ) {
var transcludeCtrl , cloneCount = 2 ;
module ( function ( ) {
directive ( 'transclude' , valueFn ( {
transclude : 'content' ,
controller : function ( $transclude ) {
transcludeCtrl = this ;
} ,
link : function ( scope , el , attr , ctrl , $transclude ) {
var i ;
for ( i = 0 ; i < cloneCount ; i ++ ) {
$transclude ( cloneAttach ) ;
}
function cloneAttach ( clone ) {
el . append ( clone ) ;
}
}
} ) ) ;
} ) ;
inject ( function ( $compile ) {
element = $compile ( '<div transclude><span></span></div>' ) ( $rootScope ) ;
var children = element . children ( ) , i ;
expect ( transcludeCtrl ) . toBeDefined ( ) ;
expect ( element . data ( '$transcludeController' ) ) . toBe ( transcludeCtrl ) ;
for ( i = 0 ; i < cloneCount ; i ++ ) {
expect ( children . eq ( i ) . data ( '$transcludeController' ) ) . toBeUndefined ( ) ;
}
} ) ;
} ) ;
it ( 'should provide the $transclude controller local as 5th argument to the pre and post-link function' , function ( ) {
var ctrlTransclude , preLinkTransclude , postLinkTransclude ;
module ( function ( ) {
directive ( 'transclude' , valueFn ( {
transclude : 'content' ,
controller : function ( $transclude ) {
ctrlTransclude = $transclude ;
} ,
compile : function ( ) {
return {
pre : function ( scope , el , attr , ctrl , $transclude ) {
preLinkTransclude = $transclude ;
} ,
post : function ( scope , el , attr , ctrl , $transclude ) {
postLinkTransclude = $transclude ;
}
} ;
}
} ) ) ;
} ) ;
inject ( function ( $compile ) {
element = $compile ( '<div transclude></div>' ) ( $rootScope ) ;
expect ( ctrlTransclude ) . toBeDefined ( ) ;
expect ( ctrlTransclude ) . toBe ( preLinkTransclude ) ;
expect ( ctrlTransclude ) . toBe ( postLinkTransclude ) ;
} ) ;
} ) ;
it ( 'should allow an optional scope argument in $transclude' , function ( ) {
var capturedChildCtrl ;
module ( function ( ) {
directive ( 'transclude' , valueFn ( {
transclude : 'content' ,
link : function ( scope , element , attr , ctrl , $transclude ) {
$transclude ( scope , function ( clone ) {
element . append ( clone ) ;
} ) ;
}
} ) ) ;
} ) ;
inject ( function ( $compile ) {
element = $compile ( '<div transclude>{{$id}}</div>' ) ( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( element . text ( ) ) . toBe ( $rootScope . $id ) ;
} ) ;
} ) ;
it ( 'should expose the directive controller to transcluded children' , function ( ) {
var capturedChildCtrl ;
module ( function ( ) {
directive ( 'transclude' , valueFn ( {
transclude : 'content' ,
controller : function ( ) {
} ,
link : function ( scope , element , attr , ctrl , $transclude ) {
$transclude ( function ( clone ) {
element . append ( clone ) ;
} ) ;
}
} ) ) ;
directive ( 'child' , valueFn ( {
require : '^transclude' ,
link : function ( scope , element , attr , ctrl ) {
capturedChildCtrl = ctrl ;
}
} ) ) ;
} ) ;
inject ( function ( $compile ) {
element = $compile ( '<div transclude><div child></div></div>' ) ( $rootScope ) ;
expect ( capturedChildCtrl ) . toBeTruthy ( ) ;
} ) ;
} ) ;
2013-08-20 23:08:24 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
describe ( 'element transclusion' , function ( ) {
2013-08-20 23:08:24 +00:00
2013-10-26 03:56:51 +00:00
it ( 'should support basic element transclusion' , function ( ) {
module ( function ( ) {
directive ( 'trans' , function ( log ) {
return {
transclude : 'element' ,
priority : 2 ,
controller : function ( $transclude ) { this . $transclude = $transclude ; } ,
compile : function ( element , attrs , template ) {
log ( 'compile: ' + angular . mock . dump ( element ) ) ;
return function ( scope , element , attrs , ctrl ) {
log ( 'link' ) ;
var cursor = element ;
template ( scope . $new ( ) , function ( clone ) { cursor . after ( cursor = clone ) } ) ;
ctrl . $transclude ( function ( clone ) { cursor . after ( clone ) } ) ;
} ;
2013-08-20 23:08:24 +00:00
}
}
2013-10-26 03:56:51 +00:00
} ) ;
} ) ;
inject ( function ( log , $rootScope , $compile ) {
element = $compile ( '<div><div high-log trans="text" log>{{$parent.$id}}-{{$id}};</div></div>' )
( $rootScope ) ;
$rootScope . $apply ( ) ;
expect ( log ) . toEqual ( 'compile: <!-- trans: text -->; link; LOG; LOG; HIGH' ) ;
expect ( element . text ( ) ) . toEqual ( '001-002;001-003;' ) ;
2013-08-20 23:08:24 +00:00
} ) ;
} ) ;
2013-10-03 23:24:24 +00:00
2013-10-26 03:56:51 +00:00
it ( 'should only allow one element transclusion per element' , function ( ) {
module ( function ( ) {
directive ( 'first' , valueFn ( {
transclude : 'element'
} ) ) ;
directive ( 'second' , valueFn ( {
transclude : 'element'
} ) ) ;
} ) ;
inject ( function ( $compile ) {
expect ( function ( ) {
$compile ( '<div first second></div>' ) ;
} ) . toThrowMinErr ( '$compile' , 'multidir' , 'Multiple directives [first, second] asking for transclusion on: ' +
'<!-- first: -->' ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} ) ;
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should only allow one element transclusion per element when directives have different priorities' , function ( ) {
// we restart compilation in this case and we need to remember the duplicates during the second compile
// regression #3893
module ( function ( ) {
directive ( 'first' , valueFn ( {
transclude : 'element' ,
priority : 100
} ) ) ;
directive ( 'second' , valueFn ( {
transclude : 'element'
} ) ) ;
} ) ;
inject ( function ( $compile ) {
expect ( function ( ) {
$compile ( '<div first second></div>' ) ;
} ) . toThrowMinErr ( '$compile' , 'multidir' , /Multiple directives \[first, second\] asking for transclusion on: <div .+/ ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} ) ;
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should only allow one element transclusion per element when async replace directive is in the mix' , function ( ) {
module ( function ( ) {
directive ( 'template' , valueFn ( {
templateUrl : 'template.html' ,
replace : true
} ) ) ;
directive ( 'first' , valueFn ( {
transclude : 'element' ,
priority : 100
} ) ) ;
directive ( 'second' , valueFn ( {
transclude : 'element'
} ) ) ;
} ) ;
inject ( function ( $compile , $httpBackend ) {
$httpBackend . expectGET ( 'template.html' ) . respond ( '<p second>template.html</p>' ) ;
$compile ( '<div template first></div>' ) ;
expect ( function ( ) {
$httpBackend . flush ( ) ;
} ) . toThrowMinErr ( '$compile' , 'multidir' , /Multiple directives \[first, second\] asking for transclusion on: <p .+/ ) ;
} ) ;
fix($compile): fix (reverse) directive postLink fn execution order
previously the compile/link fns executed in this order controlled via priority:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityHigh, PostLinkPriorityMedium, PostLinkPriorityLow
This was changed to:
- CompilePriorityHigh, CompilePriorityMedium, CompilePriorityLow
- PreLinkPriorityHigh, PreLinkPriorityMedium, PreLinkPriorityLow
- link children
- PostLinkPriorityLow, PostLinkPriorityMedium , PostLinkPriorityHigh
Using this order the child transclusion directive that gets replaced
onto the current element get executed correctly (see issue #3558),
and more generally, the order of execution of post linking function
makes more sense. The incorrect order was an oversight that has
gone unnoticed for many suns and moons.
(FYI: postLink functions are the default linking functions)
BREAKING CHANGE: the order of postLink fn is now mirror opposite of
the order in which corresponding preLinking and compile functions
execute.
Very few directives in practice rely on order of postLinking function
(unlike on the order of compile functions), so in the rare case
of this change affecting an existing directive, it might be necessary
to convert it to a preLinking function or give it negative priority
(look at the diff of this commit to see how an internal attribute
interpolation directive was adjusted).
Closes #3558
2013-10-01 14:55:47 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should support transcluded element on root content' , function ( ) {
var comment ;
module ( function ( ) {
directive ( 'transclude' , valueFn ( {
2013-10-03 23:24:24 +00:00
transclude : 'element' ,
2013-10-26 03:56:51 +00:00
compile : function ( element , attr , linker ) {
return function ( scope , element , attr ) {
comment = element ;
} ;
}
} ) ) ;
2013-10-03 23:24:24 +00:00
} ) ;
2013-10-26 03:56:51 +00:00
inject ( function ( $compile , $rootScope ) {
var element = jqLite ( '<div>before<div transclude></div>after</div>' ) . contents ( ) ;
expect ( element . length ) . toEqual ( 3 ) ;
expect ( nodeName _ ( element [ 1 ] ) ) . toBe ( 'DIV' ) ;
$compile ( element ) ( $rootScope ) ;
expect ( nodeName _ ( element [ 1 ] ) ) . toBe ( '#comment' ) ;
expect ( nodeName _ ( comment ) ) . toBe ( '#comment' ) ;
2013-10-03 23:24:24 +00:00
} ) ;
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should terminate compilation only for element trasclusion' , function ( ) {
module ( function ( ) {
directive ( 'elementTrans' , function ( log ) {
return {
transclude : 'element' ,
priority : 50 ,
compile : log . fn ( 'compile:elementTrans' )
} ;
} ) ;
directive ( 'regularTrans' , function ( log ) {
return {
transclude : true ,
priority : 50 ,
compile : log . fn ( 'compile:regularTrans' )
} ;
} ) ;
} ) ;
inject ( function ( log , $compile , $rootScope ) {
$compile ( '<div><div element-trans log="elem"></div><div regular-trans log="regular"></div></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( 'compile:elementTrans; compile:regularTrans; regular' ) ;
} ) ;
2013-10-03 23:24:24 +00:00
} ) ;
2013-10-26 02:10:50 +00:00
it ( 'should instantiate high priority controllers only once, but low priority ones each time we transclude' ,
function ( ) {
module ( function ( ) {
directive ( 'elementTrans' , function ( log ) {
return {
transclude : 'element' ,
priority : 50 ,
controller : function ( $transclude , $element ) {
log ( 'controller:elementTrans' ) ;
$transclude ( function ( clone ) {
$element . after ( clone ) ;
} ) ;
$transclude ( function ( clone ) {
$element . after ( clone ) ;
} ) ;
$transclude ( function ( clone ) {
$element . after ( clone ) ;
} ) ;
}
} ;
} ) ;
directive ( 'normalDir' , function ( log ) {
return {
controller : function ( ) {
log ( 'controller:normalDir' ) ;
}
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope , log ) {
element = $compile ( '<div><div element-trans normal-dir></div></div>' ) ( $rootScope ) ;
expect ( log ) . toEqual ( [
'controller:elementTrans' ,
'controller:normalDir' ,
'controller:normalDir' ,
'controller:normalDir'
] ) ;
} ) ;
} ) ;
2013-10-26 03:56:51 +00:00
2013-11-14 21:50:36 +00:00
it ( 'should allow to access $transclude in the same directive' , function ( ) {
var _$transclude ;
module ( function ( ) {
directive ( 'transclude' , valueFn ( {
transclude : 'element' ,
controller : function ( $transclude ) {
_$transclude = $transclude ;
}
} ) ) ;
} ) ;
inject ( function ( $compile ) {
element = $compile ( '<div transclude></div>' ) ( $rootScope ) ;
expect ( _$transclude ) . toBeDefined ( )
} ) ;
} ) ;
it ( 'should copy the directive controller to all clones' , function ( ) {
var transcludeCtrl , cloneCount = 2 ;
module ( function ( ) {
directive ( 'transclude' , valueFn ( {
transclude : 'element' ,
controller : function ( ) {
transcludeCtrl = this ;
} ,
link : function ( scope , el , attr , ctrl , $transclude ) {
var i ;
for ( i = 0 ; i < cloneCount ; i ++ ) {
$transclude ( cloneAttach ) ;
}
function cloneAttach ( clone ) {
el . after ( clone ) ;
}
}
} ) ) ;
} ) ;
inject ( function ( $compile ) {
element = $compile ( '<div><div transclude></div></div>' ) ( $rootScope ) ;
var children = element . children ( ) , i ;
for ( i = 0 ; i < cloneCount ; i ++ ) {
expect ( children . eq ( i ) . data ( '$transcludeController' ) ) . toBe ( transcludeCtrl ) ;
}
} ) ;
} ) ;
it ( 'should expose the directive controller to transcluded children' , function ( ) {
var capturedTranscludeCtrl ;
module ( function ( ) {
directive ( 'transclude' , valueFn ( {
transclude : 'element' ,
controller : function ( ) {
} ,
link : function ( scope , element , attr , ctrl , $transclude ) {
$transclude ( scope , function ( clone ) {
element . after ( clone ) ;
} ) ;
}
} ) ) ;
directive ( 'child' , valueFn ( {
require : '^transclude' ,
link : function ( scope , element , attr , ctrl ) {
capturedTranscludeCtrl = ctrl ;
}
} ) ) ;
} ) ;
inject ( function ( $compile ) {
element = $compile ( '<div transclude><div child></div></div>' ) ( $rootScope ) ;
expect ( capturedTranscludeCtrl ) . toBeTruthy ( ) ;
} ) ;
} ) ;
it ( 'should allow access to $transclude in a templateUrl directive' , function ( ) {
var transclude ;
module ( function ( ) {
directive ( 'template' , valueFn ( {
templateUrl : 'template.html' ,
replace : true
} ) ) ;
directive ( 'transclude' , valueFn ( {
transclude : 'content' ,
controller : function ( $transclude ) {
transclude = $transclude ;
}
} ) ) ;
} ) ;
inject ( function ( $compile , $httpBackend ) {
$httpBackend . expectGET ( 'template.html' ) . respond ( '<div transclude></div>' ) ;
element = $compile ( '<div template></div>' ) ( $rootScope ) ;
$httpBackend . flush ( ) ;
expect ( transclude ) . toBeDefined ( ) ;
} ) ;
} ) ;
} ) ;
2013-10-26 03:56:51 +00:00
it ( 'should safely create transclude comment node and not break with "-->"' ,
inject ( function ( $rootScope ) {
// see: https://github.com/angular/angular.js/issues/1740
element = $compile ( '<ul><li ng-repeat="item in [\'-->\', \'x\']">{{item}}|</li></ul>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toBe ( '-->|x|' ) ;
} ) ) ;
2013-02-19 17:55:05 +00:00
} ) ;
2013-08-20 23:08:24 +00:00
describe ( 'img[src] sanitization' , function ( ) {
2013-11-25 23:40:18 +00:00
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
it ( 'should NOT require trusted values for img src' , inject ( function ( $rootScope , $compile , $sce ) {
2013-06-21 19:33:03 +00:00
element = $compile ( '<img src="{{testUrl}}"></img>' ) ( $rootScope ) ;
$rootScope . testUrl = 'http://example.com/image.png' ;
$rootScope . $digest ( ) ;
expect ( element . attr ( 'src' ) ) . toEqual ( 'http://example.com/image.png' ) ;
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
// But it should accept trusted values anyway.
$rootScope . testUrl = $sce . trustAsUrl ( 'http://example.com/image2.png' ) ;
$rootScope . $digest ( ) ;
expect ( element . attr ( 'src' ) ) . toEqual ( 'http://example.com/image2.png' ) ;
2013-06-21 19:33:03 +00:00
} ) ) ;
it ( 'should not sanitize attributes other than src' , inject ( function ( $compile , $rootScope ) {
element = $compile ( '<img title="{{testUrl}}"></img>' ) ( $rootScope ) ;
$rootScope . testUrl = "javascript:doEvilStuff()" ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'title' ) ) . toBe ( 'javascript:doEvilStuff()' ) ;
} ) ) ;
2013-11-25 23:40:18 +00:00
it ( 'should use $$sanitizeUriProvider for reconfiguration of the src whitelist' , function ( ) {
module ( function ( $compileProvider , $$sanitizeUriProvider ) {
var newRe = /javascript:/ ,
returnVal ;
expect ( $compileProvider . imgSrcSanitizationWhitelist ( ) ) . toBe ( $$sanitizeUriProvider . imgSrcSanitizationWhitelist ( ) ) ;
2013-06-21 19:33:03 +00:00
2013-11-25 23:40:18 +00:00
returnVal = $compileProvider . imgSrcSanitizationWhitelist ( newRe ) ;
2013-06-21 19:33:03 +00:00
expect ( returnVal ) . toBe ( $compileProvider ) ;
2013-11-25 23:40:18 +00:00
expect ( $$sanitizeUriProvider . imgSrcSanitizationWhitelist ( ) ) . toBe ( newRe ) ;
expect ( $compileProvider . imgSrcSanitizationWhitelist ( ) ) . toBe ( newRe ) ;
2013-06-21 19:33:03 +00:00
} ) ;
2013-11-25 23:40:18 +00:00
inject ( function ( ) {
// needed to the module definition above is run...
} ) ;
} ) ;
2013-06-21 19:33:03 +00:00
2013-11-25 23:40:18 +00:00
it ( 'should use $$sanitizeUri' , function ( ) {
var $$sanitizeUri = jasmine . createSpy ( '$$sanitizeUri' ) ;
module ( function ( $provide ) {
$provide . value ( '$$sanitizeUri' , $$sanitizeUri ) ;
} ) ;
2013-06-21 19:33:03 +00:00
inject ( function ( $compile , $rootScope ) {
element = $compile ( '<img src="{{testUrl}}"></img>' ) ( $rootScope ) ;
2013-11-25 23:40:18 +00:00
$rootScope . testUrl = "someUrl" ;
2013-06-21 19:33:03 +00:00
2013-11-25 23:40:18 +00:00
$$sanitizeUri . andReturn ( 'someSanitizedUrl' ) ;
2013-06-21 19:33:03 +00:00
$rootScope . $apply ( ) ;
2013-11-25 23:40:18 +00:00
expect ( element . attr ( 'src' ) ) . toBe ( 'someSanitizedUrl' ) ;
expect ( $$sanitizeUri ) . toHaveBeenCalledWith ( $rootScope . testUrl , true ) ;
2013-06-21 19:33:03 +00:00
} ) ;
} ) ;
} ) ;
describe ( 'a[href] sanitization' , function ( ) {
2013-02-19 17:55:05 +00:00
it ( 'should not sanitize href on elements other than anchor' , inject ( function ( $compile , $rootScope ) {
element = $compile ( '<div href="{{testUrl}}"></div>' ) ( $rootScope ) ;
$rootScope . testUrl = "javascript:doEvilStuff()" ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'href' ) ) . toBe ( 'javascript:doEvilStuff()' ) ;
} ) ) ;
it ( 'should not sanitize attributes other than href' , inject ( function ( $compile , $rootScope ) {
element = $compile ( '<a title="{{testUrl}}"></a>' ) ( $rootScope ) ;
$rootScope . testUrl = "javascript:doEvilStuff()" ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'title' ) ) . toBe ( 'javascript:doEvilStuff()' ) ;
} ) ) ;
2013-11-25 23:40:18 +00:00
it ( 'should use $$sanitizeUriProvider for reconfiguration of the href whitelist' , function ( ) {
module ( function ( $compileProvider , $$sanitizeUriProvider ) {
var newRe = /javascript:/ ,
returnVal ;
expect ( $compileProvider . aHrefSanitizationWhitelist ( ) ) . toBe ( $$sanitizeUriProvider . aHrefSanitizationWhitelist ( ) ) ;
2013-02-19 17:55:05 +00:00
2013-11-25 23:40:18 +00:00
returnVal = $compileProvider . aHrefSanitizationWhitelist ( newRe ) ;
2013-02-19 17:55:05 +00:00
expect ( returnVal ) . toBe ( $compileProvider ) ;
2013-11-25 23:40:18 +00:00
expect ( $$sanitizeUriProvider . aHrefSanitizationWhitelist ( ) ) . toBe ( newRe ) ;
expect ( $compileProvider . aHrefSanitizationWhitelist ( ) ) . toBe ( newRe ) ;
} ) ;
inject ( function ( ) {
// needed to the module definition above is run...
2013-02-19 17:55:05 +00:00
} ) ;
2013-11-25 23:40:18 +00:00
} ) ;
2013-02-19 17:55:05 +00:00
2013-11-25 23:40:18 +00:00
it ( 'should use $$sanitizeUri' , function ( ) {
var $$sanitizeUri = jasmine . createSpy ( '$$sanitizeUri' ) ;
module ( function ( $provide ) {
$provide . value ( '$$sanitizeUri' , $$sanitizeUri ) ;
} ) ;
2013-02-19 17:55:05 +00:00
inject ( function ( $compile , $rootScope ) {
element = $compile ( '<a href="{{testUrl}}"></a>' ) ( $rootScope ) ;
2013-11-25 23:40:18 +00:00
$rootScope . testUrl = "someUrl" ;
2013-02-19 17:55:05 +00:00
2013-11-25 23:40:18 +00:00
$$sanitizeUri . andReturn ( 'someSanitizedUrl' ) ;
2013-02-19 17:55:05 +00:00
$rootScope . $apply ( ) ;
2013-11-25 23:40:18 +00:00
expect ( element . attr ( 'href' ) ) . toBe ( 'someSanitizedUrl' ) ;
expect ( $$sanitizeUri ) . toHaveBeenCalledWith ( $rootScope . testUrl , false ) ;
2013-02-19 17:55:05 +00:00
} ) ;
} ) ;
2013-11-25 23:40:18 +00:00
2012-01-28 00:18:16 +00:00
} ) ;
2013-02-25 23:00:47 +00:00
2013-06-21 20:03:56 +00:00
describe ( 'interpolation on HTML DOM event handler attributes onclick, onXYZ, formaction' , function ( ) {
it ( 'should disallow interpolation on onclick' , inject ( function ( $compile , $rootScope ) {
// All interpolations are disallowed.
$rootScope . onClickJs = "" ;
expect ( function ( ) {
$compile ( '<button onclick="{{onClickJs}}"></script>' ) ( $rootScope ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr (
"$compile" , "nodomevents" , "Interpolations for HTML DOM event attributes are disallowed. " +
2013-06-21 20:03:56 +00:00
"Please use the ng- versions (such as ng-click instead of onclick) instead." ) ;
expect ( function ( ) {
$compile ( '<button ONCLICK="{{onClickJs}}"></script>' ) ( $rootScope ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr (
"$compile" , "nodomevents" , "Interpolations for HTML DOM event attributes are disallowed. " +
2013-06-21 20:03:56 +00:00
"Please use the ng- versions (such as ng-click instead of onclick) instead." ) ;
expect ( function ( ) {
$compile ( '<button ng-attr-onclick="{{onClickJs}}"></script>' ) ( $rootScope ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr (
"$compile" , "nodomevents" , "Interpolations for HTML DOM event attributes are disallowed. " +
2013-06-21 20:03:56 +00:00
"Please use the ng- versions (such as ng-click instead of onclick) instead." ) ;
} ) ) ;
it ( 'should pass through arbitrary values on onXYZ event attributes that contain a hyphen' , inject ( function ( $compile , $rootScope ) {
element = $compile ( '<button on-click="{{onClickJs}}"></script>' ) ( $rootScope ) ;
$rootScope . onClickJs = 'javascript:doSomething()' ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'on-click' ) ) . toEqual ( 'javascript:doSomething()' ) ;
} ) ) ;
2013-08-31 09:48:11 +00:00
it ( 'should pass through arbitrary values on "on" and "data-on" attributes' , inject ( function ( $compile , $rootScope ) {
element = $compile ( '<button data-on="{{dataOnVar}}"></script>' ) ( $rootScope ) ;
$rootScope . dataOnVar = 'data-on text' ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'data-on' ) ) . toEqual ( 'data-on text' ) ;
2013-10-03 23:24:24 +00:00
2013-08-31 09:48:11 +00:00
element = $compile ( '<button on="{{onVar}}"></script>' ) ( $rootScope ) ;
$rootScope . onVar = 'on text' ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'on' ) ) . toEqual ( 'on text' ) ;
} ) ) ;
2013-06-21 20:03:56 +00:00
} ) ;
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
describe ( 'iframe[src]' , function ( ) {
it ( 'should pass through src attributes for the same domain' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<iframe src="{{testUrl}}"></iframe>' ) ( $rootScope ) ;
$rootScope . testUrl = "different_page" ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'src' ) ) . toEqual ( 'different_page' ) ;
} ) ) ;
it ( 'should clear out src attributes for a different domain' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<iframe src="{{testUrl}}"></iframe>' ) ( $rootScope ) ;
$rootScope . testUrl = "http://a.different.domain.example.com" ;
2013-08-13 22:30:52 +00:00
expect ( function ( ) { $rootScope . $apply ( ) } ) . toThrowMinErr (
"$interpolate" , "interr" , "Can't interpolate: {{testUrl}}\nError: [$sce:insecurl] Blocked " +
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
"loading resource from url not allowed by $sceDelegate policy. URL: " +
"http://a.different.domain.example.com" ) ;
} ) ) ;
it ( 'should clear out JS src attributes' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<iframe src="{{testUrl}}"></iframe>' ) ( $rootScope ) ;
$rootScope . testUrl = "javascript:alert(1);" ;
2013-08-13 22:30:52 +00:00
expect ( function ( ) { $rootScope . $apply ( ) } ) . toThrowMinErr (
"$interpolate" , "interr" , "Can't interpolate: {{testUrl}}\nError: [$sce:insecurl] Blocked " +
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
"loading resource from url not allowed by $sceDelegate policy. URL: " +
"javascript:alert(1);" ) ;
} ) ) ;
it ( 'should clear out non-resource_url src attributes' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<iframe src="{{testUrl}}"></iframe>' ) ( $rootScope ) ;
$rootScope . testUrl = $sce . trustAsUrl ( "javascript:doTrustedStuff()" ) ;
2013-08-13 22:30:52 +00:00
expect ( $rootScope . $apply ) . toThrowMinErr (
"$interpolate" , "interr" , "Can't interpolate: {{testUrl}}\nError: [$sce:insecurl] Blocked " +
feat($sce): new $sce service for Strict Contextual Escaping.
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Strict Contextual Escaping
--------------------------
Strict Contextual Escaping (SCE) is a mode in which AngularJS requires
bindings in certain contexts to result in a value that is marked as safe
to use for that context One example of such a context is binding
arbitrary html controlled by the user via ng-bind-html-unsafe. We
refer to these contexts as privileged or SCE contexts.
As of version 1.2, Angular ships with SCE enabled by default.
Note: When enabled (the default), IE8 in quirks mode is not supported.
In this mode, IE8 allows one to execute arbitrary javascript by the use
of the expression() syntax. Refer
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx
to learn more about them. You can ensure your document is in standards
mode and not quirks mode by adding <!doctype html> to the top of your
HTML document.
SCE assists in writing code in way that (a) is secure by default and (b)
makes auditing for security vulnerabilities such as XSS, clickjacking,
etc. a lot easier.
Here's an example of a binding in a privileged context:
<input ng-model="userHtml">
<div ng-bind-html-unsafe="{{userHtml}}">
Notice that ng-bind-html-unsafe is bound to {{userHtml}} controlled by
the user. With SCE disabled, this application allows the user to render
arbitrary HTML into the DIV. In a more realistic example, one may be
rendering user comments, blog articles, etc. via bindings. (HTML is
just one example of a context where rendering user controlled input
creates security vulnerabilities.)
For the case of HTML, you might use a library, either on the client side, or on the server side,
to sanitize unsafe HTML before binding to the value and rendering it in the document.
How would you ensure that every place that used these types of bindings was bound to a value that
was sanitized by your library (or returned as safe for rendering by your server?) How can you
ensure that you didn't accidentally delete the line that sanitized the value, or renamed some
properties/fields and forgot to update the binding to the sanitized value?
To be secure by default, you want to ensure that any such bindings are disallowed unless you can
determine that something explicitly says it's safe to use a value for binding in that
context. You can then audit your code (a simple grep would do) to ensure that this is only done
for those values that you can easily tell are safe - because they were received from your server,
sanitized by your library, etc. You can organize your codebase to help with this - perhaps
allowing only the files in a specific directory to do this. Ensuring that the internal API
exposed by that code doesn't markup arbitrary values as safe then becomes a more manageable task.
In the case of AngularJS' SCE service, one uses $sce.trustAs (and
shorthand methods such as $sce.trustAsHtml, etc.) to obtain values that
will be accepted by SCE / privileged contexts.
In privileged contexts, directives and code will bind to the result of
$sce.getTrusted(context, value) rather than to the value directly.
Directives use $sce.parseAs rather than $parse to watch attribute
bindings, which performs the $sce.getTrusted behind the scenes on
non-constant literals.
As an example, ngBindHtmlUnsafe uses $sce.parseAsHtml(binding
expression). Here's the actual code (slightly simplified):
var ngBindHtmlUnsafeDirective = ['$sce', function($sce) {
return function(scope, element, attr) {
scope.$watch($sce.parseAsHtml(attr.ngBindHtmlUnsafe), function(value) {
element.html(value || '');
});
};
}];
Impact on loading templates
---------------------------
This applies both to the ng-include directive as well as templateUrl's
specified by directives.
By default, Angular only loads templates from the same domain and
protocol as the application document. This is done by calling
$sce.getTrustedResourceUrl on the template URL. To load templates from
other domains and/or protocols, you may either either whitelist them or
wrap it into a trusted value.
*Please note*:
The browser's Same Origin Policy and Cross-Origin Resource Sharing
(CORS) policy apply in addition to this and may further restrict whether
the template is successfully loaded. This means that without the right
CORS policy, loading templates from a different domain won't work on all
browsers. Also, loading templates from file:// URL does not work on
some browsers.
This feels like too much overhead for the developer?
----------------------------------------------------
It's important to remember that SCE only applies to interpolation expressions.
If your expressions are constant literals, they're automatically trusted
and you don't need to call $sce.trustAs on them.
e.g. <div ng-html-bind-unsafe="'<b>implicitly trusted</b>'"></div> just works.
Additionally, a[href] and img[src] automatically sanitize their URLs and
do not pass them through $sce.getTrusted. SCE doesn't play a role here.
The included $sceDelegate comes with sane defaults to allow you to load
templates in ng-include from your application's domain without having to
even know about SCE. It blocks loading templates from other domains or
loading templates over http from an https served document. You can
change these by setting your own custom whitelists and blacklists for
matching such URLs.
This significantly reduces the overhead. It is far easier to pay the
small overhead and have an application that's secure and can be audited
to verify that with much more ease than bolting security onto an
application later.
2013-05-14 21:51:39 +00:00
"loading resource from url not allowed by $sceDelegate policy. URL: javascript:doTrustedStuff()" ) ;
} ) ) ;
it ( 'should pass through $sce.trustAs() values in src attributes' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<iframe src="{{testUrl}}"></iframe>' ) ( $rootScope ) ;
$rootScope . testUrl = $sce . trustAsResourceUrl ( "javascript:doTrustedStuff()" ) ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'src' ) ) . toEqual ( 'javascript:doTrustedStuff()' ) ;
} ) ) ;
} ) ;
2013-06-21 20:03:56 +00:00
2013-11-12 23:32:52 +00:00
describe ( 'form[action]' , function ( ) {
it ( 'should pass through action attribute for the same domain' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<form action="{{testUrl}}"></form>' ) ( $rootScope ) ;
$rootScope . testUrl = "different_page" ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'action' ) ) . toEqual ( 'different_page' ) ;
} ) ) ;
it ( 'should clear out action attribute for a different domain' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<form action="{{testUrl}}"></form>' ) ( $rootScope ) ;
$rootScope . testUrl = "http://a.different.domain.example.com" ;
expect ( function ( ) { $rootScope . $apply ( ) } ) . toThrowMinErr (
"$interpolate" , "interr" , "Can't interpolate: {{testUrl}}\nError: [$sce:insecurl] Blocked " +
"loading resource from url not allowed by $sceDelegate policy. URL: " +
"http://a.different.domain.example.com" ) ;
} ) ) ;
it ( 'should clear out JS action attribute' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<form action="{{testUrl}}"></form>' ) ( $rootScope ) ;
$rootScope . testUrl = "javascript:alert(1);" ;
expect ( function ( ) { $rootScope . $apply ( ) } ) . toThrowMinErr (
"$interpolate" , "interr" , "Can't interpolate: {{testUrl}}\nError: [$sce:insecurl] Blocked " +
"loading resource from url not allowed by $sceDelegate policy. URL: " +
"javascript:alert(1);" ) ;
} ) ) ;
it ( 'should clear out non-resource_url action attribute' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<form action="{{testUrl}}"></form>' ) ( $rootScope ) ;
$rootScope . testUrl = $sce . trustAsUrl ( "javascript:doTrustedStuff()" ) ;
expect ( $rootScope . $apply ) . toThrowMinErr (
"$interpolate" , "interr" , "Can't interpolate: {{testUrl}}\nError: [$sce:insecurl] Blocked " +
"loading resource from url not allowed by $sceDelegate policy. URL: javascript:doTrustedStuff()" ) ;
} ) ) ;
it ( 'should pass through $sce.trustAs() values in action attribute' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<form action="{{testUrl}}"></form>' ) ( $rootScope ) ;
$rootScope . testUrl = $sce . trustAsResourceUrl ( "javascript:doTrustedStuff()" ) ;
$rootScope . $apply ( ) ;
expect ( element . attr ( 'action' ) ) . toEqual ( 'javascript:doTrustedStuff()' ) ;
} ) ) ;
} ) ;
if ( ! msie || msie >= 11 ) {
describe ( 'iframe[srcdoc]' , function ( ) {
it ( 'should NOT set iframe contents for untrusted values' , inject ( function ( $compile , $rootScope , $sce ) {
element = $compile ( '<iframe srcdoc="{{html}}"></iframe>' ) ( $rootScope ) ;
$rootScope . html = '<div onclick="">hello</div>' ;
expect ( function ( ) { $rootScope . $digest ( ) ; } ) . toThrowMinErr ( '$interpolate' , 'interr' , new RegExp (
/Can't interpolate: {{html}}\n/ . source +
/[^[]*\[\$sce:unsafe\] Attempting to use an unsafe value in a safe context./ . source ) ) ;
} ) ) ;
it ( 'should NOT set html for wrongly typed values' , inject ( function ( $rootScope , $compile , $sce ) {
element = $compile ( '<iframe srcdoc="{{html}}"></iframe>' ) ( $rootScope ) ;
$rootScope . html = $sce . trustAsCss ( '<div onclick="">hello</div>' ) ;
expect ( function ( ) { $rootScope . $digest ( ) ; } ) . toThrowMinErr ( '$interpolate' , 'interr' , new RegExp (
/Can't interpolate: {{html}}\n/ . source +
/[^[]*\[\$sce:unsafe\] Attempting to use an unsafe value in a safe context./ . source ) ) ;
} ) ) ;
it ( 'should set html for trusted values' , inject ( function ( $rootScope , $compile , $sce ) {
element = $compile ( '<iframe srcdoc="{{html}}"></iframe>' ) ( $rootScope ) ;
$rootScope . html = $sce . trustAsHtml ( '<div onclick="">hello</div>' ) ;
$rootScope . $digest ( ) ;
2013-11-22 08:52:57 +00:00
expect ( angular . lowercase ( element . attr ( 'srcdoc' ) ) ) . toEqual ( '<div onclick="">hello</div>' ) ;
2013-11-12 23:32:52 +00:00
} ) ) ;
} ) ;
}
2013-02-25 23:00:47 +00:00
describe ( 'ngAttr* attribute binding' , function ( ) {
it ( 'should bind after digest but not before' , inject ( function ( $compile , $rootScope ) {
$rootScope . name = "Misko" ;
element = $compile ( '<span ng-attr-test="{{name}}"></span>' ) ( $rootScope ) ;
expect ( element . attr ( 'test' ) ) . toBeUndefined ( ) ;
$rootScope . $digest ( ) ;
expect ( element . attr ( 'test' ) ) . toBe ( 'Misko' ) ;
} ) ) ;
it ( 'should work with different prefixes' , inject ( function ( $compile , $rootScope ) {
$rootScope . name = "Misko" ;
element = $compile ( '<span ng:attr:test="{{name}}" ng-Attr-test2="{{name}}" ng_Attr_test3="{{name}}"></span>' ) ( $rootScope ) ;
expect ( element . attr ( 'test' ) ) . toBeUndefined ( ) ;
expect ( element . attr ( 'test2' ) ) . toBeUndefined ( ) ;
2013-07-28 16:40:16 +00:00
expect ( element . attr ( 'test3' ) ) . toBeUndefined ( ) ;
2013-02-25 23:00:47 +00:00
$rootScope . $digest ( ) ;
expect ( element . attr ( 'test' ) ) . toBe ( 'Misko' ) ;
expect ( element . attr ( 'test2' ) ) . toBe ( 'Misko' ) ;
expect ( element . attr ( 'test3' ) ) . toBe ( 'Misko' ) ;
} ) ) ;
it ( 'should work if they are prefixed with x- or data-' , inject ( function ( $compile , $rootScope ) {
$rootScope . name = "Misko" ;
element = $compile ( '<span data-ng-attr-test2="{{name}}" x-ng-attr-test3="{{name}}" data-ng:attr-test4="{{name}}"></span>' ) ( $rootScope ) ;
expect ( element . attr ( 'test2' ) ) . toBeUndefined ( ) ;
expect ( element . attr ( 'test3' ) ) . toBeUndefined ( ) ;
expect ( element . attr ( 'test4' ) ) . toBeUndefined ( ) ;
$rootScope . $digest ( ) ;
expect ( element . attr ( 'test2' ) ) . toBe ( 'Misko' ) ;
expect ( element . attr ( 'test3' ) ) . toBe ( 'Misko' ) ;
expect ( element . attr ( 'test4' ) ) . toBe ( 'Misko' ) ;
} ) ) ;
2013-09-18 14:00:34 +00:00
describe ( 'when an attribute has a dash-separated name' , function ( ) {
it ( 'should work with different prefixes' , inject ( function ( $compile , $rootScope ) {
$rootScope . name = "JamieMason" ;
element = $compile ( '<span ng:attr:dash-test="{{name}}" ng-Attr-dash-test2="{{name}}" ng_Attr_dash-test3="{{name}}"></span>' ) ( $rootScope ) ;
expect ( element . attr ( 'dash-test' ) ) . toBeUndefined ( ) ;
expect ( element . attr ( 'dash-test2' ) ) . toBeUndefined ( ) ;
expect ( element . attr ( 'dash-test3' ) ) . toBeUndefined ( ) ;
$rootScope . $digest ( ) ;
expect ( element . attr ( 'dash-test' ) ) . toBe ( 'JamieMason' ) ;
expect ( element . attr ( 'dash-test2' ) ) . toBe ( 'JamieMason' ) ;
expect ( element . attr ( 'dash-test3' ) ) . toBe ( 'JamieMason' ) ;
} ) ) ;
it ( 'should work if they are prefixed with x- or data-' , inject ( function ( $compile , $rootScope ) {
$rootScope . name = "JamieMason" ;
element = $compile ( '<span data-ng-attr-dash-test2="{{name}}" x-ng-attr-dash-test3="{{name}}" data-ng:attr-dash-test4="{{name}}"></span>' ) ( $rootScope ) ;
expect ( element . attr ( 'dash-test2' ) ) . toBeUndefined ( ) ;
expect ( element . attr ( 'dash-test3' ) ) . toBeUndefined ( ) ;
expect ( element . attr ( 'dash-test4' ) ) . toBeUndefined ( ) ;
$rootScope . $digest ( ) ;
expect ( element . attr ( 'dash-test2' ) ) . toBe ( 'JamieMason' ) ;
expect ( element . attr ( 'dash-test3' ) ) . toBe ( 'JamieMason' ) ;
expect ( element . attr ( 'dash-test4' ) ) . toBe ( 'JamieMason' ) ;
} ) ) ;
} ) ;
2013-02-25 23:00:47 +00:00
} ) ;
2013-05-24 19:41:38 +00:00
describe ( 'multi-element directive' , function ( ) {
it ( 'should group on link function' , inject ( function ( $compile , $rootScope ) {
$rootScope . show = false ;
element = $compile (
'<div>' +
'<span ng-show-start="show"></span>' +
'<span ng-show-end></span>' +
'</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
var spans = element . find ( 'span' ) ;
feat(ngAnimate): complete rewrite of animations
- ngAnimate directive is gone and was replaced with class based animations/transitions
- support for triggering animations on css class additions and removals
- done callback was added to all animation apis
- $animation and $animator where merged into a single $animate service with api:
- $animate.enter(element, parent, after, done);
- $animate.leave(element, done);
- $animate.move(element, parent, after, done);
- $animate.addClass(element, className, done);
- $animate.removeClass(element, className, done);
BREAKING CHANGE: too many things changed, we'll write up a separate doc with migration instructions
2013-06-18 17:59:57 +00:00
expect ( spans . eq ( 0 ) ) . toBeHidden ( ) ;
expect ( spans . eq ( 1 ) ) . toBeHidden ( ) ;
2013-05-24 19:41:38 +00:00
} ) ) ;
it ( 'should group on compile function' , inject ( function ( $compile , $rootScope ) {
$rootScope . show = false ;
element = $compile (
'<div>' +
'<span ng-repeat-start="i in [1,2]">{{i}}A</span>' +
'<span ng-repeat-end>{{i}}B;</span>' +
'</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toEqual ( '1A1B;2A2B;' ) ;
} ) ) ;
2013-06-11 20:08:21 +00:00
it ( 'should support grouping over text nodes' , inject ( function ( $compile , $rootScope ) {
$rootScope . show = false ;
element = $compile (
'<div>' +
'<span ng-repeat-start="i in [1,2]">{{i}}A</span>' +
':' + // Important: proves that we can iterate over non-elements
'<span ng-repeat-end>{{i}}B;</span>' +
'</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toEqual ( '1A:1B;2A:2B;' ) ;
} ) ) ;
2013-05-24 19:41:38 +00:00
it ( 'should group on $root compile function' , inject ( function ( $compile , $rootScope ) {
$rootScope . show = false ;
element = $compile (
'<div></div>' +
'<span ng-repeat-start="i in [1,2]">{{i}}A</span>' +
'<span ng-repeat-end>{{i}}B;</span>' +
'<div></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
element = jqLite ( element [ 0 ] . parentNode . childNodes ) ; // reset because repeater is top level.
expect ( element . text ( ) ) . toEqual ( '1A1B;2A2B;' ) ;
} ) ) ;
2013-08-20 21:41:27 +00:00
it ( 'should group on nested groups of same directive' , inject ( function ( $compile , $rootScope ) {
2013-05-24 19:41:38 +00:00
$rootScope . show = false ;
element = $compile (
'<div></div>' +
'<div ng-repeat-start="i in [1,2]">{{i}}A</div>' +
'<span ng-bind-start="\'.\'"></span>' +
'<span ng-bind-end></span>' +
'<div ng-repeat-end>{{i}}B;</div>' +
'<div></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
element = jqLite ( element [ 0 ] . parentNode . childNodes ) ; // reset because repeater is top level.
expect ( element . text ( ) ) . toEqual ( '1A..1B;2A..2B;' ) ;
} ) ) ;
it ( 'should group on nested groups' , inject ( function ( $compile , $rootScope ) {
$rootScope . show = false ;
element = $compile (
'<div></div>' +
'<div ng-repeat-start="i in [1,2]">{{i}}(</div>' +
'<span ng-repeat-start="j in [2,3]">{{j}}-</span>' +
'<span ng-repeat-end>{{j}}</span>' +
'<div ng-repeat-end>){{i}};</div>' +
'<div></div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
element = jqLite ( element [ 0 ] . parentNode . childNodes ) ; // reset because repeater is top level.
expect ( element . text ( ) ) . toEqual ( '1(2-23-3)1;2(2-23-3)2;' ) ;
} ) ) ;
it ( 'should throw error if unterminated' , function ( ) {
module ( function ( $compileProvider ) {
$compileProvider . directive ( 'foo' , function ( ) {
return {
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
expect ( function ( ) {
element = $compile (
'<div>' +
'<span foo-start></span>' +
'</div>' ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr ( "$compile" , "uterdir" , "Unterminated attribute, found 'foo-start' but no matching 'foo-end' found." ) ;
2013-05-24 19:41:38 +00:00
} ) ;
} ) ;
2013-09-25 22:35:50 +00:00
it ( 'should correctly collect ranges on multiple directives on a single element' , function ( ) {
module ( function ( $compileProvider ) {
$compileProvider . directive ( 'emptyDirective' , function ( ) {
return function ( scope , element ) {
element . data ( 'x' , 'abc' ) ;
} ;
} ) ;
$compileProvider . directive ( 'rangeDirective' , function ( ) {
return {
link : function ( scope ) {
scope . x = 'X' ;
scope . y = 'Y' ;
}
} ;
} ) ;
} ) ;
inject ( function ( $compile , $rootScope ) {
element = $compile (
'<div>' +
'<div range-directive-start empty-directive>{{x}}</div>' +
'<div range-directive-end>{{y}}</div>' +
'</div>'
) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . text ( ) ) . toBe ( 'XY' ) ;
expect ( angular . element ( element [ 0 ] . firstChild ) . data ( 'x' ) ) . toBe ( 'abc' ) ;
} ) ;
} ) ;
2013-08-20 21:41:27 +00:00
it ( 'should throw error if unterminated (containing termination as a child)' , function ( ) {
2013-05-24 19:41:38 +00:00
module ( function ( $compileProvider ) {
$compileProvider . directive ( 'foo' , function ( ) {
return {
} ;
} ) ;
} ) ;
2013-08-01 22:39:22 +00:00
inject ( function ( $compile ) {
2013-05-24 19:41:38 +00:00
expect ( function ( ) {
element = $compile (
'<div>' +
'<span foo-start><span foo-end></span></span>' +
'</div>' ) ;
2013-08-13 22:30:52 +00:00
} ) . toThrowMinErr ( "$compile" , "uterdir" , "Unterminated attribute, found 'foo-start' but no matching 'foo-end' found." ) ;
2013-05-24 19:41:38 +00:00
} ) ;
} ) ;
it ( 'should support data- and x- prefix' , inject ( function ( $compile , $rootScope ) {
$rootScope . show = false ;
element = $compile (
'<div>' +
'<span data-ng-show-start="show"></span>' +
'<span data-ng-show-end></span>' +
'<span x-ng-show-start="show"></span>' +
'<span x-ng-show-end></span>' +
'</div>' ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
var spans = element . find ( 'span' ) ;
feat(ngAnimate): complete rewrite of animations
- ngAnimate directive is gone and was replaced with class based animations/transitions
- support for triggering animations on css class additions and removals
- done callback was added to all animation apis
- $animation and $animator where merged into a single $animate service with api:
- $animate.enter(element, parent, after, done);
- $animate.leave(element, done);
- $animate.move(element, parent, after, done);
- $animate.addClass(element, className, done);
- $animate.removeClass(element, className, done);
BREAKING CHANGE: too many things changed, we'll write up a separate doc with migration instructions
2013-06-18 17:59:57 +00:00
expect ( spans . eq ( 0 ) ) . toBeHidden ( ) ;
expect ( spans . eq ( 1 ) ) . toBeHidden ( ) ;
expect ( spans . eq ( 2 ) ) . toBeHidden ( ) ;
expect ( spans . eq ( 3 ) ) . toBeHidden ( ) ;
2013-05-24 19:41:38 +00:00
} ) ) ;
} ) ;
2013-08-02 00:13:36 +00:00
describe ( '$animate animation hooks' , function ( ) {
beforeEach ( module ( 'mock.animate' ) ) ;
it ( 'should automatically fire the addClass and removeClass animation hooks' ,
inject ( function ( $compile , $animate , $rootScope ) {
var data , element = jqLite ( '<div class="{{val1}} {{val2}} fire"></div>' ) ;
$compile ( element ) ( $rootScope ) ;
$rootScope . $digest ( ) ;
expect ( element . hasClass ( 'fire' ) ) . toBe ( true ) ;
$rootScope . val1 = 'ice' ;
$rootScope . val2 = 'rice' ;
$rootScope . $digest ( ) ;
data = $animate . flushNext ( 'addClass' ) ;
expect ( data . params [ 1 ] ) . toBe ( 'ice rice' ) ;
expect ( element . hasClass ( 'ice' ) ) . toBe ( true ) ;
expect ( element . hasClass ( 'rice' ) ) . toBe ( true ) ;
expect ( element . hasClass ( 'fire' ) ) . toBe ( true ) ;
$rootScope . val2 = 'dice' ;
$rootScope . $digest ( ) ;
data = $animate . flushNext ( 'removeClass' ) ;
expect ( data . params [ 1 ] ) . toBe ( 'rice' ) ;
data = $animate . flushNext ( 'addClass' ) ;
expect ( data . params [ 1 ] ) . toBe ( 'dice' ) ;
expect ( element . hasClass ( 'ice' ) ) . toBe ( true ) ;
expect ( element . hasClass ( 'dice' ) ) . toBe ( true ) ;
expect ( element . hasClass ( 'fire' ) ) . toBe ( true ) ;
$rootScope . val1 = '' ;
$rootScope . val2 = '' ;
$rootScope . $digest ( ) ;
data = $animate . flushNext ( 'removeClass' ) ;
expect ( data . params [ 1 ] ) . toBe ( 'ice dice' ) ;
expect ( element . hasClass ( 'ice' ) ) . toBe ( false ) ;
expect ( element . hasClass ( 'dice' ) ) . toBe ( false ) ;
expect ( element . hasClass ( 'fire' ) ) . toBe ( true ) ;
} ) ) ;
} ) ;
2010-03-18 19:20:06 +00:00
} ) ;