Browse Source

Improve HTML parser, add the possibility to parse Array/Object attributes

pull/437/head
Artur Arseniev 9 years ago
parent
commit
73f4183e32
  1. 16
      src/dom_components/model/Component.js
  2. 12
      src/parser/model/ParserHtml.js
  3. 2
      test/specs/dom_components/model/Component.js
  4. 30
      test/specs/parser/model/ParserHtml.js

16
src/dom_components/model/Component.js

@ -98,6 +98,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
toolbar: null, toolbar: null,
}, },
initialize(props = {}, opt = {}) { initialize(props = {}, opt = {}) {
const em = opt.sm || opt.em || {}; const em = opt.sm || opt.em || {};
@ -205,6 +206,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return this; return this;
}, },
initComponents() { initComponents() {
let comps = new Components(this.get('components'), this.opt); let comps = new Components(this.get('components'), this.opt);
comps.parent = this; comps.parent = this;
@ -212,6 +214,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return this; return this;
}, },
/** /**
* Initialize callback * Initialize callback
*/ */
@ -260,6 +263,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
} }
}, },
/** /**
* Script updated * Script updated
*/ */
@ -267,6 +271,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
this.set('scriptUpdated', 1); this.set('scriptUpdated', 1);
}, },
/** /**
* Once traits are updated I have to populates model's attributes * Once traits are updated I have to populates model's attributes
*/ */
@ -293,6 +298,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
found && this.set('attributes', attrs); found && this.set('attributes', attrs);
}, },
/** /**
* Init toolbar * Init toolbar
*/ */
@ -328,6 +334,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
} }
}, },
/** /**
* Load traits * Load traits
* @param {Array} traits * @param {Array} traits
@ -346,6 +353,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return this; return this;
}, },
/** /**
* Normalize input classes from array to array of objects * Normalize input classes from array to array of objects
* @param {Array} arr * @param {Array} arr
@ -376,6 +384,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return res; return res;
}, },
/** /**
* Override original clone method * Override original clone method
* @private * @private
@ -409,6 +418,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return new this.constructor(attr, this.opt); return new this.constructor(attr, this.opt);
}, },
/** /**
* Get the name of the component * Get the name of the component
* @return {string} * @return {string}
@ -422,6 +432,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return customName || name; return customName || name;
}, },
/** /**
* Get the icon string * Get the icon string
* @return {string} * @return {string}
@ -431,6 +442,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return icon ? icon + ' ' : ''; return icon ? icon + ' ' : '';
}, },
/** /**
* Return HTML string of the component * Return HTML string of the component
* @param {Object} opts Options * @param {Object} opts Options
@ -479,6 +491,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return code; return code;
}, },
/** /**
* Returns object of attributes for HTML * Returns object of attributes for HTML
* @return {Object} * @return {Object}
@ -490,6 +503,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return attr; return attr;
}, },
/** /**
* Return a shallow copy of the model's attributes for JSON * Return a shallow copy of the model's attributes for JSON
* stringification. * stringification.
@ -508,6 +522,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return obj; return obj;
}, },
/** /**
* Return model id * Return model id
* @return {string} * @return {string}
@ -517,6 +532,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
return attrs.id || this.cid; return attrs.id || this.cid;
}, },
/** /**
* Return script in string format, cleans 'function() {..' from scripts * Return script in string format, cleans 'function() {..' from scripts
* if it's a function * if it's a function

12
src/parser/model/ParserHtml.js

@ -107,13 +107,19 @@ module.exports = config => {
model.classes = this.parseClass(nodeValue); model.classes = this.parseClass(nodeValue);
else if (nodeName == 'contenteditable') else if (nodeName == 'contenteditable')
continue; continue;
else if(nodeName.indexOf(modelAttrStart) === 0){ else if (nodeName.indexOf(modelAttrStart) === 0) {
var modelAttr = nodeName.replace(modelAttrStart, ''); const modelAttr = nodeName.replace(modelAttrStart, '');
const valueLen = nodeValue.length;
const firstChar = nodeValue && nodeValue.substr(0, 1);
const lastChar = nodeValue && nodeValue.substr(valueLen - 1);
nodeValue = nodeValue === 'true' ? true : nodeValue; nodeValue = nodeValue === 'true' ? true : nodeValue;
nodeValue = nodeValue === 'false' ? false : nodeValue; nodeValue = nodeValue === 'false' ? false : nodeValue;
nodeValue = (firstChar == '{' && lastChar == '}') ||
(firstChar == '[' && lastChar == ']') ? JSON.parse(nodeValue) : nodeValue;
model[modelAttr] = nodeValue; model[modelAttr] = nodeValue;
}else } else {
model.attributes[nodeName] = nodeValue; model.attributes[nodeName] = nodeValue;
}
} }

2
test/specs/dom_components/model/Component.js

@ -17,7 +17,7 @@ module.exports = {
let compOpts; let compOpts;
let em; let em;
describe.only('Component', () => { describe('Component', () => {
beforeEach(() => { beforeEach(() => {
em = new Editor({}); em = new Editor({});

30
test/specs/parser/model/ParserHtml.js

@ -5,7 +5,7 @@ const DomComponents = require('dom_components');
module.exports = { module.exports = {
run() { run() {
describe('ParserHtml', () => { describe.only('ParserHtml', () => {
var obj; var obj;
beforeEach(() => { beforeEach(() => {
@ -382,6 +382,34 @@ module.exports = {
expect(obj.parse(str).html).toEqual(result); expect(obj.parse(str).html).toEqual(result);
}); });
it('Parse attributes with object inside', () => {
var str = `<div data-gjs-test='{ "prop1": "value1", "prop2": 10, "prop3": true}'>test2 </div>`;
var result = {
tagName: 'div',
attributes: {},
type: 'text',
test: {
prop1: 'value1',
prop2: 10,
prop3: true,
},
content: 'test2 ',
};
expect(obj.parse(str).html).toEqual(result);
});
it('Parse attributes with arrays inside', () => {
var str = `<div data-gjs-test='["value1", "value2"]'>test2 </div>`;
var result = {
tagName: 'div',
attributes: {},
type: 'text',
test: ['value1', 'value2'],
content: 'test2 ',
};
expect(obj.parse(str).html).toEqual(result);
});
}); });
} }

Loading…
Cancel
Save