Bug 1290901 - [webext] Handle nested namespaced API schema. r=aswan

MozReview-Commit-ID: H26E8ZwLrqJ

--HG--
extra : rebase_source : d3eaba631b4f3aca55e0edb037776c13a319934d
This commit is contained in:
Luca Greco 2016-08-15 15:34:43 +02:00
Родитель 192781cb4a
Коммит d59eaef91b
2 изменённых файлов: 31 добавлений и 0 удалений

Просмотреть файл

@ -356,6 +356,12 @@ class ProxyContext extends ExtensionContext {
}
function findPathInObject(obj, path) {
// Split any nested namespace (e.g devtools.inspectedWindow) element
// and concatenate them into a flatten array.
path = path.reduce((acc, el) => {
return acc.concat(el.split("."));
}, []);
for (let elt of path) {
obj = obj[elt] || undefined;
}

Просмотреть файл

@ -1651,8 +1651,33 @@ this.Schemas = {
}
}
// Remove the namespace object if it is empty
if (!Object.keys(obj).length) {
delete dest[namespace];
// process the next namespace.
continue;
}
// If the nested namespaced API object (e.g devtools.inspectedWindow) is not empty,
// then turn `dest["nested.namespace"]` into `dest["nested"]["namespace"]`.
if (namespace.includes(".")) {
let apiObj = dest[namespace];
delete dest[namespace];
let nsLevels = namespace.split(".");
let currentObj = dest;
for (let nsLevel of nsLevels.slice(0, -1)) {
if (!currentObj[nsLevel]) {
// Create the namespace level if it doesn't exist yet.
currentObj = Cu.createObjectIn(currentObj, {defineAs: nsLevel});
} else {
// Move currentObj to the nested object if it already exists.
currentObj = currentObj[nsLevel];
}
}
// Copy the apiObj as the final nested level.
currentObj[nsLevels.pop()] = apiObj;
}
}
},