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

Provide DOM-element source code location information #30

Closed
cvrebert opened this issue Dec 6, 2014 · 13 comments
Closed

Provide DOM-element source code location information #30

cvrebert opened this issue Dec 6, 2014 · 13 comments

Comments

@cvrebert
Copy link

cvrebert commented Dec 6, 2014

It would be great if parse5 supported the startIndex property of htmlparser2's DOM.
See https://github.com/fb55/domhandler#option-withstartindices for a description.
It's very useful for e.g. lint tools that want to point out the source code locations of DOM elements.

@inikulin
Copy link
Owner

inikulin commented Dec 8, 2014

Hi.

This makes sense. If you wouldn't mind I'll rename issue, since I would like to implement it for both tree adapters, so it requires more generic title.

@inikulin inikulin changed the title support startIndex in htmlparser2 TreeAdapter Provide DOM-element source code location information Dec 8, 2014
@cvrebert
Copy link
Author

cvrebert commented Dec 8, 2014

Great! 😄

@angelozerr
Copy link

@inikulin I'm very interested too with this feature, because I would like to use SimpleApiParser to retrieve attributes id of an HTML DOM with location.

Indeed, I would like to write a tern plugin http://ternjs.net/doc/demo.html to provide completion, navigation for attributes id for document.getElementById inside a JS editor :

  • test.html :
<html>
  <script src="test.js" />
  <div id="MyId" ></div>
</html>
  • test.js :
var elt = document.getElementById(' // Here Ctrl+Space shows MyId

To manage this case, I will load test.html with parse5 to retrieve attributes id.

I need location to support navigation to the HTML file :

var elt = document.getElementById('MyId' // Here Ctrl+click on MyId open the test.html and select MyId.

So I need location for this case.

Hope you will have time to support this issue. Thanks!

@inikulin
Copy link
Owner

Hi, guys.
I will try to get my hands on this as soon as possible. Meanwhile, I have question regarding this functionality: do you need just token's (and appropriate element's in case of DOM-tree parsing) start index or end index is also required?

@angelozerr
Copy link

I will try to get my hands on this as soon as possible.

Cool!

do you need just token's (and appropriate element's in case of DOM-tree parsing) start index or end index is also required?

In my case, I need start/end index for attribute value :

<div id="MyId" >
              |     |
         start   end 

So the token could have this structure :

{
 "type": "START_TAG_TOKEN",
 "tagName": "div",
 "selfClosing": false,
 "attrs": [
  {
   "name": {
    "content": "id",
    "start": 5,
    "end": 7
   },
   "value": {
    "content": "MyId",
    "start": 9,
    "end": 13
   }
  }
 ],
 "start": 0,
 "end": 16
}

@cvrebert
Copy link
Author

I only need the start index for my use case. And I don't need any attribute-level info.

@inikulin
Copy link
Owner

@cvrebert Is it important for you to have the same property naming (startIndex) as in htmlparser2? I would like add __locations property or something like this for parse5.

@cvrebert
Copy link
Author

I don't care what you name it, so long as it doesn't overlap with any existing browser DOM property.

@inikulin
Copy link
Owner

Good, thank you.

@stevenvachon
Copy link
Contributor

Line/column position for start/end would be awesome as well.

@angelozerr
Copy link

In my case I need position and not line/column. So I think it should be cool if we could customize that with a boolean option lineCharPositions

@inikulin
Copy link
Owner

New version 1.4.0 with this functionality was published to the npm (see changelog).

@angelozerr I haven't implemented location info for the attributes in this release, since it's a quite tricky thing to do and I don't have a good idea how to implement it at the moment, so I've created separate issue for this functionality (#43).

@angelozerr
Copy link

@angelozerr I haven't implemented location info for the attributes in this release

Thanks for this info. I will wait for this features in a future release, if you can implement it.

inikulin added a commit that referenced this issue Apr 16, 2018
Parse errors for a bunch of insertion modes with a word "head" in them
43081j pushed a commit to 43081j/parse5 that referenced this issue Dec 28, 2021
Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.5.3 to 4.5.4.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](microsoft/TypeScript@v4.5.3...v4.5.4)

---
updated-dependencies:
- dependency-name: typescript
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants