示例#1
0
 /// <summary>
 /// Compress HTML by removing unnecessary whitespace and comments
 /// </summary>
 /// <param name="html">The HTML content to minify. A <see cref="string"/> or <see cref="Stream"/> can also be used.</param>
 /// <param name="writer">The writer where the HTML output is written. For best results, use a <see cref="HtmlTextWriter"/></param>
 /// <param name="settings">Settings to control how the HTML is compressed</param>
 public static void Minify(TextSource html, XmlWriter writer, HtmlMinifySettings settings = null)
 {
     using (var reader = new HtmlReader(html, false))
     {
         reader.Minify(settings).ToHtml(writer);
     }
 }
示例#2
0
        public void RoundTrip_Svg02()
        {
            //var html = @"<svg xmlns=""http://www.w3.org/2000/svg""><path d=""M182,65 L256,93 L354,65"" /></svg>";
            var html = @"<svg version=""1.1"" xmlns:user=""urn:user-scripts"" xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" xmlns:aras=""http://www.aras.com"" xmlns:data=""http://www.aras.com/customer"" xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink""><defs><marker id=""Triangle"" viewBox=""0 0 10 6"" refX=""22"" refY=""3"" markerWidth=""10"" markerHeight=""6"" markerUnits=""userSpaceOnUse"" orient=""auto"" style=""fill:#999""><path d=""M 0 0 L 10 3 L 0 6 z"" /></marker></defs><path id=""path_74BAF626F20A48A59CEACE75D5CE166A"" style=""stroke: rgb(60%, 60%, 60%); fill:none; stroke-width: 1px; marker-end: url(#Triangle)"" d=""M182,65 L256,93 L354,65"" /><path id=""path_00A55285D7EE441E8D08F356B6BED0A9"" style=""stroke: rgb(60%, 60%, 60%); fill:none; stroke-width: 1px; marker-end: url(#Triangle)"" d=""M182,65 L182,20 L540,20"" /><path id=""path_0B14FEF62A4644A7A57903E4F41B0A30"" style=""stroke: rgb(60%, 60%, 60%); fill:none; stroke-width: 1px; marker-end: url(#Triangle)"" d=""M182,65 L182,165 L540,165"" /></svg>";

            using (var reader = new HtmlReader(html))
            {
                var rendered = reader.Minify().ToHtml();
                Assert.Equal(html, rendered);
            }
        }
示例#3
0
        /// <summary>
        /// Compress HTML by removing unnecessary whitespace and comments
        /// </summary>
        /// <param name="html">The HTML content to minify. A <see cref="string"/> or <see cref="Stream"/> can also be used.</param>
        /// <param name="settings">Settings to control how the HTML is compressed</param>
        /// <returns>An <see cref="HtmlString"/> containing the compressed HTML</returns>
        public static HtmlString Minify(TextSource html, HtmlMinifySettings settings = null)
        {
            var sb = Pool.NewStringBuilder();

            sb.EnsureCapacity(html.Length);
            using (var sw = new StringWriter(sb))
                using (var reader = new HtmlReader(html, false))
                {
                    reader.Minify(settings).ToHtml(sw, new HtmlWriterSettings());
                    sw.Flush();
                    return(new HtmlString(sb.ToPool()));
                }
        }
示例#4
0
        public void RoundTrip_SciptTagWithXml()
        {
            var html = @"<script type=""text/xml""><first><another attr=""value"" /></first></script>";

            using (var reader = new HtmlReader(html))
            {
                var rendered = reader.ToHtml();
                Assert.Equal(html, rendered);
            }

            using (var reader = new HtmlReader(html))
            {
                var rendered = reader.Minify().ToHtml();
                Assert.Equal(html, rendered);
            }
        }