Exemple #1
0
 /// <summary>
 /// Constructs a instance of the CSS cleaning options
 /// </summary>
 public CleaningOptions()
 {
     Compatibility             = "*";
     FormattingOptions         = new FormattingOptions();
     Level                     = OptimizationLevel.One;
     Level1OptimizationOptions = new Level1OptimizationOptions();
     Level2OptimizationOptions = new Level2OptimizationOptions();
     Severity                  = 0;
 }
Exemple #2
0
        /// <summary>
        /// Converts a cleaning options to JSON
        /// </summary>
        /// <param name="options">Cleaning options</param>
        /// <returns>Cleaning options in JSON format</returns>
        private static JObject ConvertCleaningOptionsToJson(CleaningOptions options)
        {
            FormattingOptions         formattingOptions         = options.FormattingOptions;
            BreaksInsertingOptions    breaksInsertingOptions    = formattingOptions.BreaksInsertingOptions;
            SpacesInsertingOptions    spacesInsertingOptions    = formattingOptions.SpacesInsertingOptions;
            Level1OptimizationOptions level1OptimizationOptions = options.Level1OptimizationOptions;
            Level2OptimizationOptions level2OptimizationOptions = options.Level2OptimizationOptions;

            var optionsJson = new JObject(
                new JProperty("compatibility", options.Compatibility),
                new JProperty("format", new JObject(
                                  new JProperty("breaks", new JObject(
                                                    new JProperty("afterAtRule", breaksInsertingOptions.AfterAtRule),
                                                    new JProperty("afterBlockBegins", breaksInsertingOptions.AfterBlockBegins),
                                                    new JProperty("afterBlockEnds", breaksInsertingOptions.AfterBlockEnds),
                                                    new JProperty("afterComment", breaksInsertingOptions.AfterComment),
                                                    new JProperty("afterProperty", breaksInsertingOptions.AfterProperty),
                                                    new JProperty("afterRuleBegins", breaksInsertingOptions.AfterRuleBegins),
                                                    new JProperty("afterRuleEnds", breaksInsertingOptions.AfterRuleEnds),
                                                    new JProperty("beforeBlockEnds", breaksInsertingOptions.BeforeBlockEnds),
                                                    new JProperty("betweenSelectors", breaksInsertingOptions.BetweenSelectors)
                                                    )),
                                  new JProperty("indentBy", formattingOptions.IndentBy),
                                  new JProperty("indentWith", ConvertIndentTypeEnumValueToCode(formattingOptions.IndentWith)),
                                  new JProperty("spaces", new JObject(
                                                    new JProperty("aroundSelectorRelation", spacesInsertingOptions.AroundSelectorRelation),
                                                    new JProperty("beforeBlockBegins", spacesInsertingOptions.BeforeBlockBegins),
                                                    new JProperty("beforeValue", spacesInsertingOptions.BeforeValue)
                                                    )),
                                  new JProperty("wrapAt", formattingOptions.WrapAt)
                                  )),
                new JProperty("level", ConvertOptimizationLevelEnumValueToJson(options.Level,
                                                                               level1OptimizationOptions, level2OptimizationOptions))
                );

            return(optionsJson);
        }
Exemple #3
0
        /// <summary>
        /// Converts a optimization level enum value to JSON
        /// </summary>
        /// <param name="optimizationLevel">Optimization level enum value</param>
        /// <param name="level1OptimizationOptions">Level 1 optimization options</param>
        /// <param name="level2OptimizationOptions">Level 2 optimization options</param>
        /// <returns>Optimization level in JSON format</returns>
        internal static JToken ConvertOptimizationLevelEnumValueToJson(OptimizationLevel optimizationLevel,
                                                                       Level1OptimizationOptions level1OptimizationOptions, Level2OptimizationOptions level2OptimizationOptions)
        {
            JToken optimizationLevelJson;

            if (optimizationLevel == OptimizationLevel.Zero)
            {
                optimizationLevelJson = new JValue("0");
            }
            else if (optimizationLevel == OptimizationLevel.One || optimizationLevel == OptimizationLevel.Two)
            {
                var jObj = new JObject();
                jObj.Add("1", new JObject(
                             new JProperty("cleanupCharsets", level1OptimizationOptions.CleanupCharsets),
                             new JProperty("normalizeUrls", level1OptimizationOptions.NormalizeUrls),
                             new JProperty("optimizeBackground", level1OptimizationOptions.OptimizeBackground),
                             new JProperty("optimizeBorderRadius", level1OptimizationOptions.OptimizeBorderRadius),
                             new JProperty("optimizeFilter", level1OptimizationOptions.OptimizeFilter),
                             new JProperty("optimizeFont", level1OptimizationOptions.OptimizeFont),
                             new JProperty("optimizeFontWeight", level1OptimizationOptions.OptimizeFontWeight),
                             new JProperty("optimizeOutline", level1OptimizationOptions.OptimizeOutline),
                             new JProperty("removeEmpty", level1OptimizationOptions.RemoveEmpty),
                             new JProperty("removeNegativePaddings", level1OptimizationOptions.RemoveNegativePaddings),
                             new JProperty("removeQuotes", level1OptimizationOptions.RemoveQuotes),
                             new JProperty("removeWhitespace", level1OptimizationOptions.RemoveWhitespace),
                             new JProperty("replaceMultipleZeros", level1OptimizationOptions.ReplaceMultipleZeros),
                             new JProperty("replaceTimeUnits", level1OptimizationOptions.ReplaceTimeUnits),
                             new JProperty("replaceZeroUnits", level1OptimizationOptions.ReplaceZeroUnits),
                             new JProperty("roundingPrecision",
                                           ParseRoundingPrecision(level1OptimizationOptions.RoundingPrecision)),
                             new JProperty("selectorsSortingMethod", ConvertSelectorsSortingMethodEnumValueToCode(
                                               level1OptimizationOptions.SelectorsSortingMethod)),
                             new JProperty("specialComments", level1OptimizationOptions.SpecialComments),
                             new JProperty("tidyAtRules", level1OptimizationOptions.TidyAtRules),
                             new JProperty("tidyBlockScopes", level1OptimizationOptions.TidyBlockScopes),
                             new JProperty("tidySelectors", level1OptimizationOptions.TidySelectors)
                             ));

                if (optimizationLevel == OptimizationLevel.Two)
                {
                    jObj.Add("2", new JObject(
                                 new JProperty("mergeAdjacentRules", level2OptimizationOptions.MergeAdjacentRules),
                                 new JProperty("mergeIntoShorthands", level2OptimizationOptions.MergeIntoShorthands),
                                 new JProperty("mergeMedia", level2OptimizationOptions.MergeMedia),
                                 new JProperty("mergeNonAdjacentRules", level2OptimizationOptions.MergeNonAdjacentRules),
                                 new JProperty("mergeSemantically", level2OptimizationOptions.MergeSemantically),
                                 new JProperty("overrideProperties", level2OptimizationOptions.OverrideProperties),
                                 new JProperty("removeEmpty", level2OptimizationOptions.RemoveEmpty),
                                 new JProperty("reduceNonAdjacentRules", level2OptimizationOptions.ReduceNonAdjacentRules),
                                 new JProperty("removeDuplicateFontRules", level2OptimizationOptions.RemoveDuplicateFontRules),
                                 new JProperty("removeDuplicateMediaBlocks", level2OptimizationOptions.RemoveDuplicateMediaBlocks),
                                 new JProperty("removeDuplicateRules", level2OptimizationOptions.RemoveDuplicateRules),
                                 new JProperty("removeUnusedAtRules", level2OptimizationOptions.RemoveUnusedAtRules),
                                 new JProperty("restructureRules", level2OptimizationOptions.RestructureRules),
                                 new JProperty("skipProperties", ParseSkippingProperties(level2OptimizationOptions.SkipProperties))
                                 ));
                }

                optimizationLevelJson = jObj;
            }
            else
            {
                throw new InvalidCastException(string.Format(CoreStrings.Common_EnumValueToCodeConversionFailed,
                                                             optimizationLevel.ToString(), typeof(OptimizationLevel)));
            }

            return(optimizationLevelJson);
        }
        /// <summary>
        /// Constructs a instance of Clean CSS minifier
        /// </summary>
        /// <param name="createJsEngineInstance">Delegate that creates an instance of JS engine</param>
        /// <param name="cleanConfig">Configuration settings of Clean-css Minifier</param>
        public CleanCssMinifier(Func <IJsEngine> createJsEngineInstance, CleanSettings cleanConfig)
        {
            CssMinifierSettings        cssMinifierConfig        = cleanConfig.Css;
            FormattingSettings         formattingConfig         = cssMinifierConfig.Formatting;
            BreaksInsertingSettings    breaksInsertingConfig    = formattingConfig.BreaksInserting;
            SpacesInsertingSettings    spacesInsertingConfig    = formattingConfig.SpacesInserting;
            Level1OptimizationSettings level1OptimizationConfig = cssMinifierConfig.Level1Optimizations;
            Level2OptimizationSettings level2OptimizationConfig = cssMinifierConfig.Level2Optimizations;

            Compatibility     = cssMinifierConfig.Compatibility;
            FormattingOptions = new FormattingOptions
            {
                BreaksInsertingOptions = new BreaksInsertingOptions
                {
                    AfterAtRule      = breaksInsertingConfig.AfterAtRule,
                    AfterBlockBegins = breaksInsertingConfig.AfterBlockBegins,
                    AfterBlockEnds   = breaksInsertingConfig.AfterBlockEnds,
                    AfterComment     = breaksInsertingConfig.AfterComment,
                    AfterProperty    = breaksInsertingConfig.AfterProperty,
                    AfterRuleBegins  = breaksInsertingConfig.AfterRuleBegins,
                    AfterRuleEnds    = breaksInsertingConfig.AfterRuleEnds,
                    BeforeBlockEnds  = breaksInsertingConfig.BeforeBlockEnds,
                    BetweenSelectors = breaksInsertingConfig.BetweenSelectors
                },
                IndentBy               = formattingConfig.IndentBy,
                IndentWith             = formattingConfig.IndentWith,
                SpacesInsertingOptions = new SpacesInsertingOptions
                {
                    AroundSelectorRelation = spacesInsertingConfig.AroundSelectorRelation,
                    BeforeBlockBegins      = spacesInsertingConfig.BeforeBlockBegins,
                    BeforeValue            = spacesInsertingConfig.BeforeValue
                },
                WrapAt = formattingConfig.WrapAt
            };
            Level = cssMinifierConfig.Level;
            Level1OptimizationOptions = new Level1OptimizationOptions
            {
                CleanupCharsets        = level1OptimizationConfig.CleanupCharsets,
                NormalizeUrls          = level1OptimizationConfig.NormalizeUrls,
                OptimizeBackground     = level1OptimizationConfig.OptimizeBackground,
                OptimizeBorderRadius   = level1OptimizationConfig.OptimizeBorderRadius,
                OptimizeFilter         = level1OptimizationConfig.OptimizeFilter,
                OptimizeFont           = level1OptimizationConfig.OptimizeFont,
                OptimizeFontWeight     = level1OptimizationConfig.OptimizeFontWeight,
                OptimizeOutline        = level1OptimizationConfig.OptimizeOutline,
                RemoveEmpty            = level1OptimizationConfig.RemoveEmpty,
                RemoveNegativePaddings = level1OptimizationConfig.RemoveNegativePaddings,
                RemoveQuotes           = level1OptimizationConfig.RemoveQuotes,
                RemoveWhitespace       = level1OptimizationConfig.RemoveWhitespace,
                ReplaceMultipleZeros   = level1OptimizationConfig.ReplaceMultipleZeros,
                ReplaceTimeUnits       = level1OptimizationConfig.ReplaceTimeUnits,
                ReplaceZeroUnits       = level1OptimizationConfig.ReplaceZeroUnits,
                RoundingPrecision      = level1OptimizationConfig.RoundingPrecision,
                SelectorsSortingMethod = level1OptimizationConfig.SelectorsSortingMethod,
                SpecialComments        = level1OptimizationConfig.SpecialComments,
                TidyAtRules            = level1OptimizationConfig.TidyAtRules,
                TidyBlockScopes        = level1OptimizationConfig.TidyBlockScopes,
                TidySelectors          = level1OptimizationConfig.TidySelectors
            };
            Level2OptimizationOptions = new Level2OptimizationOptions
            {
                MergeAdjacentRules         = level2OptimizationConfig.MergeAdjacentRules,
                MergeIntoShorthands        = level2OptimizationConfig.MergeIntoShorthands,
                MergeMedia                 = level2OptimizationConfig.MergeMedia,
                MergeNonAdjacentRules      = level2OptimizationConfig.MergeNonAdjacentRules,
                MergeSemantically          = level2OptimizationConfig.MergeSemantically,
                OverrideProperties         = level2OptimizationConfig.OverrideProperties,
                RemoveEmpty                = level2OptimizationConfig.RemoveEmpty,
                ReduceNonAdjacentRules     = level2OptimizationConfig.ReduceNonAdjacentRules,
                RemoveDuplicateFontRules   = level2OptimizationConfig.RemoveDuplicateFontRules,
                RemoveDuplicateMediaBlocks = level2OptimizationConfig.RemoveDuplicateMediaBlocks,
                RemoveDuplicateRules       = level2OptimizationConfig.RemoveDuplicateRules,
                RemoveUnusedAtRules        = level2OptimizationConfig.RemoveUnusedAtRules,
                RestructureRules           = level2OptimizationConfig.RestructureRules,
                SkipProperties             = level2OptimizationConfig.SkipProperties
            };
            Severity = cssMinifierConfig.Severity;

            if (createJsEngineInstance == null)
            {
                string jsEngineName = cleanConfig.JsEngine.Name;
                if (string.IsNullOrWhiteSpace(jsEngineName))
                {
                    throw new ConfigurationErrorsException(
                              string.Format(CoreStrings.Configuration_JsEngineNotSpecified,
                                            "clean",
                                            @"
  * JavaScriptEngineSwitcher.Msie
  * JavaScriptEngineSwitcher.V8
  * JavaScriptEngineSwitcher.ChakraCore",
                                            "MsieJsEngine")
                              );
                }

                createJsEngineInstance = () => JsEngineSwitcher.Current.CreateEngine(jsEngineName);
            }
            _createJsEngineInstance = createJsEngineInstance;
        }