123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- 'use strict'
-
- const DEFAULT_RAW = {
- after: '\n',
- beforeClose: '\n',
- beforeComment: '\n',
- beforeDecl: '\n',
- beforeOpen: ' ',
- beforeRule: '\n',
- colon: ': ',
- commentLeft: ' ',
- commentRight: ' ',
- emptyBody: '',
- indent: ' ',
- semicolon: false
- }
-
- function capitalize(str) {
- return str[0].toUpperCase() + str.slice(1)
- }
-
- class Stringifier {
- constructor(builder) {
- this.builder = builder
- }
-
- atrule(node, semicolon) {
- let name = '@' + node.name
- let params = node.params ? this.rawValue(node, 'params') : ''
-
- if (typeof node.raws.afterName !== 'undefined') {
- name += node.raws.afterName
- } else if (params) {
- name += ' '
- }
-
- if (node.nodes) {
- this.block(node, name + params)
- } else {
- let end = (node.raws.between || '') + (semicolon ? ';' : '')
- this.builder(name + params + end, node)
- }
- }
-
- beforeAfter(node, detect) {
- let value
- if (node.type === 'decl') {
- value = this.raw(node, null, 'beforeDecl')
- } else if (node.type === 'comment') {
- value = this.raw(node, null, 'beforeComment')
- } else if (detect === 'before') {
- value = this.raw(node, null, 'beforeRule')
- } else {
- value = this.raw(node, null, 'beforeClose')
- }
-
- let buf = node.parent
- let depth = 0
- while (buf && buf.type !== 'root') {
- depth += 1
- buf = buf.parent
- }
-
- if (value.includes('\n')) {
- let indent = this.raw(node, null, 'indent')
- if (indent.length) {
- for (let step = 0; step < depth; step++) value += indent
- }
- }
-
- return value
- }
-
- block(node, start) {
- let between = this.raw(node, 'between', 'beforeOpen')
- this.builder(start + between + '{', node, 'start')
-
- let after
- if (node.nodes && node.nodes.length) {
- this.body(node)
- after = this.raw(node, 'after')
- } else {
- after = this.raw(node, 'after', 'emptyBody')
- }
-
- if (after) this.builder(after)
- this.builder('}', node, 'end')
- }
-
- body(node) {
- let last = node.nodes.length - 1
- while (last > 0) {
- if (node.nodes[last].type !== 'comment') break
- last -= 1
- }
-
- let semicolon = this.raw(node, 'semicolon')
- for (let i = 0; i < node.nodes.length; i++) {
- let child = node.nodes[i]
- let before = this.raw(child, 'before')
- if (before) this.builder(before)
- this.stringify(child, last !== i || semicolon)
- }
- }
-
- comment(node) {
- let left = this.raw(node, 'left', 'commentLeft')
- let right = this.raw(node, 'right', 'commentRight')
- this.builder('/*' + left + node.text + right + '*/', node)
- }
-
- decl(node, semicolon) {
- let between = this.raw(node, 'between', 'colon')
- let string = node.prop + between + this.rawValue(node, 'value')
-
- if (node.important) {
- string += node.raws.important || ' !important'
- }
-
- if (semicolon) string += ';'
- this.builder(string, node)
- }
-
- document(node) {
- this.body(node)
- }
-
- raw(node, own, detect) {
- let value
- if (!detect) detect = own
-
- // Already had
- if (own) {
- value = node.raws[own]
- if (typeof value !== 'undefined') return value
- }
-
- let parent = node.parent
-
- if (detect === 'before') {
- // Hack for first rule in CSS
- if (!parent || (parent.type === 'root' && parent.first === node)) {
- return ''
- }
-
- // `root` nodes in `document` should use only their own raws
- if (parent && parent.type === 'document') {
- return ''
- }
- }
-
- // Floating child without parent
- if (!parent) return DEFAULT_RAW[detect]
-
- // Detect style by other nodes
- let root = node.root()
- if (!root.rawCache) root.rawCache = {}
- if (typeof root.rawCache[detect] !== 'undefined') {
- return root.rawCache[detect]
- }
-
- if (detect === 'before' || detect === 'after') {
- return this.beforeAfter(node, detect)
- } else {
- let method = 'raw' + capitalize(detect)
- if (this[method]) {
- value = this[method](root, node)
- } else {
- root.walk(i => {
- value = i.raws[own]
- if (typeof value !== 'undefined') return false
- })
- }
- }
-
- if (typeof value === 'undefined') value = DEFAULT_RAW[detect]
-
- root.rawCache[detect] = value
- return value
- }
-
- rawBeforeClose(root) {
- let value
- root.walk(i => {
- if (i.nodes && i.nodes.length > 0) {
- if (typeof i.raws.after !== 'undefined') {
- value = i.raws.after
- if (value.includes('\n')) {
- value = value.replace(/[^\n]+$/, '')
- }
- return false
- }
- }
- })
- if (value) value = value.replace(/\S/g, '')
- return value
- }
-
- rawBeforeComment(root, node) {
- let value
- root.walkComments(i => {
- if (typeof i.raws.before !== 'undefined') {
- value = i.raws.before
- if (value.includes('\n')) {
- value = value.replace(/[^\n]+$/, '')
- }
- return false
- }
- })
- if (typeof value === 'undefined') {
- value = this.raw(node, null, 'beforeDecl')
- } else if (value) {
- value = value.replace(/\S/g, '')
- }
- return value
- }
-
- rawBeforeDecl(root, node) {
- let value
- root.walkDecls(i => {
- if (typeof i.raws.before !== 'undefined') {
- value = i.raws.before
- if (value.includes('\n')) {
- value = value.replace(/[^\n]+$/, '')
- }
- return false
- }
- })
- if (typeof value === 'undefined') {
- value = this.raw(node, null, 'beforeRule')
- } else if (value) {
- value = value.replace(/\S/g, '')
- }
- return value
- }
-
- rawBeforeOpen(root) {
- let value
- root.walk(i => {
- if (i.type !== 'decl') {
- value = i.raws.between
- if (typeof value !== 'undefined') return false
- }
- })
- return value
- }
-
- rawBeforeRule(root) {
- let value
- root.walk(i => {
- if (i.nodes && (i.parent !== root || root.first !== i)) {
- if (typeof i.raws.before !== 'undefined') {
- value = i.raws.before
- if (value.includes('\n')) {
- value = value.replace(/[^\n]+$/, '')
- }
- return false
- }
- }
- })
- if (value) value = value.replace(/\S/g, '')
- return value
- }
-
- rawColon(root) {
- let value
- root.walkDecls(i => {
- if (typeof i.raws.between !== 'undefined') {
- value = i.raws.between.replace(/[^\s:]/g, '')
- return false
- }
- })
- return value
- }
-
- rawEmptyBody(root) {
- let value
- root.walk(i => {
- if (i.nodes && i.nodes.length === 0) {
- value = i.raws.after
- if (typeof value !== 'undefined') return false
- }
- })
- return value
- }
-
- rawIndent(root) {
- if (root.raws.indent) return root.raws.indent
- let value
- root.walk(i => {
- let p = i.parent
- if (p && p !== root && p.parent && p.parent === root) {
- if (typeof i.raws.before !== 'undefined') {
- let parts = i.raws.before.split('\n')
- value = parts[parts.length - 1]
- value = value.replace(/\S/g, '')
- return false
- }
- }
- })
- return value
- }
-
- rawSemicolon(root) {
- let value
- root.walk(i => {
- if (i.nodes && i.nodes.length && i.last.type === 'decl') {
- value = i.raws.semicolon
- if (typeof value !== 'undefined') return false
- }
- })
- return value
- }
-
- rawValue(node, prop) {
- let value = node[prop]
- let raw = node.raws[prop]
- if (raw && raw.value === value) {
- return raw.raw
- }
-
- return value
- }
-
- root(node) {
- this.body(node)
- if (node.raws.after) this.builder(node.raws.after)
- }
-
- rule(node) {
- this.block(node, this.rawValue(node, 'selector'))
- if (node.raws.ownSemicolon) {
- this.builder(node.raws.ownSemicolon, node, 'end')
- }
- }
-
- stringify(node, semicolon) {
- /* c8 ignore start */
- if (!this[node.type]) {
- throw new Error(
- 'Unknown AST node type ' +
- node.type +
- '. ' +
- 'Maybe you need to change PostCSS stringifier.'
- )
- }
- /* c8 ignore stop */
- this[node.type](node, semicolon)
- }
- }
-
- module.exports = Stringifier
- Stringifier.default = Stringifier
|