UNPKG

3.67 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt The complete set of authors may be found
7 * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
8 * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
9 * Google as part of the polymer project is also subject to an additional IP
10 * rights grant found at http://polymer.github.io/PATENTS.txt
11 */
12Object.defineProperty(exports, "__esModule", { value: true });
13const common_1 = require("./common");
14const node_visitor_1 = require("./node-visitor");
15/**
16 * Class that implements basic stringification of an AST produced by the Parser.
17 */
18class Stringifier extends node_visitor_1.NodeVisitor {
19 /**
20 * Stringify an AST such as one produced by a Parser.
21 * @param ast A node object representing the root of an AST.
22 * @return The stringified CSS corresponding to the AST.
23 */
24 stringify(ast) {
25 return this.visit(ast) || '';
26 }
27 /**
28 * Visit and stringify a Stylesheet node.
29 * @param stylesheet A Stylesheet node.
30 * @return The stringified CSS of the Stylesheet.
31 */
32 [common_1.nodeType.stylesheet](stylesheet) {
33 let rules = '';
34 for (let i = 0; i < stylesheet.rules.length; ++i) {
35 rules += this.visit(stylesheet.rules[i]);
36 }
37 return rules;
38 }
39 /**
40 * Visit and stringify an At Rule node.
41 * @param atRule An At Rule node.
42 * @return The stringified CSS of the At Rule.
43 */
44 [common_1.nodeType.atRule](atRule) {
45 return `@${atRule.name}` +
46 (atRule.parameters ? ` ${atRule.parameters}` : '') +
47 (atRule.rulelist ? `${this.visit(atRule.rulelist)}` : ';');
48 }
49 /**
50 * Visit and stringify a Rulelist node.
51 * @param rulelist A Rulelist node.
52 * @return The stringified CSS of the Rulelist.
53 */
54 [common_1.nodeType.rulelist](rulelist) {
55 let rules = '{';
56 for (let i = 0; i < rulelist.rules.length; ++i) {
57 rules += this.visit(rulelist.rules[i]);
58 }
59 return rules + '}';
60 }
61 /**
62 * Visit and stringify a Comment node.
63 * @param comment A Comment node.
64 * @return The stringified CSS of the Comment.
65 */
66 [common_1.nodeType.comment](comment) {
67 return `${comment.value}`;
68 }
69 /**
70 * Visit and stringify a Seletor node.
71 * @param ruleset A Ruleset node.
72 * @return The stringified CSS of the Ruleset.
73 */
74 [common_1.nodeType.ruleset](ruleset) {
75 return `${ruleset.selector}${this.visit(ruleset.rulelist)}`;
76 }
77 /**
78 * Visit and stringify a Declaration node.
79 * @param declaration A Declaration node.
80 * @return The stringified CSS of the Declaration.
81 */
82 [common_1.nodeType.declaration](declaration) {
83 return declaration.value != null ?
84 `${declaration.name}:${this.visit(declaration.value)};` :
85 `${declaration.name};`;
86 }
87 /**
88 * Visit and stringify an Expression node.
89 * @param expression An Expression node.
90 * @return The stringified CSS of the Expression.
91 */
92 [common_1.nodeType.expression](expression) {
93 return `${expression.text}`;
94 }
95 /**
96 * Visit a discarded node.
97 * @param discarded A Discarded node.
98 * @return An empty string, since Discarded nodes are discarded.
99 */
100 [common_1.nodeType.discarded](_discarded) {
101 return '';
102 }
103}
104exports.Stringifier = Stringifier;
105//# sourceMappingURL=stringifier.js.map
\No newline at end of file