Skip to content

Commit

Permalink
Fix SAXParser text location
Browse files Browse the repository at this point in the history
Fixes #266.
  • Loading branch information
RReverser committed Aug 10, 2018
1 parent 533e7cc commit c6d0fb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
7 changes: 6 additions & 1 deletion packages/parse5-sax-parser/lib/index.js
Expand Up @@ -87,7 +87,12 @@ class SAXParser extends Transform {
if (this.pendingText === null) {
this.currentTokenLocation = token.location;
} else {
this.currentTokenLocation.endOffset = token.location.endOffset;
const { endLine, endCol, endOffset } = token.location;
Object.assign(this.currentTokenLocation, {
endLine,
endCol,
endOffset
});
}
}

Expand Down
14 changes: 9 additions & 5 deletions packages/parse5-sax-parser/test/location-info.test.js
Expand Up @@ -29,16 +29,20 @@ exports['Location info (SAX)'] = function() {
});
};

exports['Regression - location info for text (GH-153)'] = function() {
exports['Regression - location info for text (GH-153, GH-266)'] = function() {
const html = '<!DOCTYPE html><html><head><title>Here is a title</title></html>';
const parser = new SAXParser({ sourceCodeLocationInfo: true });
const texts = [];

parser.on('text', ({ sourceCodeLocation }) => {
texts.push(html.substring(sourceCodeLocation.startOffset, sourceCodeLocation.endOffset));
assert.deepStrictEqual(sourceCodeLocation, {
startLine: 1,
startCol: 35,
startOffset: 34,
endLine: 1,
endCol: 50,
endOffset: 49
});
});

parser.end(html);

assert.deepEqual(texts, ['Here is a title']);
};

0 comments on commit c6d0fb5

Please sign in to comment.