Skip to content

Commit

Permalink
updates tests #2
Browse files Browse the repository at this point in the history
  • Loading branch information
GGAlanSmithee committed Oct 31, 2015
1 parent 615c19b commit 478af18
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 323 deletions.
65 changes: 33 additions & 32 deletions src/objectify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
import fs from 'fs';
import { parse } from 'css';

import getType, { SelectorType } from './core/selector-type';

/**
* Converts an external CSS stylesheet to a JavaScript stylesheet object
*
* @author Alan Smithee
* @param {string} path path to the external stylesheet to convert
* @return {object} CSSObject
*/
export default function objectify(path) {
const data = fs.readFileSync(path, 'utf8');

if (!data || !parse(data).stylesheet) {
return {};
}

return toObject(parse(data).stylesheet);
}

/**
* Converts a stylesheet AST to a JavaScript stylesheet object
*
Expand All @@ -21,44 +40,26 @@ export function toObject(stylesheet) {

for (let rule of stylesheet.rules) {
for (let selector of rule.selectors) {
console.log(selector);

// todo resolve selector type (regex) - WIP in /core/selector-type.js

let obj = style[selector] || {};

let obj = {};

for (let declaration of rule.declarations) {
if (obj[declaration.property] && obj[declaration.property].indexOf('!important') !== -1) {
if (declaration.value.indexOf('!important') !== -1) {
obj[declaration.property] = declaration.value;
}

continue;
}

obj[declaration.property] = declaration.value;
}

style[selector] = obj;
// todo refactor and generalize for all psuedoselector
if (getType(selector) === SelectorType.TypeClass) {
const [ topSelector, subSelector ] = selector.split(/(\.[a-zA-Z]+)/g);

if (!style[topSelector]) {
style[topSelector] = { };
}

style[topSelector][subSelector] = Object.assign(style[topSelector][subSelector] || {}, obj);
} else {
style[selector] = Object.assign(style[selector] || {}, obj);
}
}
}

return style;
}

/**
* Converts an external CSS stylesheet to a JavaScript stylesheet object
*
* @author Alan Smithee
* @param {string} path path to the external stylesheet to convert
* @return {object} CSSObject
*/
export default function objectify(path) {
const data = fs.readFileSync(path, 'utf8');

if (!data) {
return {};
}

return toObject(parse(data).stylesheet);
}
140 changes: 70 additions & 70 deletions test/class-selectors.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
// import { expect } from 'chai';
import { expect } from 'chai';

// import { parse } from 'css';
import { parse } from 'css';

// import { toObject } from './../src/objectify';
import { toObject } from './../src/objectify';

// describe('toObject(stylesheet)', function() {
// describe('objectifies class selectors', () => {
// it('one class', () => {
// const expected = {
// '.class' : {
// 'border': '1px solid black'
// }
// };
describe('toObject(stylesheet)', function() {
describe('objectifies class selectors', () => {
it('one class', () => {
const expected = {
'.class' : {
'border': '1px solid black'
}
};

// const actual = toObject(parse(`.class { border: 1px solid black; }`).stylesheet);
const actual = toObject(parse(`.class { border: 1px solid black; }`).stylesheet);

// expect(actual).to.deep.equal(expected);
// });
expect(actual).to.deep.equal(expected);
});

// it('two classes', () => {
// const expected = {
// '.class' : {
// 'border': '1px solid black'
// },
it('two classes', () => {
const expected = {
'.class' : {
'border': '1px solid black'
},

// '.class2' : {
// 'background-color': '#2233FF'
// }
// };
'.class2' : {
'background-color': '#2233FF'
}
};

// const actual = toObject(parse(
// `.class {
// border: 1px solid black;
// }
const actual = toObject(parse(
`.class {
border: 1px solid black;
}
// .class2 {
// background-color: #2233FF;
// }`
// ).stylesheet);
.class2 {
background-color: #2233FF;
}`
).stylesheet);

// expect(actual).to.deep.equal(expected);
// });
expect(actual).to.deep.equal(expected);
});

// it('re-accuring class', () => {
// const expected = {
// '.class' : {
// 'border': '1px solid black',
// 'background-color': '#2233FF'
// }
// };
it('re-accuring class', () => {
const expected = {
'.class' : {
'border': '1px solid black',
'background-color': '#2233FF'
}
};

// const actual = toObject(parse(
// `.class {
// border: 1px solid black;
// }
const actual = toObject(parse(
`.class {
border: 1px solid black;
}
// .class {
// background-color: #2233FF;
// }`
// ).stylesheet);
.class {
background-color: #2233FF;
}`
).stylesheet);

// expect(actual).to.deep.equal(expected);
// });
expect(actual).to.deep.equal(expected);
});

// it('re-accuring class with conflicting (overriding) styles', () => {
// const expected = {
// '.class' : {
// 'border': '1px solid black',
// 'background-color': 'white'
// }
// };
it('re-accuring class with conflicting (overriding) styles', () => {
const expected = {
'.class' : {
'border': '1px solid black',
'background-color': 'white'
}
};

// const actual = toObject(parse(
// `.class {
// border: 1px solid black;
// background-color: #2233FF;
// }
const actual = toObject(parse(
`.class {
border: 1px solid black;
background-color: #2233FF;
}
// .class {
// background-color: white;
// }`
// ).stylesheet);
.class {
background-color: white;
}`
).stylesheet);

// expect(actual).to.deep.equal(expected);
// });
// });
// });
expect(actual).to.deep.equal(expected);
});
});
});
24 changes: 0 additions & 24 deletions test/element-class-selectors.js

This file was deleted.

102 changes: 0 additions & 102 deletions test/element-selectors.js

This file was deleted.

Loading

0 comments on commit 478af18

Please sign in to comment.