[SOLVED] TypeError: $thisType.htmlDocument is null

Please report bugs here. If you are unsure whether something is a bug or an expected behavior, please post it on the "Technical Support" forum instead, and wait for a moderator to handle/move the post.
joaofig
Posts: 9
Joined: Sat Oct 28, 2017 6:32 am

[SOLVED] TypeError: $thisType.htmlDocument is null

Postby joaofig » Thu Nov 23, 2017 7:00 am

Starting with the latest Beta 12.6, I am now seeing this error raised at function INTERNAL_HtmlDomManager_CreateDomFromStringAndAppendIt

Chrome reports it here:

Code: Select all

function INTERNAL_HtmlDomManager_CreateDomFromStringAndAppendIt (domAsString, parentRef, associatedUIElement) {
    if (true) {
      // Next line throws the error: Uncaught TypeError: Cannot read property 'createElement' of null
      var obj = $thisType.htmlDocument['createElement']("div");
      obj.innerHTML = domAsString;
      var obj2 = obj.firstChild;
      obj2.associatedUIElement = associatedUIElement;
      parentRef['appendChild'](obj2);
      var result = obj2;
    } else {
      var text = $T1E()['CreateNew']();
      var uniqueIdentifier = JSIL.IgnoredMember("get_UniqueIdentifier()", $asm_CSharpXamlForHtml5.DotNetForHtml5.Core.INTERNAL_HtmlDomElementReference.$Cast(parentRef)).LValue;
      var javaScriptToExecute = $T1B()['Format']("\r\n// Create a temporary parent div to which we can write the innerHTML, then extract the contents:\r\nvar tempDiv = document.createElement(\"div\");\r\ntempDiv.innerHTML = \"{0}\";\r\nvar newElement = tempDiv.firstChild;\r\nnewElement.setAttribute(\"id\", \"{1}\");\r\nvar parentElement = document.getElementById(\"{2}\");\r\nparentElement.appendChild(newElement);\r\n", System.String.Replace(System.String.Replace(System.String.Replace(domAsString, "\"", "'"), "\r", ""), "\n", ""), text, uniqueIdentifier);
      JSIL.IgnoredMember("ExecuteJavaScript(javaScriptToExecute, commentForDebugging)", javaScriptToExecute, null).LValue;
      JSIL.IgnoredMember("INTERNAL_idsToUIElements", $thisType).LValue['Add'](text, associatedUIElement);
      JSIL.IgnoredType("DotNetForHtml5.Core.INTERNAL_HtmlDomElementReference");
    }
    return result;
  };


Is there anything I can do about this?
TIA,
JP

JS-Support @Userware
Site Admin
Posts: 927
Joined: Tue Apr 08, 2014 3:42 pm

Re: TypeError: $thisType.htmlDocument is null

Postby JS-Support @Userware » Thu Nov 23, 2017 7:08 am

Thanks for reporting this issue. We are going to investigate and fix it right away.

Regards,
JS-Support

joaofig
Posts: 9
Joined: Sat Oct 28, 2017 6:32 am

Re: TypeError: $thisType.htmlDocument is null

Postby joaofig » Fri Nov 24, 2017 3:35 am

This is somehow related to the FileOpenDialog plugin that you published. When I remove the control from the page, the error goes away. I did copy and paste your code into my project, no modifications added.

Hope this helps!

joaofig
Posts: 9
Joined: Sat Oct 28, 2017 6:32 am

Re: TypeError: $thisType.htmlDocument is null

Postby joaofig » Fri Nov 24, 2017 3:55 am

I managed to work around this issue by removing the call to CSharpXamlForHtml5.DomManagement.SetHtmlRepresentation. Here's my final code:

Code: Select all

        public ControlForDisplayingTheFileOpenDialog()
        {
            ResultKind = FileOpenDialog.ResultKind.Text; //Note: this is to set the default value of the property.

            //CSharpXamlForHtml5.DomManagement.SetHtmlRepresentation(this,
            //    "<input type='file'></input>");

            this.Loaded += ControlForDisplayingAFileOpenDialog_Loaded;
        }


The handler was changed to:

Code: Select all

void ControlForDisplayingAFileOpenDialog_Loaded(object sender, RoutedEventArgs e)
{
    // Get the reference to the "input" element:
    var div = Interop.GetDiv(this);
    object inputElement = Interop.ExecuteJavaScript("(function(){var input = document.createElement('input'); input.type = 'file'; $0.innerHTML=''; $0.appendChild(input); return input; })()", div);

    Action<object> onFileOpened = (result) =>
    {
        this.FileOpened?.Invoke(this, new FileOpenedEventArgs(result, this.ResultKind));
    };
    string resultKindStr = _resultKindStr; // Note: this is here to "capture" the variable so that it can be used inside the "addEventListener" below.

    // Apply the "Filter" property to limit the choice of the user to the specified extensions:
    SetFilter(this.Filter);

    // Listen to the "change" property of the "input" element, and call the callback:
    CSHTML5.Interop.ExecuteJavaScript(@"
        $0.addEventListener(""change"", function(e) {
            if(!e) {
                e = window.event;
            }
            var fullPath = $0.value;
            var filename = '';
            if (fullPath) {
                var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
                filename = fullPath.substring(startIndex);
                if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
                    filename = filename.substring(1);
                }
            }
            var input = e.target;
            var file = input.files[0];
            var reader = new FileReader();
            reader.onload = function() {
                var callback = $1;
                var result = reader.result;
                //if (file.type == 'text/plain')
                callback(result);
            };
            var resultKind = $3;
            if (resultKind == 'DataURL') {
                reader.readAsDataURL(file);
            }
            else {
                reader.readAsText(file);
            }
            var isRunningInTheSimulator = $2;
            if (isRunningInTheSimulator) {
                alert(""The file open dialog is not supported in the Simulator. Please test in the browser instead."");
            }
        });", inputElement, onFileOpened, CSHTML5.Interop.IsRunningInTheSimulator, resultKindStr);
}


Nothing else needs to be changed.

JS-Support @Userware
Site Admin
Posts: 927
Joined: Tue Apr 08, 2014 3:42 pm

Re: TypeError: $thisType.htmlDocument is null

Postby JS-Support @Userware » Thu Nov 30, 2017 12:55 am

Hi,

This issue has been fixed in Beta 12.7 and newer.

Thanks.
Regards,
JS-Support


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 3 guests

 

 

cron