webview / webview

Tiny cross-platform webview library for C/C++. Uses WebKit (GTK/Cocoa) and Edge WebView2 (Windows).

Home Page:https://webview.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot use set_html with string longer than 1572834 characters on Windows

rcorre opened this issue · comments

What OS are you using (uname -a, or Windows version)?

Windows 10, 19045.4291.

IE: 11.0.19041.3636

What programming language are you using (C/C++/Go/Rust)?

C++, MSVC v142

What did you expect to see and what you saw instead?

If I try to use set_html with a string longer than 1,572,834 characters on windows, the HTML never loads. The devtools inspector shows an empty HTML document. A string of exactly 1,572,834 characters loads fine and quickly, but 1,572,835 never loads. FWIW, I suspect that due to UTF-16 conversion in widen_string, the string is effectively doubled in size.

I cannot repro this on mac, and have been able to use set_html with significantly longer strings there.

You can repro with the attached script, just pass the desired html size as an argument:

#include <iostream>
#include <string>
#include "webview.h"

int main( int argc, char* argv[] )
{
    if ( argc < 2 ) {
        std::cerr << "Usage: ./example <size>" << std::endl;
        return -1;
    }
    int size = std::stoi( argv[ 1 ]);
    std::cout << "Using HTML of size: " << size << std::endl;
    std::string html( size, 'a');

    ::webview::webview w( true, nullptr );
    w.set_title( "example");
    w.set_size( 1024, 512, WEBVIEW_HINT_NONE );
    w.set_html( html );
    w.run();
    return 0;
}

Thanks for the report.

I checked the documentation of WebView2 and it states the following:

The htmlContent parameter may not be larger than 2 MB (2 * 1024 * 1024 bytes) in total size. The origin of the new page is about:blank.

1,572,834 characters is closer to 3 MB after conversion to UTF-16.

I suspect the only way around that is to navigate to a file, virtual hostname or with a custom URI scheme, but those need to be implemented in the library unless you use the native handles and do it yourself.

Ah, thank you! I was searching for a documented limitation but couldn't find it.

I know this is a different context, but I've recently been using QWebViewEngine (which is a Chomium wrapper) of the QT libraries. And, on the setHtml method documentation we read, “Content larger than 2 MB cannot be displayed, because setHtml() converts the provided HTML to percent-encoding and places data: in front of it to create the URL that it navigates to.”

https://doc.qt.io/qt-6/qwebengineview.html#setHtml

I'm not quite sure exactly how the numbers add up to 2 MiB but here's my partial guess.

Length Data
29 data:text/html;charset=utf-8,
1572834 content
1 null

These numbers add up to 1572864 bytes or 1.5 MiB before conversion to UTF-16.