Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
feat(parser): adds peekToken to parser (#30)
Browse files Browse the repository at this point in the history
Can now see what token is coming w/o consuming it.
  • Loading branch information
Joshua Newman committed Apr 27, 2016
1 parent 2406b32 commit 262cc02
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/HtmlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default class HtmlParser {
}

if (fix) {
this._fixedReadToken = fixedReadTokenFactory(this, fixedTokenOptions,
() => this._readTokenImpl());
this._fixedReadToken = fixedReadTokenFactory(this, fixedTokenOptions, () => this._readTokenImpl());
this._fixedPeekToken = fixedReadTokenFactory(this, fixedTokenOptions, () => this._peekTokenImpl());
}
}

Expand All @@ -74,24 +74,38 @@ export default class HtmlParser {
* The implementation of the token reading.
*
* @private
* @returns {Token}
* @returns {?Token}
*/
_readTokenImpl() {
// Enumerate detects in order
const token = this._peekTokenImpl();
if (token) {
this.stream = this.stream.slice(token.length);
return token;
}
}

/**
*
* @returns {?Token}
*/
_peekTokenImpl() {
for (let type in detect) {
if (detect.hasOwnProperty(type)) {
if (detect[type].test(this.stream)) {
const token = streamReaders[type](this.stream);
if (token) {
token.text = this.stream.substr(0, token.length);
this.stream = this.stream.slice(token.length);
return token;
}
}
}
}
}

peekToken() {
return this._fixedReadToken ? this._fixedReadToken() : this._peekTokenImpl();
}

/**
* The public token reading interface. Delegates to the basic token reading
* or a version that performs fixups depending on the `autoFix` setting in
Expand Down
13 changes: 13 additions & 0 deletions test/helpers/testPeek.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import HtmlParser from '../../src/HtmlParser';

export function peeks(s, options = {}, theTest) {
if (typeof options == 'function') {
theTest = options;
options = {};
}

const parser = typeof s === 'string' ? new HtmlParser(s, options) : s;
const tok = parser.peekToken();

return () => theTest(tok, parser);
}
14 changes: 14 additions & 0 deletions test/unit/attributes.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {parses} from '../helpers/testParse';
import {peeks} from '../helpers/testPeek';

describe('HtmlParser', () => {
describe('#readToken', () => {
Expand Down Expand Up @@ -112,4 +113,17 @@ describe('HtmlParser', () => {
}));
});

describe('#peek', () => {
const TAG = '<img/>';

it('does not mutate the stream', peeks(TAG, (token, parser) => {
expect(parser.rest()).to.equal(token.toString());
}));
it('shows the next token', peeks(TAG, (token) => {
expect(token.toString()).to.equal(TAG);
}));
it('returns undefined when there are no more tokens', peeks('', (token) => {
expect(token).to.be(undefined);
}));
});
});

0 comments on commit 262cc02

Please sign in to comment.