Skip to content

Commit

Permalink
Merge pull request #655 from tidev/chorePlatformId
Browse files Browse the repository at this point in the history
chore: use platform for id
  • Loading branch information
cb1kenobi authored Apr 7, 2024
2 parents 92adc68 + 1fd2543 commit 4cfd5cd
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/tiappxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const xml = appc.xml;
const __ = appc.i18n(__dirname).__;

const defaultDOMParserArgs = { errorHandler: function () {} };

module.exports = tiapp;

function toXml(dom, parent, name, value) {
Expand Down Expand Up @@ -390,7 +389,7 @@ function toXml(dom, parent, name, value) {
node.appendChild(dom.createTextNode('\r\n' + new Array(2).join('\t')));
}

function toJS(obj, doc) {
function toJS(obj, doc, targetPlatform) {
var node = doc.firstChild;
while (node) {
if (node.nodeType === xml.ELEMENT_NODE) {
Expand Down Expand Up @@ -817,9 +816,17 @@ function toJS(obj, doc) {
obj[node.tagName] = node.firstChild && node.firstChild.data.replace(/\n/g, '').trim() || '';
break;

case 'id':
if ((targetPlatform && xml.getAttr(node, 'platform') === targetPlatform) || obj[node.tagName] === undefined) {
obj[node.tagName] = '' + xml.getValue(node);
if (typeof obj[node.tagName] === 'string') {
obj[node.tagName] = obj[node.tagName].replace(/\n/g, '');
}
}
break;

case 'name':
case 'guid':
case 'id':
case 'icon':
// need to strip out line returns which shouldn't be there in the first place
obj[node.tagName] = '' + xml.getValue(node);
Expand Down Expand Up @@ -855,20 +862,21 @@ function toJS(obj, doc) {
}
}

function tiapp(filename) {
function tiapp(filename, platform) {

Object.defineProperty(this, 'load', {
value: function (file) {
if (!fs.existsSync(file)) {
throw new Error(__('tiapp.xml file does not exist'));
}
toJS(this, (new DOMParser(defaultDOMParserArgs).parseFromString(fs.readFileSync(file).toString(), 'text/xml')).documentElement);
toJS(this, (new DOMParser(defaultDOMParserArgs).parseFromString(fs.readFileSync(file).toString(), 'text/xml')).documentElement, platform);
return this;
}
});

Object.defineProperty(this, 'parse', {
value: function (str) {
toJS(this, (new DOMParser(defaultDOMParserArgs).parseFromString(str, 'text/xml')).documentElement);
toJS(this, (new DOMParser(defaultDOMParserArgs).parseFromString(str, 'text/xml')).documentElement, platform);
return this;
}
});
Expand Down

0 comments on commit 4cfd5cd

Please sign in to comment.