Skip to content

Commit 76c6187

Browse files
author
Sanders Kleinfeld
committed
Utility template calculate-output-href for calculating href output values for <a> elements.
1 parent fd2b7db commit 76c6187

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

htmlbook-xsl/xrefgen.xsl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,51 @@
498498
</xsl:choose>
499499
</xsl:template>
500500

501+
<!-- Utility template for processing @href attributes on <a> elements -->
502+
<!-- For XREFs, grab either the text content after the last # sign, or all the content if there is no # sign -->
503+
<!-- For non-XREFs, don't touch at all -->
504+
<xsl:template name="calculate-output-href">
505+
<xsl:param name="source-href-value" select="@href"/>
506+
<xsl:param name="href-is-xref"/>
507+
508+
<xsl:variable name="is-xref">
509+
<xsl:choose>
510+
<xsl:when test="($href-is-xref = 0) or ($href-is-xref = 1)">
511+
<xsl:value-of select="$href-is-xref"/>
512+
</xsl:when>
513+
<xsl:otherwise>
514+
<xsl:call-template name="href-is-xref">
515+
<xsl:with-param name="href-value" select="$source-href-value"/>
516+
</xsl:call-template>
517+
</xsl:otherwise>
518+
</xsl:choose>
519+
</xsl:variable>
520+
521+
<xsl:choose>
522+
<xsl:when test="$is-xref = 1">
523+
<xsl:choose>
524+
<!-- If there is more than one # sign in content, recursively call template to get content after first # -->
525+
<xsl:when test="contains(substring-after($source-href-value, '#'), '#')">
526+
<xsl:call-template name="calculate-output-href">
527+
<xsl:with-param name="source-href-value" select="substring-after($source-href-value, '#')"/>
528+
<xsl:with-param name="href-is-xref" select="1"/>
529+
</xsl:call-template>
530+
</xsl:when>
531+
<!-- If there is a # sign in content, grab the # and all content thereafter -->
532+
<xsl:when test="contains($source-href-value, '#')">
533+
<xsl:value-of select="concat('#', substring-after($source-href-value, '#'))"/>
534+
</xsl:when>
535+
<!-- Otherwise, just use all the text as is-->
536+
<xsl:otherwise>
537+
<xsl:value-of select="$source-href-value"/>
538+
</xsl:otherwise>
539+
</xsl:choose>
540+
</xsl:when>
541+
<xsl:otherwise>
542+
<!-- Just use the text as is -->
543+
<xsl:value-of select="$source-href-value"/>
544+
</xsl:otherwise>
545+
</xsl:choose>
546+
</xsl:template>
547+
501548
</xsl:stylesheet>

htmlbook-xsl/xspec/xrefgen.xspec

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,4 +750,47 @@
750750
<x:expect label="It is considered to be an XREF">1</x:expect>
751751
</x:scenario>
752752

753+
<!-- Tests for calculate-output-href for handling of href attributes on <a> elements -->
754+
<x:scenario label="If an href is in the format #id">
755+
<x:call template="calculate-output-href">
756+
<x:param name="source-href-value" select="'#whatever'"/>
757+
</x:call>
758+
<x:expect label="It should be output as is">#whatever</x:expect>
759+
</x:scenario>
760+
761+
<x:scenario label="If an href is in the format filename.html#id">
762+
<x:call template="calculate-output-href">
763+
<x:param name="source-href-value" select="'ch01.html#whatever'"/>
764+
</x:call>
765+
<x:expect label="Output just the id">#whatever</x:expect>
766+
</x:scenario>
767+
768+
<x:scenario label="If an href contains a '://' scheme (http://)">
769+
<x:call template="calculate-output-href">
770+
<x:param name="source-href-value" select="'http://oreilly.com/whatever.html#whatever'"/>
771+
</x:call>
772+
<x:expect label="It should be output as is">http://oreilly.com/whatever.html#whatever</x:expect>
773+
</x:scenario>
774+
775+
<x:scenario label="If an href contains a mailto: scheme">
776+
<x:call template="calculate-output-href">
777+
<x:param name="source-href-value" select="'[email protected]'"/>
778+
</x:call>
779+
<x:expect label="It should be output as is">[email protected]</x:expect>
780+
</x:scenario>
781+
782+
<x:scenario label="If href text does not contain absolute URL or # sign">
783+
<x:call template="calculate-output-href">
784+
<x:param name="source-href-value" select="'ch01.html'"/>
785+
</x:call>
786+
<x:expect label="It should be output as is">ch01.html</x:expect>
787+
</x:scenario>
788+
789+
<x:scenario label="If href text does not contain absolute URL and contains multiple # signs">
790+
<x:call template="calculate-output-href">
791+
<x:param name="source-href-value" select="'why_would_you#do_this#idonotknow'"/>
792+
</x:call>
793+
<x:expect label="Output the content from the *last* # sign to the end">#idonotknow</x:expect>
794+
</x:scenario>
795+
753796
</x:description>

0 commit comments

Comments
 (0)