11import { isPresent , isBlank , BaseException , assertionsEnabled , RegExpWrapper } from 'angular2/src/facade/lang' ;
2- import { List , MapWrapper , StringMapWrapper } from 'angular2/src/facade/collection' ;
2+ import { List , MapWrapper } from 'angular2/src/facade/collection' ;
33import { DOM } from 'angular2/src/dom/dom_adapter' ;
44import { SelectorMatcher } from '../selector' ;
55import { CssSelector } from '../selector' ;
@@ -10,9 +10,6 @@ import {CompileStep} from './compile_step';
1010import { CompileElement } from './compile_element' ;
1111import { CompileControl } from './compile_control' ;
1212
13- import { isSpecialProperty } from './element_binder_builder' ;
14- import { dashCaseToCamelCase , camelCaseToDashCase } from './util' ;
15-
1613var PROPERTY_BINDING_REGEXP = RegExpWrapper . create ( '^ *([^\\s\\|]+)' ) ;
1714
1815/**
@@ -60,53 +57,15 @@ export class DirectiveParser extends CompileStep {
6057 } ) ;
6158
6259 // Note: We assume that the ViewSplitter already did its work, i.e. template directive should
63- // only be present on <template> elements any more !
60+ // only be present on <template> elements!
6461 var isTemplateElement = DOM . isTemplateElement ( current . element ) ;
65- var matchedProperties ; // StringMap - used in dev mode to store all properties that have been matched
6662
6763 this . _selectorMatcher . match ( cssSelector , ( selector , directive ) => {
68- matchedProperties = updateMatchedProperties ( matchedProperties , selector , directive ) ;
69- checkDirectiveValidity ( directive , current , isTemplateElement ) ;
70- current . addDirective ( directive ) ;
64+ current . addDirective ( checkDirectiveValidity ( directive , current , isTemplateElement ) ) ;
7165 } ) ;
72-
73- // raise error if some directives are missing
74- checkMissingDirectives ( current , matchedProperties , isTemplateElement ) ;
7566 }
7667}
7768
78- // calculate all the properties that are used or interpreted by all directives
79- // those properties correspond to the directive selectors and the directive bindings
80- function updateMatchedProperties ( matchedProperties , selector , directive ) {
81- if ( assertionsEnabled ( ) ) {
82- var attrs = selector . attrs ;
83- if ( ! isPresent ( matchedProperties ) ) {
84- matchedProperties = StringMapWrapper . create ( ) ;
85- }
86- if ( isPresent ( attrs ) ) {
87- for ( var idx = 0 ; idx < attrs . length ; idx += 2 ) {
88- // attribute name is stored on even indexes
89- StringMapWrapper . set ( matchedProperties , dashCaseToCamelCase ( attrs [ idx ] ) , true ) ;
90- }
91- }
92- // some properties can be used by the directive, so we need to register them
93- if ( isPresent ( directive . annotation ) && isPresent ( directive . annotation . bind ) ) {
94- var bindMap = directive . annotation . bind ;
95- StringMapWrapper . forEach ( bindMap , ( value , key ) => {
96- // value is the name of the property that is interpreted
97- // e.g. 'myprop' or 'myprop | double' when a pipe is used to transform the property
98-
99- // keep the property name and remove the pipe
100- var bindProp = RegExpWrapper . firstMatch ( PROPERTY_BINDING_REGEXP , value ) ;
101- if ( isPresent ( bindProp ) && isPresent ( bindProp [ 1 ] ) ) {
102- StringMapWrapper . set ( matchedProperties , dashCaseToCamelCase ( bindProp [ 1 ] ) , true ) ;
103- }
104- } ) ;
105- }
106- }
107- return matchedProperties ;
108- }
109-
11069// check if the directive is compatible with the current element
11170function checkDirectiveValidity ( directive , current , isTemplateElement ) {
11271 var isComponent = directive . annotation instanceof Component || directive . annotation instanceof DynamicComponent ;
@@ -125,26 +84,6 @@ function checkDirectiveValidity(directive, current, isTemplateElement) {
12584 } else if ( isComponent && alreadyHasComponent ) {
12685 throw new BaseException ( `Multiple component directives not allowed on the same element - check ${ current . elementDescription } ` ) ;
12786 }
128- }
12987
130- // validates that there is no missing directive - dev mode only
131- function checkMissingDirectives ( current , matchedProperties , isTemplateElement ) {
132- if ( assertionsEnabled ( ) ) {
133- var ppBindings = current . propertyBindings ;
134- if ( isPresent ( ppBindings ) ) {
135- // check that each property corresponds to a real property or has been matched by a directive
136- MapWrapper . forEach ( ppBindings , ( expression , prop ) => {
137- if ( ! DOM . hasProperty ( current . element , prop ) && ! isSpecialProperty ( prop ) ) {
138- if ( ! isPresent ( matchedProperties ) || ! isPresent ( StringMapWrapper . get ( matchedProperties , prop ) ) ) {
139- throw new BaseException ( `Missing directive to handle '${ camelCaseToDashCase ( prop ) } ' in ${ current . elementDescription } ` ) ;
140- }
141- }
142- } ) ;
143- }
144- // template only store directives as attribute when they are not bound to expressions
145- // so we have to validate the expression case too (e.g. !if="condition")
146- if ( isTemplateElement && ! current . isViewRoot && ! isPresent ( current . viewportDirective ) ) {
147- throw new BaseException ( `Missing directive to handle: ${ current . elementDescription } ` ) ;
148- }
149- }
88+ return directive ;
15089}
0 commit comments