Repository navigation
feat(compiler): add schema support for elements #3225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
pkozlowski-opensource
wants to merge
1
commit into
angular:master
from
pkozlowski-opensource:schema_registry
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
modules/angular2/src/render/dom/schema/dom_element_schema.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import {DOM} from 'angular2/src/dom/dom_adapter'; | ||
|
|
||
| import {ElementSchemaEntry} from './element_schema_entry'; | ||
|
|
||
| export class DOMElementSchema implements ElementSchemaEntry { | ||
| private _domElement; | ||
|
|
||
| constructor(public tagName: string, public extendsElement: string = null) { | ||
| // TODO(pk): here are are creating a new DOM element for each entry which if far from being | ||
| // optimal. Consider changing the signature of hasProperty so it takes an element - in that | ||
| // case we would be constructing a new DOM elements only for potential web components. | ||
| this._domElement = DOM.createElement(tagName); | ||
| } | ||
|
|
||
| hasProperty(propName: string): boolean { return DOM.hasProperty(this._domElement, propName); } | ||
|
|
||
| getMappedPropName(propName: string): string { return null; } | ||
| } |
16 changes: 16 additions & 0 deletions
16
modules/angular2/src/render/dom/schema/dom_element_schema_registry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import {ElementSchemaEntry} from './element_schema_entry'; | ||
| import {DOMElementSchema} from './dom_element_schema'; | ||
| import {ElementSchemaRegistryImpl} from './element_schema_registry'; | ||
|
|
||
| export const DEFAULT_HTML_EL_NAME = 'HTMLElement'; | ||
|
|
||
| export class DomElementSchemaRegistry extends ElementSchemaRegistryImpl { | ||
| constructor(entries: List<ElementSchemaEntry>, | ||
| private _defaultElementToExtend: string = DEFAULT_HTML_EL_NAME) { | ||
| super(entries); | ||
| } | ||
|
|
||
| handleEntryNotFound(tagName: string): ElementSchemaEntry { | ||
| return new DOMElementSchema(tagName, this._defaultElementToExtend); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
modules/angular2/src/render/dom/schema/element_schema_entry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import {StringMap, StringMapWrapper} from 'angular2/src/facade/collection'; | ||
|
|
||
| export interface ElementSchemaEntry { | ||
| tagName: string; | ||
| extendsElement: string; | ||
| hasProperty(propName: string): boolean; | ||
| getMappedPropName(propName: string): string; | ||
| } | ||
|
|
||
| export class HashElementSchema implements ElementSchemaEntry { | ||
| constructor(public tagName: string, private _properties: StringMap<string, string>, | ||
| public extendsElement: string = null) {} | ||
|
|
||
| hasProperty(propName: string): boolean { | ||
| return StringMapWrapper.contains(this._properties, propName); | ||
| } | ||
|
|
||
| getMappedPropName(propName: string): string { | ||
| return StringMapWrapper.get(this._properties, propName); | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
modules/angular2/src/render/dom/schema/element_schema_registry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import {isPresent, BaseException} from 'angular2/src/facade/lang'; | ||
| import {List, Map} from 'angular2/src/facade/collection'; | ||
|
|
||
| import {ElementSchemaEntry} from './element_schema_entry'; | ||
|
|
||
| export class ElementSchemaRegistry { | ||
| hasProperty(elName: string, propName: string): boolean { return true; } | ||
|
|
||
| getMappedPropName(elName: string, propName: string): string { return propName; } | ||
| } | ||
|
|
||
| export class SchemaRegistryError extends BaseException { | ||
| constructor(message) { super(message); } | ||
| } | ||
|
|
||
| export class ElementSchemaRegistryImpl extends ElementSchemaRegistry { | ||
| private _schema: Map<string, ElementSchemaEntry> = new Map(); | ||
|
|
||
| constructor(entries: List<ElementSchemaEntry>) { | ||
| super(); | ||
| entries.forEach((entry) => this._schema.set(entry.tagName, entry)); | ||
| } | ||
|
|
||
| hasProperty(elName: string, propName: string): boolean { | ||
| var entry = this.getEntry(elName); | ||
|
|
||
| while (!entry.hasProperty(propName) && isPresent(entry.extendsElement)) { | ||
| entry = this.getEntry(entry.extendsElement); | ||
| } | ||
|
|
||
| return entry.hasProperty(propName); | ||
| } | ||
|
|
||
| getMappedPropName(elName: string, propName: string): string { | ||
| var entry = this.getEntry(elName); | ||
| var mappedPropName = entry.getMappedPropName(propName); | ||
|
|
||
| while (!isPresent(mappedPropName) && isPresent(entry.extendsElement)) { | ||
| entry = this.getEntry(entry.extendsElement); | ||
| mappedPropName = entry.getMappedPropName(propName); | ||
| } | ||
|
|
||
| return isPresent(mappedPropName) ? mappedPropName : propName; | ||
| } | ||
|
|
||
| getEntry(tagName: string): ElementSchemaEntry { | ||
| var entry = this._schema.get(tagName); | ||
| if (!isPresent(entry)) { | ||
| entry = this.handleEntryNotFound(tagName); | ||
| if (isPresent(entry)) { | ||
| this._schema.set(tagName, entry); | ||
| } | ||
| } | ||
| return entry; | ||
| } | ||
|
|
||
| handleEntryNotFound(tagName: string): ElementSchemaEntry { | ||
| throw new SchemaRegistryError(`Missing schema entry for '${tagName}'`); | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
modules/angular2/test/render/dom/schema/dom_element_schema_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { | ||
| AsyncTestCompleter, | ||
| beforeEach, | ||
| ddescribe, | ||
| xdescribe, | ||
| describe, | ||
| el, | ||
| expect, | ||
| iit, | ||
| inject, | ||
| it, | ||
| xit, | ||
| beforeEachBindings, | ||
| SpyObject, | ||
| IS_DARTIUM | ||
| } from 'angular2/test_lib'; | ||
|
|
||
| import {DOMElementSchema} from 'angular2/src/render/dom/schema/dom_element_schema'; | ||
|
|
||
| export function main() { | ||
| // DOMElementSchema can only be used on the JS side where we can safely | ||
| // use reflection for DOM elements | ||
| if (IS_DARTIUM) return; | ||
|
|
||
| describe('DOMElementSchema', () => { | ||
|
|
||
| it('should detect properties on regular elements', () => { | ||
| var entry = new DOMElementSchema('div'); | ||
|
|
||
| expect(entry.hasProperty('id')).toBeTruthy(); | ||
| expect(entry.hasProperty('title')).toBeTruthy(); | ||
| expect(entry.hasProperty('unknown')).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('should detect properties on custom-like elements', () => { | ||
| var entry = new DOMElementSchema('custom-like'); | ||
|
|
||
| expect(entry.hasProperty('id')).toBeTruthy(); | ||
| expect(entry.hasProperty('title')).toBeTruthy(); | ||
| expect(entry.hasProperty('unknown')).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('should not re-map property names', () => { | ||
| var entry = new DOMElementSchema('div'); | ||
|
|
||
| expect(entry.getMappedPropName('title')).toBeNull(); | ||
| expect(entry.getMappedPropName('exotic-unknown')).toBeNull(); | ||
| }); | ||
| }); | ||
| } |
70 changes: 70 additions & 0 deletions
70
modules/angular2/test/render/dom/schema/element_schema_registry_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { | ||
| AsyncTestCompleter, | ||
| beforeEach, | ||
| ddescribe, | ||
| describe, | ||
| el, | ||
| expect, | ||
| iit, | ||
| inject, | ||
| it, | ||
| xit, | ||
| beforeEachBindings, | ||
| SpyObject, | ||
| } from 'angular2/test_lib'; | ||
|
|
||
| import {HashElementSchema} from 'angular2/src/render/dom/schema/element_schema_entry'; | ||
| import {ElementSchemaRegistryImpl} from 'angular2/src/render/dom/schema/element_schema_registry'; | ||
|
|
||
| export function main() { | ||
| describe('ElementSchemaRegistryImpl', () => { | ||
|
|
||
| it('should detect properties from entries', () => { | ||
| var registry = | ||
| new ElementSchemaRegistryImpl([new HashElementSchema('foo-bar', {'id': 'id'})]); | ||
| expect(registry.hasProperty('foo-bar', 'id')).toBeTruthy(); | ||
| expect(registry.hasProperty('foo-bar', 'unknown')).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('should detect properties from extended entries', () => { | ||
| var registry = new ElementSchemaRegistryImpl([ | ||
| new HashElementSchema('foo-bar', {'id': 'id'}), | ||
| new HashElementSchema('bar-baz', {}, 'foo-bar') | ||
| ]); | ||
| expect(registry.hasProperty('bar-baz', 'id')).toBeTruthy(); | ||
| expect(registry.hasProperty('bar-baz', 'unknown')).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('should handle properties re-mapping from entries', () => { | ||
| var registry = | ||
| new ElementSchemaRegistryImpl([new HashElementSchema('foo-bar', {'id': 'myId'})]); | ||
| expect(registry.getMappedPropName('foo-bar', 'id')).toEqual('myId'); | ||
| expect(registry.getMappedPropName('foo-bar', 'unknown')).toEqual('unknown'); | ||
| }); | ||
|
|
||
| it('should handle properties re-mapping from extended entries', () => { | ||
| var registry = new ElementSchemaRegistryImpl([ | ||
| new HashElementSchema('foo-bar', {'id': 'myId'}), | ||
| new HashElementSchema('bar-baz', {}, 'foo-bar') | ||
| ]); | ||
| expect(registry.getMappedPropName('bar-baz', 'id')).toEqual('myId'); | ||
| expect(registry.getMappedPropName('bar-baz', 'unknown')).toEqual('unknown'); | ||
| }); | ||
|
|
||
| it('should use property re-mapping from the top-level entry', () => { | ||
| var registry = new ElementSchemaRegistryImpl([ | ||
| new HashElementSchema('foo-bar', {'id': 'myId'}), | ||
| new HashElementSchema('bar-baz', {'id': 'topId'}, 'foo-bar'), | ||
| new HashElementSchema('baz-foo', {}, 'bar-baz') | ||
| ]); | ||
| expect(registry.getMappedPropName('baz-foo', 'id')).toEqual('topId'); | ||
| expect(registry.getMappedPropName('baz-foo', 'unknown')).toEqual('unknown'); | ||
| }); | ||
|
|
||
| it('should throw for unknown entries', () => { | ||
| var registry = new ElementSchemaRegistryImpl([]); | ||
| expect(() => { registry.hasProperty('div', 'someProp'); }) | ||
| .toThrowError("Missing schema entry for 'div'"); | ||
| }); | ||
| }) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need an interface with methods? Why not just field names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to fields. One downside of this is that instances of the
ElementSchemaEntryare mutable now.