-
-
Notifications
You must be signed in to change notification settings - Fork 24
Open
Labels
Milestone
Description
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();
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
FlorianRappl commentedon Dec 28, 2015
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.