Skip to content

Commit 8572a8f

Browse files
committed
Add toSource() to the constructs
1 parent 21cbfbe commit 8572a8f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

parse-css.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,9 @@ class Stylesheet extends CSSParserRule {
12961296
rules: this.rules,
12971297
}
12981298
}
1299+
toSource() {
1300+
return "\n".join(this.rules.map(x=>x.toSource({indent:0}))) + "\n";
1301+
}
12991302
}
13001303

13011304
class AtRule extends CSSParserRule {
@@ -1316,6 +1319,24 @@ class AtRule extends CSSParserRule {
13161319
rules: this.rules,
13171320
}
13181321
}
1322+
toSource(indent=0) {
1323+
let s = printIndent(indent) + "@" + escapeIdent(this.name);
1324+
s += this.prelude.map(x=>x.toSource());
1325+
if(this.declarations == null) {
1326+
s += ";\n";
1327+
return s;
1328+
}
1329+
s += "{\n";
1330+
if(this.declarations) {
1331+
s += "\n".join(this.declarations.map(x=>x.toSource(indent+1)));
1332+
s += "\n";
1333+
}
1334+
if(this.rules) {
1335+
s += "\n".join(this.rules.map(x=>x.toSource(indent+1)));
1336+
s += "\n";
1337+
}
1338+
s += printIndent(indent) + "}";
1339+
}
13191340
}
13201341

13211342
class QualifiedRule extends CSSParserRule {
@@ -1334,6 +1355,24 @@ class QualifiedRule extends CSSParserRule {
13341355
rules: this.rules,
13351356
}
13361357
}
1358+
toSource(indent=0) {
1359+
let s = printIndent(indent);
1360+
s += this.prelude.map(x=>x.toSource());
1361+
if(this.declarations == null) {
1362+
s += ";\n";
1363+
return s;
1364+
}
1365+
s += "{\n";
1366+
if(this.declarations) {
1367+
s += "\n".join(this.declarations.map(x=>x.toSource(indent+1)));
1368+
s += "\n";
1369+
}
1370+
if(this.rules) {
1371+
s += "\n".join(this.rules.map(x=>x.toSource(indent+1)));
1372+
s += "\n";
1373+
}
1374+
s += printIndent(indent) + "}";
1375+
}
13371376
}
13381377

13391378
class Declaration extends CSSParserRule {
@@ -1352,6 +1391,14 @@ class Declaration extends CSSParserRule {
13521391
important: this.important,
13531392
}
13541393
}
1394+
toSource(indent=0) {
1395+
let s = printIndent(indent) + escapeIdent(this.name) + ": ";
1396+
s += "".join(this.value.map(x=>x.toSource()))
1397+
if(this.important) {
1398+
s += " !important";
1399+
}
1400+
s += ";";
1401+
}
13551402
}
13561403

13571404
class SimpleBlock extends CSSParserRule {
@@ -1368,6 +1415,10 @@ class SimpleBlock extends CSSParserRule {
13681415
value: this.value,
13691416
}
13701417
}
1418+
toSource() {
1419+
const mirror = {"{":"}", "[":"]", "(":")"};
1420+
return this.name + "".join(this.value.map(x=>x.toSource())) + mirror[this.name];
1421+
}
13711422
}
13721423

13731424
class Func extends CSSParserRule {
@@ -1384,6 +1435,13 @@ class Func extends CSSParserRule {
13841435
value: this.value,
13851436
}
13861437
}
1438+
toSource() {
1439+
return escapeIdent(this.name) + "(" + "".join(this.value.map(x=>x.toSource())) + ")";
1440+
}
1441+
}
1442+
1443+
function printIndent(level) {
1444+
return "\t".repeat(level);
13871445
}
13881446

13891447

0 commit comments

Comments
 (0)