Skip to content

Commit

Permalink
[83] Test string values obtained from namespace nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
JLRishe committed Dec 16, 2023
1 parent 6250f52 commit 71ab3ed
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
43 changes: 41 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ describe('xpath', () => {
assert.strictEqual(1, lastChapter.length);
assert.strictEqual("The burrow", lastChapter[0].textContent);

// #135 - issues with context position
const blockQuotes = parseXml(`<html>
<body>
<div>
Expand Down Expand Up @@ -441,6 +442,8 @@ describe('xpath', () => {
});

it('should select and sort namespace nodes properly', () => {
// #83

const doc = parseXml('<book xmlns:b="http://book.com" xmlns="default-book" xmlns:a="http://author.com" xmlns:p="http://publisher"/>');

const namespaces = xpath.parse('/*/namespace::*').select({ node: doc });
Expand Down Expand Up @@ -471,8 +474,6 @@ describe('xpath', () => {
<chapter>Chapter 4</chapter>
</book>`)

const [c1, c2, c3] = allChildEls(doc.documentElement);

assert.equal(
xpath.parse('/*/chapter[last()]/preceding-sibling::*[1]').evaluateString({ node: doc }),
'Chapter 3',
Expand Down Expand Up @@ -618,6 +619,44 @@ describe('xpath', () => {

assert.strictEqual(str, 'e');
});

it('should get string values from namespace nodes', ()=> {
const doc = parseXml('<book xmlns:author="http://author" xmlns="https://book" />');

assert.equal(
xpath.parse('string(/*/namespace::author)').evaluateString({ node: doc }),
'http://author'
);
assert.equal(
xpath.parse('name(/*/namespace::author)').evaluateString({ node: doc }),
'author'
);
assert.equal(
xpath.parse('local-name(/*/namespace::author)').evaluateString({ node: doc }),
'author'
);
assert.equal(
xpath.parse('namespace-uri(/*/namespace::author)').evaluateString({ node: doc }),
''
);

assert.equal(
xpath.parse('string(/*/namespace::*[not(local-name())])').evaluateString({ node: doc }),
'https://book'
);
assert.equal(
xpath.parse('name(/*/namespace::*[not(local-name())])').evaluateString({ node: doc }),
''
);
assert.equal(
xpath.parse('local-name(/*/namespace::*[not(local-name())])').evaluateString({ node: doc }),
''
);
assert.equal(
xpath.parse('namespace-uri(/*/namespace::*[not(local-name())])').evaluateString({ node: doc }),
''
);
});
});

describe('parsed expressions', () => {
Expand Down
8 changes: 5 additions & 3 deletions xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3451,10 +3451,10 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
this.isXPathNamespace = true;
this.baseNode = node;
this.ownerDocument = p.ownerDocument;
this.nodeName = "#namespace";
this.nodeName = pre;
this.prefix = pre;
this.localName = pre;
this.namespaceURI = uri;
this.namespaceURI = null;
this.nodeValue = uri;
this.ownerElement = p;
this.nodeType = NodeTypes.NAMESPACE_NODE;
Expand Down Expand Up @@ -3679,17 +3679,19 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
Functions.namespaceURI = function () {
var c = arguments[0];
var n;

if (arguments.length == 1) {
n = c.contextNode;
} else if (arguments.length == 2) {
n = arguments[1].evaluate(c).first();
} else {
throw new Error("Function namespace-uri expects (node-set?)");
}

if (n == null) {
return new XString("");
}
return new XString(n.namespaceURI);
return new XString(n.namespaceURI || '');
};

Functions.name = function () {
Expand Down

0 comments on commit 71ab3ed

Please sign in to comment.