Skip to content

Commit 57f4a8a

Browse files
author
Sanders Kleinfeld
committed
Minor tweaks to xrefgen handling in accordance with specs set forth in test suite.
1 parent 59bfc91 commit 57f4a8a

File tree

2 files changed

+65
-47
lines changed

2 files changed

+65
-47
lines changed

htmlbook-xsl/xrefgen.xsl

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@
1313

1414
<!-- Default rule for TOC generation -->
1515

16-
<!-- ToDo: Add support for separate handling for data-type="link", where it's an internal cross-reference, but you definitely
17-
do not want to override text node -->
18-
1916
<!-- All XREFs must be tagged with a @data-type containing XREF -->
20-
<xsl:template match="h:a[contains(@data-type, 'xref')]">
17+
<xsl:template match="h:a[contains(@data-type, 'xref')]" name="process-as-xref">
2118
<xsl:param name="autogenerate-xrefs" select="$autogenerate-xrefs"/>
2219
<xsl:variable name="calculated-output-href">
2320
<xsl:call-template name="calculate-output-href">
2421
<xsl:with-param name="source-href-value" select="@href"/>
2522
</xsl:call-template>
2623
</xsl:variable>
2724
<xsl:variable name="href-anchor" select="substring-after($calculated-output-href, '#')"/>
25+
<xsl:variable name="is-xref">
26+
<xsl:call-template name="href-is-xref">
27+
<xsl:with-param name="href-value" select="@href"/>
28+
</xsl:call-template>
29+
</xsl:variable>
2830
<xsl:copy>
2931
<xsl:apply-templates select="@*[not(name(.) = 'href')]"/>
3032
<xsl:attribute name="href" select="$calculated-output-href"/>
3133
<xsl:choose>
3234
<!-- Generate XREF text node if $autogenerate-xrefs is enabled -->
33-
<xsl:when test="$autogenerate-xrefs = 1">
35+
<xsl:when test="($autogenerate-xrefs = 1) and ($is-xref = 1)">
3436
<xsl:choose>
3537
<!-- If we can locate the target, process gentext with "xref-to" -->
3638
<xsl:when test="count(key('id', $href-anchor)) > 0">
@@ -61,19 +63,35 @@
6163
</xsl:copy>
6264
</xsl:template>
6365

64-
<!-- href handling for a elements that are not indexterms, xrefs, or footnoterefs -->
66+
<!-- href and content handling for a elements that are not indexterms, xrefs, or footnoterefs -->
6567
<xsl:template match="h:a[not((contains(@data-type, 'xref')) or
6668
(contains(@data-type, 'footnoteref')) or
6769
(contains(@data-type, 'indexterm')))][@href]">
68-
<xsl:copy>
69-
<xsl:apply-templates select="@*[not(name(.) = 'href')]"/>
70-
<xsl:attribute name="href">
71-
<xsl:call-template name="calculate-output-href">
72-
<xsl:with-param name="source-href-value" select="@href"/>
73-
</xsl:call-template>
74-
</xsl:attribute>
75-
<xsl:apply-templates/>
76-
</xsl:copy>
70+
<!-- If the element is empty, does not have data-type="link", and is a valid XREF, go ahead and treat it like an <a> element with data-type="xref" -->
71+
<xsl:variable name="is-xref">
72+
<xsl:call-template name="href-is-xref">
73+
<xsl:with-param name="href-value" select="@href"/>
74+
</xsl:call-template>
75+
</xsl:variable>
76+
<xsl:choose>
77+
<xsl:when test="(not(node())) and
78+
($is-xref = 1) and
79+
not(@data-type='link')">
80+
<xsl:call-template name="process-as-xref"/>
81+
</xsl:when>
82+
<!-- Otherwise just process href and apply-templates for everything else -->
83+
<xsl:otherwise>
84+
<xsl:copy>
85+
<xsl:apply-templates select="@*[not(name(.) = 'href')]"/>
86+
<xsl:attribute name="href">
87+
<xsl:call-template name="calculate-output-href">
88+
<xsl:with-param name="source-href-value" select="@href"/>
89+
</xsl:call-template>
90+
</xsl:attribute>
91+
<xsl:apply-templates/>
92+
</xsl:copy>
93+
</xsl:otherwise>
94+
</xsl:choose>
7795
</xsl:template>
7896

7997
<!-- Adapted from docbook-xsl templates in xhtml/xref.xsl -->
@@ -542,9 +560,9 @@
542560
<xsl:when test="contains($source-href-value, '#')">
543561
<xsl:value-of select="concat('#', substring-after($source-href-value, '#'))"/>
544562
</xsl:when>
545-
<!-- Otherwise, just use all the text as is-->
563+
<!-- Otherwise, just use all the text as is, with a # sign prepended-->
546564
<xsl:otherwise>
547-
<xsl:value-of select="$source-href-value"/>
565+
<xsl:value-of select="concat('#', $source-href-value)"/>
548566
</xsl:otherwise>
549567
</xsl:choose>
550568
</xsl:when>

htmlbook-xsl/xspec/xrefgen.xspec

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -352,42 +352,42 @@
352352

353353
<x:scenario label="Where id doesn't exist (no text node)">
354354
<x:context select="//h:a[@id = 'bogus_no_text_node']"/>
355-
<x:expect label="XSL should resolve to content after # sign">
355+
<x:expect label="href should resolve to # sign and content that follows">
356356
<a id="bogus_no_text_node" data-type="xref" href="#bogus">...</a>
357357
</x:expect>
358358
</x:scenario>
359359

360360
<x:scenario label="Where id doesn't exist (text node)">
361361
<x:context select="//h:a[@id = 'bogus_text_node']"/>
362-
<x:expect label="href should resolve to content after # sign">
362+
<x:expect label="href should resolve to # sign and content that follows">
363363
<a id="bogus_text_node" data-type="xref" href="#bogus">...</a>
364364
</x:expect>
365365
</x:scenario>
366366

367367
<x:scenario label="Where id doesn't exist and no # sign (no text node)">
368368
<x:context select="//h:a[@id = 'random_bogus_no_text_node']"/>
369-
<x:expect label="href should be left untouched">
370-
<a id="random_bogus_no_text_node" data-type="xref" href="random_bogus_text">...</a>
369+
<x:expect label="existing href should be prepended by a # sign">
370+
<a id="random_bogus_no_text_node" data-type="xref" href="#random_bogus_text">...</a>
371371
</x:expect>
372372
</x:scenario>
373373

374374
<x:scenario label="Where id doesn't exist and no # sign (text node)">
375375
<x:context select="//h:a[@id = 'random_bogus_text_node']"/>
376-
<x:expect label="href should be left untouched">
377-
<a id="random_bogus_text_node" data-type="xref" href="random_bogus_text">...</a>
376+
<x:expect label="existing href should be prepended by a # sign">
377+
<a id="random_bogus_text_node" data-type="xref" href="#random_bogus_text">...</a>
378378
</x:expect>
379379
</x:scenario>
380380

381381
<x:scenario label="Where href is a bogus file/id pair (no text node)">
382382
<x:context select="//h:a[@id = 'fileref_bogus_no_text_node']"/>
383-
<x:expect label="href should resolve to content after # sign">
383+
<x:expect label="href should resolve to # sign and content that follows">
384384
<a id="fileref_bogus_no_text_node" data-type="xref" href="#bogus">...</a>
385385
</x:expect>
386386
</x:scenario>
387387

388388
<x:scenario label="Where href is a bogus file/id pair (text node)">
389389
<x:context select="//h:a[@id = 'fileref_bogus_text_node']"/>
390-
<x:expect label="href should resolve to content after # sign">
390+
<x:expect label="href should resolve to # sign and content that follows">
391391
<a id="fileref_bogus_text_node" data-type="xref" href="#bogus">...</a>
392392
</x:expect>
393393
</x:scenario>
@@ -510,7 +510,7 @@
510510
</x:scenario>
511511
</x:scenario>
512512

513-
<x:scenario label="When an XREF element is matched that contains an href pointing to a Web URL (not a valid XREF)">
513+
<x:scenario label="When an 'a' element is matched that contains an href pointing to a Web URL (not a valid XREF)">
514514
<x:context>
515515
<section id="chapter1" data-type="chapter">
516516
<p>Here comes a cross-reference: see <a id="no_text_node" href="http://oreilly.com/whatever.html#chapter1"/></p>
@@ -588,7 +588,7 @@
588588
</x:scenario>
589589
</x:scenario>
590590

591-
<x:scenario label="When an XREF element is matched that contains an href pointing to a bogus id">
591+
<x:scenario label="When an 'a' element is matched that contains an href pointing to a bogus id">
592592
<x:context>
593593
<section id="chapter1" data-type="chapter">
594594
<p>Here comes a bogus cross-reference: see <a id="bogus_no_text_node" href="#bogus"/></p>
@@ -609,84 +609,84 @@
609609

610610
<x:scenario label="Where id doesn't exist (no text node)">
611611
<x:context select="//h:a[@id = 'bogus_no_text_node']"/>
612-
<x:expect label="href should resolve to content after # sign">
612+
<x:expect label="href should resolve to # sign and content that follows">
613613
<a id="bogus_no_text_node" href="#bogus">...</a>
614614
</x:expect>
615615
</x:scenario>
616616

617617
<x:scenario label="Where id doesn't exist (text node)">
618618
<x:context select="//h:a[@id = 'bogus_text_node']"/>
619-
<x:expect label="href should resolve to content after # sign">
619+
<x:expect label="href should resolve to # sign and content that follows">
620620
<a id="bogus_text_node" href="#bogus">...</a>
621621
</x:expect>
622622
</x:scenario>
623623

624624
<x:scenario label="Where id doesn't exist and no # sign (no text node)">
625625
<x:context select="//h:a[@id = 'random_bogus_no_text_node']"/>
626-
<x:expect label="href should be left untouched">
627-
<a id="random_bogus_no_text_node" href="random_bogus_text">...</a>
626+
<x:expect label="href should resolve to # sign and existing href value">
627+
<a id="random_bogus_no_text_node" href="#random_bogus_text">...</a>
628628
</x:expect>
629629
</x:scenario>
630630

631631
<x:scenario label="Where id doesn't exist and no # sign (text node)">
632632
<x:context select="//h:a[@id = 'random_bogus_text_node']"/>
633-
<x:expect label="href should be left untouched">
634-
<a id="random_bogus_text_node" href="random_bogus_text">...</a>
633+
<x:expect label="href should resolve to # sign and existing href value">
634+
<a id="random_bogus_text_node" href="#random_bogus_text">...</a>
635635
</x:expect>
636636
</x:scenario>
637637

638638
<x:scenario label="Where href is a bogus file/id pair (no text node)">
639639
<x:context select="//h:a[@id = 'fileref_bogus_no_text_node']"/>
640-
<x:expect label="href should resolve to content after # sign">
640+
<x:expect label="href should resolve to # sign and content that follows">
641641
<a id="fileref_bogus_no_text_node" href="#bogus">...</a>
642642
</x:expect>
643643
</x:scenario>
644644

645645
<x:scenario label="Where href is a bogus file/id pair (text node)">
646646
<x:context select="//h:a[@id = 'fileref_bogus_text_node']"/>
647-
<x:expect label="href should resolve to content after # sign">
647+
<x:expect label="href should resolve to # sign and content that follows">
648648
<a id="fileref_bogus_text_node" href="#bogus">...</a>
649649
</x:expect>
650650
</x:scenario>
651651

652652
<x:scenario label="Where id doesn't exist (link; no text node)">
653653
<x:context select="//h:a[@id = 'bogus_no_text_node_link']"/>
654-
<x:expect label="href should resolve to content after # sign">
654+
<x:expect label="href should resolve to # sign and content that follows">
655655
<a id="bogus_no_text_node_link" data-type="link" href="#bogus">...</a>
656656
</x:expect>
657657
</x:scenario>
658658

659659
<x:scenario label="Where id doesn't exist (link; text node)">
660660
<x:context select="//h:a[@id = 'bogus_text_node_link']"/>
661-
<x:expect label="href should resolve to content after # sign">
661+
<x:expect label="href should resolve to # sign and content that follows">
662662
<a id="bogus_text_node_link" data-type="link" href="#bogus">...</a>
663663
</x:expect>
664664
</x:scenario>
665665

666666
<x:scenario label="Where id doesn't exist and no # sign (link; no text node)">
667667
<x:context select="//h:a[@id = 'random_bogus_no_text_node_link']"/>
668-
<x:expect label="href should be left untouched">
669-
<a id="random_bogus_no_text_node_link" data-type="link" href="random_bogus_text">...</a>
668+
<x:expect label="href should resolve to # sign and existing href value">
669+
<a id="random_bogus_no_text_node_link" data-type="link" href="#random_bogus_text">...</a>
670670
</x:expect>
671671
</x:scenario>
672672

673673
<x:scenario label="Where id doesn't exist and no # sign (link; text node)">
674674
<x:context select="//h:a[@id = 'random_bogus_text_node_link']"/>
675-
<x:expect label="href should be left untouched">
676-
<a id="random_bogus_text_node_link" data-type="link" href="random_bogus_text">...</a>
675+
<x:expect label="href should resolve to # sign and existing href value">
676+
<a id="random_bogus_text_node_link" data-type="link" href="#random_bogus_text">...</a>
677677
</x:expect>
678678
</x:scenario>
679679

680680
<x:scenario label="Where href is a bogus file/id pair (link; no text node)">
681681
<x:context select="//h:a[@id = 'fileref_bogus_no_text_node_link']"/>
682-
<x:expect label="href should resolve to content after # sign">
682+
<x:expect label="href should resolve to # sign and content that follows">
683683
<a id="fileref_bogus_no_text_node_link" data-type="link" href="#bogus">...</a>
684684
</x:expect>
685685
</x:scenario>
686686

687687
<x:scenario label="Where href is a bogus file/id pair (link; text node)">
688688
<x:context select="//h:a[@id = 'fileref_bogus_text_node_link']"/>
689-
<x:expect label="href should resolve to content after # sign">
689+
<x:expect label="href should resolve to # sign and content that follows">
690690
<a id="fileref_bogus_text_node_link" data-type="link" href="#bogus">...</a>
691691
</x:expect>
692692
</x:scenario>
@@ -774,16 +774,16 @@
774774

775775
<x:scenario label="If an href contains a mailto: scheme">
776776
<x:call template="calculate-output-href">
777-
<x:param name="source-href-value" select="'[email protected]'"/>
777+
<x:param name="source-href-value" select="'mailto:[email protected]'"/>
778778
</x:call>
779-
<x:expect label="It should be output as is">[email protected]</x:expect>
779+
<x:expect label="It should be output as is">mailto:[email protected]</x:expect>
780780
</x:scenario>
781781

782782
<x:scenario label="If href text does not contain absolute URL or # sign">
783783
<x:call template="calculate-output-href">
784-
<x:param name="source-href-value" select="'ch01.html'"/>
784+
<x:param name="source-href-value" select="'random_text'"/>
785785
</x:call>
786-
<x:expect label="It should be output as is">ch01.html</x:expect>
786+
<x:expect label="It should be output as is, prepended by a # sign">#random_text</x:expect>
787787
</x:scenario>
788788

789789
<x:scenario label="If href text does not contain absolute URL and contains multiple # signs">

0 commit comments

Comments
 (0)