Created type definition for Node CSS parser

This commit is contained in:
Ilya Verbitskiy
2015-11-19 21:47:10 -06:00
parent 16134c168d
commit c4a1b81320
2 changed files with 161 additions and 0 deletions

26
css/css-tests.ts Normal file
View File

@@ -0,0 +1,26 @@
/// <reference path="./css.d.ts" />
import css = require("css");
var parserOptions: css.ParserOptions;
parserOptions = {
silent: true,
source: "test.css"
};
var stylesheet = css.parse("body { font-size: 12px; }", parserOptions);
var comment: css.Comment;
comment = {
type: "comment",
comment: "Hello World"
};
stylesheet.stylesheet.rules.push(comment);
var stringifyOptions: css.StringifyOptions;
stringifyOptions = {
indent: " "
};
var content = css.stringify(stylesheet, stringifyOptions);
console.log(content);

135
css/css.d.ts vendored Normal file
View File

@@ -0,0 +1,135 @@
// Type definitions for css
// Project: https://github.com/reworkcss/css
// Definitions by: Ilya Verbitskiy <https://github.com/ilich>
// Definitions: https://github.com/ilich/DefinitelyTyped
declare module "css" {
// Options
interface ParserOptions {
silent?: boolean;
source?: string;
}
interface StringifyOptions {
indent?: string;
compress?: boolean;
sourcemap?: string;
inputSourcemaps?: boolean;
}
// Errors
interface ParserError {
message?: string;
reason?: string;
filename?: string;
line?: number;
column?: number;
source?: string;
}
// AST Tree
interface Position {
line?: number;
column?: number;
}
interface Node {
type?: string;
parent?: Node;
position?: {
start?: Position;
end?: Position;
source?: string;
content?: string;
};
}
interface Rule extends Node {
selectors?: Array<string>;
declarations?: Array<Declaration|Comment>;
}
interface Declaration extends Node {
property?: string;
value?: string;
}
interface Comment extends Node {
comment?: string;
}
interface Charset {
charset?: string;
}
interface CustomMedia {
name?: string;
media?: string;
}
interface Document {
document?: string;
vendor?: string;
rules?: Array<Rule|Comment|AtRule>;
}
interface FontFace {
declarations?: Array<Declaration|Comment>;
}
interface Host {
rules?: Array<Rule|Comment|AtRule>;
}
interface Import {
import?: string;
}
interface KeyFrames {
name?: string;
vendor?: string;
keyframes?: Array<KeyFrame|Comment>;
}
interface KeyFrame {
values?: Array<string>;
declarations?: Array<Declaration|Comment>;
}
interface Media {
media?: string;
rules?: Array<Rule|Comment|AtRule>;
}
interface Namespace {
namespace?: string;
}
interface Page {
selectors?: Array<string>;
declarations?: Array<Declaration|Comment>;
}
interface Supports {
supports?: string;
rules?: Array<Rule|Comment|AtRule>;
}
type AtRule = Charset|CustomMedia|Document|FontFace|Host|Import|KeyFrames|Media|Namespace|Page|Supports;
interface Stylesheet extends Node {
stylesheet?: {
rules?: Array<Rule|Comment|AtRule>;
parsingErrors?: Array<ParserError>
};
}
// API
function parse(code?: string, options?: ParserOptions): Stylesheet;
function stringify(stylesheet?: Stylesheet, options?: StringifyOptions): string;
}