• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3var ParsingUnit = module.exports = function (parser) {
4    this.parser = parser;
5    this.suspended = false;
6    this.parsingLoopLock = false;
7    this.callback = null;
8};
9
10ParsingUnit.prototype._stateGuard = function (suspend) {
11    if (this.suspended && suspend)
12        throw new Error('parse5: Parser was already suspended. Please, check your control flow logic.');
13
14    else if (!this.suspended && !suspend)
15        throw new Error('parse5: Parser was already resumed. Please, check your control flow logic.');
16
17    return suspend;
18};
19
20ParsingUnit.prototype.suspend = function () {
21    this.suspended = this._stateGuard(true);
22
23    return this;
24};
25
26ParsingUnit.prototype.resume = function () {
27    this.suspended = this._stateGuard(false);
28
29    //NOTE: don't enter parsing loop if it is locked. Without this lock _runParsingLoop() may be called
30    //while parsing loop is still running. E.g. when suspend() and resume() called synchronously.
31    if (!this.parsingLoopLock)
32        this.parser._runParsingLoop();
33
34    return this;
35};
36
37ParsingUnit.prototype.documentWrite = function (html) {
38    this.parser.tokenizer.preprocessor.write(html);
39
40    return this;
41};
42
43ParsingUnit.prototype.handleScripts = function (scriptHandler) {
44    this.parser.scriptHandler = scriptHandler;
45
46    return this;
47};
48
49ParsingUnit.prototype.done = function (callback) {
50    this.callback = callback;
51
52    return this;
53};
54