Skip to content

Commit

Permalink
add support for a listener to capture parse events
Browse files Browse the repository at this point in the history
  • Loading branch information
ahgittin committed Feb 10, 2016
1 parent fe94035 commit 3f9f8d4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function State(input, options) {
this.onWarning = options['onWarning'] || null;
this.legacy = options['legacy'] || false;
this.json = options['json'] || false;
this.listener = options['listener'] || null;

this.implicitTypes = this.schema.compiledImplicit;
this.typeMap = this.schema.compiledTypeMap;
Expand Down Expand Up @@ -1266,6 +1267,10 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact
flowIndent,
blockIndent;

if (state.listener !== null) {
state.listener('open', state);
}

state.tag = null;
state.anchor = null;
state.kind = null;
Expand Down Expand Up @@ -1399,6 +1404,9 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact
}
}

if (state.listener !== null) {
state.listener('close', state);
}
return null !== state.tag || null !== state.anchor || hasContent;
}

Expand Down
62 changes: 62 additions & 0 deletions test/issues/0248-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

var assert = require('assert');
var yaml = require('../../');

test('Listener informed on a very simple scalar.', function () {
var history = [];
function l(eventType, state) {
history.push([ eventType, state.position ]);
}

yaml.load('a_simple_scalar', { listener: l });

// 2 open events then 2 close events
assert.equal(history.length, 4);
assert.equal(history[0][0], 'open');
assert.equal(history[1][0], 'open');
assert.equal(history[2][0], 'close');
assert.equal(history[3][0], 'close');
assert.equal(history[0][1], 0);
assert.equal(history[3][1], 16);
});

test('Listener informed on a map with a list.', function () {
var history = [];
function l(eventType, state) {
history.push([ eventType, state.position, state.result ]);
}

yaml.load('{ a: 1, b: [ 0, xyz ] }', { listener: l });

var i = -1;
assert.equal(history[++i][0], 'open'); // doc
assert.equal(history[++i][0], 'open'); // map

assert.equal(history[++i][0], 'open'); // key
assert.equal(history[++i][0], 'close');
assert.equal(history[i][2], 'a');

assert.equal(history[++i][0], 'open'); // a value
assert.equal(history[++i][0], 'close');
assert.equal(history[i][2], 1);

assert.equal(history[++i][0], 'open'); // key
assert.equal(history[++i][0], 'close');
assert.equal(history[i][2], 'b');

assert.equal(history[++i][0], 'open'); // b value (list)
assert.equal(history[++i][0], 'open'); // item in list
assert.equal(history[++i][0], 'close');
assert.equal(history[i][2], 0);
assert.equal(history[++i][0], 'open'); // item in list
assert.equal(history[++i][0], 'close');

assert.equal(history[++i][0], 'close'); // b value (list) end
assert.deepEqual(history[i][2], [ 0, 'xyz' ]);

assert.equal(history[++i][0], 'close'); // map end
assert.equal(history[++i][0], 'close'); // doc end

assert.equal(history.length, ++i);
});

0 comments on commit 3f9f8d4

Please sign in to comment.