Browse Source

Add `atRuleType` to CSSRule

pull/803/head
Artur Arseniev 8 years ago
parent
commit
a16b045685
  1. 40
      src/css_composer/model/CssRule.js
  2. 12
      test/specs/css_composer/model/CssModels.js

40
src/css_composer/model/CssRule.js

@ -23,6 +23,9 @@ module.exports = Backbone.Model.extend(Styleable).extend({
// Indicates if the rule is stylable // Indicates if the rule is stylable
stylable: true, stylable: true,
// Type of at-rule, eg. 'media', 'font-face', etc.
atRuleType: '',
// If true, sets '!important' on all properties // If true, sets '!important' on all properties
// You can use an array to specify properties to set important // You can use an array to specify properties to set important
// Used in view // Used in view
@ -47,6 +50,18 @@ module.exports = Backbone.Model.extend(Styleable).extend({
this.set('selectors', new Selectors(selectors)); this.set('selectors', new Selectors(selectors));
}, },
/**
* Returns an at-rule statement if possible, eg. '@media (...)', '@keyframes'
* @return {string}
*/
getAtRule() {
const type = this.get('atRuleType');
const condition = this.get('mediaText');
return (
(type ? `@${type}` : '') + (condition && type ? ` ${condition}` : '')
);
},
/** /**
* Return selectors fo the rule as a string * Return selectors fo the rule as a string
* @return {string} * @return {string}
@ -69,7 +84,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
*/ */
toCSS(opts = {}) { toCSS(opts = {}) {
let result = ''; let result = '';
const media = this.get('mediaText'); const atRule = this.getAtRule();
const style = this.styleToString(opts); const style = this.styleToString(opts);
const selectors = this.selectorsToString(); const selectors = this.selectorsToString();
@ -77,8 +92,8 @@ module.exports = Backbone.Model.extend(Styleable).extend({
result = `${selectors}{${style}}`; result = `${selectors}{${style}}`;
} }
if (media && result) { if (atRule && result) {
result = `@media ${media}{${result}}`; result = `${atRule}{${result}}`;
} }
return result; return result;
@ -93,11 +108,11 @@ module.exports = Backbone.Model.extend(Styleable).extend({
* @return {Boolean} * @return {Boolean}
* @private * @private
*/ */
compare(selectors, state, width, ruleProps) { compare(selectors, state, width, ruleProps = {}) {
var otherRule = ruleProps || {};
var st = state || ''; var st = state || '';
var wd = width || ''; var wd = width || '';
var selectorsAdd = otherRule.selectorsAdd || ''; var selectorsAdd = ruleProps.selectorsAdd || '';
var atRuleType = ruleProps.atRuleType || '';
var cId = 'cid'; var cId = 'cid';
//var a1 = _.pluck(selectors.models || selectors, cId); //var a1 = _.pluck(selectors.models || selectors, cId);
//var a2 = _.pluck(this.get('selectors').models, cId); //var a2 = _.pluck(this.get('selectors').models, cId);
@ -117,11 +132,14 @@ module.exports = Backbone.Model.extend(Styleable).extend({
if (re === 0) return f; if (re === 0) return f;
} }
if (this.get('state') !== st) return f; if (
this.get('state') !== st ||
if (this.get('mediaText') !== wd) return f; this.get('mediaText') !== wd ||
this.get('selectorsAdd') !== selectorsAdd ||
if (this.get('selectorsAdd') !== selectorsAdd) return f; this.get('atRuleType') !== atRuleType
) {
return f;
}
return true; return true;
} }

12
test/specs/css_composer/model/CssModels.js

@ -5,7 +5,7 @@ var Selector = require('selector_manager/model/Selector');
module.exports = { module.exports = {
run() { run() {
describe('CssRule', () => { describe.only('CssRule', () => {
let obj; let obj;
beforeEach(() => { beforeEach(() => {
@ -74,11 +74,21 @@ module.exports = {
it('toCSS wraps correctly inside media rule', () => { it('toCSS wraps correctly inside media rule', () => {
const media = '(max-width: 768px)'; const media = '(max-width: 768px)';
obj.set('atRuleType', 'media');
obj.set('mediaText', media); obj.set('mediaText', media);
obj.get('selectors').add({ name: 'test1' }); obj.get('selectors').add({ name: 'test1' });
obj.setStyle({ color: 'red' }); obj.setStyle({ color: 'red' });
expect(obj.toCSS()).toEqual(`@media ${media}{.test1{color:red;}}`); expect(obj.toCSS()).toEqual(`@media ${media}{.test1{color:red;}}`);
}); });
it('toCSS with a generic at-rule', () => {
obj.set('atRuleType', 'font-face');
obj.get('selectors').add({ name: 'test1' });
obj.setStyle({ 'font-family': 'Open Sans' });
expect(obj.toCSS()).toEqual(
`@font-face{.test1{font-family:Open Sans;}}`
);
});
}); });
describe('CssRules', () => { describe('CssRules', () => {

Loading…
Cancel
Save