Skip to content

Commit 47f8298

Browse files
committed
classes, removed any types
1 parent efe7910 commit 47f8298

File tree

8 files changed

+132
-127
lines changed

8 files changed

+132
-127
lines changed

src/cssPlugin.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/cssWalkers.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/main.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,61 @@
11
import * as postcss from 'postcss';
2-
import { cssPlugin, getContents } from './cssPlugin';
3-
4-
// typings on postcss don't appear to be correct
5-
const postcss2 = postcss as Function;
2+
import { DeclarationWalker, RuleWalker } from './walkers';
63

74
export function convertCss(contents: string): Promise<string> {
5+
let fileContents = '';
6+
7+
// create a css plugin for post css to parse and walk the document
8+
const cssPlugin = postcss.plugin('css-to-typestyle', (options: postcss.ProcessOptions): postcss.Transformer => {
9+
return (root: postcss.Root) => {
10+
fileContents = `import{cssRule,fontFace}from'typestyle';`;
11+
12+
const ruleWalker = new RuleWalker();
13+
const declarationWalker = new DeclarationWalker();
14+
15+
// process regular rules
16+
root.walkRules((rule: postcss.Rule) => {
17+
const parentType = rule.parent && rule.parent.type;
18+
if (parentType === 'atrule') {
19+
return;
20+
}
21+
ruleWalker.walk(rule);
22+
});
23+
const normalRules = ruleWalker.getRules();
24+
fileContents += Object.keys(normalRules)
25+
.map((r: string) => `\ncssRule('${r}',${JSON.stringify(normalRules[r])});`)
26+
.join('');
27+
28+
// process at rules
29+
root.walkAtRules((atRule: postcss.AtRule) => {
30+
// process keyframes and media
31+
if (atRule.name === 'keyframes' || atRule.name.indexOf('media') === 0) {
32+
ruleWalker.clearRules();
33+
atRule.walkRules(ruleWalker.walk);
34+
fileContents += `\ncssRule('@${atRule.name} ${atRule.params}',` + JSON.stringify({ $nest: ruleWalker.getRules() }) + ');';
35+
return;
36+
}
37+
// process font-face
38+
if (atRule.name === 'font-face') {
39+
declarationWalker.clearProperties();
40+
atRule.walkDecls(declarationWalker.walk);
41+
fileContents += `\nfontFace(` + JSON.stringify(declarationWalker.getProperties()) + ');';
42+
return;
43+
}
44+
// process page
45+
if (atRule.name === 'page') {
46+
declarationWalker.clearProperties();
47+
atRule.walkDecls(declarationWalker.walk);
48+
fileContents += `\ncssRule('@page',` + JSON.stringify(declarationWalker.getProperties()) + ');';
49+
return;
50+
}
51+
});
52+
};
53+
});
54+
855
return new Promise((resolve, reject) => {
56+
const postcss2 = postcss as Function;
957
postcss2([cssPlugin])
1058
.process(contents, {})
11-
.then(() => resolve(getContents()), (err: any) => reject(err));
59+
.then(() => resolve(fileContents), (err: string) => reject(err));
1260
});
1361
};
14-

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Properties = {
2+
[key: string]: (string | number | (string | number)[]);
3+
};
File renamed without changes.

src/walkers/DeclarationWalker.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as postcss from 'postcss';
2+
import { isNumber, toCamelCase } from '../utils/strings';
3+
import { Properties } from '../types';
4+
5+
export class DeclarationWalker {
6+
private _props: Properties = {};
7+
8+
public clearProperties(): void {
9+
this._props = {};
10+
}
11+
12+
public getProperties(): Properties {
13+
return this._props;
14+
}
15+
16+
public walk = (dec: postcss.Declaration): void => {
17+
// get property name
18+
const declarations = this._props;
19+
const propName = dec.prop;
20+
const prop = propName.indexOf('-') === 0 ? propName : toCamelCase(propName);
21+
22+
const objValue = dec.value;
23+
let value: string | number;
24+
if (typeof objValue === 'number') {
25+
value = objValue as number;
26+
} else if (isNumber(objValue)) {
27+
value = parseFloat(objValue);
28+
} else {
29+
value = objValue;
30+
}
31+
32+
if (!declarations.hasOwnProperty(prop)) {
33+
// set the value directly
34+
declarations[prop] = value;
35+
} else {
36+
// if property has been collected, convert to an array for fallback
37+
const propValue = declarations[prop];
38+
if (typeof propValue === 'string' || typeof propValue === 'number') {
39+
declarations[prop] = [propValue];
40+
}
41+
// add to the list of values
42+
(declarations[prop] as (string | number)[]).push(value)
43+
}
44+
}
45+
}
46+

src/walkers/RuleWalker.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as postcss from 'postcss';
2+
import { DeclarationWalker } from './DeclarationWalker';
3+
import { Properties } from '../types';
4+
5+
6+
export class RuleWalker {
7+
private _rules: { [selector: string]: Properties; } = {};
8+
9+
public clearRules(): void {
10+
this._rules = {};
11+
}
12+
13+
public getRules(): { [selector: string]: Properties; } {
14+
return this._rules;
15+
}
16+
17+
public walk = (rule: postcss.Rule): void => {
18+
const decWalker = new DeclarationWalker();
19+
20+
// find all rules and collect the properties
21+
rule.walkDecls((dec: postcss.Declaration) => {
22+
decWalker.walk(dec);
23+
});
24+
25+
// convert to string and add to file
26+
this._rules[rule.selector] = decWalker.getProperties();
27+
}
28+
}

src/walkers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './DeclarationWalker';
2+
export * from './RuleWalker';

0 commit comments

Comments
 (0)