Rewrite EXSLT string:replace to be conformant
authorNick Wellnhofer <wellnhofer@aevum.de>
Tue, 4 Sep 2012 06:38:06 +0000 (14:38 +0800)
committerDaniel Veillard <veillard@redhat.com>
Tue, 4 Sep 2012 06:38:06 +0000 (14:38 +0800)
commit0602c535e9c34efca22dba1b1b7465e0618e7bfc
tree283ce2e4e5da6db29b4494fdfaf7241e52bccbf2
parent39b0c27af8a18e38b95bf50c40767610d8a13906
Rewrite EXSLT string:replace to be conformant

For https://bugzilla.gnome.org/show_bug.cgi?id=569703

The libexslt implementation of str:replace fails to conform to its
specification on several counts:

a) the current version returns a string; it's supposed to
   return a nodeset.

b) the current version treats the replacements as strings;
   it's supposed to treat them as nodes.

c) the current version can modify replacement text; it's
   supposed to only modify text from the original string.

d) the current version ignores the requirement to perform
   substitutions in descending order of search string length.

Steps to reproduce:
a) the returning of a string rather than a nodeset can be seen
   by simply inspecting the code.

b) the code explicity converts replacement nodes to strings;
   this can be seen by inspection.
d) the failure to perform substitutions in descending order
   of search string length can be seen in the lack of any
   sorting in the source code.

c) the problem of modifying text not belonging to the original
   string can be seen in the following stylesheet, which can
   be simply applied to itself to produce output.

<xsl:stylesheet version="1.0"
    extension-element-prefixes="str exsl"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    xmlns:str="http://exslt.org/strings"
    >
<xsl:variable name="Text">
    Price is $1.10
</xsl:variable>

<xsl:template match="/">
    <xsl:apply-templates select="exsl:node-set($Text)/text()"/>
</xsl:template>
<xsl:template match="text()">
   <xsl:variable name="Replace">
        <FromXml>
            <from>$</from>
            <from>\</from>
        </FromXml>
        <ToTex>
            <to>\$</to>
            <to>$\backslash$</to>
        </ToTex>
    </xsl:variable>
    <xsl:value-of

select="str:replace(.,exsl:node-set($Replace)/FromXml/from,exsl:node-set($Replace)/ToTex/to)"/>
</xsl:template>
</xsl:stylesheet>

Actual results:
The output is:

<?xml version="1.0"?>

    Price is $\backslash$$1.10

Expected results:
The output should be:
<?xml version="1.0"?>

    Price is \$1.10

Does this happen every time?
yes.

Other information:
str:replace specification is at:

http://www.exslt.org/str/functions/replace/str.replace.html
libexslt/strings.c
tests/exslt/strings/replace.1.out
tests/exslt/strings/replace.1.xml
tests/exslt/strings/replace.1.xsl