Skip to content
Browse files

Refactoring code

  • Loading branch information...
1 parent a82df2b commit 689108f2936998f7dadebf758d06ba621c49807f @StevenLooman committed Aug 14, 2012
Showing with 32 additions and 46 deletions.
  1. +4 −0 lib/recorder.js
  2. +5 −20 lib/saxpath.js
  3. +1 −5 lib/state.js
  4. +10 −6 test/recorder.js
  5. +12 −15 test/saxpath.js
View
4 lib/recorder.js
@@ -24,6 +24,10 @@ Recorder.prototype.stop = function() {
// this.currentNode = null;
};
+Recorder.prototype.isRecording = function() {
+ return this.recording;
+};
+
Recorder.prototype.onOpenTag = function(node) {
if (!this.recording) {
return;
View
25 lib/saxpath.js
@@ -1,5 +1,6 @@
var util = require('util');
var events = require('events');
+
var XPathParser = require('./xpath_parser');
var State = require('./state');
var Recorder = require('./recorder');
@@ -13,11 +14,7 @@ function SaXPath(saxParser, xpath, recorder) {
this.saxParser.on('text', this.onText.bind(this));
this.saxParser.on('end', this.onEnd.bind(this));
- if (recorder) {
- this.recorder = recorder;
- } else {
- this.recorder = new Recorder();
- }
+ this.recorder = recorder || new Recorder();
this.saxParser.on('opentag', this.recorder.onOpenTag.bind(this.recorder));
this.saxParser.on('closetag', this.recorder.onCloseTag.bind(this.recorder));
this.saxParser.on('text', this.recorder.onText.bind(this.recorder));
@@ -27,7 +24,6 @@ function SaXPath(saxParser, xpath, recorder) {
this.currentDepth = -1;
this.currentState = 0;
- this.recordingDepth = -1;
}
@@ -38,7 +34,7 @@ SaXPath.prototype.parseXPathExpr = function(expr) {
var stack = [];
var i;
- var previousState = null; // XXX: dummy state?
+ var previousState = null;
for (i = 0; i < expr.length; ++i) {
var part = expr[i];
var state = new State(part);
@@ -61,11 +57,6 @@ SaXPath.prototype.parseXPathExpr = function(expr) {
};
-SaXPath.prototype.isRecording = function() {
- return this.recordingDepth !== -1 && this.currentDepth >= this.recordingDepth;
-};
-
-
SaXPath.prototype.onOpenTag = function(node) {
this.currentDepth += 1;
console.log('=== open tag, node:', node, 'depth:', this.currentDepth, 'state:', this.currentState);
@@ -77,7 +68,6 @@ SaXPath.prototype.onOpenTag = function(node) {
if (state.matches(node, this.currentDepth)) {
// move forward a state
- state.matchedDepth = this.currentDepth;
this.currentState += 1;
if (this.currentState !== this.states.length) {
@@ -88,8 +78,7 @@ SaXPath.prototype.onOpenTag = function(node) {
}
// have we reached the top of the state-stack?
- if (this.currentState === this.states.length && this.recordingDepth === -1) {
- this.recordingDepth = this.currentDepth;
+ if (this.currentState === this.states.length && !this.recorder.isRecording()) {
this.recorder.start();
}
};
@@ -103,11 +92,8 @@ SaXPath.prototype.onCloseTag = function(tag) {
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;
+ if (this.recorder.isRecording()) {
this.recorder.stop();
}
@@ -120,7 +106,6 @@ SaXPath.prototype.onCloseTag = function(tag) {
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
6 lib/state.js
@@ -1,6 +1,4 @@
function State(part) {
- this.matchedDepth = -1;
-
var axis = part[0];
var name = part[1];
var predicates = part[2];
@@ -28,8 +26,6 @@ function State(part) {
this.predicates = predicates.slice(1).slice(0, -1);
}
}
-
- this.matchedDepth = -1;
}
@@ -59,7 +55,7 @@ State.prototype.matches = function(node, depth) {
};
State.prototype.unmatches = function(tag, depth) {
- var unmatch = depth < this.matchedDepth;
+ var unmatch = depth < this.enteredDepth;
if (unmatch) {
console.log('unmatch');
}
View
16 test/recorder.js
@@ -1,32 +1,36 @@
function TestRecorder() {
this.tape = [];
- this.isRecording = false;
+ this.tapeOn = false;
}
TestRecorder.prototype.start = function() {
- this.isRecording = true;
+ this.tapeOn = true;
};
TestRecorder.prototype.stop = function() {
- this.isRecording = false;
+ this.tapeOn = false;
+};
+
+TestRecorder.prototype.isRecording = function() {
+ return this.tapeOn;
};
TestRecorder.prototype.onOpenTag = function(node) {
- if (this.isRecording) {
+ if (this.tapeOn) {
this.tape.push(node);
}
};
TestRecorder.prototype.onCloseTag = function(tag) {
- if (this.isRecording) {
+ if (this.tapeOn) {
this.tape.push(tag);
}
};
TestRecorder.prototype.onText = function (text) {
- if (this.isRecording) {
+ if (this.tapeOn) {
this.tape.push(text);
}
};
View
27 test/saxpath.js
@@ -1,17 +1,17 @@
-var assert = require('assert');
+var assert = require('assert');
var TestRecorder = require('./recorder');
-var fs = require('fs');
-var sax = require('sax');
-var xps = require('..');
+var fs = require('fs');
+var sax = require('sax');
+var saxpath = require('..');
describe('SaXPath', function() {
it('should match /bookstore/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, '/bookstore/book', recorder);
+ var streamer = new saxpath.SaXPath(saxParser, '/bookstore/book', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
@@ -27,7 +27,7 @@ describe('SaXPath', function() {
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);
+ var streamer = new saxpath.SaXPath(saxParser, '/bookstore/book[@category="COOKING"]', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
@@ -44,7 +44,7 @@ describe('SaXPath', function() {
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);
+ var streamer = new saxpath.SaXPath(saxParser, '/bookstore/title', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
@@ -59,8 +59,7 @@ describe('SaXPath', function() {
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);
+ var streamer = new saxpath.SaXPath(saxParser, '//book', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
@@ -76,15 +75,15 @@ describe('SaXPath', function() {
var fileStream = fs.createReadStream('test/bookstore.xml');
var recorder = new TestRecorder();
var saxParser = sax.createStream(true);
- var streamer = new xps.SaXPath(saxParser, '//book[@category="COOKING"]', recorder);
- console.log(streamer);
+ var streamer = new saxpath.SaXPath(saxParser, '//book[@category="COOKING"]', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
function testNodesRecorded() {
assert.ok(recorder.tape.length > 0);
assert.equal(recorder.tape[0].name, 'book');
+ assert.deepEqual(recorder.tape[0].attributes, { category: 'COOKING' });
done();
}
});
@@ -93,8 +92,7 @@ describe('SaXPath', function() {
var fileStream = fs.createReadStream('test/bookstore.xml');
var recorder = new TestRecorder();
var saxParser = sax.createStream(true);
- var streamer = new xps.SaXPath(saxParser, '//book/title', recorder);
- console.log(streamer);
+ var streamer = new saxpath.SaXPath(saxParser, '//book/title', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);
@@ -110,8 +108,7 @@ describe('SaXPath', function() {
var fileStream = fs.createReadStream('test/bookstore.xml');
var recorder = new TestRecorder();
var saxParser = sax.createStream(true);
- var streamer = new xps.SaXPath(saxParser, '//book//title', recorder);
- console.log(streamer);
+ var streamer = new saxpath.SaXPath(saxParser, '//book//title', recorder);
saxParser.on('end', testNodesRecorded);
fileStream.pipe(saxParser);

0 comments on commit 689108f

Please sign in to comment.
Something went wrong with that request. Please try again.