From 9b3473cb3ce175b7a1471b4b8188132a4e84a8f8 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 14 Mar 2018 09:44:06 +0100 Subject: [PATCH] MOBILE-2378 remotethemes: Get only the 35 styles --- .../remotethemes/providers/remotethemes.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/addon/remotethemes/providers/remotethemes.ts b/src/addon/remotethemes/providers/remotethemes.ts index c55ccfa60..18aa85ff1 100644 --- a/src/addon/remotethemes/providers/remotethemes.ts +++ b/src/addon/remotethemes/providers/remotethemes.ts @@ -30,6 +30,7 @@ import { Md5 } from 'ts-md5/dist/md5'; @Injectable() export class AddonRemoteThemesProvider { static COMPONENT = 'mmaRemoteStyles'; + protected SEPARATOR_35 = /\/\*\*? *3\.5(\.0)? *styles? *\*\//i; // A comment like "/* 3.5 styles */". protected logger; protected stylesEls: {[siteId: string]: {element: HTMLStyleElement, hash: string}} = {}; @@ -155,7 +156,7 @@ export class AddonRemoteThemesProvider { return fileUrl; } } else { - if (infos.mobilecssurl === '') { + if (infos && infos.mobilecssurl === '') { // CSS URL is empty. Delete downloaded files (if any). this.filepoolProvider.removeFilesByComponent(siteId, AddonRemoteThemesProvider.COMPONENT, 1); } @@ -170,13 +171,28 @@ export class AddonRemoteThemesProvider { }).then((response): any => { const text = response && response.text(); if (typeof text == 'string') { - return {fileUrl: fileUrl, styles: text}; + return {fileUrl: fileUrl, styles: this.get35Styles(text)}; } else { return Promise.reject(null); } }); } + /** + * Check if the CSS code has a separator for 3.5 styles. If it does, get only the styles after the separator. + * + * @param {string} cssCode The CSS code to check. + * @return {string} The filtered styles. + */ + protected get35Styles(cssCode: string): string { + const separatorPos = cssCode.search(this.SEPARATOR_35); + if (separatorPos > -1) { + return cssCode.substr(separatorPos).replace(this.SEPARATOR_35, ''); + } + + return cssCode; + } + /** * Load styles for a certain site. * @@ -230,8 +246,10 @@ export class AddonRemoteThemesProvider { } return this.http.get(url).toPromise().then((response) => { - const text = response && response.text(); + let text = response && response.text(); if (typeof text == 'string') { + text = this.get35Styles(text); + const styleEl = document.createElement('style'); styleEl.setAttribute('id', 'mobilecssurl-tmpsite'); styleEl.innerHTML = text;