Skip to content

Can't get current coordinates of element #21

@LeMoussel

Description

@LeMoussel

I test few JavaScript functions (running on Awesonium) with AngleSharp Scripting
This test demonstrates that it is not possible to retrieve current coordinates of DOM element

ScriptingService javascriptService = new ScriptingService();
IConfiguration config = Configuration.Default.WithDefaultLoader().With(javascriptService).WithCss();
IDocument ashDocument = await BrowsingContext.New(config).OpenAsync("http://crawlbin.com/");
await Task.WhenAll(ashDocument.Requests);

string jsScript = @"
var JSAngleSharpExtension = {
    getPathTo: function(element) {
        if (element.id !== '' && element.id !== null) return '//*[@id=\'' + element.id + '\']';
        if (element === document.body) return element.tagName.toLowerCase();

        var ix = 0;
        var siblings = element.parentNode.childNodes;
        for (var i = 0; i < siblings.length; i++) {
            var sibling = siblings[i];
            if (sibling === element) return JSAngleSharpExtension.getPathTo(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
            if (sibling.nodeType === 1 && sibling.tagName === element.tagName) { ix++; }
        }
    },

    getPageXY: function(element) {
        var x = 0, y = 0;
        while (element) {
            x += element.offsetLeft;
            y += element.offsetTop;
            element = element.offsetParent;
        }
        return [x, y];
    },

    getLinks: function() {
        var nodeArray = [];
        var nodeList = document.links; 

        for (var i = 0; i < nodeList.length; ++i) {
            var xpathElement = JSAngleSharpExtension.getPathTo(nodeList[i]);
            nodeArray.push({
                                href: nodeList[i].href,
                                anchor: nodeList[i].textContent.replace(/[\n\r\t]+/g, ' ').trim(),
                                xPath: xpathElement,
                                position: JSAngleSharpExtension.getPageXY(nodeList[i])
                            });
        }
        return JSON.stringify(nodeArray);
    }
};

JSAngleSharpExtension.getLinks();
";

string jsonLinksdata = javascriptService.Engine.Execute(jsScript, new ScriptOptions() { Context = ashDocument.DefaultView, Document = ashDocument }).AsString();

Activity

FlorianRappl

FlorianRappl commented on Dec 28, 2015

@FlorianRappl
Contributor

How should that be possible? There is no rendering engine thus the DOM does not know anything about a potential layout.

Yes, this will be possible some time in the future, but never with AngleSharp + AngleSharp.Scripting alone. You will need at least one more library, which gives you a RenderingContext implementation with a whole render tree and CSS evaluation (not only parsing, but real evaluation in the context of a rendering device).

I mark it as enhancement without any milestone set. The test could then be used for some integration tests.

added this to the v0.7 milestone on Jul 18, 2016
modified the milestones: v0.7, v0.6 on Jan 5, 2019
modified the milestones: v0.12, v1.0, v0.13 on May 13, 2019
modified the milestones: v1.0, vNext on Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @FlorianRappl@LeMoussel

        Issue actions

          Can't get current coordinates of element · Issue #21 · AngleSharp/AngleSharp.Js