Skip to content

Commit 77984e1

Browse files
committed
fix(cordova): remove unwanted allow-navigation entries
Fixes #1841
1 parent 0346adc commit 77984e1

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

packages/@ionic/cli-utils/src/lib/integrations/cordova/config.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as et from 'elementtree';
88

99
import { IProject, ResourcesPlatform } from '../../../definitions';
1010
import { FatalException } from '../../errors';
11+
import { shortid } from '../../utils/uuid';
1112

1213
const debug = Debug('ionic:cli-utils:lib:integrations:cordova:config');
1314

@@ -19,9 +20,10 @@ export interface PlatformEngine {
1920

2021
export class ConfigXml {
2122
protected _doc?: et.ElementTree;
23+
protected _sessionid?: string;
2224
protected saving = false;
2325

24-
constructor(public filePath: string) {}
26+
constructor(readonly filePath: string) {}
2527

2628
get doc() {
2729
if (!this._doc) {
@@ -31,6 +33,14 @@ export class ConfigXml {
3133
return this._doc;
3234
}
3335

36+
get sessionid() {
37+
if (!this._sessionid) {
38+
throw new Error('No doc loaded.');
39+
}
40+
41+
return this._sessionid;
42+
}
43+
3444
static async load(filePath: string): Promise<ConfigXml> {
3545
if (!filePath) {
3646
throw new Error('Must supply file path.');
@@ -51,6 +61,7 @@ export class ConfigXml {
5161

5262
try {
5363
this._doc = et.parse(configFileContents);
64+
this._sessionid = shortid();
5465
} catch (e) {
5566
throw new Error(`Cannot parse config.xml file: ${e.stack ? e.stack : e}`);
5667
}
@@ -103,7 +114,7 @@ export class ConfigXml {
103114
let navElement = root.find(`allow-navigation[@href='${newSrc}']`);
104115

105116
if (!navElement) {
106-
navElement = et.SubElement(root, 'allow-navigation', { href: newSrc });
117+
navElement = et.SubElement(root, 'allow-navigation', { sessionid: this.sessionid, href: newSrc });
107118
}
108119
}
109120

@@ -124,6 +135,12 @@ export class ConfigXml {
124135
contentElement.set('src', originalSrc);
125136
delete contentElement.attrib['original-src'];
126137
}
138+
139+
const navElements = root.findall(`allow-navigation[@sessionid='${this.sessionid}']`);
140+
141+
for (const navElement of navElements) {
142+
root.remove(navElement);
143+
}
127144
}
128145

129146
getPreference(prefName: string): string | undefined {

packages/@ionic/cli-utils/src/lib/utils/uuid.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ import * as uuidv4 from 'uuid/v4';
33
export function generateUUID(): string {
44
return uuidv4().toString();
55
}
6+
7+
export function shortid(): string {
8+
return generateUUID().substring(0, 8);
9+
}

0 commit comments

Comments
 (0)