Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ed32fde

Browse files
author
Sanders Kleinfeld
committedJun 18, 2014
Merge pull request #148 from oreillymedia/indexterm-whitespace
Whitespace-stripping handling for indexterms
2 parents 0bf9cf6 + 13b629e commit ed32fde

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
 

‎htmlbook-xsl/common.xsl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,4 +680,25 @@
680680
</xsl:choose>
681681
</xsl:template>
682682

683+
<!-- Returns 1 when a text node contains only whitespace (defined as standard space chars or nbsp chars) -->
684+
<!-- Otherwise returns 0 -->
685+
<xsl:template name="whitespace-only-in-text">
686+
<xsl:param name="text.content" select="."/>
687+
<xsl:choose>
688+
<!-- Fine to consider empty text nodes to be whitespace-only; facilitates recursion here -->
689+
<xsl:when test="string-length($text.content) = 0">1</xsl:when>
690+
<xsl:when test="(substring($text.content, 1, 1) = ' ') or
691+
(substring($text.content, 1, 1) = '&#xa0;')">
692+
<!-- If first character is a whitespace char, recurse on rest of string to see if it's all whitespace -->
693+
<xsl:call-template name="whitespace-only-in-text">
694+
<xsl:with-param name="text.content">
695+
<xsl:value-of select="substring($text.content, 2)"/>
696+
</xsl:with-param>
697+
</xsl:call-template>
698+
</xsl:when>
699+
<!-- Otherwise, there's at least 1 text character, so return 0 (false) -->
700+
<xsl:otherwise>0</xsl:otherwise>
701+
</xsl:choose>
702+
</xsl:template>
703+
683704
</xsl:stylesheet>

‎htmlbook-xsl/elements.xsl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@
6666
</xsl:copy>
6767
</xsl:template>
6868

69+
<!-- Special handling for indexterm text nodes when there are no sibling text or element nodes -->
70+
<xsl:template match="h:a[@data-type='indexterm']/text()[not(preceding-sibling::node()) and not(following-sibling::node())]">
71+
<xsl:variable name="whitespace-only-in-indexterm">
72+
<xsl:call-template name="whitespace-only-in-text">
73+
<xsl:with-param name="text.content" select="."/>
74+
</xsl:call-template>
75+
</xsl:variable>
76+
<!-- Only copy over text when it's not whitespace-only -->
77+
<!-- In other words, strip out whitespace-only text nodes in indexterms -->
78+
<xsl:if test="$whitespace-only-in-indexterm != 1">
79+
<xsl:value-of select="."/>
80+
</xsl:if>
81+
</xsl:template>
82+
6983
<xsl:template match="h:section[@data-type]/*[self::h:h1 or self::h:h2 or self::h:h3 or self::h:h4 or self::h:h5 or self::h:h6]|
7084
h:section[@data-type]/h:header/*[self::h:h1 or self::h:h2 or self::h:h3 or self::h:h4 or self::h:h5 or self::h:h6]|
7185
h:div[@data-type = 'part' or @data-type = 'example' or @data-type = 'equation']/*[self::h:h1 or self::h:h2 or self::h:h3 or self::h:h4 or self::h:h5 or self::h:h6]|

‎htmlbook-xsl/xspec/common.xspec

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,4 +1041,74 @@ sect1
10411041

10421042
</x:scenario>
10431043

1044+
<x:scenario label="When calling whitespace-only-in-text on an empty text node">
1045+
<x:call template="whitespace-only-in-text">
1046+
<x:param name="text.content" select="''"/>
1047+
</x:call>
1048+
<x:expect label="It should return 'true'">1</x:expect>
1049+
</x:scenario>
1050+
1051+
<x:scenario label="When calling whitespace-only-in-text on a single space character">
1052+
<x:call template="whitespace-only-in-text">
1053+
<x:param name="text.content" select="' '"/>
1054+
</x:call>
1055+
<x:expect label="It should return 'true'">1</x:expect>
1056+
</x:scenario>
1057+
1058+
<x:scenario label="When calling whitespace-only-in-text on an nbsp character">
1059+
<x:call template="whitespace-only-in-text">
1060+
<x:param name="text.content" select="'&#xa0;'"/>
1061+
</x:call>
1062+
<x:expect label="It should return 'true'">1</x:expect>
1063+
</x:scenario>
1064+
1065+
<x:scenario label="When calling whitespace-only-in-text on a word character">
1066+
<x:call template="whitespace-only-in-text">
1067+
<x:param name="text.content" select="'b'"/>
1068+
</x:call>
1069+
<x:expect label="It should return 'false'">0</x:expect>
1070+
</x:scenario>
1071+
1072+
<x:scenario label="When calling whitespace-only-in-text on a mix of whitespace character">
1073+
<x:call template="whitespace-only-in-text">
1074+
<x:param name="text.content" select="' &#xa0; '"/>
1075+
</x:call>
1076+
<x:expect label="It should return 'true'">1</x:expect>
1077+
</x:scenario>
1078+
1079+
<x:scenario label="When calling whitespace-only-in-text on a mix of whitespace character (2)">
1080+
<x:call template="whitespace-only-in-text">
1081+
<x:param name="text.content" select="' '"/>
1082+
</x:call>
1083+
<x:expect label="It should return 'true'">1</x:expect>
1084+
</x:scenario>
1085+
1086+
<x:scenario label="When calling whitespace-only-in-text on a mix of whitespace character (3)">
1087+
<x:call template="whitespace-only-in-text">
1088+
<x:param name="text.content" select="'&#xa0;&#xa0;&#xa0;'"/>
1089+
</x:call>
1090+
<x:expect label="It should return 'true'">1</x:expect>
1091+
</x:scenario>
1092+
1093+
<x:scenario label="When calling whitespace-only-in-text on a mix of word characters">
1094+
<x:call template="whitespace-only-in-text">
1095+
<x:param name="text.content" select="'chocolatechipcookies'"/>
1096+
</x:call>
1097+
<x:expect label="It should return 'false'">0</x:expect>
1098+
</x:scenario>
1099+
1100+
<x:scenario label="When calling whitespace-only-in-text on a mix of word and whitespace characters">
1101+
<x:call template="whitespace-only-in-text">
1102+
<x:param name="text.content" select="'chocolate chip cookies'"/>
1103+
</x:call>
1104+
<x:expect label="It should return 'false'">0</x:expect>
1105+
</x:scenario>
1106+
1107+
<x:scenario label="When calling whitespace-only-in-text on a mix of word and whitespace characters (2)">
1108+
<x:call template="whitespace-only-in-text">
1109+
<x:param name="text.content" select="'chocolate&#xa0;chip cookies'"/>
1110+
</x:call>
1111+
<x:expect label="It should return 'false'">0</x:expect>
1112+
</x:scenario>
1113+
10441114
</x:description>

‎htmlbook-xsl/xspec/elements.xspec

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ sect5:1
7272
<x:expect label="Preserve the id" test="h:aside[1]/@id = 'watermelon'"/>
7373
</x:scenario>
7474

75+
<!-- Indexterm tests -->
7576
<x:scenario label="When encountering an indexterm without an id">
7677
<x:context>
7778
<a data-type="indexterm" data-primary="rubygems"/>
@@ -88,6 +89,69 @@ sect5:1
8889
<x:expect label="Preserve the id" test="h:a[@data-type='indexterm'][1]/@id = 'avocado'"/>
8990
</x:scenario>
9091

92+
<x:scenario label="When encountering an indexterm that contains only whitespace">
93+
<x:context>
94+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and"> </a>
95+
</x:context>
96+
<x:expect label="Strip the whitespace">
97+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and" id="..."/>
98+
</x:expect>
99+
</x:scenario>
100+
101+
<x:scenario label="When encountering an indexterm that contains only whitespace (including nbsp)">
102+
<x:context>
103+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and">&#xa0; &#xa0;</a>
104+
</x:context>
105+
<x:expect label="Strip the whitespace">
106+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and" id="..."/>
107+
</x:expect>
108+
</x:scenario>
109+
110+
<x:scenario label="When encountering an indexterm that contains text and whitespace">
111+
<x:context>
112+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and">Unicode&#xa0; &#xa0;is fun</a>
113+
</x:context>
114+
<x:expect label="Preserve content as is">
115+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and" id="...">Unicode&#xa0; &#xa0;is fun</a>
116+
</x:expect>
117+
</x:scenario>
118+
119+
<x:scenario label="When encountering an indexterm that contains child nodes only">
120+
<x:context>
121+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and"><em>Unicode</em></a>
122+
</x:context>
123+
<x:expect label="Preserve content as is">
124+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and" id="..."><em>Unicode</em></a>
125+
</x:expect>
126+
</x:scenario>
127+
128+
<x:scenario label="When encountering an indexterm that contains child nodes only (2)">
129+
<x:context>
130+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and"><br/></a>
131+
</x:context>
132+
<x:expect label="Preserve content as is">
133+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and" id="..."><br/></a>
134+
</x:expect>
135+
</x:scenario>
136+
137+
<x:scenario label="When encountering an indexterm that contains child nodes and text">
138+
<x:context>
139+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and"><em>123</em>456</a>
140+
</x:context>
141+
<x:expect label="Preserve content as is">
142+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and" id="..."><em>123</em>456</a>
143+
</x:expect>
144+
</x:scenario>
145+
146+
<x:scenario label="When encountering an indexterm that contains child nodes, text, and whitespace">
147+
<x:context>
148+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and"><em>Unicode</em>&#xa0; &#xa0;is fun</a>
149+
</x:context>
150+
<x:expect label="Preserve content as is">
151+
<a data-type="indexterm" data-primary="xslt" data-secondary="xml and" id="..."><em>Unicode</em>&#xa0; &#xa0;is fun</a>
152+
</x:expect>
153+
</x:scenario>
154+
91155
<!-- PDF bookmark attributes -->
92156
<x:scenario label="When encountering a chapter-level section">
93157
<x:context>

0 commit comments

Comments
 (0)
Please sign in to comment.