Skip to content

Commit 4baffcf

Browse files
author
Sanders Kleinfeld
committed
Revised chunk.xsl handling for gentext nodes for XREFs.
1 parent 57f4a8a commit 4baffcf

File tree

2 files changed

+240
-1
lines changed

2 files changed

+240
-1
lines changed

htmlbook-xsl/chunk.xsl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ sect5:s
322322
<!-- Custom XREF template in chunk.xsl, because we need to take chunk filename into account, and update hrefs. -->
323323
<!-- All XREFs must be tagged with a @data-type containing XREF -->
324324
<xsl:template match="h:a[contains(@data-type, 'xref')]">
325+
<xsl:param name="autogenerate-xrefs" select="$autogenerate-xrefs"/>
325326
<xsl:variable name="href-anchor">
326327
<xsl:choose>
327328
<!-- If href contains an # (as it should), we're going to assume the subsequent text is the referent id -->
@@ -334,6 +335,11 @@ sect5:s
334335
</xsl:otherwise>
335336
</xsl:choose>
336337
</xsl:variable>
338+
<xsl:variable name="is-xref">
339+
<xsl:call-template name="href-is-xref">
340+
<xsl:with-param name="href-value" select="@href"/>
341+
</xsl:call-template>
342+
</xsl:variable>
337343
<xsl:copy>
338344
<xsl:apply-templates select="@*[not(local-name() = 'href')]"/>
339345
<xsl:choose>
@@ -361,7 +367,7 @@ sect5:s
361367
</xsl:choose>
362368
<xsl:choose>
363369
<!-- Generate XREF text node if $autogenerate-xrefs is enabled -->
364-
<xsl:when test="$autogenerate-xrefs = 1">
370+
<xsl:when test="($autogenerate-xrefs = 1) and ($is-xref = 1)">
365371
<xsl:choose>
366372
<!-- If we can locate the target, process gentext with "xref-to" -->
367373
<xsl:when test="count(key('id', $href-anchor)) > 0">

htmlbook-xsl/xspec/chunk.xspec

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec"
4+
xmlns:functx="http://www.functx.com"
5+
xmlns="http://www.w3.org/1999/xhtml"
6+
xmlns:ncx="http://www.daisy.org/z3986/2005/ncx/"
7+
xmlns:h="http://www.w3.org/1999/xhtml"
8+
xmlns:e="http://github.com/oreillymedia/epubrenderer"
9+
stylesheet="../chunk.xsl">
10+
11+
<!-- Tests around text nodes for formal XREF elements (those with data-type='xref') -->
12+
<x:scenario label="When *empty* XREF element is matched">
13+
<x:context select="(//h:section//h:a[@data-type='xref'])[1]">
14+
<section id="chapter1" data-type="chapter">
15+
<p>Here comes a cross-reference: see <a data-type="xref" href="#chapter1"/></p>
16+
</section>
17+
</x:context>
18+
19+
<x:scenario label="And autogenerate-xrefs param is disabled">
20+
<x:context>
21+
<x:param name="autogenerate-xrefs" select="0"/>
22+
</x:context>
23+
<x:expect label="No XREF text node should be generated">
24+
<a data-type="xref" href="..."/>
25+
</x:expect>
26+
</x:scenario>
27+
28+
<x:scenario label="And autogenerate-xrefs param is enabled">
29+
<x:context>
30+
<x:param name="autogenerate-xrefs" select="1"/>
31+
</x:context>
32+
<!-- Content based on params set globally for this spec doc and localization gentext -->
33+
<!-- If localizations change, test may produce false negative -->
34+
<x:expect label="XREF text node should be generated with proper content">
35+
<a data-type="xref" href="...">Chapter 1</a>
36+
</x:expect>
37+
</x:scenario>
38+
</x:scenario>
39+
40+
<x:scenario label="When *nonempty* XREF element is matched">
41+
<x:context select="(//h:section//h:a[@data-type='xref'])[1]">
42+
<section id="chapter1" data-type="chapter">
43+
<p>Here comes a cross-reference: see <a data-type="xref" href="#chapter1">PLACEHOLDER</a></p>
44+
</section>
45+
</x:context>
46+
47+
<x:scenario label="And autogenerate-xrefs param is disabled">
48+
<x:context>
49+
<x:param name="autogenerate-xrefs" select="0"/>
50+
</x:context>
51+
<x:expect label="No XREF text node should be generated">
52+
<a data-type="xref" href="...">PLACEHOLDER</a>
53+
</x:expect>
54+
</x:scenario>
55+
56+
<x:scenario label="And autogenerate-xrefs param is enabled">
57+
<x:context>
58+
<x:param name="autogenerate-xrefs" select="1"/>
59+
</x:context>
60+
<!-- Content based on params set globally for this spec doc and localization gentext -->
61+
<!-- If localizations change, test may produce false negative -->
62+
<x:expect label="XREF text node should be generated with proper content">
63+
<a data-type="xref" href="...">Chapter 1</a>
64+
</x:expect>
65+
</x:scenario>
66+
</x:scenario>
67+
68+
<x:scenario label="When an XREF points to an id in another location">
69+
<x:context>
70+
<section id="chapter1" data-type="chapter">
71+
<p>Here comes an XREF with an href pointing to another file: <a data-type="xref" id="empty_another_file" href="ch01.html#chapter1"/></p>
72+
<p>Another XREF with an href pointing to another file: <a data-type="xref" id="nonempty_another_file" href="ch01.html#chapter1">PLACEHOLDER</a></p>
73+
<p>XREF pointing to a URL <a data-type="xref" id="empty_web_url" href="http://oreilly.com/index.html#chapter1"/></p>
74+
<p>Another XREF with an href pointing to another file: <a data-type="xref" id="nonempty_web_url" href="http://oreilly.com/index.html#chapter1">PLACEHOLDER</a></p>
75+
</section>
76+
</x:context>
77+
78+
<x:scenario label="Which is another local file (text node of XREF empty)">
79+
<x:context select="//h:a[@id='empty_another_file']"/>
80+
<!-- Content based on params set globally for this spec doc and localization gentext -->
81+
<!-- If localizations change, test may produce false negative -->
82+
<x:expect label="XREF text node should be generated with proper content">
83+
<a data-type="xref" id="empty_another_file" href="...">Chapter 1</a>
84+
</x:expect>
85+
</x:scenario>
86+
87+
<x:scenario label="Which is another local file (text node of XREF nonempty)">
88+
<x:context select="//h:a[@id='nonempty_another_file']"/>
89+
<!-- Content based on params set globally for this spec doc and localization gentext -->
90+
<!-- If localizations change, test may produce false negative -->
91+
<x:expect label="XREF text node should be generated with proper content">
92+
<a data-type="xref" id="nonempty_another_file" href="...">Chapter 1</a>
93+
</x:expect>
94+
</x:scenario>
95+
96+
<!-- For now, we're arguing that if the @href value is not a legit XREF,
97+
that should override the fact that data-type="xref" is on the <a> -->
98+
<x:scenario label="Which is a Web URL instead of a valid XREF (text node empty)">
99+
<x:context select="//h:a[@id='empty_web_url']"/>
100+
<x:expect label="XREF text node should be left untouched">
101+
<a data-type="xref" id="empty_web_url" href="..."/>
102+
</x:expect>
103+
</x:scenario>
104+
105+
<x:scenario label="Which is a Web URL instead of a valid XREF (text node nonempty)">
106+
<x:context select="//h:a[@id='nonempty_web_url']"/>
107+
<x:expect label="XREF text node should be left untouched">
108+
<a data-type="xref" id="nonempty_web_url" href="...">PLACEHOLDER</a>
109+
</x:expect>
110+
</x:scenario>
111+
112+
</x:scenario>
113+
114+
115+
<x:scenario label="When an XREF has a bogus href">
116+
<x:context>
117+
<section id="chapter1" data-type="chapter">
118+
<p>Here comes a bogus cross-reference: see <a data-type="xref" id="empty_bogus" href="#bogusbogus"/></p>
119+
<p>Another bogus cross-referece: see <a data-type="xref" id="empty_file_bogus" href="ch01.html#bogus"/></p>
120+
<p>Third bogus cross-reference: see <a data-type="xref" id="nonempty_bogus" href="#bogusbogus">I do believe this is bogus</a></p>
121+
<p>Fourth bogus cross-reference: See <a data-type="xref" id="nonempty_file_bogus" href="ch01.html#bogus">I do believe this is bogus</a></p>
122+
</section>
123+
</x:context>
124+
125+
<x:scenario label="And an empty text node">
126+
<x:context select="//h:a[@id='empty_bogus']"/>
127+
<x:expect label="Three question marks should be used for text node">
128+
<a data-type="xref" id="empty_bogus" href="...">???</a>
129+
</x:expect>
130+
</x:scenario>
131+
132+
<x:scenario label="Referencing another file and has an empty text node">
133+
<x:context select="//h:a[@id='empty_file_bogus']"/>
134+
<x:expect label="Three question marks should be used for text node">
135+
<a data-type="xref" id="empty_file_bogus" href="...">???</a>
136+
</x:expect>
137+
</x:scenario>
138+
139+
<x:scenario label="And a nonempty text node">
140+
<x:context select="//h:a[@id='nonempty_bogus']"/>
141+
<x:expect label="Three question marks should be used for text node">
142+
<a data-type="xref" id="nonempty_bogus" href="...">???</a>
143+
</x:expect>
144+
</x:scenario>
145+
146+
<x:scenario label="Referencing another file and has a nonempty text node">
147+
<x:context select="//h:a[@id='nonempty_file_bogus']"/>
148+
<x:expect label="Three question marks should be used for text node">
149+
<a data-type="xref" id="nonempty_file_bogus" href="...">???</a>
150+
</x:expect>
151+
</x:scenario>
152+
</x:scenario>
153+
154+
<!-- Tests for text nodes of <a> elements that do not have data-type="xref" -->
155+
<x:scenario label="If an 'a' element does not contain data-type='xref'">
156+
<x:context>
157+
<section id="chapter1" data-type="chapter">
158+
<a id="no_data_type" href="#chapter1">DO NOT TOUCH ME</a> <!-- Don't update this one -->
159+
<a id="link" data-type="link" href="#chapter1">DO NOT TOUCH ME</a> <!-- Don't update this one -->
160+
<a id="no_data_type_no_text" href="#chapter1"/> <!-- Update this one -->
161+
<a id="link_no_text" data-type="link" href="#chapter1"/> <!-- Don't update this one -->
162+
<a id="no_data_type_no_text_bogus_href" href="#bogus"/> <!-- Update this one -->
163+
<a id="link_no_text_bogus_href" data-type="link" href="#bogus"/> <!-- Don't update this one -->
164+
<a id="no_data_type_no_text_href_not_xref" href="http://oreilly.com"/> <!-- Don't update this one -->
165+
<a id="link_no_text_href_not_xref" data-type="link" href="http://oreilly.com"/> <!-- Don't update this one -->
166+
</section>
167+
</x:context>
168+
169+
<x:scenario label="And has a text node already">
170+
<x:context select="//h:a[@id='no_data_type']"/>
171+
<x:expect label="Text node should not be modified">
172+
<a id="no_data_type" href="...">DO NOT TOUCH ME</a>
173+
</x:expect>
174+
</x:scenario>
175+
176+
<x:scenario label="And has a text node already (link)">
177+
<x:context select="//h:a[@id='link']"/>
178+
<x:expect label="Text node should not be modified">
179+
<a id="link" data-type="link" href="...">DO NOT TOUCH ME</a>
180+
</x:expect>
181+
</x:scenario>
182+
183+
<x:scenario label="And does not have a text node already, and has a valid XREF link">
184+
<x:context select="//h:a[@id='no_data_type_no_text']"/>
185+
<!-- Content based on params set globally for this spec doc and localization gentext -->
186+
<!-- If localizations change, test may produce false negative -->
187+
<x:expect label="Text node should be updated with proper gentext">
188+
<a id="no_data_type_no_text" href="...">Chapter 1</a>
189+
</x:expect>
190+
</x:scenario>
191+
192+
<x:scenario label="And does not have a text node already, but is a link with a valid XREF link">
193+
<x:context select="//h:a[@id='link_no_text']"/>
194+
<x:expect label="Text node should not be modified">
195+
<a id="link_no_text" data-type="link" href="..."/>
196+
</x:expect>
197+
</x:scenario>
198+
199+
<x:scenario label="And does not have a text node already, and has a bogus XREF link">
200+
<x:context select="//h:a[@id='no_data_type_no_text_bogus_href']"/>
201+
<!-- Content based on params set globally for this spec doc and localization gentext -->
202+
<!-- If localizations change, test may produce false negative -->
203+
<x:expect label="Text node should be updated with question marks">
204+
<a id="no_data_type_no_text_bogus_href" href="...">???</a>
205+
</x:expect>
206+
</x:scenario>
207+
208+
<x:scenario label="And does not have a text node already, and is a link with a bogus XREF link">
209+
<x:context select="//h:a[@id='link_no_text_bogus_href']"/>
210+
<x:expect label="Text node should not be modified">
211+
<a id="link_no_text_bogus_href" data-type="link" href="..."/>
212+
</x:expect>
213+
</x:scenario>
214+
215+
<x:scenario label="And does not have a text node already, and has an href that is not an XREF">
216+
<x:context select="//h:a[@id='no_data_type_no_text_href_not_xref']"/>
217+
<x:expect label="Text node should not be modified">
218+
<a id="no_data_type_no_text_href_not_xref" href="..."/>
219+
</x:expect>
220+
</x:scenario>
221+
222+
<x:scenario label="And does not have a text node already, and is a link with an href that is not an XREF">
223+
<x:context select="//h:a[@id='link_no_text_href_not_xref']"/>
224+
<x:expect label="Text node should not be modified">
225+
<a id="link_no_text_href_not_xref" data-type="link" href="..."/>
226+
</x:expect>
227+
</x:scenario>
228+
</x:scenario>
229+
230+
<!-- Tests for hrefs on a elems with data-type="XREF" -->
231+
232+
233+
</x:description>

0 commit comments

Comments
 (0)