In my development project, I am creating a .NET wrapper for the popular java-script library called Linkify.js, utilizing Microsoft's ClearScript.
The challenge I am facing involves implementing the attributes property within the options object parameter for the linkifyHtml function.
This parameter in the java-script code should look like this:
attributes: {
title: "External Link",
etc: "etc..."
}
To tackle this issue, I came up with the idea of defining a Attributes
property that returns an anonymous type:
Public Class LinkifyFormatOptions
Public Property Attributes As Func(Of LinkifyLinkType, String, Object) =
Function(linkType As LinkifyLinkType, href As String) As Object
Select Case linkType
Case LinkifyLinkType.Url
Return New With {.test_attribute = "This is a test attribute"}
Case Else
Return Nothing
End Select
End Function
End Class
... which is then used to build the options object like this:
Friend Function GetLinkifyOptions() As Object
Dim options As New With {
.attributes = Function(href As String, linkType As String) As Object
Select Case linkType
Case "url"
Return Me.Attributes(LinkifyLinkType.Url, href)
Case "email"
Return Me.Attributes(LinkifyLinkType.Email, href)
Case "hashtag"
Return Me.Attributes(LinkifyLinkType.Hashtag, href)
Case "mention"
Return Me.Attributes(LinkifyLinkType.Mention, href)
Case "ticket"
Return Me.Attributes(LinkifyLinkType.Ticket, href)
Case Else
Return Nothing
End Select
End Function,
.className = etc...,
.formatHref = etc...,
.etc = etc...
}
End Function
... and eventually utilized in calling the java-script's linkifyHtml
function:
Imports Microsoft.ClearScript
Imports Microsoft.ClearScript.V8
Public Shared Function Linkify(str As String,
[interface] As LinkifyInterface,
Optional plugins As LinkifyPlugins = LinkifyPlugins.None,
Optional formatOptions As LinkifyFormatOptions = Nothing) As String
' Validations and other operations...
Using engine As New V8ScriptEngine("linkify_engine", V8ScriptEngineFlags.DisableGlobalMembers)
Dim options As Object = formatOptions.GetLinkifyOptions()
' loading linkify.js scripts and more...
Dim linkifyFunction As ScriptObject = DirectCast(engine.Evaluate("linkifyHtml"), ScriptObject)
Return CStr(linkifyFunction.Invoke(asConstructor:=False, str, options))
End Using
End Function
The glitch arises from the fact that the ClearScript engine exposes unwanted members of the Object
type, such as "ToString", "GetHashCode", etc.
After invoking the java-script's linkifyHtml
function, the output shows unnecessary details like so:
<p>Domain: <a href="http://example.domain.com" $test_attribute="This is a test attribute" ToString="[object Object]" Equals="[object Object]" GetHashCode="[object Object]" GetType="[object Object]" Finalize="[object Object]" MemberwiseClone="[object Object]" test_attribute="This is a test attribute" toJSON="function toJSON() { [native code] }">example.domain.com</a></p>
Although I managed to solve some issues by setting the V8ScriptEngine.SuppressInstanceMethodEnumeration Property like this:
Using engine As New V8ScriptEngine("linkify_engine", V8ScriptEngineFlags.DisableGlobalMembers)
engine.SuppressInstanceMethodEnumeration = True
' and more...
End Using
There are still remnants like the reflected toJSON
function persisting in the output, and I'm unsure how to eliminate them:
<p>Domain: <a href="http://example.domain.com" $test_attribute="This is a test attribute" test_attribute="This is a test attribute" toJSON="function toJSON() { [native code] }">example.domain.com</a></p>
I aim for the desired output to be clean, devoid of the reflected toJSON
member and any symbol-prefixed members like $test_attribute
:
<p>Domain: <a href="http://example.domain.com" test_attribute="This is a test attribute">example.domain.com</a></p>
If possible, I'd appreciate guidance on how to hide the unwanted members and ensure a seamless operation using qualified types or specific attributes within the Microsoft.ClearScript
namespace.
I experimented with custom type definitions to override typical members and applied various attributes without success in masking the toJSON
function. Any insights on resolving this hurdle would be greatly appreciated.