Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented //-axis

  • Loading branch information...
commit 560a74e723fe4158277e0fcbc9e8858d00272acd 1 parent 7e061ba
@StevenLooman authored
Showing with 70 additions and 26 deletions.
  1. +27 −13 lib/saxpath.js
  2. +15 −2 lib/state.js
  3. +2 −1  run.js
  4. +26 −10 test/saxpath.js
View
40 lib/saxpath.js
@@ -68,7 +68,7 @@ SaXPath.prototype.isRecording = function() {
SaXPath.prototype.onOpenTag = function(node) {
this.currentDepth += 1;
- console.log('=== open tag, node:', node, 'depth:', this.currentDepth);
+ console.log('=== open tag, node:', node, 'depth:', this.currentDepth, 'state:', this.currentState);
if (this.currentState < this.states.length) {
// still matching states
@@ -96,21 +96,35 @@ SaXPath.prototype.onOpenTag = function(node) {
SaXPath.prototype.onCloseTag = function(tag) {
this.currentDepth -= 1;
- console.log('=== close tag, node:', tag, 'depth:', this.currentDepth);
-
- var state = this.states[this.currentState - 1];
- if (state.unmatches(tag, this.currentDepth)) {
- state.leave(this.currentDepth);
- state.matchedDepth = -1;
+ console.log('=== close tag, node:', tag, 'depth:', this.currentDepth, 'state:', this.currentState);
+
+ var state;
+ if (this.currentState === this.states.length) {
+ state = this.states[this.currentState - 1];
+ if (state.unmatches(tag, this.currentDepth)) {
+ state.leave(this.currentDepth);
+ state.matchedDepth = -1;
+
+ if (this.recordingDepth !== -1) {
+ // stop recording
+ this.recordingDepth = -1;
+ this.recorder.stop();
+ }
- if (this.recordingDepth !== -1) {
- // stop recording
- this.recordingDepth = -1;
- this.recorder.stop();
+ // go back a state
+ this.currentState -= 1;
}
+ }
- // go back a state
- this.currentState -= 1;
+ if (this.currentState < this.states.length) {
+ state = this.states[this.currentState];
+ if (state.unmatches(tag, this.currentDepth)) {
+ state.leave(this.currentDepth);
+ state.matchedDepth = -1;
+
+ // go back a state
+ this.currentState -= 1;
+ }
}
};
View
17 lib/state.js
@@ -7,8 +7,21 @@ function State(part) {
if (axis === '/') {
this.axis = 'child';
- this.name = name;
- this.immediate = true;
+
+ if (name) {
+ this.name = name;
+ }
+
+ if (predicates) {
+ // strip '[' and ']'
+ this.predicates = predicates.slice(1).slice(0, -1);
+ }
+ } else if (axis === '//') {
+ this.axis = 'self-or-descendant';
+
+ if (name) {
+ this.name = name;
+ }
if (predicates) {
// strip '[' and ']'
View
3  run.js
@@ -9,7 +9,8 @@ var filename = 'test/bookstore.xml';
function main() {
var saxParser = sax.createStream(true);
// var streamer = new xps.SaXPath(saxParser, '/bookstore/book');
- var streamer = new xps.SaXPath(saxParser, '/bookstore/book[@category="COOKING"]');
+// var streamer = new xps.SaXPath(saxParser, '/bookstore/book[@category="COOKING"]');
+ var streamer = new xps.SaXPath(saxParser, '//book');
var fileStream = fs.createReadStream(filename);
fileStream.pipe(saxParser);
View
36 test/saxpath.js
@@ -8,10 +8,10 @@ var xps = require('..');
describe('SaXPath', function() {
it('should match /bookstore/book', function(done) {
- var recorder = new TestRecorder();
- var saxParser = sax.createStream(true);
- var streamer = new xps.SaXPath(saxParser, '/bookstore/book', recorder);
var fileStream = fs.createReadStream('test/bookstore.xml');
+ var recorder = new TestRecorder();
+ var saxParser = sax.createStream(true);
+ var streamer = new xps.SaXPath(saxParser, '/bookstore/book', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
@@ -24,10 +24,10 @@ describe('SaXPath', function() {
});
it('should match /bookstore/book[@category="COOKING"]', function(done) {
- var recorder = new TestRecorder();
- var saxParser = sax.createStream(true);
- var streamer = new xps.SaXPath(saxParser, '/bookstore/book[@category="COOKING"]', recorder);
var fileStream = fs.createReadStream('test/bookstore.xml');
+ var recorder = new TestRecorder();
+ var saxParser = sax.createStream(true);
+ var streamer = new xps.SaXPath(saxParser, '/bookstore/book[@category="COOKING"]', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
@@ -41,18 +41,34 @@ describe('SaXPath', function() {
});
it('should not match /bookstore/title', function(done) {
- var recorder = new TestRecorder();
- var saxParser = sax.createStream(true);
- var streamer = new xps.SaXPath(saxParser, '/bookstore/title', recorder);
var fileStream = fs.createReadStream('test/bookstore.xml');
+ var recorder = new TestRecorder();
+ var saxParser = sax.createStream(true);
+ var streamer = new xps.SaXPath(saxParser, '/bookstore/title', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
function testNodesRecorded() {
- console.log(recorder.tape);
assert.ok(recorder.tape.length === 0);
done();
}
});
+
+ it('should match //book', function(done) {
+ var fileStream = fs.createReadStream('test/bookstore.xml');
+ var recorder = new TestRecorder();
+ var saxParser = sax.createStream(true);
+ var streamer = new xps.SaXPath(saxParser, '//book', recorder);
+ console.log(streamer);
+
+ saxParser.on('end', testNodesRecorded);
+ fileStream.pipe(saxParser);
+
+ function testNodesRecorded() {
+ assert.ok(recorder.tape.length > 0);
+ assert.equal(recorder.tape[0].name, 'book');
+ done();
+ }
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.