From 5ec72b06bea2745040478eddacf4414768bce1d9 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 7 Aug 2018 13:09:09 +0200 Subject: [PATCH] MOBILE-2526 core: Fix text.replace is not a function --- src/core/grades/providers/helper.ts | 8 ++++---- src/providers/utils/text.ts | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/core/grades/providers/helper.ts b/src/core/grades/providers/helper.ts index c7c7a62a1..36e825802 100644 --- a/src/core/grades/providers/helper.ts +++ b/src/core/grades/providers/helper.ts @@ -46,8 +46,8 @@ export class CoreGradesHelperProvider { protected formatGradeRow(tableRow: any): any { const row = {}; for (const name in tableRow) { - if (typeof(tableRow[name].content) != 'undefined') { - let content = tableRow[name].content; + if (typeof tableRow[name].content != 'undefined' && tableRow[name].content !== null) { + let content = String(tableRow[name].content); if (name == 'itemname') { this.setRowIcon(row, content); @@ -81,8 +81,8 @@ export class CoreGradesHelperProvider { protected formatGradeRowForTable(tableRow: any): any { const row = {}; for (let name in tableRow) { - if (typeof(tableRow[name].content) != 'undefined') { - let content = tableRow[name].content; + if (typeof tableRow[name].content != 'undefined' && tableRow[name].content !== null) { + let content = String(tableRow[name].content); if (name == 'itemname') { this.setRowIcon(row, content); diff --git a/src/providers/utils/text.ts b/src/providers/utils/text.ts index 7477db9ff..016fd3982 100644 --- a/src/providers/utils/text.ts +++ b/src/providers/utils/text.ts @@ -129,7 +129,7 @@ export class CoreTextUtilsProvider { * @return {string} Clean text. */ cleanTags(text: string, singleLine?: boolean): string { - if (typeof text == 'number') { + if (typeof text != 'string') { return text; } @@ -197,6 +197,10 @@ export class CoreTextUtilsProvider { * @return {number} Number of words. */ countWords(text: string): number { + if (!text || typeof text != 'string') { + return 0; + } + // Clean HTML scripts and tags. text = text.replace(/]*>([\S\s]*?)<\/script>/gmi, ''); text = text.replace(/<\/?(?!\!)[^>]*>/gi, ''); @@ -285,7 +289,7 @@ export class CoreTextUtilsProvider { * @return {string} Escaped text. */ escapeForRegex(text: string): string { - if (!text) { + if (!text || typeof text != 'string') { return ''; } @@ -476,6 +480,10 @@ export class CoreTextUtilsProvider { * @return {string} Treated text. */ removeSpecialCharactersForFiles(text: string): string { + if (!text || typeof text != 'string') { + return ''; + } + return text.replace(/[#:\/\?\\]+/g, '_'); } @@ -487,6 +495,10 @@ export class CoreTextUtilsProvider { * @return {string} Treated text. */ replaceNewLines(text: string, newValue: string): string { + if (!text || typeof text != 'string') { + return ''; + } + return text.replace(/(?:\r\n|\r|\n)/g, newValue); } @@ -498,7 +510,7 @@ export class CoreTextUtilsProvider { * @return {string} Treated text. */ replacePluginfileUrls(text: string, files: any[]): string { - if (text) { + if (text && typeof text == 'string') { const fileURL = this.getTextPluginfileUrl(files); if (fileURL) { return text.replace(/@@PLUGINFILE@@/g, fileURL); @@ -516,7 +528,7 @@ export class CoreTextUtilsProvider { * @return {string} Treated text. */ restorePluginfileUrls(text: string, files: any[]): string { - if (text) { + if (text && typeof text == 'string') { const fileURL = this.getTextPluginfileUrl(files); if (fileURL) { return text.replace(new RegExp(this.escapeForRegex(fileURL), 'g'), '@@PLUGINFILE@@'); @@ -626,7 +638,7 @@ export class CoreTextUtilsProvider { * @return {Promise} Promise resolved with the formatted text. */ treatMultilangTags(text: string): Promise { - if (!text) { + if (!text || typeof text != 'string') { return Promise.resolve(''); }