コード例 #1
0
        public ActionResult Javascript(string keys)
        {
            string result = _provider.GetFromCache("resources.javascript." + Request.Url.PathAndQuery, () =>
            {
                var scriptFiles = keys.Split('|').ToList();

                var sb = new StringBuilder();

                foreach (var s in scriptFiles)
                {
                    var script = MothScriptHelper.GetFileContent(new List <string> {
                        s
                    }, HttpContext).ToString();
                    if (_provider.Enable.ScriptMinification)
                    {
                        script = JavaScriptCompressor.Compress(script, false, true, true, false, Int32.MaxValue, Encoding.UTF8, new CultureInfo("en-US"));
                    }
                    sb.AppendLine(script);
                }

                return(sb.ToString());
            }, _provider.CacheDurations.ExternalScript);

            return(new ContentResult()
            {
                Content = result,
                ContentType = "text/javascript",
            });
        }
コード例 #2
0
ファイル: DataUriHelper.cs プロジェクト: endforward/Moth
        private static TagBuilder GetTagbuilder(DataUriImage image, IDictionary <string, object> htmlAttributes)
        {
            TagBuilder tb = new TagBuilder("span");

            tb.MergeAttributes(htmlAttributes);

            tb.MergeAttribute("style", string.Format(";width:{0}px;height:{1}px;display:inline-block;", image.Width, image.Height));
            tb.MergeAttribute("class", image.Id);

            MothScriptHelper.RegisterDataUri(image.Id, image.ImageUrl);

            return(tb);
        }
コード例 #3
0
        public ActionResult Css(string keys)
        {
            var cssFiles = keys.Split('|').ToList();

            string result = _provider.GetFromCache("resources.css." + Request.Url.PathAndQuery, () =>
            {
                var sb = new StringBuilder();
                string stylos;

                if (_provider.Enable.CssPreprocessing)
                {
                    foreach (var f in cssFiles)
                    {
                        CSSDocument doc;
                        var definition = GenerateSpriteDefinition(f);

                        sb.AppendLine(definition.Document.ToOutput());
                    }
                    stylos = sb.ToString();
                }
                else
                {
                    stylos = MothScriptHelper.GetFileContent(cssFiles, HttpContext).ToString();
                }

                if (_provider.Enable.ScriptMinification)
                {
                    // minification
                    stylos = CssCompressor.Compress(stylos);
                }
                return(stylos);
            }, _provider.CacheDurations.ExternalScript);


            return(new ContentResult()
            {
                Content = result,
                ContentType = "text/css"
            });
        }
コード例 #4
0
        public void Dispose()
        {
            string content;


            if (_originalTw == null)
            {
                if (_htmlHelper.ViewContext.Writer is StringWriter)
                {
                    // MVC 3
                    var stringBuilder = ((StringWriter)_htmlHelper.ViewContext.Writer).GetStringBuilder();

                    char[] inlineScript = new char[stringBuilder.Length - _startIndexOf];
                    stringBuilder.CopyTo(_startIndexOf, inlineScript, 0, inlineScript.Length);

                    content = new string(inlineScript);

                    stringBuilder.Remove(_startIndexOf, inlineScript.Length);
                }
                else
                {
                    // fak joe
                    return;
                }
            }
            else
            {
                _tw.Flush();

                _ms.Seek(0, SeekOrigin.Begin);

                using (StreamReader sr = new StreamReader(_ms))
                {
                    content = sr.ReadToEnd();
                }
            }

            var key = "inputhelper.scripts." + new MurmurHash2UInt32Hack().Hash(Encoding.UTF8.GetBytes(content));

            if (Provider.Enable.ScriptMinification)
            {
                var minified = Provider.GetFromCache(key,
                                                     () => JavaScriptCompressor.Compress(content, false, true, true, false, Int32.MaxValue, Encoding.UTF8, new CultureInfo("en-US")),
                                                     Provider.CacheDurations.InlineScript);

                if (!content.Contains("<script") && minified.Contains("<script"))
                {
                    // YUI compressor makes some mistakes when doing inline thingy's. Like making '<scr' + 'ipt>' into <script> which breaks browser
                    // so we won't compress this part. sorry :-)
                }
                else
                {
                    content = minified;
                }
            }

            if (_position == ScriptPositionEnum.Current)
            {
                if (_originalTw == null)
                {
                    // MVC 3?
                    _htmlHelper.ViewContext.Writer.Write(content);
                }
                else
                {
                    _originalTw.Write(content);
                    _tw.Dispose();
                }
            }
            else
            {
                MothScriptHelper.RegisterInlineScript(content);
            }
            // Set the writer back to the original, whether we are outputting here or at the bottom
            if (_originalTw != null)
            {
                ((HtmlTextWriter)_htmlHelper.ViewContext.Writer).InnerWriter = _originalTw;
            }
        }