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

Attribute#sourceRange return null when parse body attribute #2204

Open
runt0 opened this issue Sep 22, 2024 · 0 comments
Open

Attribute#sourceRange return null when parse body attribute #2204

runt0 opened this issue Sep 22, 2024 · 0 comments

Comments

@runt0
Copy link

runt0 commented Sep 22, 2024

Hello
I want to parse my doc and read the attribute range, I had read the doc and the issue https://github.com/jhy/jsoup/pull/1114.I found some time, the org.jsoup.nodes.Attribute#sourceRange return null. After test, I found if body tag is preceded by some tags, like p, span the org.jsoup.nodes.Attribute#sourceRange will return null,otherwise, it works normally. I don't know if this is a bug, is there some way to recognize it and get the range of attr?

  • my code:
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;

import java.io.FileInputStream;

public class TestMain {
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("tmp.html");
        String text = new String(fis.readAllBytes());
        Parser parser = org.jsoup.parser.Parser.htmlParser();
        parser.setTrackPosition(true);
        Document doc = Jsoup.parse(text,parser);
        for (Element element : doc.getAllElements()){
            Attribute targetAttr = element.attribute("target");
            if (targetAttr !=  null){
                System.out.println(targetAttr.sourceRange());
            }
        }
    }
}
  • tmp.html
<!DOCTYPE html>
<head></head>
<span></span>
<body target="text">
</body>
</html>
  • result
-1,-1:-1--1,-1:-1=-1,-1:-1--1,-1:-1
  • tmp.html
<!DOCTYPE html>
<head></head>
<body target="text">
</body>
</html>
  • result(expect result)
3,7:38-3,13:44=3,15:46-3,19:50
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

1 participant