+<!--
+ Nokogiri's pretty-printer is a bit weird. Regardless of the indentation
+ setting, if an element has no child text nodes then it will be pretty-
+ printed. This works by adding arbitrary whitespace to that element, and
+ then all of its children are eligible to be pretty-printed.
+
+ If an element has any text nodes at all, then it is not pretty-printed and
+ neither are any of its descendents.
+
+ Adding arbitrary whitespace to <pre> is bad, so we inject zero-width non-
+ breaking spaces to prevent this. This will render fine but the spaces
+ should be removed before final output to avoid problems with copy+paste.
+-->
+<xsl:template match='xhtml:pre'>
+ <xsl:copy>
+ <xsl:apply-templates select='node()|@*' />
+ <xsl:text>⁠</xsl:text>
+ </xsl:copy>
+</xsl:template>
+
+<!--
+ Likewise, adding spaces between consecutive span-level elements where
+ none existed before won't go over well.
+-->
+<xsl:template name='glue-preceding-span'>
+ <xsl:if test='f:element-is-span(preceding-sibling::node()[1])'>
+ <xsl:text>⁠</xsl:text>
+ </xsl:if>
+</xsl:template>
+
+<xsl:template match='*[f:element-is-span()]'>
+ <xsl:call-template name='glue-preceding-span' />
+ <xsl:copy>
+ <xsl:apply-templates select='node()|@*' />
+ <xsl:text>⁠</xsl:text> <!-- avoid breaking within a span element -->
+ </xsl:copy>
+</xsl:template>
+
+<!--
+ Manually strip whitespace-only text nodes so the pretty printer can do its
+ thing on remaining elements.
+-->
+<xsl:template match='text()[normalize-space(.) = ""]'>
+ <xsl:choose>
+ <!-- preserve anything according to xml:space -->
+ <xsl:when test='ancestor::*[@xml:space][1][@xml:space="preserve"]'>
+ <xsl:copy />
+ </xsl:when>
+ <!-- preserve anything under <pre> -->
+ <xsl:when test='ancestor::xhtml:pre'><xsl:copy /></xsl:when>
+ </xsl:choose>
+</xsl:template>
+