Skip to content

Commit 5d10135

Browse files
committed
Merge pull request DefinitelyTyped#6849 from ilich/master
Type definitions for Node.js CSS parser.
2 parents 02c52e9 + 7dd89da commit 5d10135

File tree

2 files changed

+320
-0
lines changed

2 files changed

+320
-0
lines changed

css/css-tests.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/// <reference path="./css.d.ts" />
2+
3+
import css = require("css");
4+
5+
// Check that user can parse, modify and persist CSS content
6+
7+
var parserOptions: css.ParserOptions;
8+
parserOptions = {
9+
silent: true,
10+
source: "test.css"
11+
};
12+
13+
var stylesheet = css.parse("body { font-size: 12px; }", parserOptions);
14+
15+
var comment: css.Comment;
16+
comment = {
17+
type: "comment",
18+
comment: "Hello World"
19+
};
20+
21+
stylesheet.stylesheet.rules.push(comment);
22+
23+
var stringifyOptions: css.StringifyOptions;
24+
stringifyOptions = {
25+
indent: " "
26+
};
27+
28+
var content = css.stringify(stylesheet, stringifyOptions);
29+
30+
// Create new stylesheet and save it
31+
32+
var bgDeclaration: css.Declaration = {
33+
type: "declaration",
34+
property: "background",
35+
value: "#eee"
36+
};
37+
38+
var colorDeclaration: css.Declaration = {
39+
type: "declaration",
40+
property: "color",
41+
value: "#888"
42+
};
43+
44+
var ruleComment: css.Comment = {
45+
type: "comment",
46+
comment: "New CSS AST Tree Rule"
47+
};
48+
49+
var bodyRule: css.Rule = {
50+
type: "rule",
51+
selectors: ["body"],
52+
declarations: [ruleComment, bgDeclaration, colorDeclaration]
53+
};
54+
55+
var newStylesheet: css.Stylesheet = {
56+
type: "stylesheet",
57+
stylesheet: {
58+
rules: [bodyRule]
59+
}
60+
};
61+
62+
content = css.stringify(newStylesheet);

css/css.d.ts

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
// Type definitions for css
2+
// Project: https://github.com/reworkcss/css
3+
// Definitions by: Ilya Verbitskiy <https://github.com/ilich>
4+
// Definitions: https://github.com/ilich/DefinitelyTyped
5+
6+
/**
7+
* CSS parser / stringifier for Node.js
8+
*/
9+
declare module "css" {
10+
11+
/**
12+
* css.parse options
13+
*/
14+
interface ParserOptions {
15+
/** Silently fail on parse errors */
16+
silent?: boolean;
17+
/** The path to the file containing css. Makes errors and source maps more helpful, by letting them know where code comes from. */
18+
source?: string;
19+
}
20+
21+
/**
22+
* css.stringify options
23+
*/
24+
interface StringifyOptions {
25+
/** The string used to indent the output. Defaults to two spaces. */
26+
indent?: string;
27+
/** Omit comments and extraneous whitespace. */
28+
compress?: boolean;
29+
/** Return a sourcemap along with the CSS output.
30+
* Using the source option of css.parse is strongly recommended
31+
* when creating a source map. Specify sourcemap: 'generator'
32+
* to return the SourceMapGenerator object instead of serializing the source map.
33+
*/
34+
sourcemap?: string;
35+
/** (enabled by default, specify false to disable)
36+
* Reads any source maps referenced by the input files
37+
* when generating the output source map. When enabled,
38+
* file system access may be required for reading the referenced source maps.
39+
*/
40+
inputSourcemaps?: boolean;
41+
}
42+
43+
/**
44+
* Error thrown during parsing.
45+
*/
46+
interface ParserError {
47+
/** The full error message with the source position. */
48+
message?: string;
49+
/** The error message without position. */
50+
reason?: string;
51+
/** The value of options.source if passed to css.parse. Otherwise undefined. */
52+
filename?: string;
53+
line?: number;
54+
column?: number;
55+
/** The portion of code that couldn't be parsed. */
56+
source?: string;
57+
}
58+
59+
// ---------------------------------------------------------------------------------
60+
// AST Tree
61+
// ---------------------------------------------------------------------------------
62+
63+
/**
64+
* Information about a position in the code.
65+
* The line and column numbers are 1-based: The first line is 1 and the first column of a line is 1 (not 0).
66+
*/
67+
interface Position {
68+
line?: number;
69+
column?: number;
70+
}
71+
72+
/**
73+
* Base AST Tree Node.
74+
*/
75+
interface Node {
76+
/** The possible values are the ones listed in the Types section on https://github.com/reworkcss/css page. */
77+
type?: string;
78+
/** A reference to the parent node, or null if the node has no parent. */
79+
parent?: Node;
80+
/** Information about the position in the source string that corresponds to the node. */
81+
position?: {
82+
start?: Position;
83+
end?: Position;
84+
/** The value of options.source if passed to css.parse. Otherwise undefined. */
85+
source?: string;
86+
/** The full source string passed to css.parse. */
87+
content?: string;
88+
};
89+
}
90+
91+
interface Rule extends Node {
92+
/** The list of selectors of the rule, split on commas. Each selector is trimmed from whitespace and comments. */
93+
selectors?: Array<string>;
94+
/** Array of nodes with the types declaration and comment. */
95+
declarations?: Array<Declaration|Comment>;
96+
}
97+
98+
interface Declaration extends Node {
99+
/** The property name, trimmed from whitespace and comments. May not be empty. */
100+
property?: string;
101+
/** The value of the property, trimmed from whitespace and comments. Empty values are allowed. */
102+
value?: string;
103+
}
104+
105+
/**
106+
* A rule-level or declaration-level comment. Comments inside selectors, properties and values etc. are lost.
107+
*/
108+
interface Comment extends Node {
109+
comment?: string;
110+
}
111+
112+
/**
113+
* The @charset at-rule.
114+
*/
115+
interface Charset {
116+
/** The part following @charset. */
117+
charset?: string;
118+
}
119+
120+
/**
121+
* The @custom-media at-rule
122+
*/
123+
interface CustomMedia {
124+
/** The ---prefixed name. */
125+
name?: string;
126+
/** The part following the name. */
127+
media?: string;
128+
}
129+
130+
/**
131+
* The @document at-rule.
132+
*/
133+
interface Document {
134+
/** The part following @document. */
135+
document?: string;
136+
/** The vendor prefix in @document, or undefined if there is none. */
137+
vendor?: string;
138+
/** Array of nodes with the types rule, comment and any of the at-rule types. */
139+
rules?: Array<Rule|Comment|AtRule>;
140+
}
141+
142+
/**
143+
* The @font-face at-rule.
144+
*/
145+
interface FontFace {
146+
/** Array of nodes with the types declaration and comment. */
147+
declarations?: Array<Declaration|Comment>;
148+
}
149+
150+
/**
151+
* The @host at-rule.
152+
*/
153+
interface Host {
154+
/** Array of nodes with the types rule, comment and any of the at-rule types. */
155+
rules?: Array<Rule|Comment|AtRule>;
156+
}
157+
158+
/**
159+
* The @import at-rule.
160+
*/
161+
interface Import {
162+
/** The part following @import. */
163+
import?: string;
164+
}
165+
166+
/**
167+
* The @keyframes at-rule.
168+
*/
169+
interface KeyFrames {
170+
/** The name of the keyframes rule. */
171+
name?: string;
172+
/** The vendor prefix in @keyframes, or undefined if there is none. */
173+
vendor?: string;
174+
/** Array of nodes with the types keyframe and comment. */
175+
keyframes?: Array<KeyFrame|Comment>;
176+
}
177+
178+
interface KeyFrame {
179+
/** The list of "selectors" of the keyframe rule, split on commas. Each “selector” is trimmed from whitespace. */
180+
values?: Array<string>;
181+
/** Array of nodes with the types declaration and comment. */
182+
declarations?: Array<Declaration|Comment>;
183+
}
184+
185+
/**
186+
* The @media at-rule.
187+
*/
188+
interface Media {
189+
/** The part following @media. */
190+
media?: string;
191+
/** Array of nodes with the types rule, comment and any of the at-rule types. */
192+
rules?: Array<Rule|Comment|AtRule>;
193+
}
194+
195+
/**
196+
* The @namespace at-rule.
197+
*/
198+
interface Namespace {
199+
/** The part following @namespace. */
200+
namespace?: string;
201+
}
202+
203+
/**
204+
* The @page at-rule.
205+
*/
206+
interface Page {
207+
/** The list of selectors of the rule, split on commas. Each selector is trimmed from whitespace and comments. */
208+
selectors?: Array<string>;
209+
/** Array of nodes with the types declaration and comment. */
210+
declarations?: Array<Declaration|Comment>;
211+
}
212+
213+
/**
214+
* The @supports at-rule.
215+
*/
216+
interface Supports {
217+
/** The part following @supports. */
218+
supports?: string;
219+
/** Array of nodes with the types rule, comment and any of the at-rule types. */
220+
rules?: Array<Rule|Comment|AtRule>;
221+
}
222+
223+
/** All at-rules. */
224+
type AtRule = Charset|CustomMedia|Document|FontFace|Host|Import|KeyFrames|Media|Namespace|Page|Supports;
225+
226+
/**
227+
* The root node returned by css.parse.
228+
*/
229+
interface Stylesheet extends Node {
230+
stylesheet?: {
231+
/** Array of nodes with the types rule, comment and any of the at-rule types. */
232+
rules?: Array<Rule|Comment|AtRule>;
233+
/** Array of Errors. Errors collected during parsing when option silent is true. */
234+
parsingErrors?: Array<ParserError>
235+
};
236+
}
237+
238+
// ---------------------------------------------------------------------------------
239+
240+
/**
241+
* Accepts a CSS string and returns an AST object.
242+
*
243+
* @param {string} code - CSS code.
244+
* @param {ParserOptions} options - CSS parser options.
245+
* @return {Stylesheet} AST object built using provides CSS code.
246+
*/
247+
function parse(code: string, options?: ParserOptions): Stylesheet;
248+
249+
/**
250+
* Accepts an AST object (as css.parse produces) and returns a CSS string.
251+
*
252+
* @param {Stylesheet} stylesheet - AST tree.
253+
* @param {StringifyOptions} options - AST tree to string serializaiton options.
254+
* @return {string} CSS code.
255+
*/
256+
function stringify(stylesheet: Stylesheet, options?: StringifyOptions): string;
257+
258+
}

0 commit comments

Comments
 (0)