<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<script>

if (window.testRunner)
    testRunner.dumpAsText();

// Select an element an return its value.
function selectElement(doc, element)
{
    if (element.tagName.toLowerCase() == 'input') {
        element.focus();
        element.select();
        return element.value;
    } else {
        doc.getSelection().selectAllChildren(element);
        return element.innerText;
    }
}

function testCopyAndPasteTexts(fromDocument, fromName, toDocument, toName)
{
    var resultsElement = document.getElementById("results");

    var pasteboardElement = toDocument.getElementById("pasteboard");

    var fromElementNames = ["div", "transform", "secure", "control"];
    for (var i = 0; i < fromElementNames.length; i++) {
        var fromElementName = fromElementNames[i];
        var fromElement = fromDocument.getElementById(fromElementName);
        selectElement(fromDocument, fromElement);
        fromDocument.execCommand("Copy");

        var toElementNameSuffixes = ["text-control", "content-editable"];
        for (var j = 0; j < toElementNameSuffixes.length; j++) {
            var toElementNameSuffix = toElementNameSuffixes[j];

            var testName = "from " + fromName.toUpperCase() + " " + fromElementName + " to " + toName.toUpperCase() + " " + toElementNameSuffix + ": ";
            var toDescriptionText = toDocument.createTextNode(testName);
            pasteboardElement.appendChild(toDescriptionText);
            var toElement = null;
            if (toElementNameSuffix == "text-control") {
                toElement = toDocument.createElement("input");
                toElement.value = "FAIL";
            } else {
                toElement = toDocument.createElement("span");
                toElement.contentEditable = "true";
                toElement.innerHTML = "FAIL";
            }
            pasteboardElement.appendChild(toElement);
            pasteboardElement.appendChild(toDocument.createElement("br"));

            selectElement(toDocument, toElement);
            toDocument.execCommand("Paste");
            var actualText = selectElement(toDocument, toElement);

            var result = "PASS";
            var expectedLeadingString = "\\\\ from ";
            if (fromElementName == "secure")
                expectedLeadingString = "•••••••";
            else if (fromElementName == "transform")
                expectedLeadingString = expectedLeadingString.toUpperCase();
            if (actualText.search(expectedLeadingString) != 0)
                result = "FAIL: the actual text was '" + actualText + "' (char code of the first character was " + actualText.charCodeAt(0) + ")";

            resultsElement.innerHTML += testName + result + "<br>";
        }
    }
}

function test()
{
    testCopyAndPasteTexts(frames[0].document, "euc", document, "utf8");
    testCopyAndPasteTexts(document, "utf8", frames[0].document, "euc");
}

</script>
</head>

<body onload="test()">

<div>
<iframe src="resources/copy-backslash-euc.html" width="600" height="300"></iframe>
</div>

<h2>Original texts which will be copied</h2>
<div id="div">\ from UTF8 page</div>
<div id="transform" style="text-transform:uppercase">\ from UTF8 page (transformed)</div>
<div id="secure" style="-webkit-text-security:disc">\ from UTF8 page (secure)</div>
<input id="control" value="\ from UTF8 text control">

<h2>Texts in the EUC frame will be pasted here</h2>
<div id="pasteboard"></div>

<br>

<h2>Results</h2>
<p id="results">
</p>

</body>

</html>
