Skip to content

Attribute#sourceRange return null when parse body attribute #2204

Closed

Description

@runt0

Hello
I want to parse my doc and read the attribute range, I had read the doc and the issue #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

Activity

  1. added
    bugA confirmed bug, that we should fix
    on Nov 22, 2024
  2. self-assigned this
    on Nov 22, 2024
  3. added this to the 1.18.2 milestone on Nov 22, 2024
  4. jhy commented on Nov 22, 2024

    @jhy
    Owner

    Likely happening because the first span creates the actual body element, and then we copy the second bodys attributes over and miss the range.

  5. added
    fixedAn {bug|improvement} that has been {fixed|implemented}
    on Nov 22, 2024
  6. jhy commented on Nov 22, 2024

    @jhy
    Owner

    Thanks for the clear report; fixed!

  7. added a commit that references this issue on Nov 23, 2024
    91b5a56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions