diff --git a/src/parsers/installrdf.js b/src/parsers/installrdf.js index 138dad4547c..7b03379c06e 100644 --- a/src/parsers/installrdf.js +++ b/src/parsers/installrdf.js @@ -3,6 +3,9 @@ import * as messages from 'messages'; import log from 'logger'; import { singleLineString } from 'utils'; +const TARGET_APPLICATION_FIELDS = [ + 'id', 'minVersion', 'maxVersion', +]; export default class InstallRdfParser { @@ -21,6 +24,7 @@ export default class InstallRdfParser { type: this._getAddonType(), version: this._getVersion(), restartless: this._getIsBootstrapped(), + application: this._getApplication(), }); } @@ -105,6 +109,40 @@ export default class InstallRdfParser { this._getTopLevelNodeByTag('em:bootstrap')) === 'true'; } + _getApplication() { + var targetApplications = this._getTopLevelNodesByTag( + 'em:targetApplication'); + var applications = []; + + for (let targetApplication of targetApplications) { + let application = {}; + let descriptionNodes = Array.prototype.slice.call( + targetApplication.childNodes) + .filter((node) => node.nodeName === 'Description'); + + if (descriptionNodes.length !== 1) { + throw new Error(singleLineString` node should only + have a single descendant `); + } + + for (var i in descriptionNodes[0].childNodes) { + let childNode = descriptionNodes[0].childNodes[i]; + if (TARGET_APPLICATION_FIELDS.indexOf(childNode.localName) !== -1) { + application[childNode.localName] = this._getNodeValue(childNode); + } + } + + if (application.id && application.maxVersion && application.minVersion) { + applications.push(application); + } else { + throw new Error(singleLineString`targetApplication must contain an id, + minVersion and maxVersion fields to be valid.`); + } + } + + return applications; + } + _getGUID() { // Install.rdf only. var guid = this._getNodeValue(this._getTopLevelNodeByTag('em:id')); diff --git a/src/parsers/manifestjson.js b/src/parsers/manifestjson.js index 3f8ed3f3990..2f1abb4e866 100644 --- a/src/parsers/manifestjson.js +++ b/src/parsers/manifestjson.js @@ -19,9 +19,14 @@ export default class ManifestJSONParser { type: this._getType(), version: this._getVersion(), restartless: true, // All web extensions are restartless + application: this._getApplication(), }); } + _getApplication() { + return this.parsedJSON.applications; + } + _getType() { return PACKAGE_EXTENSION; } diff --git a/tests/parsers/test.installrdf.js b/tests/parsers/test.installrdf.js index f53a79dba68..ac6925e19d5 100644 --- a/tests/parsers/test.installrdf.js +++ b/tests/parsers/test.installrdf.js @@ -287,6 +287,114 @@ describe('InstallRdfParser._getIsBootstrapped()', () => { }); }); +describe('InstallRdfParser._getApplication()', () => { + it('should retrieve the application info', () => { + var id = '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}'; + var minVersion = '1.5'; + var maxVersion = '3.0.*'; + var rdf = validRDF(` + + ${id} + ${minVersion} + ${maxVersion} + + `); + var rdfScanner = new RDFScanner(rdf, INSTALL_RDF); + return rdfScanner.getContents() + .then((xmlDoc) => { + var installRdfParser = new InstallRdfParser(xmlDoc); + return installRdfParser._getApplication(); + }) + .then((applications) => { + assert.equal(applications[0].id, id); + assert.equal(applications[0].minVersion, minVersion); + assert.equal(applications[0].maxVersion, maxVersion); + }); + }); + + it('should retrieve the application info', () => { + var expectedApplications = [{ + id: '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}', + minVersion: '1.5', + maxVersion: '3.0.*', + }, { + id: '{abcdefgh-c20a-464f-9b0e-13a3a9e97384}', + minVersion: '2.5', + maxVersion: '4.0.*', + }, + ]; + var rdf = validRDF(` + + ${expectedApplications[0].id} + ${expectedApplications[0].minVersion} + ${expectedApplications[0].maxVersion} + + + + + ${expectedApplications[1].id} + ${expectedApplications[1].minVersion} + ${expectedApplications[1].maxVersion} + + `); + var rdfScanner = new RDFScanner(rdf, INSTALL_RDF); + return rdfScanner.getContents() + .then((xmlDoc) => { + var installRdfParser = new InstallRdfParser(xmlDoc); + return installRdfParser._getApplication(); + }) + .then((applications) => { + for (var i = 0; i < applications.length; i++) { + assert.equal(applications[i].id, expectedApplications[i].id); + assert.equal(applications[i].minVersion, + expectedApplications[i].minVersion); + assert.equal(applications[i].maxVersion, + expectedApplications[i].maxVersion); + } + }); + }); + + it('should reject on missing Description node', () => { + var rdf = validRDF( + ``); + var rdfScanner = new RDFScanner(rdf, INSTALL_RDF); + return rdfScanner.getContents() + .then((xmlDoc) => { + var installRdfParser = new InstallRdfParser(xmlDoc); + return installRdfParser._getApplication(); + }) + .then(unexpectedSuccess) + .catch((err) => { + assert.equal(err.message, + singleLineString` node should only have a + single descendant `); + }); + }); + + it('should reject on missing id node', () => { + var minVersion = '1.5'; + var maxVersion = '3.0.*'; + var rdf = validRDF(` + + ${minVersion} + ${maxVersion} + + `); + var rdfScanner = new RDFScanner(rdf, INSTALL_RDF); + return rdfScanner.getContents() + .then((xmlDoc) => { + var installRdfParser = new InstallRdfParser(xmlDoc); + return installRdfParser._getApplication(); + }) + .then(unexpectedSuccess) + .catch((err) => { + assert.equal(err.message, + singleLineString`targetApplication must contain an id, + minVersion and maxVersion fields to be valid.`); + }); + }); +}); + describe('InstallRdfParser._getDescriptionNode()', function() { it('should reject on missing RDF node', () => {