Ejemplo n.º 1
0
        public static void AddStyle(string css, bool inline)
        {
            if (string.IsNullOrWhiteSpace(css))
            {
                return;
            }

            if (inline)
            {
                InlineStyles.Add(css);
            }
            else
            {
                Styles.Add(css);
            }
        }
Ejemplo n.º 2
0
        public void ConstructorAllowsSettingInlineStyles()
        {
            var stylesDic = new Dictionary <string, string>
            {
                { "huge", "font-size: 6em" }
            };
            var qdc = new XmlConverter(_hugeOps,
                                       new XmlConverterOptions
            {
                InlineStyles = new InlineStyles
                {
                    Size = InlineStyles.MakeLookup(stylesDic)
                }
            });
            var xml = qdc.Convert().OuterXml;

            xml.Should().Contain("<span style=\"font-size: 6em\">huge</span>");
        }
Ejemplo n.º 3
0
        public void GetCssStylesReturnsNothingWhereNoEntryMapped()
        {
            var op = new DeltaInsertOp("f",
                                       new OpAttributes {
                Size = "biggest"
            });
            var styleDic = new Dictionary <string, string>()
            {
                { "small", "font-size: 0.75em" }
            };
            var c = new OpToXmlConverter(op, new OpToXmlConverterOptions {
                InlineStyles = new InlineStyles()
                {
                    Size = InlineStyles.MakeLookup(styleDic)
                }
            });

            c.GetCssStyles().Should().BeEquivalentTo(new string[] { });
        }
Ejemplo n.º 4
0
        private static string BuildInlineStyles()
        {
            if (!InlineStyles.Any())
            {
                return(string.Empty);
            }

            StringBuilder sb = new StringBuilder();

            //sb.Append($@"<style type=""text/css"">");

            foreach (var css in InlineStyles)
            {
                sb.Append($@"{css}");
            }

            //sb.Append($@"</style>");

            return(sb.ToString());
        }
Ejemplo n.º 5
0
        public void GetCssStylesCustomStyle()
        {
            var op = new DeltaInsertOp("f",
                                       new OpAttributes {
                Size = "huge"
            });
            var styleDic = new Dictionary <string, string>()
            {
                { "huge", "font-size: 6em" }
            };
            var c = new OpToXmlConverter(op, new OpToXmlConverterOptions()
            {
                InlineStyles = new InlineStyles()
                {
                    Size = (value, dop) => InlineStyles.LookupValue(styleDic, value)
                }
            });

            c.GetCssStyles().Should().BeEquivalentTo(new string[] {
                "font-size: 6em"
            });
        }
Ejemplo n.º 6
0
        public void GetCssStylesUsesDefaultsWhereUnspecified()
        {
            // Here there's no inlineStyle specified for "size", but we still render it
            // because we fall back to the default.
            var op = new DeltaInsertOp("f",
                                       new OpAttributes {
                Size = "huge"
            });
            var styleDic = new Dictionary <string, string>()
            {
                { "serif", "font-family: serif" }
            };
            var c = new OpToXmlConverter(op, new OpToXmlConverterOptions()
            {
                InlineStyles = new InlineStyles()
                {
                    Font = (value, dop) => InlineStyles.LookupValue(styleDic, value)
                }
            });

            c.GetCssStyles().Should().BeEquivalentTo(new string[] {
                "font-size: 2.5em"
            });
        }
Ejemplo n.º 7
0
 internal string GetStyle(string key)
 {
     return(InlineStyles.GetValue(key));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Add <div></div> to the list of child elements
 /// </summary>
 /// <param name="elementCfg">Element configuration</param>
 /// <param name="styles">Element styles</param>
 public void AddDiv(Action <HtmlDiv> elementCfg, InlineStyles styles)
 => this.AddChild <HtmlDiv, InlineStyles>(elementCfg, styles);
Ejemplo n.º 9
0
 /// <summary>
 /// Add <h></h> to the list of child elements.
 /// </summary>
 /// <param name="elementCfg">Element configuration</param>
 /// <param name="styles">Element styles</param>
 /// <remarks>Default indent level is 1.</remarks>
 public void AddHeader(Action <HtmlHeader> elementCfg, InlineStyles styles)
 => this.AddChild <HtmlHeader, InlineStyles>(elementCfg, styles);
Ejemplo n.º 10
0
 /// <summary>
 /// Add <p></p> to the list of child elements
 /// </summary>
 /// <param name="elementCfg">Element configuration</param>
 /// <param name="styles">Element styles</param>
 public void AddParagraph(Action <HtmlParagraph> elementCfg, InlineStyles styles)
 => this.AddChild <HtmlParagraph, InlineStyles>(elementCfg, styles);
Ejemplo n.º 11
0
 /// <summary>
 /// Add <table></table> to the list of child elements
 /// </summary>
 /// <param name="elementCfg">Element configuration</param>
 /// <param name="styles">Element styles</param>
 public void AddTable(Action <HtmlTable> elementCfg, InlineStyles styles)
 => this.AddChild <HtmlTable, InlineStyles>(elementCfg, styles);
Ejemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HtmlContainerElement"/> class.
 /// </summary>
 /// <param name="styles">Inline styles</param>
 public HtmlContainerElement(InlineStyles styles)
     : base(styles)
 {
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Build simple html document
        /// </summary>
        /// <returns>String containing HTML document content</returns>
        internal static string Build()
        {
            // Define Styles

            // Define header style
            InlineStyles h1Style = new InlineStyles();

            h1Style.AddStyle("color", "red");
            h1Style.AddStyle("text-decoration", "underline");

            // Create merged style
            InlineStyles h2Style = new InlineStyles();

            h2Style.AddStyle("font-size", "5px");
            h2Style.MergeWith(h1Style, false);

            // Table style configuration
            Action <InlineStyles> tableStyleCfg = (stylesCfg) =>
            {
                stylesCfg.AddStyle("background", "blue");
            };

            // Row style configuration
            Action <InlineStyles> rowStyleCfg = (stylesCfg) =>
            {
                stylesCfg.AddStyle("color", "green");
            };

            // Cell (merged) style configuration
            Action <InlineStyles> cellStyleCfg = tableStyleCfg + rowStyleCfg;

            // Build HTML

            // Create root element
            HtmlDocument document = new HtmlDocument();

            // Add body to the document
            document.AddBody(body =>
            {
                // Add header with Hello world text
                body.AddHeader(header =>
                {
                    header.AddContent("Hello world");
                }, h1Style);

                // Add table
                body.AddTable(table =>
                {
                    table.AddBody(tbody =>
                    {
                        // Add 10 rows
                        for (int i = 0; i < 10; i++)
                        {
                            tbody.AddRow(trow =>
                            {
                                trow.AddCell(tcell =>
                                {
                                    tcell.AddContent($"Column in row {i}");
                                }, cellStyleCfg);
                            }, rowStyleCfg);
                        }
                    }, tableStyleCfg);
                },
                              styles =>
                {
                    styles.AddStyle("background", "yellow");
                });
            }, (InlineStyles)null);

            return(document.ToString());

/* --------------------------------------
*  RESULT:
*  <html>
*  <body>
*   <h1 style="color: red; text-decoration: underline; ">Hello world</h1>
*   <table style="background: yellow; ">
*       <tbody style="background: blue; ">
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 0</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 1</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 2</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 3</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 4</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 5</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 6</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 7</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 8</td>
*           </tr>
*           <tr style="color: green; ">
*               <td style="background: blue; color: green; ">Column in row 9</td>
*           </tr>
*       </tbody>
*   </table>
*  </body>
*  </html>
*  -------------------------------------- */
        }
Ejemplo n.º 14
0
 // This works a little bit differently then the TagBuilder.MergeAttribute() method
 // This version does not throw on null or whitespace key and removes the attribute if value is null
 public void MergeStyle(string key, string value)
 {
     InlineStyles.Merge(key, value);
 }
Ejemplo n.º 15
0
 public void MergeStyles(object attributes)
 {
     InlineStyles.Merge(attributes);
 }
Ejemplo n.º 16
0
 // This works a little bit differently then the TagBuilder.MergeAttribute() method
 // This version does not throw on null or whitespace key and removes the attribute if value is null
 internal TThis MergeStyle(string key, string value, bool replaceExisting = true)
 {
     InlineStyles.Merge(key, value, replaceExisting);
     return(this.GetThis());
 }
Ejemplo n.º 17
0
 internal TThis MergeStyles <TKey, TValue>(IDictionary <TKey, TValue> attributes, bool replaceExisting = true)
 {
     InlineStyles.Merge(attributes, replaceExisting);
     return(this.GetThis());
 }
Ejemplo n.º 18
0
 internal TThis MergeStyles(object attributes, bool replaceExisting = true)
 {
     InlineStyles.Merge(attributes, replaceExisting);
     return(this.GetThis());
 }
Ejemplo n.º 19
0
 public void MergeStyles <TKey, TValue>(IDictionary <TKey, TValue> attributes)
 {
     InlineStyles.Merge(attributes);
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HtmlElement"/> class.
 /// </summary>
 /// <param name="styles">Inline styles</param>
 public HtmlElement(InlineStyles styles)
 {
     this.Children = new List <HtmlElement>();
     this.Styles   = styles;
 }
Ejemplo n.º 21
0
 public string GetStyle(string key)
 {
     return(InlineStyles.GetValue(key));
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Add <span></span> to the list of child elements
 /// </summary>
 /// <param name="elementCfg">Element configuration</param>
 /// <param name="styles">Element styles</param>
 public void AddSpan(Action <HtmlSpan> elementCfg, InlineStyles styles)
 => this.AddChild <HtmlSpan, InlineStyles>(elementCfg, styles);