Skip to content

Commit

Permalink
fix lineNumberStart on html parse (#59)
Browse files Browse the repository at this point in the history
* fix lineNumberStart on html parse

* fix lineNumberStart

* Revert "fix lineNumberStart"

This reverts commit b30c620.

---------

Co-authored-by: youthlin <youthlin.chen@bytedance.com>
  • Loading branch information
youthlin and youthlin authored Feb 7, 2023
1 parent 4390d5a commit c5b19e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/html/extractors/factories/embeddedJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { HtmlUtils } from '../../utils';
import { Validate } from '../../../utils/validate';

export function embeddedJsExtractor(selector: string, jsParser: JsParser): IHtmlExtractorFunction {
Validate.required.nonEmptyString({selector});
Validate.required.argument({jsParser});
Validate.required.nonEmptyString({ selector });
Validate.required.argument({ jsParser });

let selectors = new ElementSelectorSet(selector);

return (node: Node, fileName: string) => {
return (node: Node, fileName: string, _, lineNumberStart) => {
if (typeof (<Element>node).tagName !== 'string') {
return;
}
Expand All @@ -23,8 +23,11 @@ export function embeddedJsExtractor(selector: string, jsParser: JsParser): IHtml
preserveIndentation: true,
replaceNewLines: false
});
if (element.sourceCodeLocation && element.sourceCodeLocation.startLine) {
lineNumberStart = lineNumberStart + element.sourceCodeLocation.startLine - 1;
}
jsParser.parseString(source, fileName, {
lineNumberStart: element.sourceCodeLocation && element.sourceCodeLocation.startLine
lineNumberStart
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type Node = parse5.DefaultTreeNode;
export type TextNode = parse5.DefaultTreeTextNode;
export type Element = parse5.DefaultTreeElement;

export type IHtmlExtractorFunction = (node: Node, fileName: string, addMessage: IAddMessageCallback) => void;
export type IHtmlExtractorFunction = (node: Node, fileName: string, addMessage: IAddMessageCallback, lineNumberStart: number) => void;

export class HtmlParser extends Parser<IHtmlExtractorFunction, IParseOptions> {

Expand All @@ -25,7 +25,7 @@ export class HtmlParser extends Parser<IHtmlExtractorFunction, IParseOptions> {
});

for (let extractor of this.extractors) {
extractor(node, fileName, addMessageCallback);
extractor(node, fileName, addMessageCallback, lineNumberStart);
}

let childNodes = node.content ? node.content.childNodes : node.childNodes;
Expand Down
8 changes: 8 additions & 0 deletions tests/html/extractors/factories/embeddedJs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ describe('HTML: Embedded JS Extractor', () => {
});
});

test('with lineNumberStart option', () => {
htmlParser.parseString(`<script>Foo</script>`, 'foo.html', { lineNumberStart: 10 });

expect(jsParserMock.parseString).toHaveBeenCalledWith('Foo', 'foo.html', {
lineNumberStart: 10
});
});

test('separate line', () => {
htmlParser.parseString(`<script>\nFoo\n</script>`, 'foo.html');

Expand Down

0 comments on commit c5b19e9

Please sign in to comment.