Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep buffer for long text during transformation #357

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/parse5-html-rewriting-stream/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { escapeString } = require('parse5/lib/serializer');

class RewritingStream extends SAXParser {
constructor() {
super({ sourceCodeLocationInfo: true });
super({ sourceCodeLocationInfo: true, bufferSafekeeping: true });

this.posTracker = this.locInfoMixin.posTracker;
}
Expand All @@ -21,7 +21,9 @@ class RewritingStream extends SAXParser {
const start = location.startOffset - droppedBufferSize;
const end = location.endOffset - droppedBufferSize;

return this.tokenizer.preprocessor.html.slice(start, end);
const slice = this.tokenizer.preprocessor.html.slice(start, end);
this.bufferSafekeepingMixin.safekeepingOffset = location.endOffset;
return slice;
}

// Events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,21 @@ exports['Regression - RewritingStream - Should not accept binary input (GH-269)'

assert.throws(() => stream.write(buf), TypeError);
};

exports['Regression - RewritingStream - should pass long text correctly (GH-292)'] = done => {
const source = 'a'.repeat(65540);
const parser = new RewritingStream();
let output = '';

parser.on('data', data => {
output += data.toString();
});

parser.once('finish', () => {
assert.strictEqual(output.length, source.length);
done();
});

parser.write(source);
parser.end();
};
8 changes: 8 additions & 0 deletions packages/parse5-sax-parser/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Mixin = require('parse5/lib/utils/mixin');
const mergeOptions = require('parse5/lib/utils/merge-options');
const DevNullStream = require('./dev-null-stream');
const ParserFeedbackSimulator = require('./parser-feedback-simulator');
const BufferSafekeepingPreprocessorMixin = require('../../parse5/lib/extensions/buffer-safekeeping/preprocessor-mixin');

const DEFAULT_OPTIONS = {
sourceCodeLocationInfo: false
Expand All @@ -20,9 +21,16 @@ class SAXParser extends Transform {

this.tokenizer = new Tokenizer(options);
this.locInfoMixin = null;
this.bufferSafekeepingMixin = null;

if (this.options.sourceCodeLocationInfo) {
this.locInfoMixin = Mixin.install(this.tokenizer, LocationInfoTokenizerMixin);
if (this.options.bufferSafekeeping) {
this.bufferSafekeepingMixin = Mixin.install(
this.tokenizer.preprocessor,
BufferSafekeepingPreprocessorMixin
);
}
}

this.parserFeedbackSimulator = new ParserFeedbackSimulator(this.tokenizer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const Mixin = require('../../utils/mixin');

class BufferSafekeepingPreprocessorMixin extends Mixin {
constructor(preprocessor) {
super(preprocessor);

this.safekeepingOffset = 0;
}

_getOverriddenMethods(mxn, orig) {
return {
getDropPosition() {
return Math.min(orig.getDropPosition(), mxn.safekeepingOffset - mxn.droppedBufferSize);
}
};
}
}

module.exports = BufferSafekeepingPreprocessorMixin;
29 changes: 23 additions & 6 deletions packages/parse5/lib/tokenizer/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,32 @@ class Preprocessor {
}

dropParsedChunk() {
if (this.pos > this.bufferWaterline) {
this.lastCharPos -= this.pos;
this.html = this.html.substring(this.pos);
this.pos = 0;
this.lastGapPos = -1;
this.gapStack = [];
const lastPos = this.getDropPosition();
if (lastPos > this.bufferWaterline) {
this.lastCharPos -= lastPos;
this.html = this.html.substring(lastPos);
this.pos -= lastPos;
if (this.pos === 0) {
this.lastGapPos = -1;
this.gapStack = [];
} else {
this.gapStack.push(this.lastGapPos);
const newGapStack = [-1];
for (let i = 1; i < this.gapStack.length; i++) {
if (this.gapStack[i] >= lastPos) {
newGapStack.push(this.gapStack[i] >= lastPos);
}
}
this.gapStack = newGapStack;
this.lastGapPos = this.gapStack.pop();
}
}
}

getDropPosition() {
return this.pos;
}

write(chunk, isLastChunk) {
if (this.html) {
this.html += chunk;
Expand Down