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

Can't get current coordinates of element #21

Open
LeMoussel opened this issue Dec 28, 2015 · 1 comment
Open

Can't get current coordinates of element #21

LeMoussel opened this issue Dec 28, 2015 · 1 comment
Milestone

Comments

@LeMoussel
Copy link

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();
@FlorianRappl
Copy link
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.

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

No branches or pull requests

2 participants