Skip to content

Commit

Permalink
css:label-targets: support percent encoding in fragments
Browse files Browse the repository at this point in the history
see #24
  • Loading branch information
bertfrees committed Sep 15, 2020
1 parent 7c1ad3e commit f7cf467
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
15 changes: 9 additions & 6 deletions braille/css-utils/src/main/resources/xml/label-targets.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
version="2.0">

<xsl:include href="http://www.daisy.org/pipeline/modules/common-utils/generate-id.xsl"/>
<xsl:include href="http://www.daisy.org/pipeline/modules/file-utils/library.xsl"/>

<xsl:key name="url-1" match="*[@id]" use="resolve-uri(concat('#',@id),base-uri(.))"/>
<xsl:key name="url-2" match="*[@xml:id]" use="resolve-uri(concat('#',@xml:id),base-uri(.))"/>
<xsl:key name="url-1" match="*[@id]" use="pf:normalize-uri(resolve-uri(concat('#',encode-for-uri(@id)),base-uri(.)))"/>
<xsl:key name="url-2" match="*[@xml:id]" use="pf:normalize-uri(resolve-uri(concat('#',@xml:id),base-uri(.)))"/>

<xsl:function name="f:get-target" as="element()?">
<xsl:param name="target" as="xs:string"/> <!-- resolved and normalized URI -->
Expand All @@ -28,6 +29,7 @@
<xsl:variable name="target" as="xs:string" select="@target"/>
<xsl:variable name="target" as="xs:string" select="if (contains($target,'#')) then $target else concat('#',$target)"/>
<xsl:variable name="target" as="xs:anyURI" select="resolve-uri($target,base-uri(.))"/>
<xsl:variable name="target" as="xs:string" select="pf:normalize-uri($target)"/>
<xsl:sequence select="$target"/>
</xsl:for-each>
</xsl:variable>
Expand All @@ -54,8 +56,9 @@
</xsl:template>

<xsl:template match="*[@xml:id or @id][not(@css:id)]">
<xsl:variable name="uri" as="xs:anyURI*"
select="for $id in (@xml:id|@id) return resolve-uri(concat('#',$id),base-uri(.))"/>
<xsl:variable name="uri" as="xs:string*"
select="(@id/pf:normalize-uri(resolve-uri(concat('#',encode-for-uri(.)),base-uri(..))),
@xml:id/pf:normalize-uri(resolve-uri(concat('#',.),base-uri(..))))"/>
<xsl:choose>
<xsl:when test="$referenced-elements[.=$uri and f:get-target(.) is current()]">
<xsl:copy>
Expand All @@ -77,8 +80,8 @@
css:string[@name][@target]|
css:counter[@target]|
css:content[@target]">
<xsl:variable name="uri" as="xs:anyURI"
select="resolve-uri(if (contains(@target,'#')) then @target else concat('#',@target),base-uri(.))"/>
<xsl:variable name="uri" as="xs:string"
select="pf:normalize-uri(resolve-uri(if (contains(@target,'#')) then @target else concat('#',@target),base-uri(.)))"/>
<xsl:variable name="target" as="element()?" select="f:get-target($uri)"/>
<xsl:choose>
<xsl:when test="exists($target)">
Expand Down
32 changes: 32 additions & 0 deletions braille/css-utils/src/test/xprocspec/test_label-targets.xprocspec
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,36 @@
</x:expect>
</x:scenario>

<x:scenario label="test_03">
<x:call step="css:label-targets">
<x:input port="source">
<x:document type="inline">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<a class="pageref" href="#%23x">
<css:counter target="#%23x" name="page"/>
</a>
<a class="page" id="#x"/>
</body>
</html>
</x:document>
</x:input>
</x:call>
<x:context label="result">
<x:document type="port" port="result"/>
</x:context>
<x:expect label="result" type="compare">
<x:document type="inline">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<a class="pageref" href="#%23x">
<css:counter target="target_1" original-target="#%23x" name="page"/>
</a>
<a class="page" id="#x" css:id="target_1"/>
</body>
</html>
</x:document>
</x:expect>
</x:scenario>

</x:description>

0 comments on commit f7cf467

Please sign in to comment.