-
Notifications
You must be signed in to change notification settings - Fork 10
/
RichTextBlockHtml2Xaml.xslt
84 lines (72 loc) · 3.67 KB
/
RichTextBlockHtml2Xaml.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<!-- The html root element must be div, it translates to a xaml richtextblock.-->
<xsl:template match="/div" priority="9">
<RichTextBlock>
<RichTextBlock.Resources>
<Style x:Key="Bullet" TargetType="Ellipse">
<Setter Property="Fill" Value="Black" />
<Setter Property="Width" Value="6" />
<Setter Property="Height" Value="6" />
<Setter Property="Margin" Value="-30,0,0,1" />
</Style>
<Style x:Key="Link" TargetType="HyperlinkButton">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FontSize" Value="11" />
<Setter Property="Margin" Value="-15,-11" />
</Style>
</RichTextBlock.Resources>
<xsl:if test="normalize-space(text()) != ''">
<Paragraph><xsl:value-of select="normalize-space(text())" /></Paragraph>
</xsl:if>
<xsl:apply-templates select="/div/*" />
</RichTextBlock>
</xsl:template>
<xsl:template match="div" priority="0">
<Span><xsl:apply-templates /></Span>
</xsl:template>
<!-- XAML Paragraphs cannot contain paragraphs, so we convert top-level html paragraphs to xaml paragraphs and convert nested html paragraphs to xaml spans with linebreaks -->
<xsl:template match="/div/P | /div/p" priority="9">
<Paragraph><xsl:apply-templates /></Paragraph>
</xsl:template>
<xsl:template match="P | p" priority="0">
<Span><LineBreak /><xsl:apply-templates /><LineBreak /></Span>
</xsl:template>
<!-- The RichTextBlock XAML element can contain only paragraph child elements, so any unknown html child elements of the root element will become XAML paragraphs -->
<xsl:template match="/div/*">
<Paragraph><xsl:apply-templates /></Paragraph>
</xsl:template>
<!-- Lists can only occur outside paragraphs, at the top level -->
<xsl:template match="/div/UL | /div/ul"><xsl:apply-templates /></xsl:template>
<xsl:template match="/div/UL/LI | /div/ul/LI | /div/UL/li | /div/ul/li" priority="9" >
<Paragraph Margin="20,0,0,0"><Span><InlineUIContainer><Ellipse Style="{{StaticResource Bullet}}"/></InlineUIContainer><xsl:apply-templates /><LineBreak /></Span></Paragraph>
</xsl:template>
<!-- An UL can only contain LI, so ignore all other elements within an UL -->
<xsl:template match="/div/UL/* | /div/ul/*" priority="8" />
<xsl:template match="B | b | STRONG | strong">
<Bold><xsl:apply-templates /></Bold>
</xsl:template>
<xsl:template match="I | i">
<Italic><xsl:apply-templates /></Italic>
</xsl:template>
<xsl:template match="U | u">
<Underline><xsl:apply-templates /></Underline>
</xsl:template>
<xsl:template match="BR | br" priority="0" >
<LineBreak />
</xsl:template>
<xsl:template match="A | a">
<Span><InlineUIContainer><HyperlinkButton Style="{{StaticResource Link}}"><xsl:attribute name="NavigateUri"><xsl:value-of select="@href"/></xsl:attribute><xsl:apply-templates /></HyperlinkButton></InlineUIContainer></Span>
</xsl:template>
<xsl:template match="IMG | img">
<Span><InlineUIContainer><Image Stretch="None" ><xsl:attribute name="Source"><xsl:value-of select="@src"/></xsl:attribute><xsl:apply-templates /></Image></InlineUIContainer></Span>
</xsl:template>
<!-- Note that by default, the text content of any unmatched HTML elements will be copied in the XAML. -->
</xsl:stylesheet>