Skip to content

Commit

Permalink
Merge pull request #1677 from bryan-m-hughes/timob-7997
Browse files Browse the repository at this point in the history
[TIMOB-7997] Implemented getters and setters for XML.
  • Loading branch information
cb1kenobi committed Mar 15, 2012
2 parents ec1c558 + 180245b commit 1ea21a5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
3 changes: 1 addition & 2 deletions apidoc/Titanium/XML/XML.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ description: |
Mobile Web directly exposes the [DOM Level 3](http://www.w3.org/TR/DOM-Level-3-Core/core.html) implementation
in the browser. New features introduced in DOM Level 3 are not documented here for brevity. For information on
these features, please use the W3C documentation. Getters and setters for properties are not supported on
Mobile Web.
these features, please use the W3C documentation.
extends: Titanium.Module
since: "0.9"
methods:
Expand Down
52 changes: 52 additions & 0 deletions mobileweb/titanium/Ti/XML.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
define(["Ti/_/Evented", "Ti/_/lang"], function(Evented, lang) {

// Add getters and setters to the various prototypes
var elements = [[
Document,
"doctype,implementation,documentElement,inputEncoding,xmlEncoding,domConfig",
"xmlStandalone,xmlVersion,strictErrorChecking,documentURI"
],[
Node,
"nodeName,nodeType,parentNode,childNodes,firstChild,lastChild,previousSibling,nextSibling,attributes,ownerDocument,namespaceURI,localName,baseURI",
"textContent,nodeValue,prefix"
],[
NamedNodeMap,
"length",
],[
CharacterData,
"length",
"data"
],[
Attr,
"name,specified,ownerElement,schemaTypeInfo,isId",
"value"
],[
Element,
"tagName,schemaTypeInfo"
],[
Text,
"isElementContentWhitespace,wholeText"
],[
DocumentType,
"name,entities,notations,publicId,systemId,internalSubset"
],[
Notation,
"publicId,systemId"
],[
NodeList,
"length"
],[
Entity,
"publicId,systemId,notationName,inputEncoding,xmlEncoding,xmlVersion"
],[
ProcessingInstruction,
"target",
"data"
]
];
for(var i = 0; i < elements.length; i++) {
lang.generateAccessors(elements[i][0],elements[i][1],elements[i][2]);
}
Object.defineProperty(Element.prototype, "text", {
get: function() { return this.textContent; },
enumerable: true
});

return lang.setObject("Ti.XML", Evented, {

Expand Down
27 changes: 27 additions & 0 deletions mobileweb/titanium/Ti/_/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ define(function() {
}
return dest;
},

generateAccessors: function(definition, readOnlyProps, props) {

function generateGetter(prop) {
var getterName = "get" + prop.substring(0, 1).toUpperCase() + prop.substring(1);
if (!(getterName in definition.prototype)) {
definition.prototype[getterName] = function() {
return this[prop];
}
}
}

function generateSetter(prop) {
var setterName = "set" + prop.substring(0, 1).toUpperCase() + prop.substring(1);
if (!(setterName in definition.prototype)) {
definition.prototype[setterName] = function(value) {
return this[prop] = value;
}
}
}

readOnlyProps && readOnlyProps.split(",").forEach(generateGetter);
props && props.split(",").forEach(function(prop) {
generateGetter(prop);
generateSetter(prop);
});
},

setObject: function(name) {
var parts = name.split("."),
Expand Down

0 comments on commit 1ea21a5

Please sign in to comment.