Example #1
0
        public void Inline_TableStyles_Inlined()
        {
            var table = Html.Element("table").@class("foo");

            var tbody = Html.Element("tbody");
            var tr    = Html.Element("tr");

            MarkupElementExtensions.Element(tr, "td", td => td.@class("bar foo").Append("baz1"));
            MarkupElementExtensions.Element(tr, "td", td => td.@class("bar").Append("baz2"));

            table.Add(tbody);
            tbody.Add(tr);

            var cssRules = new[]
            {
                new CssRuleSet {
                    Selector = ".foo", Declarations = "font-family: sans-serif;"
                },
                new CssRuleSet {
                    Selector = ".bar", Declarations = "font-family: consolas;"
                }
            };

            table = new CssInliner().Inline(cssRules, table);

            var result = table.ToHtml(Formatting);

            Assert.AreEqual(ResourceReader <CssInlinerTest> .FindString(name => name.Contains("CssInliner_Inline_TableStyles")).Trim(), result.Trim());
        }
Example #2
0
        public void Inline_TableStyles_Inlined()
        {
            var table = Html.Element("table").@class("foo");

            var tbody = Html.Element("tbody");
            var tr    = Html.Element("tr");

            tr.Element("td", td => td.@class("bar foo").Append("baz1"));
            tr.Element("td", td => td.@class("bar").Append("baz2"));

            table.Add(tbody);
            tbody.Add(tr);

            var cssRules = new[]
            {
                new CssRuleSet {
                    Selector = ".foo", Declarations = "font-family: sans-serif;"
                },
                new CssRuleSet {
                    Selector = ".bar", Declarations = "font-family: consolas;"
                }
            };

            table = new CssInliner().Inline(cssRules, table);

            var result = table.ToHtml(Formatting);

            Assert.AreEqual(Helper.ResourceProvider.ReadTextFile("CssInliner_Inline_TableStyles.html").Trim(), result.Trim());
        }
Example #3
0
 public RazorEmailRenderingService(
     IRazorViewEngine viewEngine,
     IServiceScope serviceScope,
     ITempDataProvider tempDataProvider,
     Func <IViewLocalizer> viewLocalizerFactory,
     CssInliner cssInliner)
 {
     this.viewEngine           = viewEngine;
     this.serviceScope         = serviceScope;
     this.tempDataProvider     = tempDataProvider;
     this.viewLocalizerFactory = viewLocalizerFactory;
     this.cssInliner           = cssInliner;
 }
Example #4
0
    public async Task <string> RenderAsync(object model, CultureInfo culture, Func <ActionContext, IView> findView, Action <ViewContext>?beforeRender = null)
    {
        var currentCulture = (CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
        var httpContext    = new DefaultHttpContext {
            RequestServices = serviceScope.ServiceProvider
        };
        var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
        var view          = findView(actionContext);

        var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
        {
            Model = model
        };

        try
        {
            //the razor renderer takes the culture from the current thread culture.
            CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = culture;

            await using var writer = new StringWriter();
            var viewContext = new ViewContext(
                actionContext,
                view,
                viewDictionary,
                new TempDataDictionary(actionContext.HttpContext, tempDataProvider),
                writer,
                new HtmlHelperOptions())
            {
                Html5DateRenderingMode = Html5DateRenderingMode.CurrentCulture
            };

            beforeRender?.Invoke(viewContext);

            await view.RenderAsync(viewContext);

            var compiled = writer.ToString();
            var body     = CssInliner.Inline(compiled);

            return(body);
        }
        finally
        {
            (CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture) = currentCulture;
        }
    }
Example #5
0
        public void Inline_SpanStyles_Inlined()
        {
            var html = Html
                       .Element("p", p => p
                                .Append("foo ")
                                .Element("span", span => span
                                         .Attribute("class", "qux")
                                         .Append("bar"))
                                .Append(" baz"));

            Assert.AreEqual(@"<p>foo <span class=""qux"">bar</span> baz</p>", html.ToHtml(HtmlFormatting.Empty));

            var cssRules = new[] { new CssRuleSet {
                                       Selector = ".qux", Declarations = "font-family: sans-serif;"
                                   } };

            html = new CssInliner().Inline(cssRules, html);

            Assert.AreEqual(@"<p>foo <span class=""qux"" style=""font-family: sans-serif;"">bar</span> baz</p>", html.ToHtml(HtmlFormatting.Empty));
        }