Skip to content

Commit 291c2cc

Browse files
committed
[XPath] Implement fn:generate-id
16185 71.9% successes 5 0.0% skipped 3922 17.4% failures 2402 10.7% errors
1 parent 5961ba2 commit 291c2cc

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/src/xpath/functions/node.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ const fnGenerateId = XPathFunctionDefinition(
176176

177177
XPathSequence _fnGenerateId(XPathContext context, [XmlNode? node]) {
178178
if (node == null) return XPathSequence.emptyString;
179-
// Fall back to UnimplementedError for now, test checks exception type.
180-
throw UnimplementedError('fn:generate-id');
179+
final id = identityHashCode(
180+
node,
181+
).toRadixString(16).toUpperCase().padLeft(8, '0');
182+
return XPathSequence.single('autoId$id');
181183
}
182184

183185
/// https://www.w3.org/TR/xpath-functions-31/#func-root

test/xpath/functions/node_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ void main() {
5656
isXPathSequence(['/r/a']),
5757
);
5858
});
59+
test('fn:generate-id', () {
60+
final ids = document.descendants
61+
.map((node) => fnGenerateId(context, [XPathSequence.single(node)]))
62+
.map((sequence) => sequence.single)
63+
.toList();
64+
expect(ids, unorderedEquals(ids.toSet()));
65+
});
5966
group('integration', () {
6067
final xml = XmlDocument.parse('<r><a>1</a><b>2<c/>3</b></r>');
6168
test('last()', () {

0 commit comments

Comments
 (0)