fontoxpath icon indicating copy to clipboard operation
fontoxpath copied to clipboard

Registering the same custom function twice does not work

Open rrthomas opened this issue 4 years ago • 2 comments

Example code:

import {evaluateXPathToString, registerCustomXPathFunction, Options} from 'fontoxpath'

const foo = 'https://example.org/foo.dtd'


registerCustomXPathFunction(
  {localName: 'foo', namespaceURI: foo},
  ['xs:string'], 'xs:string', (_, arg: string) => {
    return arg
  }
)

registerCustomXPathFunction(
  {localName: 'foo', namespaceURI: foo},
  ['xs:string'], 'xs:string', (_, arg: string) => {
    return `${arg}bar`
  }
)

const URI_BY_PREFIX: {[key: string]: string} = {foo}

const xQueryOptions: Options = {
  namespaceResolver: (prefix: string) => URI_BY_PREFIX[prefix],
}

console.log(evaluateXPathToString('foo:foo("foo")', null, null, null, xQueryOptions))

Expected output: foobar. Actual output: foo. Tested with 3.19.0.

rrthomas avatar Jul 21 '21 10:07 rrthomas

Thanks for reporting @rrthomas! Out of intrest, do you have a use case for "overwriting" an existing function?

Arguably, the 2nd call to registerCustomXPathFunction should have thrown.

devatwork avatar Jul 21 '21 12:07 devatwork

My use was a custom function with a hidden variable that is set in the context. Originally, that variable was a normal TypeScript value, which meant that I wanted to re-register the function, which didn't work. I worked around the limitation by instead making the hidden variable an object property, which I could then update without re-registering the function. So it's not essential to me, no. It would be good if I had got an error rather than the re-registering being silently ignored.

rrthomas avatar Jul 21 '21 12:07 rrthomas