Skip to content

Commit

Permalink
Add #18; XMLTree.query results always an array
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiebremer committed Feb 28, 2024
1 parent be4cabb commit af0086f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 40 deletions.
23 changes: 14 additions & 9 deletions amd/tsl-core-xml.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,12 @@ declare module "XMLSelector" {
* String with selector terms to parse.
*
* @return
* The XMLSelector instance with the parsed selector terms, or undefined on
* error.
* The XMLSelector instance with the parsed selector terms.
*
* @throws
* SyntaxError, if unexpected patterns in selector terms are found.
*/
static parse(selectorString: string): (XMLSelector | undefined);
static parse(selectorString: string): XMLSelector;
/**
* @param selector
* Selector to match against.
Expand All @@ -462,9 +464,9 @@ declare module "XMLSelector" {
* Matching term to search for.
*
* @return
* List of matching XML tags, or `undefined`.
* List of matching XML tags.
*/
find(nodes: Array<XMLNode>, term: SelectorTerm): (Array<XMLTag> | undefined);
find(nodes: Array<XMLNode>, term: SelectorTerm): Array<XMLTag>;
/**
* Creates a list of XML tags matching the selector conditions. The
* matching is done using depth-first pre-order traversal of the XML nodes.
Expand All @@ -473,9 +475,9 @@ declare module "XMLSelector" {
* Array of nodes to search in.
*
* @return
* List of matching XML tags, or `undefined`.
* List of matching XML tags.
*/
query(nodes: Array<XMLNode>): (Array<XMLTag> | undefined);
query(nodes: Array<XMLNode>): Array<XMLTag>;
}
export default XMLSelector;
}
Expand Down Expand Up @@ -536,9 +538,12 @@ declare module "XMLTree" {
* Selector to match against.
*
* @return
* List of XML nodes matching the selector, or `undefined`.
* List of XML nodes matching the selector.
*
* @throws
* SyntaxError, if unexpected patterns in selector terms are found.
*/
query(selector: string): (Array<XMLTag> | undefined);
query(selector: string): Array<XMLTag>;
/**
* Converts the tree of nodes back to XML text.
*/
Expand Down
35 changes: 17 additions & 18 deletions amd/tsl-core-xml.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/tsl-core-xml.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions lib/XMLTree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ export declare class XMLTree {
* Selector to match against.
*
* @return
* List of XML nodes matching the selector, or `undefined`.
* List of XML nodes matching the selector.
*
* @throws
* SyntaxError, if unexpected patterns in selector terms are found.
*/
query(selector: string): (Array<XMLTag> | undefined);
query(selector: string): Array<XMLTag>;
/**
* Converts the tree of nodes back to XML text.
*/
Expand Down
9 changes: 5 additions & 4 deletions lib/XMLTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ export class XMLTree {
* Selector to match against.
*
* @return
* List of XML nodes matching the selector, or `undefined`.
* List of XML nodes matching the selector.
*
* @throws
* SyntaxError, if unexpected patterns in selector terms are found.
*/
query(selector) {
const xmlSelector = XMLSelector.parse(selector);
if (xmlSelector) {
return xmlSelector.query(this.roots);
}
return xmlSelector.query(this.roots);
}
/**
* Converts the tree of nodes back to XML text.
Expand Down
11 changes: 6 additions & 5 deletions src/XMLTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,17 @@ export class XMLTree {
* Selector to match against.
*
* @return
* List of XML nodes matching the selector, or `undefined`.
* List of XML nodes matching the selector.
*
* @throws
* SyntaxError, if unexpected patterns in selector terms are found.
*/
public query (
selector: string
): ( Array<XMLTag> | undefined ) {
): Array<XMLTag> {
const xmlSelector = XMLSelector.parse( selector );

if ( xmlSelector ) {
return xmlSelector.query( this.roots );
}
return xmlSelector.query( this.roots );
}


Expand Down
2 changes: 1 addition & 1 deletion tst/XMLTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ test( 'Test XMLTree on HTML', async ( assert: test.Assert ) => {

assert.deepStrictEqual(
tree.query( 'template div.else-clazz[@click$=true]' ),
undefined,
[],
'Query should not match any div node.'
);

Expand Down

0 comments on commit af0086f

Please sign in to comment.