-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add optional support for source locations #63
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6faf50d
feat: add optional support for source locations
devongovett 7d6f76b
Move location tracker to a separate file and refactor into class
devongovett d560a26
Merge branch 'master' into source_locations
devongovett d61007b
Add description to docs
devongovett e18551c
Merge branch 'source_locations' of github.com:devongovett/posthtml-pa…
devongovett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import {Parser, ParserOptions} from 'htmlparser2'; | ||
import {Directive, Node, Options, Attributes} from '../types/index.d'; | ||
import {Directive, Node, NodeTag, Options, Attributes} from '../types/index.d'; | ||
|
||
const defaultOptions: ParserOptions = { | ||
lowerCaseTags: false, | ||
|
@@ -49,6 +49,34 @@ const parser = (html: string, options: Options = {}): Node[] => { | |
return result; | ||
} | ||
|
||
const lastLoc = { | ||
line: 1, | ||
column: 1 | ||
}; | ||
|
||
let lastIndex = 0; | ||
function getLoc(index: number) { | ||
if (index < lastIndex) { | ||
throw new Error('Source indices must be monotonic'); | ||
} | ||
|
||
while (lastIndex < index) { | ||
if (html.charCodeAt(lastIndex) === /* \n */ 10) { | ||
lastLoc.line++; | ||
lastLoc.column = 1; | ||
} else { | ||
lastLoc.column++; | ||
} | ||
|
||
lastIndex++; | ||
} | ||
|
||
return { | ||
line: lastLoc.line, | ||
column: lastLoc.column | ||
}; | ||
} | ||
|
||
function onprocessinginstruction(name: string, data: string) { | ||
const directives = defaultDirectives.concat(options.directives ?? []); | ||
const last: Node = bufferArrayLast(); | ||
|
@@ -92,7 +120,15 @@ const parser = (html: string, options: Options = {}): Node[] => { | |
} | ||
|
||
function onopentag(tag: string, attrs: Attributes) { | ||
const buf: Node = {tag}; | ||
const start = getLoc(parser.startIndex); | ||
const buf: NodeTag = {tag}; | ||
|
||
if (options.sourceLocations) { | ||
buf.loc = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will be not bad if we use the name for example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, if you prefer. |
||
start, | ||
end: start | ||
}; | ||
} | ||
|
||
if (Object.keys(attrs).length > 0) { | ||
buf.attrs = normalizeArributes(attrs); | ||
|
@@ -104,6 +140,10 @@ const parser = (html: string, options: Options = {}): Node[] => { | |
function onclosetag() { | ||
const buf: Node | undefined = bufArray.pop(); | ||
|
||
if (buf && typeof buf === 'object' && buf.loc && parser.endIndex !== null) { | ||
buf.loc.end = getLoc(parser.endIndex); | ||
} | ||
|
||
if (buf) { | ||
const last = bufferArrayLast(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it will be good if you take it into a separate method / file with explicit transmission of the required parameters.
I think it will be not bad if we use the name for example
getLocation
.