Bug 1119670 - Implement processing of scope member of web manifest. r=ehsan

This commit is contained in:
Marcos Caceres 2015-01-15 19:46:00 -05:00
parent 5537c3a3b3
commit e0b62916dd
3 changed files with 126 additions and 1 deletions

View File

@ -135,6 +135,39 @@ this.ManifestProcessor.prototype.process = function({
return (displayModes.has(value)) ? value : defaultDisplayMode;
}
function processScopeMember(manifest, manifestURL, docURL, startURL) {
const spec = {
objectName: 'manifest',
object: manifest,
property: 'scope',
expectedType: 'string',
dontTrim: true
},
value = extractValue(spec);
let scopeURL;
try {
scopeURL = new URL(value, manifestURL);
} catch (e) {
let msg = 'The URL of scope is invalid.';
issueDeveloperWarning(msg);
return undefined;
}
if (scopeURL.origin !== docURL.origin) {
let msg = 'Scope needs to be same-origin as Document.';
issueDeveloperWarning(msg);
return undefined;
}
//If start URL is not within scope of scope URL:
if (startURL && startURL.origin !== scopeURL.origin || !startURL.pathname.startsWith(scopeURL.pathname)) {
let msg = 'The start URL is outside the scope, so scope is invalid.';
issueDeveloperWarning(msg);
return undefined;
}
return scopeURL;
}
function processStartURLMember(manifest, manifestURL, docURL) {
const obj = {
objectName: 'manifest',
@ -319,5 +352,6 @@ this.ManifestProcessor.prototype.process = function({
icons: processIconsMember(manifest, manifestURL),
short_name: processShortNameMember(manifest)
};
processedManifest.scope = processScopeMember(manifest, manifestURL, docURL, processedManifest.start_url);
return processedManifest;
};

View File

@ -13,4 +13,5 @@ support-files =
[test_ManifestProcessor_JSON.html]
[test_ManifestProcessor_name_and_short_name.html]
[test_ManifestProcessor_orientation.html]
[test_ManifestProcessor_start_url.html]
[test_ManifestProcessor_start_url.html]
[test_ManifestProcessor_scope.html]

View File

@ -0,0 +1,90 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1079453
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1079453</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script src="common.js"></script>
<script>
/**
* Manifest scope
* https://w3c.github.io/manifest/#scope-member
**/
'use strict';
var expected = 'Expect non-string scope to be undefined';
typeTests.forEach((type) => {
data.jsonText = JSON.stringify({
scope: type
});
var result = processor.process(data);
ise(result.scope, undefined, expected);
});
var expected = 'Expect different origin to be treated as undefined';
data.jsonText = JSON.stringify({
scope: 'http://not-same-origin'
});
var result = processor.process(data);
ise(result.scope, undefined, expected);
var expected = 'Expect the empty string to be treated as undefined.';
data.jsonText = JSON.stringify({
scope: ''
});
var result = processor.process(data);
ise(result.scope, undefined, expected);
var expected = 'Resolve URLs relative to manifest.';
var URLs = ['path', '/path', '../../path'];
URLs.forEach((url) => {
data.jsonText = JSON.stringify({
scope: url,
start_url: "/path"
});
var absURL = new URL(url, manifestURL).toString();
var result = processor.process(data);
console.log(result);
ise(String(result.scope), absURL, expected);
});
var expected = 'If start URL is not in scope, return undefined.';
data.jsonText = JSON.stringify({
scope: 'foo',
start_url: 'bar'
});
var result = processor.process(data);
ise(result.scope, undefined, expected);
var expected = 'If start URL is in scope, use the scope.';
data.jsonText = JSON.stringify({
start_url: 'foobar',
scope: 'foo'
});
var result = processor.process(data);
ise(result.scope.toString(), new URL('foo', manifestURL).toString(), expected);
var expected = 'Expect start_url to be ' + new URL('foobar', manifestURL).toString();
ise(result.start_url.toString(), new URL('foobar', manifestURL).toString(), expected);
var expected = 'If start URL is in scope, use the scope.';
data.jsonText = JSON.stringify({
start_url: '/foo/',
scope: '/foo/'
});
var result = processor.process(data);
ise(result.scope.toString(), new URL('/foo/', manifestURL).toString(), expected);
var expected = 'If start URL is in scope, use the scope.';
data.jsonText = JSON.stringify({
start_url: '.././foo/',
scope: '../foo/'
});
var result = processor.process(data);
ise(result.scope.toString(), new URL('/foo/', manifestURL).toString(), expected);
</script>
</head>