Accessing clipboardData on cut.

by lddubeau

HTML

<p contenteditable="true">This is a para</p>

JavaScript

$(function () {
    var $p = $("p").first();
    $p.on("cut", function (e) {
        var cd = e.originalEvent.clipboardData || window.clipboardData;
        // cd.types absent means IE, means we can't use anything else than text or url.
        console.log("cut");
        cd.setData(cd.types ? "text/plain" : "text", "<foo>bwip</foo>");
        // As of 20140909 there does not seem to be any way to get any call to setData with application/xml, text/xml, text/x-foo, or anything of the sort to "take". The call seems to be just ignored.
        cd.setData(cd.types ? "text/x-foo" : "text", "<foo>bwip</foo>");
        // Note that this also aborts the cutting of the text from the HTML but we're testing only how to call setData.
        e.stopPropagation();
        e.preventDefault();
        return false;
    });
    $p.on("paste", function (e) {
        var cd = e.originalEvent.clipboardData || window.clipboardData;
        console.log("paste");
        console.log(cd.types);
        console.log(cd.getData("text/plain"));
    });

});