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

Commit 678721f

Browse files
committed
fixup! feat($compile): add support for arbitrary property and event bindings
1 parent 00f2f32 commit 678721f

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@ngdoc error
2+
@name $compile:ctxoverride
3+
@fullName Context Override
4+
@description
5+
6+
This error occurs when the security context for an attribute is defined multiple times under different security contexts.
7+
8+
For example:
9+
10+
```
11+
$compileProvider.addPropertyContext("my-element", "src", "mediaUrl");
12+
$compileProvider.addPropertyContext("my-element", "src", "resourceUrl"); //throws
13+
```

src/ng/compile.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
16061606
* @param {string} ctx the context type
16071607
*/
16081608
this.addPropertyContext = function(elementName, propertyName, ctx) {
1609-
PROP_CONTEXTS[(elementName.toLowerCase() + '|' + propertyName.toLowerCase())] = ctx;
1609+
var key = (elementName.toLowerCase() + '|' + propertyName.toLowerCase());
1610+
1611+
if (key in PROP_CONTEXTS && PROP_CONTEXTS[key] !== ctx) {
1612+
throw $compileMinErr('ctxoverride', 'Property context \'{0}.{1}\' already set to \'{2}\', cannot override to \'{3}\'.', elementName, propertyName, PROP_CONTEXTS[key], ctx);
1613+
}
1614+
1615+
PROP_CONTEXTS[key] = ctx;
16101616
};
16111617

16121618
/* Default property contexts.

test/ng/compileSpec.js

+41
Original file line numberDiff line numberDiff line change
@@ -12678,6 +12678,47 @@ describe('$compile', function() {
1267812678
});
1267912679
});
1268012680

12681+
describe('addPropertyContext', function() {
12682+
function testProvider(provider) {
12683+
module(provider);
12684+
inject(function($compile) { /* done! */});
12685+
}
12686+
12687+
it('should allow adding new properties', function() {
12688+
testProvider(function($compileProvider) {
12689+
$compileProvider.addPropertyContext('div', 'title', 'mediaUrl');
12690+
$compileProvider.addPropertyContext('*', 'my-prop', 'resourceUrl');
12691+
});
12692+
});
12693+
12694+
it('should allow different sce types of a property on different element types', function() {
12695+
testProvider(function($compileProvider) {
12696+
$compileProvider.addPropertyContext('div', 'title', 'mediaUrl');
12697+
$compileProvider.addPropertyContext('span', 'title', 'css');
12698+
$compileProvider.addPropertyContext('*', 'title', 'resourceUrl');
12699+
$compileProvider.addPropertyContext('article', 'title', 'html');
12700+
});
12701+
});
12702+
12703+
it('should throw \'ctxoverride\' when changing an existing context', function() {
12704+
testProvider(function($compileProvider) {
12705+
$compileProvider.addPropertyContext('div', 'title', 'mediaUrl');
12706+
12707+
expect(function() {
12708+
$compileProvider.addPropertyContext('div', 'title', 'resourceUrl');
12709+
})
12710+
.toThrowMinErr('$compile', 'ctxoverride', 'Property context \'div.title\' already set to \'mediaUrl\', cannot override to \'resourceUrl\'.');
12711+
});
12712+
});
12713+
12714+
it('should setting the same property/element to the same value', function() {
12715+
testProvider(function($compileProvider) {
12716+
$compileProvider.addPropertyContext('div', 'title', 'mediaUrl');
12717+
$compileProvider.addPropertyContext('div', 'title', 'mediaUrl');
12718+
});
12719+
});
12720+
});
12721+
1268112722

1268212723
describe('when an attribute has an underscore-separated name', function() {
1268312724

0 commit comments

Comments
 (0)