/// <summary> /// Convert an flow document inlines into their (combined!) html representation. /// </summary> /// <param name="inlines">Flow document inlines collection.</param> /// <param name="htmlWriter">XmlTextWriter producing resulting html.</param> /// <param name="conversionResult">Conversion result to store error and warning messages. Can be null.</param> /// <param name="inheritedStyle">Inherited inline styles.</param> /// <remarks> /// Supported: Hyperlink, Bold, Underline, Italic as well as Run and InlineUIContainer for Image. /// </remarks> private static void AddInlines(InlineCollection inlines, XmlTextWriter htmlWriter, ValidationResult conversionResult, List <InlineStyles> inheritedStyle) { for (int i = 0; i < inlines.Count; ++i) { Inline inline = inlines.ElementAt(i); #region InheritedStyle + B,I,U processing bool bAddBold = false; bool bAddItalic = false; bool bAddUnderlined = false; // Bold if (inline.FontWeight != System.Windows.FontWeights.Bold && inheritedStyle.Contains(InlineStyles.Bold)) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Bold); } else if (!inheritedStyle.Contains(InlineStyles.Bold) && inline.FontWeight == System.Windows.FontWeights.Bold) { bAddBold = true; } // Italic if (inline.FontStyle != System.Windows.FontStyles.Italic && inheritedStyle.Contains(InlineStyles.Italic)) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Italic); } else if (!inheritedStyle.Contains(InlineStyles.Italic) && inline.FontStyle == System.Windows.FontStyles.Italic) { bAddItalic = true; } // Underline bool bHasUnderline = false; if (inline is Run && inline.Parent is Span) { if ((inline.Parent as Span).TextDecorations.Contains(System.Windows.TextDecorations.Underline[0])) { bHasUnderline = true; } } if (!inline.TextDecorations.Contains(System.Windows.TextDecorations.Underline[0]) && inheritedStyle.Contains(InlineStyles.Underlined)) { if (!bHasUnderline) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Underlined); } } else if (!inheritedStyle.Contains(InlineStyles.Underlined) && inline.TextDecorations.Contains(System.Windows.TextDecorations.Underline[0])) { if (!bHasUnderline) { bAddUnderlined = true; } } if (inline is HtmlHyperlink) { bAddUnderlined = false; } // see what needs to be added.. if multiple elements need to be added, than we have to iterate through // siblings to see what tag to set first (depending on what to close first). if (bAddUnderlined || bAddBold || bAddItalic) { if (bAddBold && bAddItalic || bAddBold && bAddUnderlined || bAddItalic && bAddUnderlined) { InlineStylesCount stylesCount = InlineStylesCounter.Count(inlines, i + 1); // see what has to be added first // add tags for (int z = 0; z < 3; ++z) { if (stylesCount.Order[z] == InlineStyles.Bold && bAddBold) { htmlWriter.WriteStartElement("b"); inheritedStyle.Add(InlineStyles.Bold); } else if (stylesCount.Order[z] == InlineStyles.Italic && bAddItalic) { htmlWriter.WriteStartElement("i"); inheritedStyle.Add(InlineStyles.Italic); } else if (stylesCount.Order[z] == InlineStyles.Underlined && bAddUnderlined) { htmlWriter.WriteStartElement("u"); inheritedStyle.Add(InlineStyles.Underlined); } } } else { // just add the tag of the needed style if (bAddBold) { htmlWriter.WriteStartElement("b"); inheritedStyle.Add(InlineStyles.Bold); } else if (bAddItalic) { htmlWriter.WriteStartElement("i"); inheritedStyle.Add(InlineStyles.Italic); } else if (bAddUnderlined) { htmlWriter.WriteStartElement("u"); inheritedStyle.Add(InlineStyles.Underlined); } } } #endregion // continue with text or inlines if (inline is InlineUIContainer) { InlineUIContainer container = inline as InlineUIContainer; if (container.Child is HtmlImage) { // Add image AddImage(container.Child as HtmlImage, htmlWriter, conversionResult); } else if (container.Child is System.Windows.Controls.TextBlock) { // Comment, just add text as is string text = ExtendedHtmlUtility.HtmlEntityEncode((container.Child as System.Windows.Controls.TextBlock).Text); htmlWriter.WriteRaw(text); } else { // not supported if (conversionResult != null) { if (container.Child != null) { conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, "AddInline: Unknown inline ui container child: " + container.Child.ToString())); } //else // conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, // "AddInline: Inline ui container child is null: " + container.ToString())); } } } else if (inline is HtmlHyperlink) { // Add hyperlink AddHyperlink(inline as HtmlHyperlink, htmlWriter, conversionResult, inheritedStyle); } else if (inline is Span) { Span span = inline as Span; AddInlines(span.Inlines, htmlWriter, conversionResult, inheritedStyle); } else if (inline is Run) { string text = ExtendedHtmlUtility.HtmlEntityEncode((inline as Run).Text); //string text = HtmlEncode((inline as Run).Text); ; htmlWriter.WriteRaw(text); } else { // not supported if (conversionResult != null) { conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, "AddInline: Unknown inline element: " + inline.ToString())); } } } }