Ejemplo n.º 1
0
        /// <summary>
        /// In-lines the CSS for the current HTML
        /// </summary>
        /// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
        /// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
        /// <param name="css">A string containing a style-sheet for inlining.</param>
        /// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
        /// <param name="removeComments">True to remove comments, false to leave them intact</param>
        /// <param name="precompiledStyles"></param>
        /// <returns>Returns the html input, with styles moved to inline attributes.</returns>
        public InlineResult MoveCssInline(bool removeStyleElements = false, string ignoreElements = null, string css = null, bool stripIdAndClassAttributes = false, bool removeComments = false, SortedList <string, StyleClass> precompiledStyles = null)
        {
            // Store the variables used for inlining the CSS
            _removeStyleElements       = removeStyleElements;
            _stripIdAndClassAttributes = stripIdAndClassAttributes;
            _ignoreElements            = ignoreElements;

            // Gather all of the CSS that we can work with.
            var cssSourceNodes = CssSourceNodes();
            var cssLinkNodes   = CssLinkNodes();
            var cssSources     = new List <ICssSource>(ConvertToStyleSources(cssSourceNodes));

            cssSources.AddRange(ConvertToStyleSources(cssLinkNodes));
            cssSources.AddRange(PreMailer.ConvertToStyleSources(css));

            var cssBlocks = PreMailer.GetCssBlocks(cssSources);

            if (_removeStyleElements)
            {
                RemoveStyleElements(cssSourceNodes);
                RemoveStyleElements(cssLinkNodes);
            }

            var joinedBlocks   = PreMailer.Join(cssBlocks);
            var validSelectors = CleanUnsupportedSelectors(joinedBlocks);

            if (precompiledStyles != null)
            {
                precompiledStyles.ToList().ForEach(kvp => { validSelectors.Add(kvp.Key, kvp.Value); });
            }

            var elementsWithStyles = FindElementsWithStyles(validSelectors);
            var mergedStyles       = MergeStyleClasses(elementsWithStyles);

            StyleClassApplier.ApplyAllStyles(mergedStyles);

            if (_stripIdAndClassAttributes)
            {
                StripElementAttributes("id", "class");
            }

            if (removeComments)
            {
                var comments = _document.Descendents <IComment>().ToList();

                foreach (var comment in comments)
                {
                    comment.Remove();
                }
            }

            var html = _document.ToHtml(new AutoSelectedMarkupFormatter(_document.Doctype));

            return(new InlineResult(html, _warnings));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Function to precompile a style sheet allowing it be be preserved in
        /// memory for performance purposes.
        /// </summary>
        /// <param name="css">A string containing a style-sheet for inlining.</param>
        /// <param name="warnings">A list of warnings indicating css elements that are not supported.</param>
        /// <returns>A tokenized sorted list representing the css that was passed in</returns>
        public static SortedList <string, StyleClass> PrecompileCssString(string css, out List <string> warnings)
        {
            var cssSource    = new List <ICssSource>(PreMailer.ConvertToStyleSources(css));
            var cssBlocks    = PreMailer.GetCssBlocks(cssSource);
            var joinedBlocks = PreMailer.Join(cssBlocks);

            warnings = new List <string>();
            var cleanJoinedBlocks = PreMailer.CleanUnsupportedSelectors(joinedBlocks, warnings);

            return(cleanJoinedBlocks);
        }