Possibly a bug in HtmlElement.GetAttribute()
I found that calling the HtmlElement.GetAttribute() method results in an JavaScriptException - ReferenceError when trying to read the custom html attribute data-*
For example, when you have DIV with custom attribute data-text, the error is:
JavaScriptException - ReferenceError: text is not defined Source=LINQPad.GUI -LINQPad.UI.OutputWebBrowser2+<RunScriptAsync>d__48 Void MoveNext() offset: 0x1B4 -System.Runtime.ExceptionServices.ExceptionDispatchInfo Void Throw() offset: 0x11 -System.Runtime.CompilerServices.TaskAwaiter Void HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task, System.Threading.Tasks.ConfigureAwaitOptions) offset: 0x2D -System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter TResult GetResult() offset: 0x0 -LINQPad.UI.ResultsWebBrowser2+<>c__DisplayClass23_0+<<OnHtmlActionRequested>g__GoBSync|0>d Void MoveNext() offset: 0xC25 -System.Runtime.ExceptionServices.ExceptionDispatchInfo Void Throw() offset: 0x11 -System.Runtime.ExceptionServices.ExceptionDispatchInfo Void Throw(System.Exception) offset: 0x0 ...
Example code to reproduce:
<Query Kind="Statements"> <Namespace>LINQPad.Controls</Namespace> </Query> EventHandler<PropertyEventArgs> ehObjClick = new EventHandler<PropertyEventArgs>(HandleObjClick); Div d = new Div(); d.HtmlElement.InnerText="Click me.."; d.Styles["width"] = "5em"; d.Styles["height"] = "5em"; d.Styles["border"] = "1px black solid"; d.HtmlElement.SetAttribute("data-text", "text1"); d.Click += (o, e) => ehObjClick(o, new PropertyEventArgs()); d.Dump(); void HandleObjClick(object sender, PropertyEventArgs args) { var elem = sender as LINQPad.Controls.Core.HtmlElement; if (elem != null) { XElement.Parse(elem.ToString()).Dump(); string dataText = (string)elem.InvokeScript(true, "eval", $"targetElement.getAttribute('data-text')"); $"OK: data-text: {dataText}".Dump("Get Attribute using InvokeScript (targetElement.getAttribute()):"); string err = string.Empty; try { dataText = elem.GetAttribute("data-text"); } catch (Exception ex) { err = ex.Message; } err.Dump("Get Attribute using HtmlElement.GetAttribute():"); } }
Comments
Thanks - will fix this in the next build.
Note that you can also work around it by fetching the attribute as follows:
I hadn't thought of that, thanks..