Skip to content

Commit

Permalink
implement replace function (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonliao-cb committed Mar 8, 2024
1 parent 254be3f commit f873a59
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist/
node_modules/

package-lock.json
.history/
3 changes: 2 additions & 1 deletion src/xpath/expressions/function-call-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
formatNumber
} from '../functions';
import { extCardinal, extIf, extJoin } from '../functions/non-standard';
import { lowerCase, upperCase } from '../functions/standard-20';
import { lowerCase, _replace, upperCase } from '../functions/standard-20';
import { BooleanValue } from '../values/boolean-value';
import { Expression } from './expression';

Expand All @@ -60,6 +60,7 @@ export class FunctionCallExpr extends Expression {
last,
'local-name': localName,
'lower-case': lowerCase,
'replace': _replace,
matches,
name: _name,
'namespace-uri': namespaceUri,
Expand Down
10 changes: 10 additions & 0 deletions src/xpath/functions/standard-20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ export function lowerCase(context: ExprContext) {
const str: string = this.args[0].evaluate(context).stringValue();
return new StringValue(str.toLowerCase());
}

export function _replace(context: ExprContext) {
assert(['2.0', '3.0'].includes(context.xsltVersion));
const str: string = this.args[0].evaluate(context).stringValue();
const s1 = this.args[1].evaluate(context).stringValue();
const s2 = this.args[2].evaluate(context).stringValue();

const searchPattern = new RegExp(s1, 'g');
return new StringValue(str.replace(searchPattern, s2));
}
46 changes: 46 additions & 0 deletions tests/xpath/functions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,51 @@ describe('XPath Functions', () => {

assert.equal(outXmlString, 'lily');
});

it('replace simple text', () => {
const xmlString = (
<root></root>
);

const xsltString = (
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:value-of select="replace('Lily','Li','*')" />
</xsl:template>
</xsl:stylesheet>
)

const xsltClass = new Xslt();

const xml = xmlParser.xmlParse(xmlString);
const xslt = xmlParser.xmlParse(xsltString);

const outXmlString = xsltClass.xsltProcess(xml, xslt);

assert.equal(outXmlString, '*ly');
});

it('replace regex text', () => {
const xmlString = (
<root></root>
);

const xsltString = (
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:value-of select="replace('This is some 123 text 456 with 789 numbers.', '[^\d]+', '')" />
</xsl:template>
</xsl:stylesheet>
)

const xsltClass = new Xslt();

const xml = xmlParser.xmlParse(xmlString);
const xslt = xmlParser.xmlParse(xsltString);

const outXmlString = xsltClass.xsltProcess(xml, xslt);

assert.equal(outXmlString, '123456789');
});
});
});

0 comments on commit f873a59

Please sign in to comment.