Browse Source

Refactor custom CSS parser configuration

pull/1446/head
Artur Arseniev 8 years ago
parent
commit
197273e668
  1. 10
      src/editor/index.js
  2. 8
      src/editor/model/Editor.js
  3. 11
      src/parser/index.js
  4. 5
      src/parser/model/ParserCss.js
  5. 24
      test/specs/parser/model/ParserCss.js

10
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;
},

8
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;
}
});

11
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;

5
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<Object>}
*/
parse(str) {
const { em } = config;
const customParser = em.getCustomParserCss();
const customParser = config.parserCss;
return customParser ? customParser(str) : BrowserCssParser(str);
}
});

24
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]);
});
});
}

Loading…
Cancel
Save