chore(refactor): easier to make sense of attr-to-prop map (even if a bit reduntant)

This commit is contained in:
Yegor Jbanov 2015-02-27 22:11:08 -08:00
parent 757eae8ad3
commit cbe7b8c671
4 changed files with 23 additions and 26 deletions

View File

@ -91,26 +91,12 @@ function roleSetter(element, value) {
} }
} }
// special mapping for cases where attribute name doesn't match property name
var _lazyAttrToProp;
function attrToProp() {
if (!isPresent(_lazyAttrToProp)) {
_lazyAttrToProp = StringMapWrapper.merge({
"inner-html": "innerHTML",
"readonly": "readOnly",
"tabindex": "tabIndex",
}, DOM.attrToPropMap);
}
return _lazyAttrToProp;
}
// tells if an attribute is handled by the ElementBinderBuilder step // tells if an attribute is handled by the ElementBinderBuilder step
export function isSpecialProperty(propName:string) { export function isSpecialProperty(propName:string) {
return StringWrapper.startsWith(propName, ARIA_PREFIX) return StringWrapper.startsWith(propName, ARIA_PREFIX)
|| StringWrapper.startsWith(propName, CLASS_PREFIX) || StringWrapper.startsWith(propName, CLASS_PREFIX)
|| StringWrapper.startsWith(propName, STYLE_PREFIX) || StringWrapper.startsWith(propName, STYLE_PREFIX)
|| StringMapWrapper.contains(attrToProp(), propName); || StringMapWrapper.contains(DOM.attrToPropMap, propName);
} }
/** /**
@ -257,7 +243,7 @@ export class ElementBinderBuilder extends CompileStep {
} }
_resolvePropertyName(attrName:string) { _resolvePropertyName(attrName:string) {
var mappedPropName = StringMapWrapper.get(attrToProp(), attrName); var mappedPropName = StringMapWrapper.get(DOM.attrToPropMap, attrName);
return isPresent(mappedPropName) ? mappedPropName : attrName; return isPresent(mappedPropName) ? mappedPropName : attrName;
} }
} }

View File

@ -18,10 +18,11 @@ class BrowserDomAdapter extends DomAdapter {
setRootDomAdapter(new BrowserDomAdapter()); setRootDomAdapter(new BrowserDomAdapter());
} }
// override JS logic of attribute to property mapping
@override @override
final attrToPropMap = const { final attrToPropMap = const {
"inner-html": "innerHtml" 'inner-html': 'innerHtml',
'readonly': 'readOnly',
'tabindex': 'tabIndex',
}; };
query(String selector) => document.querySelector(selector); query(String selector) => document.querySelector(selector);
@ -108,13 +109,13 @@ class BrowserDomAdapter extends DomAdapter {
createScriptTag(String attrName, String attrValue, createScriptTag(String attrName, String attrValue,
[HtmlDocument doc = null]) { [HtmlDocument doc = null]) {
if (doc == null) doc = document; if (doc == null) doc = document;
var el = doc.createElement("SCRIPT"); var el = doc.createElement('SCRIPT');
el.setAttribute(attrName, attrValue); el.setAttribute(attrName, attrValue);
return el; return el;
} }
StyleElement createStyleElement(String css, [HtmlDocument doc = null]) { StyleElement createStyleElement(String css, [HtmlDocument doc = null]) {
if (doc == null) doc = document; if (doc == null) doc = document;
var el = doc.createElement("STYLE"); var el = doc.createElement('STYLE');
el.text = css; el.text = css;
return el; return el;
} }

View File

@ -2,15 +2,19 @@ import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
import {isPresent} from 'angular2/src/facade/lang'; import {isPresent} from 'angular2/src/facade/lang';
import {DomAdapter, setRootDomAdapter} from './dom_adapter'; import {DomAdapter, setRootDomAdapter} from './dom_adapter';
var EMPTY_MAP = MapWrapper.create(); var _attrToPropMap = {
'inner-html': 'innerHTML',
'readonly': 'readOnly',
'tabindex': 'tabIndex',
};
export class BrowserDomAdapter extends DomAdapter { export class BrowserDomAdapter extends DomAdapter {
static makeCurrent() { static makeCurrent() {
setRootDomAdapter(new BrowserDomAdapter()); setRootDomAdapter(new BrowserDomAdapter());
} }
get attrToPropMap():Map { get attrToPropMap() {
return EMPTY_MAP return _attrToPropMap;
} }
query(selector) { query(selector) {
@ -75,7 +79,7 @@ export class BrowserDomAdapter extends DomAdapter {
return res; return res;
} }
clearNodes(el) { clearNodes(el) {
el.innerHTML = ""; el.innerHTML = '';
} }
appendChild(el, node) { appendChild(el, node) {
el.appendChild(node); el.appendChild(node);
@ -133,7 +137,7 @@ export class BrowserDomAdapter extends DomAdapter {
return doc.createTextNode(text); return doc.createTextNode(text);
} }
createScriptTag(attrName:string, attrValue:string, doc=document) { createScriptTag(attrName:string, attrValue:string, doc=document) {
var el = doc.createElement("SCRIPT"); var el = doc.createElement('SCRIPT');
el.setAttribute(attrName, attrValue); el.setAttribute(attrName, attrValue);
return el; return el;
} }

View File

@ -15,9 +15,15 @@ function _abstract() {
*/ */
@ABSTRACT() @ABSTRACT()
export class DomAdapter { export class DomAdapter {
get attrToPropMap():Map {
/**
* Maps attribute names to their corresponding property names for cases
* where attribute name doesn't match property name.
*/
get attrToPropMap() {
throw _abstract(); throw _abstract();
} }
parse(templateHtml:string) { parse(templateHtml:string) {
throw _abstract(); throw _abstract();
} }