Exemplo n.º 1
0
        void TrimScriptOrStyle(HtmlElement element, HtmlScriptStyleElement type)
        {
            if (settings.RemoveScriptStyleTypeAttribute && type != HtmlScriptStyleElement.JSON)
            {
                element.RemoveAttribute("type");
            }

            if (type != HtmlScriptStyleElement.Style && !settings.MinifyJs)
            {
                return;
            }

            if (type == HtmlScriptStyleElement.Style && !settings.MinifyCss)
            {
                return;
            }

            var raw = element.FirstChild as HtmlRaw;

            if (raw == null)
            {
                return;
            }

            var slice = raw.Slice;

            slice.TrimStart();
            slice.TrimEnd();

            // If the text has a comment or CDATA, we won't try to minify it
            if (slice.StartsWith("<!--") || slice.StartsWithIgnoreCase("<![CDATA["))
            {
                return;
            }

            var text = slice.ToString();

            var textMinified = MinifyJsOrCss(text, type);

            if (textMinified == null)
            {
                return;
            }

            raw.Slice = new StringSlice(textMinified);
        }
Exemplo n.º 2
0
        string MinifyJsOrCss(string text, HtmlScriptStyleElement type)
        {
            UglifyResult result;

            if (type == HtmlScriptStyleElement.Style)
            {
                result = Uglify.Css(text, "inner_css", settings.CssSettings);
            }
            else if (type == HtmlScriptStyleElement.Javascript)
            {
                result = Uglify.Js(text, "inner_js", settings.JsSettings);
            }
            else if (type == HtmlScriptStyleElement.JSON)
            {
                var jsonSettings = settings.JsSettings.Clone();
                jsonSettings.Format     = JavaScriptFormat.JSON;
                jsonSettings.SourceMode = JavaScriptSourceMode.Expression;
                jsonSettings.IgnoreConditionalCompilation = true;
                result = Uglify.Js(text, "inner_js", jsonSettings);
            }
            else
            {
                return(text);
            }

            if (result.Errors != null)
            {
                Errors.AddRange(result.Errors);
            }

            if (result.HasErrors)
            {
                HasErrors = true;
                return(text);
            }

            return(result.Code);
        }