/// <summary> /// Constructor for the PreMailer class /// </summary> /// <param name="html">The HTML input.</param> /// <param name="parsingMode">(optional) the mode.</param> public PreMailer(string html,HtmlParsingMode parsingMode) { _document = CQ.Create(html, parsingMode); _warnings = new List<string>(); _cssParser = new CssParser(); _cssSelectorParser = new CssSelectorParser(); }
/// <summary> /// Constructor for the PreMailer class /// </summary> /// <param name="html">The HTML input.</param> public PreMailer(string html) { _document = CQ.CreateDocument(html); _warnings = new List<string>(); _cssParser = new CssParser(); _cssSelectorParser = new CssSelectorParser(); }
/// <summary> /// Constructor for the PreMailer class /// </summary> /// <param name="html">The HTML input.</param> /// <param name="baseUri">Url that all relative urls will be off of</param> public PreMailer(string html, Uri baseUri = null) { _baseUri = baseUri; _document = new HtmlParser().Parse(html); _warnings = new List<string>(); _cssParser = new CssParser(); _cssSelectorParser = new CssSelectorParser(); }
private PreMailer(string html, bool removeStyleElements = false, string ignoreElements = null) { _document = CQ.CreateDocument(html); _removeStyleElements = removeStyleElements; _ignoreElements = ignoreElements; _warnings = new List<string>(); _cssParser = new CssParser(); _cssSelectorParser = new CssSelectorParser(); }
/// <summary> /// Moves the CSS embedded in the specified htmlInput to inline style attributes. /// </summary> /// <param name="htmlInput">The HTML input.</param> /// <param name="removeStyleElements">if set to <c>true</c> the style elements are removed.</param> /// <returns>Returns the html input, with styles moved to inline attributes.</returns> public string MoveCssInline(string htmlInput, bool removeStyleElements) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlInput); var styleNodes = doc.DocumentNode.SelectNodes("//style"); if (styleNodes == null) return htmlInput; // no styles to move foreach (var style in styleNodes) { if (style.Attributes["id"] != null && !String.IsNullOrWhiteSpace(style.Attributes["id"].Value) && style.Attributes["id"].Value.Equals("mobile", StringComparison.InvariantCultureIgnoreCase)) { continue; } CssParser cssParser = new CssParser(); string cssBlock = style.InnerHtml; cssParser.AddStyleSheet(cssBlock); foreach (var item in cssParser.Styles) { var styleClass = item.Value; var elements = doc.DocumentNode.QuerySelectorAll(styleClass.Name); foreach (var element in elements) { HtmlAttribute styleAttribute = element.Attributes["style"]; if (styleAttribute == null) { element.Attributes.Add("style", String.Empty); styleAttribute = element.Attributes["style"]; } StyleClass sc = cssParser.ParseStyleClass("dummy", styleAttribute.Value); sc.Merge(styleClass, true); styleAttribute.Value = sc.ToString(); } } if (removeStyleElements) { style.Remove(); } } return doc.DocumentNode.OuterHtml; }
private static SortedList<string, StyleClass> Join(IEnumerable<string> cssBlocks) { var parser = new CssParser(); foreach (var block in cssBlocks) { parser.AddStyleSheet(block); } return parser.Styles; }