diff --git a/src/editor/index.js b/src/editor/index.js index 72fb0c853..5cc2c3b2f 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -223,6 +223,12 @@ module.exports = config => { */ RichTextEditor: em.get('RichTextEditor'), + /** + * @property {Parser} + * @private + */ + Parser: em.get('Parser'), + /** * @property {Utils} * @private @@ -571,7 +577,7 @@ module.exports = config => { * Replace the default CSS parser with a custom one. * The parser function receives a CSS string as a parameter and expects * an array of CSSRule objects as a result. If you need to remove the - * custom parser, pass the `null` to this method + * custom parser, pass `null` as the argument * @param {Function|null} parser Parser function * @return {this} * @example @@ -587,7 +593,7 @@ module.exports = config => { * }); */ setCustomParserCss(parser) { - em.setCustomParserCss(parser); + this.Parser.getConfig().parserCss = parser; return this; }, diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index b757bd845..2c66604c0 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -641,13 +641,5 @@ module.exports = Backbone.Model.extend({ } else { el[varName][name] = value; } - }, - - getCustomParserCss() { - return this.get('Parser').parserCss; - }, - - setCustomParserCss(parser) { - this.get('Parser').parserCss = parser; } }); diff --git a/src/parser/index.js b/src/parser/index.js index 15d0cde39..8f714d5b8 100644 --- a/src/parser/index.js +++ b/src/parser/index.js @@ -19,6 +19,14 @@ module.exports = () => { */ name: 'Parser', + /** + * Get config object + * @return {Object} + */ + getConfig() { + return conf; + }, + /** * Initialize module. Automatically called with a new instance of the editor * @param {Object} config Configurations @@ -36,8 +44,7 @@ module.exports = () => { */ init(config = {}) { conf = { ...defaults, ...config }; - this.parserCss = conf.parserCss || null; - this.parserHtml = conf.parserHtml || null; + conf.Parser = this; pHtml = new parserHtml(conf); pCss = new parserCss(conf); return this; diff --git a/src/parser/model/ParserCss.js b/src/parser/model/ParserCss.js index 6dad4cb2b..1e9666053 100644 --- a/src/parser/model/ParserCss.js +++ b/src/parser/model/ParserCss.js @@ -1,14 +1,13 @@ import BrowserCssParser from './BrowserParserCss'; -module.exports = config => ({ +module.exports = (config = {}) => ({ /** * Parse CSS string to a desired model object * @param {String} str CSS string * @return {Array} */ parse(str) { - const { em } = config; - const customParser = em.getCustomParserCss(); + const customParser = config.parserCss; return customParser ? customParser(str) : BrowserCssParser(str); } }); diff --git a/test/specs/parser/model/ParserCss.js b/test/specs/parser/model/ParserCss.js index 756fd5e4b..b49d7b332 100644 --- a/test/specs/parser/model/ParserCss.js +++ b/test/specs/parser/model/ParserCss.js @@ -351,35 +351,25 @@ module.exports = { var str = '.test1 { color:red }'; var result = { selectors: ['test1'], - style: { color: 'red' } + style: { color: 'blue' } }; obj = new ParserCss({ - em: { - getCustomParserCss: () => () => [result] - } + parserCss: () => [result] }); expect(obj.parse(str)).toEqual([result]); }); - test.only('Parse CSS with custom async parser', done => { + test('Parse CSS with custom async parser', async () => { var str = '.test1 { color:red }'; var result = { selectors: ['test1'], - style: { color: 'red' } + style: { color: 'blue' } }; obj = new ParserCss({ - em: { - getCustomParserCss: () => { - return async function() { - return [result]; - }; - } - } - }); - obj.parse(str).then(res => { - expect(res).toEqual([result]); - done(); + parserCss: async () => [result] }); + const cssResult = await obj.parse(str); + expect(cssResult).toEqual([result]); }); }); }