@@ -3155,6 +3155,72 @@ describe('parser', function() {
3155
3155
expect ( isFunction ( s . toString ) ) . toBe ( true ) ;
3156
3156
expect ( l . toString ) . toBe ( 1 ) ;
3157
3157
} ) ) ;
3158
+
3159
+ it ( 'should overwrite undefined / null scope properties when assigning' , inject ( function ( $parse ) {
3160
+ var scope ;
3161
+
3162
+ scope = { } ;
3163
+ $parse ( 'a.b = 1' ) ( scope ) ;
3164
+ $parse ( 'c["d"] = 2' ) ( scope ) ;
3165
+ expect ( scope ) . toEqual ( { a : { b : 1 } , c : { d : 2 } } ) ;
3166
+
3167
+ scope = { a : { } } ;
3168
+ $parse ( 'a.b.c = 1' ) ( scope ) ;
3169
+ $parse ( 'a.c["d"] = 2' ) ( scope ) ;
3170
+ expect ( scope ) . toEqual ( { a : { b : { c : 1 } , c : { d : 2 } } } ) ;
3171
+
3172
+ scope = { a : undefined , c : undefined } ;
3173
+ $parse ( 'a.b = 1' ) ( scope ) ;
3174
+ $parse ( 'c["d"] = 2' ) ( scope ) ;
3175
+ expect ( scope ) . toEqual ( { a : { b : 1 } , c : { d : 2 } } ) ;
3176
+
3177
+ scope = { a : { b : undefined , c : undefined } } ;
3178
+ $parse ( 'a.b.c = 1' ) ( scope ) ;
3179
+ $parse ( 'a.c["d"] = 2' ) ( scope ) ;
3180
+ expect ( scope ) . toEqual ( { a : { b : { c : 1 } , c : { d : 2 } } } ) ;
3181
+
3182
+ scope = { a : null , c : null } ;
3183
+ $parse ( 'a.b = 1' ) ( scope ) ;
3184
+ $parse ( 'c["d"] = 2' ) ( scope ) ;
3185
+ expect ( scope ) . toEqual ( { a : { b : 1 } , c : { d : 2 } } ) ;
3186
+
3187
+ scope = { a : { b : null , c : null } } ;
3188
+ $parse ( 'a.b.c = 1' ) ( scope ) ;
3189
+ $parse ( 'a.c["d"] = 2' ) ( scope ) ;
3190
+ expect ( scope ) . toEqual ( { a : { b : { c : 1 } , c : { d : 2 } } } ) ;
3191
+ } ) ) ;
3192
+
3193
+ they ( 'should not overwrite $prop scope properties when assigning' , [ 0 , false , '' , NaN ] ,
3194
+ function ( falsyValue ) {
3195
+ inject ( function ( $parse ) {
3196
+ var scope ;
3197
+
3198
+ scope = { a : falsyValue , c : falsyValue } ;
3199
+ tryParseAndIgnoreException ( 'a.b = 1' ) ;
3200
+ tryParseAndIgnoreException ( 'c["d"] = 2' ) ;
3201
+ expect ( scope ) . toEqual ( { a : falsyValue , c : falsyValue } ) ;
3202
+
3203
+ scope = { a : { b : falsyValue , c : falsyValue } } ;
3204
+ tryParseAndIgnoreException ( 'a.b.c = 1' ) ;
3205
+ tryParseAndIgnoreException ( 'a.c["d"] = 2' ) ;
3206
+ expect ( scope ) . toEqual ( { a : { b : falsyValue , c : falsyValue } } ) ;
3207
+
3208
+ // Helpers
3209
+ //
3210
+ // Normally assigning property on a primitive should throw exception in strict mode
3211
+ // and silently fail in non-strict mode, IE seems to always have the non-strict-mode behavior,
3212
+ // so if we try to use 'expect(function() {$parse('a.b=1')({a:false});).toThrow()' for testing
3213
+ // the test will fail in case of IE because it will not throw exception, and if we just use
3214
+ // '$parse('a.b=1')({a:false})' the test will fail because it will throw exception in case of Chrome
3215
+ // so we use tryParseAndIgnoreException helper to catch the exception silently for all cases.
3216
+ //
3217
+ function tryParseAndIgnoreException ( expression ) {
3218
+ try {
3219
+ $parse ( expression ) ( scope ) ;
3220
+ } catch ( error ) { /* ignore exception */ }
3221
+ }
3222
+ } ) ;
3223
+ } ) ;
3158
3224
} ) ;
3159
3225
3160
3226
describe ( 'literal' , function ( ) {
0 commit comments