forked from zhurui/management
345 lines
36 KiB
JavaScript
345 lines
36 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
|
||
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||
|
|
||
|
var defaultRaw = {
|
||
|
colon: ': ',
|
||
|
indent: ' ',
|
||
|
beforeDecl: '\n',
|
||
|
beforeRule: '\n',
|
||
|
beforeOpen: ' ',
|
||
|
beforeClose: '\n',
|
||
|
beforeComment: '\n',
|
||
|
after: '\n',
|
||
|
emptyBody: '',
|
||
|
commentLeft: ' ',
|
||
|
commentRight: ' '
|
||
|
};
|
||
|
|
||
|
function capitalize(str) {
|
||
|
return str[0].toUpperCase() + str.slice(1);
|
||
|
}
|
||
|
|
||
|
var Stringifier = function () {
|
||
|
function Stringifier(builder) {
|
||
|
_classCallCheck(this, Stringifier);
|
||
|
|
||
|
this.builder = builder;
|
||
|
}
|
||
|
|
||
|
Stringifier.prototype.stringify = function stringify(node, semicolon) {
|
||
|
this[node.type](node, semicolon);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.root = function root(node) {
|
||
|
this.body(node);
|
||
|
if (node.raws.after) this.builder(node.raws.after);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.comment = function comment(node) {
|
||
|
var left = this.raw(node, 'left', 'commentLeft');
|
||
|
var right = this.raw(node, 'right', 'commentRight');
|
||
|
this.builder('/*' + left + node.text + right + '*/', node);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.decl = function decl(node, semicolon) {
|
||
|
var between = this.raw(node, 'between', 'colon');
|
||
|
var string = node.prop + between + this.rawValue(node, 'value');
|
||
|
|
||
|
if (node.important) {
|
||
|
string += node.raws.important || ' !important';
|
||
|
}
|
||
|
|
||
|
if (semicolon) string += ';';
|
||
|
this.builder(string, node);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rule = function rule(node) {
|
||
|
this.block(node, this.rawValue(node, 'selector'));
|
||
|
if (node.raws.ownSemicolon) {
|
||
|
this.builder(node.raws.ownSemicolon, node, 'end');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.atrule = function atrule(node, semicolon) {
|
||
|
var name = '@' + node.name;
|
||
|
var 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 {
|
||
|
var end = (node.raws.between || '') + (semicolon ? ';' : '');
|
||
|
this.builder(name + params + end, node);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.body = function body(node) {
|
||
|
var last = node.nodes.length - 1;
|
||
|
while (last > 0) {
|
||
|
if (node.nodes[last].type !== 'comment') break;
|
||
|
last -= 1;
|
||
|
}
|
||
|
|
||
|
var semicolon = this.raw(node, 'semicolon');
|
||
|
for (var i = 0; i < node.nodes.length; i++) {
|
||
|
var child = node.nodes[i];
|
||
|
var before = this.raw(child, 'before');
|
||
|
if (before) this.builder(before);
|
||
|
this.stringify(child, last !== i || semicolon);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.block = function block(node, start) {
|
||
|
var between = this.raw(node, 'between', 'beforeOpen');
|
||
|
this.builder(start + between + '{', node, 'start');
|
||
|
|
||
|
var after = void 0;
|
||
|
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');
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.raw = function raw(node, own, detect) {
|
||
|
var value = void 0;
|
||
|
if (!detect) detect = own;
|
||
|
|
||
|
// Already had
|
||
|
if (own) {
|
||
|
value = node.raws[own];
|
||
|
if (typeof value !== 'undefined') return value;
|
||
|
}
|
||
|
|
||
|
var parent = node.parent;
|
||
|
|
||
|
// Hack for first rule in CSS
|
||
|
if (detect === 'before') {
|
||
|
if (!parent || parent.type === 'root' && parent.first === node) {
|
||
|
return '';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Floating child without parent
|
||
|
if (!parent) return defaultRaw[detect];
|
||
|
|
||
|
// Detect style by other nodes
|
||
|
var 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 {
|
||
|
var method = 'raw' + capitalize(detect);
|
||
|
if (this[method]) {
|
||
|
value = this[method](root, node);
|
||
|
} else {
|
||
|
root.walk(function (i) {
|
||
|
value = i.raws[own];
|
||
|
if (typeof value !== 'undefined') return false;
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (typeof value === 'undefined') value = defaultRaw[detect];
|
||
|
|
||
|
root.rawCache[detect] = value;
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawSemicolon = function rawSemicolon(root) {
|
||
|
var value = void 0;
|
||
|
root.walk(function (i) {
|
||
|
if (i.nodes && i.nodes.length && i.last.type === 'decl') {
|
||
|
value = i.raws.semicolon;
|
||
|
if (typeof value !== 'undefined') return false;
|
||
|
}
|
||
|
});
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawEmptyBody = function rawEmptyBody(root) {
|
||
|
var value = void 0;
|
||
|
root.walk(function (i) {
|
||
|
if (i.nodes && i.nodes.length === 0) {
|
||
|
value = i.raws.after;
|
||
|
if (typeof value !== 'undefined') return false;
|
||
|
}
|
||
|
});
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawIndent = function rawIndent(root) {
|
||
|
if (root.raws.indent) return root.raws.indent;
|
||
|
var value = void 0;
|
||
|
root.walk(function (i) {
|
||
|
var p = i.parent;
|
||
|
if (p && p !== root && p.parent && p.parent === root) {
|
||
|
if (typeof i.raws.before !== 'undefined') {
|
||
|
var parts = i.raws.before.split('\n');
|
||
|
value = parts[parts.length - 1];
|
||
|
value = value.replace(/[^\s]/g, '');
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawBeforeComment = function rawBeforeComment(root, node) {
|
||
|
var value = void 0;
|
||
|
root.walkComments(function (i) {
|
||
|
if (typeof i.raws.before !== 'undefined') {
|
||
|
value = i.raws.before;
|
||
|
if (value.indexOf('\n') !== -1) {
|
||
|
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;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawBeforeDecl = function rawBeforeDecl(root, node) {
|
||
|
var value = void 0;
|
||
|
root.walkDecls(function (i) {
|
||
|
if (typeof i.raws.before !== 'undefined') {
|
||
|
value = i.raws.before;
|
||
|
if (value.indexOf('\n') !== -1) {
|
||
|
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;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawBeforeRule = function rawBeforeRule(root) {
|
||
|
var value = void 0;
|
||
|
root.walk(function (i) {
|
||
|
if (i.nodes && (i.parent !== root || root.first !== i)) {
|
||
|
if (typeof i.raws.before !== 'undefined') {
|
||
|
value = i.raws.before;
|
||
|
if (value.indexOf('\n') !== -1) {
|
||
|
value = value.replace(/[^\n]+$/, '');
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
if (value) value = value.replace(/[^\s]/g, '');
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawBeforeClose = function rawBeforeClose(root) {
|
||
|
var value = void 0;
|
||
|
root.walk(function (i) {
|
||
|
if (i.nodes && i.nodes.length > 0) {
|
||
|
if (typeof i.raws.after !== 'undefined') {
|
||
|
value = i.raws.after;
|
||
|
if (value.indexOf('\n') !== -1) {
|
||
|
value = value.replace(/[^\n]+$/, '');
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
if (value) value = value.replace(/[^\s]/g, '');
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawBeforeOpen = function rawBeforeOpen(root) {
|
||
|
var value = void 0;
|
||
|
root.walk(function (i) {
|
||
|
if (i.type !== 'decl') {
|
||
|
value = i.raws.between;
|
||
|
if (typeof value !== 'undefined') return false;
|
||
|
}
|
||
|
});
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawColon = function rawColon(root) {
|
||
|
var value = void 0;
|
||
|
root.walkDecls(function (i) {
|
||
|
if (typeof i.raws.between !== 'undefined') {
|
||
|
value = i.raws.between.replace(/[^\s:]/g, '');
|
||
|
return false;
|
||
|
}
|
||
|
});
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.beforeAfter = function beforeAfter(node, detect) {
|
||
|
var value = void 0;
|
||
|
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');
|
||
|
}
|
||
|
|
||
|
var buf = node.parent;
|
||
|
var depth = 0;
|
||
|
while (buf && buf.type !== 'root') {
|
||
|
depth += 1;
|
||
|
buf = buf.parent;
|
||
|
}
|
||
|
|
||
|
if (value.indexOf('\n') !== -1) {
|
||
|
var indent = this.raw(node, null, 'indent');
|
||
|
if (indent.length) {
|
||
|
for (var step = 0; step < depth; step++) {
|
||
|
value += indent;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawValue = function rawValue(node, prop) {
|
||
|
var value = node[prop];
|
||
|
var raw = node.raws[prop];
|
||
|
if (raw && raw.value === value) {
|
||
|
return raw.raw;
|
||
|
} else {
|
||
|
return value;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
return Stringifier;
|
||
|
}();
|
||
|
|
||
|
exports.default = Stringifier;
|
||
|
module.exports = exports['default'];
|
||
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0cmluZ2lmaWVyLmVzNiJdLCJuYW1lcyI6WyJkZWZhdWx0UmF3IiwiY29sb24iLCJpbmRlbnQiLCJiZWZvcmVEZWNsIiwiYmVmb3JlUnVsZSIsImJlZm9yZU9wZW4iLCJiZWZvcmVDbG9zZSIsImJlZm9yZUNvbW1lbnQiLCJhZnRlciIsImVtcHR5Qm9keSIsImNvbW1lbnRMZWZ0IiwiY29tbWVudFJpZ2h0IiwiY2FwaXRhbGl6ZSIsInN0ciIsInRvVXBwZXJDYXNlIiwic2xpY2UiLCJTdHJpbmdpZmllciIsImJ1aWxkZXIiLCJzdHJpbmdpZnkiLCJub2RlIiwic2VtaWNvbG9uIiwidHlwZSIsInJvb3QiLCJib2R5IiwicmF3cyIsImNvbW1lbnQiLCJsZWZ0IiwicmF3IiwicmlnaHQiLCJ0ZXh0IiwiZGVjbCIsImJldHdlZW4iLCJzdHJpbmciLCJwcm9wIiwicmF3VmFsdWUiLCJpbXBvcnR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