HtmlFormatHelper.GetStaticFragment(String) Method

Definition

Gets a string that represents an HTML fragment.

public:
 static Platform::String ^ GetStaticFragment(Platform::String ^ htmlFormat);
 static winrt::hstring GetStaticFragment(winrt::hstring const& htmlFormat);
public static string GetStaticFragment(string htmlFormat);
function getStaticFragment(htmlFormat)
Public Shared Function GetStaticFragment (htmlFormat As String) As String

Parameters

htmlFormat
String

Platform::String

winrt::hstring

The formatted HTML.

Returns

String

Platform::String

winrt::hstring

An HTML fragment based on the formatted HTML.

Examples

if (shareOperation.data.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.html)) {
    document.getElementById("htmlContentArea").className = "unhidden";
    
    shareOperation.data.getHtmlFormatAsync().then(function (html) {
        if (html !== null) {
            var htmlFragment = Windows.ApplicationModel.DataTransfer.HtmlFormatHelper.getStaticFragment(htmlFormat);

            // Set the innerHTML of the ifram to the HTML fragment.
            var iFrame = document.getElementById("htmlContent");
            iFrame.style.display = "";
            iFrame.contentDocument.documentElement.innerHTML = htmlFragment;
        } 
    });

    // Loop through any images and use the resourceMap to map each image element's src.
    var images = iFrame.contentDocument.documentElement.getElementsByTagName("img");
    if (images.length > 0) {
        shareOperation.data.getResourceMapAsync().done(function (resourceMap) {
            if (resourceMap.size > 0) {
                for (var i = 0, len = images.length; i < len; i++) {
                    var streamReference = resourceMap[images[i].getAttribute("src")];
                    if (streamReference) {
                        // Call a helper function to map the image element's src to a corresponding blob URL generated from the streamReference
                        setResourceMapURL(streamReference, images[i]);
                    }
                }
            }
        });
}
//shareOperation.reportCompleted();

Remarks

As a security precaution, you shouldn't display HTML unless you're sure it doesn't have any dynamic content. You can use the GetStaticFragment method to get shared HTML content without any dynamic elements such as script tags.

Applies to