Merge pull request #1459 from dpalou/MOBILE-2526

MOBILE-2526 core: Fix text.replace is not a function
main
Juan Leyva 2018-08-22 11:20:50 +01:00 committed by GitHub
commit 81c45f8383
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View File

@ -46,8 +46,8 @@ export class CoreGradesHelperProvider {
protected formatGradeRow(tableRow: any): any { protected formatGradeRow(tableRow: any): any {
const row = {}; const row = {};
for (const name in tableRow) { for (const name in tableRow) {
if (typeof(tableRow[name].content) != 'undefined') { if (typeof tableRow[name].content != 'undefined' && tableRow[name].content !== null) {
let content = tableRow[name].content; let content = String(tableRow[name].content);
if (name == 'itemname') { if (name == 'itemname') {
this.setRowIcon(row, content); this.setRowIcon(row, content);
@ -81,8 +81,8 @@ export class CoreGradesHelperProvider {
protected formatGradeRowForTable(tableRow: any): any { protected formatGradeRowForTable(tableRow: any): any {
const row = {}; const row = {};
for (let name in tableRow) { for (let name in tableRow) {
if (typeof(tableRow[name].content) != 'undefined') { if (typeof tableRow[name].content != 'undefined' && tableRow[name].content !== null) {
let content = tableRow[name].content; let content = String(tableRow[name].content);
if (name == 'itemname') { if (name == 'itemname') {
this.setRowIcon(row, content); this.setRowIcon(row, content);

View File

@ -131,7 +131,7 @@ export class CoreTextUtilsProvider {
* @return {string} Clean text. * @return {string} Clean text.
*/ */
cleanTags(text: string, singleLine?: boolean): string { cleanTags(text: string, singleLine?: boolean): string {
if (typeof text == 'number') { if (typeof text != 'string') {
return text; return text;
} }
@ -197,6 +197,10 @@ export class CoreTextUtilsProvider {
* @return {number} Number of words. * @return {number} Number of words.
*/ */
countWords(text: string): number { countWords(text: string): number {
if (!text || typeof text != 'string') {
return 0;
}
// Clean HTML scripts and tags. // Clean HTML scripts and tags.
text = text.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, ''); text = text.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
text = text.replace(/<\/?(?!\!)[^>]*>/gi, ''); text = text.replace(/<\/?(?!\!)[^>]*>/gi, '');
@ -285,7 +289,7 @@ export class CoreTextUtilsProvider {
* @return {string} Escaped text. * @return {string} Escaped text.
*/ */
escapeForRegex(text: string): string { escapeForRegex(text: string): string {
if (!text) { if (!text || typeof text != 'string') {
return ''; return '';
} }
@ -476,6 +480,10 @@ export class CoreTextUtilsProvider {
* @return {string} Treated text. * @return {string} Treated text.
*/ */
removeSpecialCharactersForFiles(text: string): string { removeSpecialCharactersForFiles(text: string): string {
if (!text || typeof text != 'string') {
return '';
}
return text.replace(/[#:\/\?\\]+/g, '_'); return text.replace(/[#:\/\?\\]+/g, '_');
} }
@ -487,6 +495,10 @@ export class CoreTextUtilsProvider {
* @return {string} Treated text. * @return {string} Treated text.
*/ */
replaceNewLines(text: string, newValue: string): string { replaceNewLines(text: string, newValue: string): string {
if (!text || typeof text != 'string') {
return '';
}
return text.replace(/(?:\r\n|\r|\n)/g, newValue); return text.replace(/(?:\r\n|\r|\n)/g, newValue);
} }
@ -498,7 +510,7 @@ export class CoreTextUtilsProvider {
* @return {string} Treated text. * @return {string} Treated text.
*/ */
replacePluginfileUrls(text: string, files: any[]): string { replacePluginfileUrls(text: string, files: any[]): string {
if (text) { if (text && typeof text == 'string') {
const fileURL = this.getTextPluginfileUrl(files); const fileURL = this.getTextPluginfileUrl(files);
if (fileURL) { if (fileURL) {
return text.replace(/@@PLUGINFILE@@/g, fileURL); return text.replace(/@@PLUGINFILE@@/g, fileURL);
@ -516,7 +528,7 @@ export class CoreTextUtilsProvider {
* @return {string} Treated text. * @return {string} Treated text.
*/ */
restorePluginfileUrls(text: string, files: any[]): string { restorePluginfileUrls(text: string, files: any[]): string {
if (text) { if (text && typeof text == 'string') {
const fileURL = this.getTextPluginfileUrl(files); const fileURL = this.getTextPluginfileUrl(files);
if (fileURL) { if (fileURL) {
return text.replace(new RegExp(this.escapeForRegex(fileURL), 'g'), '@@PLUGINFILE@@'); return text.replace(new RegExp(this.escapeForRegex(fileURL), 'g'), '@@PLUGINFILE@@');
@ -626,7 +638,7 @@ export class CoreTextUtilsProvider {
* @return {Promise<string>} Promise resolved with the formatted text. * @return {Promise<string>} Promise resolved with the formatted text.
*/ */
treatMultilangTags(text: string): Promise<string> { treatMultilangTags(text: string): Promise<string> {
if (!text) { if (!text || typeof text != 'string') {
return Promise.resolve(''); return Promise.resolve('');
} }