Skip to content

Commit 9f7c56b

Browse files
authored
Query results in array (#19)
- Add #18; XMLTree.query results always in array - Add SyntaxError to XMLSelector.parse
1 parent 8decbfb commit 9f7c56b

11 files changed

+104
-80
lines changed

amd/tsl-core-xml.d.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,12 @@ declare module "XMLSelector" {
441441
* String with selector terms to parse.
442442
*
443443
* @return
444-
* The XMLSelector instance with the parsed selector terms, or undefined on
445-
* error.
444+
* The XMLSelector instance with the parsed selector terms.
445+
*
446+
* @throws
447+
* SyntaxError, if unexpected patterns in selector terms are found.
446448
*/
447-
static parse(selectorString: string): (XMLSelector | undefined);
449+
static parse(selectorString: string): XMLSelector;
448450
/**
449451
* @param selector
450452
* Selector to match against.
@@ -462,9 +464,9 @@ declare module "XMLSelector" {
462464
* Matching term to search for.
463465
*
464466
* @return
465-
* List of matching XML tags, or `undefined`.
467+
* List of matching XML tags.
466468
*/
467-
find(nodes: Array<XMLNode>, term: SelectorTerm): (Array<XMLTag> | undefined);
469+
find(nodes: Array<XMLNode>, term: SelectorTerm): Array<XMLTag>;
468470
/**
469471
* Creates a list of XML tags matching the selector conditions. The
470472
* matching is done using depth-first pre-order traversal of the XML nodes.
@@ -473,9 +475,9 @@ declare module "XMLSelector" {
473475
* Array of nodes to search in.
474476
*
475477
* @return
476-
* List of matching XML tags, or `undefined`.
478+
* List of matching XML tags.
477479
*/
478-
query(nodes: Array<XMLNode>): (Array<XMLTag> | undefined);
480+
query(nodes: Array<XMLNode>): Array<XMLTag>;
479481
}
480482
export default XMLSelector;
481483
}
@@ -536,9 +538,12 @@ declare module "XMLTree" {
536538
* Selector to match against.
537539
*
538540
* @return
539-
* List of XML nodes matching the selector, or `undefined`.
541+
* List of XML nodes matching the selector.
542+
*
543+
* @throws
544+
* SyntaxError, if unexpected patterns in selector terms are found.
540545
*/
541-
query(selector: string): (Array<XMLTag> | undefined);
546+
query(selector: string): Array<XMLTag>;
542547
/**
543548
* Converts the tree of nodes back to XML text.
544549
*/

amd/tsl-core-xml.js

Lines changed: 17 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/tsl-core-xml.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/XMLSelector.d.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ export declare class XMLSelector {
3434
* String with selector terms to parse.
3535
*
3636
* @return
37-
* The XMLSelector instance with the parsed selector terms, or undefined on
38-
* error.
37+
* The XMLSelector instance with the parsed selector terms.
38+
*
39+
* @throws
40+
* SyntaxError, if unexpected patterns in selector terms are found.
3941
*/
40-
static parse(selectorString: string): (XMLSelector | undefined);
42+
static parse(selectorString: string): XMLSelector;
4143
/**
4244
* @param selector
4345
* Selector to match against.
@@ -55,9 +57,9 @@ export declare class XMLSelector {
5557
* Matching term to search for.
5658
*
5759
* @return
58-
* List of matching XML tags, or `undefined`.
60+
* List of matching XML tags.
5961
*/
60-
find(nodes: Array<XMLNode>, term: SelectorTerm): (Array<XMLTag> | undefined);
62+
find(nodes: Array<XMLNode>, term: SelectorTerm): Array<XMLTag>;
6163
/**
6264
* Creates a list of XML tags matching the selector conditions. The
6365
* matching is done using depth-first pre-order traversal of the XML nodes.
@@ -66,8 +68,8 @@ export declare class XMLSelector {
6668
* Array of nodes to search in.
6769
*
6870
* @return
69-
* List of matching XML tags, or `undefined`.
71+
* List of matching XML tags.
7072
*/
71-
query(nodes: Array<XMLNode>): (Array<XMLTag> | undefined);
73+
query(nodes: Array<XMLNode>): Array<XMLTag>;
7274
}
7375
export default XMLSelector;

lib/XMLSelector.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ export class XMLSelector {
9999
* String with selector terms to parse.
100100
*
101101
* @return
102-
* The XMLSelector instance with the parsed selector terms, or undefined on
103-
* error.
102+
* The XMLSelector instance with the parsed selector terms.
103+
*
104+
* @throws
105+
* SyntaxError, if unexpected patterns in selector terms are found.
104106
*/
105107
static parse(selectorString) {
106108
const selectorsStrings = selectorString.split(spaceRegExp);
@@ -110,11 +112,9 @@ export class XMLSelector {
110112
let terms;
111113
for (let i = 0, iEnd = selectorsStrings.length; i < iEnd; ++i) {
112114
match = selectorsStrings[i].match(XMLRegExp.selector);
113-
if (!match) {
114-
continue;
115-
}
116-
if (match[0] !== selectorsStrings[i]) {
117-
return;
115+
if (!match ||
116+
match[0] !== selectorsStrings[i]) {
117+
throw new SyntaxError('Unexpected pattern\n' + selectorsStrings[i]);
118118
}
119119
terms = {};
120120
// Tag name
@@ -185,7 +185,7 @@ export class XMLSelector {
185185
* Matching term to search for.
186186
*
187187
* @return
188-
* List of matching XML tags, or `undefined`.
188+
* List of matching XML tags.
189189
*/
190190
find(nodes, term) {
191191
var _a, _b;
@@ -214,9 +214,7 @@ export class XMLSelector {
214214
}
215215
}
216216
}
217-
if (findings.length) {
218-
return findings;
219-
}
217+
return findings;
220218
}
221219
/**
222220
* Creates a list of XML tags matching the selector conditions. The
@@ -226,15 +224,15 @@ export class XMLSelector {
226224
* Array of nodes to search in.
227225
*
228226
* @return
229-
* List of matching XML tags, or `undefined`.
227+
* List of matching XML tags.
230228
*/
231229
query(nodes) {
232230
const selectors = this.selectors;
233-
let findings;
231+
let findings = [];
234232
for (let i = 0, iEnd = selectors.length; i < iEnd; ++i) {
235233
findings = this.find(nodes, selectors[i]);
236234
if (!findings) {
237-
return;
235+
break;
238236
}
239237
nodes = findings;
240238
}

lib/XMLTree.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ export declare class XMLTree {
6565
* Selector to match against.
6666
*
6767
* @return
68-
* List of XML nodes matching the selector, or `undefined`.
68+
* List of XML nodes matching the selector.
69+
*
70+
* @throws
71+
* SyntaxError, if unexpected patterns in selector terms are found.
6972
*/
70-
query(selector: string): (Array<XMLTag> | undefined);
73+
query(selector: string): Array<XMLTag>;
7174
/**
7275
* Converts the tree of nodes back to XML text.
7376
*/

lib/XMLTree.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,14 @@ export class XMLTree {
141141
* Selector to match against.
142142
*
143143
* @return
144-
* List of XML nodes matching the selector, or `undefined`.
144+
* List of XML nodes matching the selector.
145+
*
146+
* @throws
147+
* SyntaxError, if unexpected patterns in selector terms are found.
145148
*/
146149
query(selector) {
147150
const xmlSelector = XMLSelector.parse(selector);
148-
if (xmlSelector) {
149-
return xmlSelector.query(this.roots);
150-
}
151+
return xmlSelector.query(this.roots);
151152
}
152153
/**
153154
* Converts the tree of nodes back to XML text.

src/XMLSelector.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ function matchAttributes (
120120
return true;
121121
}
122122

123+
123124
function matchClasses (
124125
classValue?: string,
125126
classNeedles: Array<string> = []
@@ -169,12 +170,14 @@ export class XMLSelector {
169170
* String with selector terms to parse.
170171
*
171172
* @return
172-
* The XMLSelector instance with the parsed selector terms, or undefined on
173-
* error.
173+
* The XMLSelector instance with the parsed selector terms.
174+
*
175+
* @throws
176+
* SyntaxError, if unexpected patterns in selector terms are found.
174177
*/
175178
public static parse (
176179
selectorString: string
177-
): ( XMLSelector | undefined ) {
180+
): XMLSelector {
178181
const selectorsStrings = selectorString.split( spaceRegExp );
179182
const selectors: Array<SelectorTerm> = [];
180183
const selector = new XMLSelector( selectors );
@@ -185,12 +188,11 @@ export class XMLSelector {
185188
for ( let i = 0, iEnd = selectorsStrings.length; i < iEnd; ++i ) {
186189
match = selectorsStrings[i].match( XMLRegExp.selector );
187190

188-
if ( !match ) {
189-
continue;
190-
}
191-
192-
if ( match[0] !== selectorsStrings[i] ) {
193-
return;
191+
if (
192+
!match ||
193+
match[0] !== selectorsStrings[i]
194+
) {
195+
throw new SyntaxError( 'Unexpected pattern\n' + selectorsStrings[i] );
194196
}
195197

196198
terms = {};
@@ -297,12 +299,12 @@ export class XMLSelector {
297299
* Matching term to search for.
298300
*
299301
* @return
300-
* List of matching XML tags, or `undefined`.
302+
* List of matching XML tags.
301303
*/
302304
public find (
303305
nodes: Array<XMLNode>,
304306
term: SelectorTerm
305-
): ( Array<XMLTag> | undefined ) {
307+
): Array<XMLTag> {
306308
const findings: Array<XMLTag> = [];
307309

308310
for ( const node of nodes ) {
@@ -341,9 +343,7 @@ export class XMLSelector {
341343

342344
}
343345

344-
if ( findings.length ) {
345-
return findings;
346-
}
346+
return findings;
347347
}
348348

349349

@@ -355,20 +355,20 @@ export class XMLSelector {
355355
* Array of nodes to search in.
356356
*
357357
* @return
358-
* List of matching XML tags, or `undefined`.
358+
* List of matching XML tags.
359359
*/
360360
public query (
361361
nodes: Array<XMLNode>
362-
): ( Array<XMLTag> | undefined ) {
362+
): Array<XMLTag> {
363363
const selectors = this.selectors;
364364

365-
let findings: ( Array<XMLTag> | undefined );
365+
let findings: Array<XMLTag> = [];
366366

367367
for ( let i = 0, iEnd = selectors.length; i < iEnd; ++i ) {
368368
findings = this.find( nodes, selectors[i] );
369369

370370
if ( !findings ) {
371-
return;
371+
break;
372372
}
373373

374374
nodes = findings;

src/XMLTree.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,17 @@ export class XMLTree {
226226
* Selector to match against.
227227
*
228228
* @return
229-
* List of XML nodes matching the selector, or `undefined`.
229+
* List of XML nodes matching the selector.
230+
*
231+
* @throws
232+
* SyntaxError, if unexpected patterns in selector terms are found.
230233
*/
231234
public query (
232235
selector: string
233-
): ( Array<XMLTag> | undefined ) {
236+
): Array<XMLTag> {
234237
const xmlSelector = XMLSelector.parse( selector );
235238

236-
if ( xmlSelector ) {
237-
return xmlSelector.query( this.roots );
238-
}
239+
return xmlSelector.query( this.roots );
239240
}
240241

241242

0 commit comments

Comments
 (0)