123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- var override = require('../utils/override');
-
- var Breaks = {
- AfterAtRule: 'afterAtRule',
- AfterBlockBegins: 'afterBlockBegins',
- AfterBlockEnds: 'afterBlockEnds',
- AfterComment: 'afterComment',
- AfterProperty: 'afterProperty',
- AfterRuleBegins: 'afterRuleBegins',
- AfterRuleEnds: 'afterRuleEnds',
- BeforeBlockEnds: 'beforeBlockEnds',
- BetweenSelectors: 'betweenSelectors'
- };
-
- var IndentWith = {
- Space: ' ',
- Tab: '\t'
- };
-
- var Spaces = {
- AroundSelectorRelation: 'aroundSelectorRelation',
- BeforeBlockBegins: 'beforeBlockBegins',
- BeforeValue: 'beforeValue'
- };
-
- var DEFAULTS = {
- breaks: breaks(false),
- indentBy: 0,
- indentWith: IndentWith.Space,
- spaces: spaces(false),
- wrapAt: false
- };
-
- var BEAUTIFY_ALIAS = 'beautify';
- var KEEP_BREAKS_ALIAS = 'keep-breaks';
-
- var OPTION_SEPARATOR = ';';
- var OPTION_NAME_VALUE_SEPARATOR = ':';
- var HASH_VALUES_OPTION_SEPARATOR = ',';
- var HASH_VALUES_NAME_VALUE_SEPARATOR = '=';
-
- var FALSE_KEYWORD_1 = 'false';
- var FALSE_KEYWORD_2 = 'off';
- var TRUE_KEYWORD_1 = 'true';
- var TRUE_KEYWORD_2 = 'on';
-
- function breaks(value) {
- var breakOptions = {};
-
- breakOptions[Breaks.AfterAtRule] = value;
- breakOptions[Breaks.AfterBlockBegins] = value;
- breakOptions[Breaks.AfterBlockEnds] = value;
- breakOptions[Breaks.AfterComment] = value;
- breakOptions[Breaks.AfterProperty] = value;
- breakOptions[Breaks.AfterRuleBegins] = value;
- breakOptions[Breaks.AfterRuleEnds] = value;
- breakOptions[Breaks.BeforeBlockEnds] = value;
- breakOptions[Breaks.BetweenSelectors] = value;
-
- return breakOptions;
- }
-
- function spaces(value) {
- var spaceOptions = {};
-
- spaceOptions[Spaces.AroundSelectorRelation] = value;
- spaceOptions[Spaces.BeforeBlockBegins] = value;
- spaceOptions[Spaces.BeforeValue] = value;
-
- return spaceOptions;
- }
-
- function formatFrom(source) {
- if (source === undefined || source === false) {
- return false;
- }
-
- if (typeof source == 'object' && 'indentBy' in source) {
- source = override(source, { indentBy: parseInt(source.indentBy) });
- }
-
- if (typeof source == 'object' && 'indentWith' in source) {
- source = override(source, { indentWith: mapIndentWith(source.indentWith) });
- }
-
- if (typeof source == 'object') {
- return override(DEFAULTS, source);
- }
-
- if (typeof source == 'object') {
- return override(DEFAULTS, source);
- }
-
- if (typeof source == 'string' && source == BEAUTIFY_ALIAS) {
- return override(DEFAULTS, {
- breaks: breaks(true),
- indentBy: 2,
- spaces: spaces(true)
- });
- }
-
- if (typeof source == 'string' && source == KEEP_BREAKS_ALIAS) {
- return override(DEFAULTS, {
- breaks: {
- afterAtRule: true,
- afterBlockBegins: true,
- afterBlockEnds: true,
- afterComment: true,
- afterRuleEnds: true,
- beforeBlockEnds: true
- }
- });
- }
-
- if (typeof source == 'string') {
- return override(DEFAULTS, toHash(source));
- }
-
- return DEFAULTS;
- }
-
- function toHash(string) {
- return string
- .split(OPTION_SEPARATOR)
- .reduce(function (accumulator, directive) {
- var parts = directive.split(OPTION_NAME_VALUE_SEPARATOR);
- var name = parts[0];
- var value = parts[1];
-
- if (name == 'breaks' || name == 'spaces') {
- accumulator[name] = hashValuesToHash(value);
- } else if (name == 'indentBy' || name == 'wrapAt') {
- accumulator[name] = parseInt(value);
- } else if (name == 'indentWith') {
- accumulator[name] = mapIndentWith(value);
- }
-
- return accumulator;
- }, {});
- }
-
- function hashValuesToHash(string) {
- return string
- .split(HASH_VALUES_OPTION_SEPARATOR)
- .reduce(function (accumulator, directive) {
- var parts = directive.split(HASH_VALUES_NAME_VALUE_SEPARATOR);
- var name = parts[0];
- var value = parts[1];
-
- accumulator[name] = normalizeValue(value);
-
- return accumulator;
- }, {});
- }
-
-
- function normalizeValue(value) {
- switch (value) {
- case FALSE_KEYWORD_1:
- case FALSE_KEYWORD_2:
- return false;
- case TRUE_KEYWORD_1:
- case TRUE_KEYWORD_2:
- return true;
- default:
- return value;
- }
- }
-
- function mapIndentWith(value) {
- switch (value) {
- case 'space':
- return IndentWith.Space;
- case 'tab':
- return IndentWith.Tab;
- default:
- return value;
- }
- }
-
- module.exports = {
- Breaks: Breaks,
- Spaces: Spaces,
- formatFrom: formatFrom
- };
|