Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit 8e5b457

Browse files
author
msrodri
committed
Merge pull request #28 from manifoldjs/v0.1.2
V0.1.2
2 parents 0215d75 + 6a08706 commit 8e5b457

File tree

15 files changed

+98
-14
lines changed

15 files changed

+98
-14
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/node_modules/
2+
/scripts/node_modules/
23
npm-debug.log
34
coverage.html
45
.ntvs_analysis.dat

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-plugin-hostedwebapp",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Hosted Web App Plugin",
55
"cordova": {
66
"id": "cordova-plugin-hostedwebapp",

plugin.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
33
id="cordova-plugin-hostedwebapp"
4-
version="0.1.1">
4+
version="0.1.2">
55
<name>HostedWebApp</name>
66
<description>Hosted Web App Plugin</description>
77
<license>MIT License</license>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<widget id="com.example.hello" version="0.0.1">
3+
</widget>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"start_url": "http://wat-docs.azurewebsites.net/",
3+
"name": "WAT Documentation",
4+
"orientation": "landscape",
5+
"display": "fullscreen",
6+
"scope": "http://www.domain.com"
7+
}

scripts/test/assets/jsonEmpty/manifest.json

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"start_url": "http://wat-docs.azurewebsites.net/"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
empty folder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<widget id="com.example.hello" version="0.0.1">
3+
</widget>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"start_url": "http://wat-docs.azurewebsites.net/",
3+
"name": "WAT Documentation",
4+
"orientation": "landscape",
5+
"display": "fullscreen",
6+
"scope": "http://*.domain.com"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
empty folder

scripts/test/updateConfigurationBeforePrepare.js

+44-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function initializeContext(testDir) {
5959
return ctx;
6060
}
6161

62-
describe('updateConfiguration.js', function (){
62+
describe('updateConfigurationBeforePrepare.js', function (){
6363
beforeEach(function () {
6464
tu.copyRecursiveSync(assetsDirectory, workingDirectory);
6565
});
@@ -104,7 +104,7 @@ describe('updateConfiguration.js', function (){
104104
});
105105

106106
it('Should not update name if it is missing in manifest.json', function (done) {
107-
var testDir = path.join(workingDirectory, 'jsonEmpty');
107+
var testDir = path.join(workingDirectory, 'jsonPropertiesMissing');
108108
var configXML = path.join(testDir, 'config.xml');
109109
var ctx = initializeContext(testDir);
110110

@@ -146,7 +146,7 @@ describe('updateConfiguration.js', function (){
146146
});
147147

148148
it('Should not update orientation if it is missing in manifest.json', function (done){
149-
var testDir = path.join(workingDirectory, 'jsonEmpty');
149+
var testDir = path.join(workingDirectory, 'jsonPropertiesMissing');
150150
var configXML = path.join(testDir, 'config.xml');
151151
var ctx = initializeContext(testDir);
152152

@@ -187,7 +187,7 @@ describe('updateConfiguration.js', function (){
187187
});
188188

189189
it('Should not update fullscreen if it is missing in manifest.json', function (done){
190-
var testDir = path.join(workingDirectory, 'jsonEmpty');
190+
var testDir = path.join(workingDirectory, 'jsonPropertiesMissing');
191191
var configXML = path.join(testDir, 'config.xml');
192192
var ctx = initializeContext(testDir);
193193

@@ -214,7 +214,7 @@ describe('updateConfiguration.js', function (){
214214
});
215215

216216
it('Should keep existing access rules unchanged in config.xml', function (done){
217-
var testDir = path.join(workingDirectory, 'jsonEmpty');
217+
var testDir = path.join(workingDirectory, 'jsonPropertiesMissing');
218218
var configXML = path.join(testDir, 'config.xml');
219219
var ctx = initializeContext(testDir);
220220

@@ -267,7 +267,7 @@ describe('updateConfiguration.js', function (){
267267
});
268268
});
269269

270-
it('Should add access rules for scope in config.xml', function (done){
270+
it('Should add access rules for scope in config.xml if scope is a relative URL', function (done){
271271
var testDir = path.join(workingDirectory, 'xmlEmptyWidget');
272272
var configXML = path.join(testDir, 'config.xml');
273273
var ctx = initializeContext(testDir);
@@ -286,6 +286,44 @@ describe('updateConfiguration.js', function (){
286286
});
287287
});
288288

289+
it('Should add access rules for scope in config.xml if scope is a full URL', function (done){
290+
var testDir = path.join(workingDirectory, 'fullUrlForScope');
291+
var configXML = path.join(testDir, 'config.xml');
292+
var ctx = initializeContext(testDir);
293+
294+
updateConfiguration(ctx).then(function () {
295+
var content = fs.readFileSync(configXML).toString();
296+
297+
// rules for android
298+
assert(content.match(/<platform name="android">[\s\S]*<access hap-rule="yes" origin="http:\/\/www.domain.com\/\*" \/>[\s\S]*<\/platform>/));
299+
assert(content.match(/<platform name="android">[\s\S]*<allow-navigation hap-rule="yes" href="http:\/\/www.domain.com\/\*" \/>[\s\S]*<\/platform>/));
300+
301+
// rules for ios
302+
assert(content.match(/<platform name="ios">[\s\S]*<access hap-rule="yes" origin="http:\/\/www.domain.com\/\*" \/>[\s\S]*<\/platform>/));
303+
304+
done();
305+
});
306+
});
307+
308+
it('Should add access rules for scope in config.xml if scope is a full URL with wildcard as subdomain', function (done){
309+
var testDir = path.join(workingDirectory, 'wildcardSubdomainForScope');
310+
var configXML = path.join(testDir, 'config.xml');
311+
var ctx = initializeContext(testDir);
312+
313+
updateConfiguration(ctx).then(function () {
314+
var content = fs.readFileSync(configXML).toString();
315+
316+
// rules for android
317+
assert(content.match(/<platform name="android">[\s\S]*<access hap-rule="yes" origin="http:\/\/\*.domain.com" \/>[\s\S]*<\/platform>/));
318+
assert(content.match(/<platform name="android">[\s\S]*<allow-navigation hap-rule="yes" href="http:\/\/\*.domain.com" \/>[\s\S]*<\/platform>/));
319+
320+
// rules for ios
321+
assert(content.match(/<platform name="ios">[\s\S]*<access hap-rule="yes" origin="http:\/\/\*.domain.com" \/>[\s\S]*<\/platform>/));
322+
323+
done();
324+
});
325+
});
326+
289327
it('Should add access rules from mjs_access_whitelist list', function (done){
290328
var testDir = path.join(workingDirectory, 'xmlEmptyWidget');
291329
var configXML = path.join(testDir, 'config.xml');

scripts/updateConfigurationBeforePrepare.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,18 @@ function processAccessRules(manifest) {
155155
// determine base rule based on the start_url and the scope
156156
var baseUrlPattern = manifest.start_url;
157157
if (manifest.scope && manifest.scope.length) {
158-
baseUrlPattern = url.resolve(baseUrlPattern, manifest.scope);
158+
var parsedScopeUrl = url.parse(manifest.scope);
159+
if (parsedScopeUrl.protocol) {
160+
baseUrlPattern = manifest.scope;
161+
} else {
162+
baseUrlPattern = url.resolve(baseUrlPattern, manifest.scope);
163+
}
159164
}
160165

161-
baseUrlPattern = url.resolve(baseUrlPattern, '*');
166+
// If there are no wildcards in the pattern, add '*' at the end
167+
if (baseUrlPattern.indexOf('*') === -1) {
168+
baseUrlPattern = url.resolve(baseUrlPattern, '*');
169+
}
162170

163171
// add base rule as an access rule for Android
164172
var androidAccessBaseRule = new etree.SubElement(androidRoot, 'access');
@@ -594,20 +602,34 @@ module.exports = function (context) {
594602
var manifestPath = path.join(projectRoot, 'manifest.json');
595603
fs.readFile(manifestPath, function (err, data) {
596604
if (err) {
597-
logger.error('ERROR: Failed to read manifest in at \'' + manifestPath + '\'.');
605+
logger.error('Failed to read manifest at \'' + manifestPath + '\'.');
598606
return task.reject(err);
599607
}
600608

601609
var manifestJson = data.toString().replace(/^\uFEFF/, '');
602610
var appManifestPath = path.join(projectRoot, 'www', 'manifest.json');
603611
fs.writeFile(appManifestPath, manifestJson, function (err) {
604612
if (err) {
605-
logger.error('ERROR: Failed to copy manifest to \'www\' folder.');
613+
logger.error('Failed to copy manifest to \'www\' folder.');
606614
return task.reject(err);
607615
}
608616

609617
var manifest = JSON.parse(manifestJson);
610618

619+
// The start_url member is required and must be a full URL.
620+
// Even though a relative URL is a valid according to the W3C spec, a full URL
621+
// is needed because the plugin cannot determine the manifest's origin.
622+
var start_url;
623+
if (manifest.start_url) {
624+
start_url = url.parse(manifest.start_url);
625+
}
626+
627+
if (!(start_url && start_url.hostname && start_url.protocol)) {
628+
logger.error('Invalid or incomplete W3C manifest.');
629+
var err = new Error('The start_url member in the manifest is required and must be a full URL.');
630+
return task.reject(err);
631+
}
632+
611633
// update name, start_url, orientation, and fullscreen from manifest
612634
if (manifest.short_name) {
613635
config.setName(manifest.short_name.replace(/\//g,'').replace(/\s/g,''));

0 commit comments

Comments
 (0)