Skip to content

Commit

Permalink
fix(upgrade): allow non-element selectors for downgraded components (#…
Browse files Browse the repository at this point in the history
…14291)

This affects the dynamic version of `upgrade` and makes it more consistent with
the static version, while removing an artificial limitation.

(This commit backports the fix from 9aafdc7 to 2.4.x.)
  • Loading branch information
gkalpak authored and mhevery committed Feb 7, 2017
1 parent 343ee8a commit 5bb47db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
8 changes: 1 addition & 7 deletions modules/@angular/upgrade/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {DirectiveResolver} from '@angular/compiler';
import {Directive, Type} from '@angular/core';

const COMPONENT_SELECTOR = /^[\w|-]*$/;
const SKEWER_CASE = /-(\w)/g;
const directiveResolver = new DirectiveResolver();

export interface AttrProp {
Expand All @@ -33,12 +32,7 @@ export interface ComponentInfo {

export function getComponentInfo(type: Type<any>): ComponentInfo {
const resolvedMetadata: Directive = directiveResolver.resolve(type);
let selector = resolvedMetadata.selector;
if (!selector.match(COMPONENT_SELECTOR)) {
throw new Error('Only selectors matching element names are supported, got: ' + selector);
}
selector = selector.replace(
SKEWER_CASE, (all: any /** TODO #9100 */, letter: string) => letter.toUpperCase());
const selector = resolvedMetadata.selector;
return {
type: type,
selector: selector,
Expand Down
7 changes: 1 addition & 6 deletions modules/@angular/upgrade/test/metadata_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@ import {getComponentInfo, parseFields} from '@angular/upgrade/src/metadata';
export function main() {
describe('upgrade metadata', () => {
it('should extract component selector', () => {
expect(getComponentInfo(ElementNameComponent).selector).toEqual('elementNameDashed');
expect(getComponentInfo(ElementNameComponent).selector).toBe('element-name-dashed');
});


describe('errors', () => {
it('should throw on missing selector', () => {
expect(() => getComponentInfo(AttributeNameComponent))
.toThrowError('Only selectors matching element names are supported, got: [attr-name]');
});

it('should throw on non element names', () => {
expect(() => getComponentInfo(NoAnnotationComponent))
.toThrowError('No Directive annotation found on NoAnnotationComponent');
});
Expand Down
19 changes: 19 additions & 0 deletions modules/@angular/upgrade/test/upgrade_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,25 @@ export function main() {
});

describe('downgrade ng2 component', () => {
it('should allow non-element selectors for downgraded components', async(() => {
@Component({selector: '[itWorks]', template: 'It works'})
class WorksComponent {
}

@NgModule({declarations: [WorksComponent], imports: [BrowserModule]})
class Ng2Module {
}

const adapter: UpgradeAdapter = new UpgradeAdapter(forwardRef(() => Ng2Module));
const ng1Module = angular.module('ng1', []);
ng1Module.directive('ng2', adapter.downgradeNg2Component(WorksComponent));

const element = html('<ng2></ng2>');
adapter.bootstrap(element, ['ng1']).ready((ref) => {
expect(multiTrim(document.body.textContent)).toBe('It works');
});
}));

it('should bind properties, events', async(() => {
const adapter: UpgradeAdapter = new UpgradeAdapter(forwardRef(() => Ng2Module));
const ng1Module = angular.module('ng1', []);
Expand Down

0 comments on commit 5bb47db

Please sign in to comment.