"use strict";

/**
 * @constructor
 */
function SerialAdapter(element)
{
    var 
        serial = this,
        send_char;

    this.enabled = true;

    this.init = function(code_fn)
    {
        this.destroy();

        send_char = code_fn;

        element.addEventListener("keypress", keypress_handler, false);
        element.addEventListener("keydown", keydown_handler, false);
        element.addEventListener("paste", paste_handler, false);
    };

    this.destroy = function() 
    {
        element.removeEventListener("keypress", keypress_handler, false);
        element.removeEventListener("keydown", keydown_handler, false);
        element.removeEventListener("paste", paste_handler, false);
    };

    this.put_str = function(str)
    {
        if(str === "\x08")
        {
            var text = element.textContent;
            element.textContent = text.substr(0, text.length - 1);
        }
        else
        {
            element.textContent += str;

            if(str === "\n")
            {
                element.scrollTop = 1e9;
            }
        }
    }

    function may_handle(e)
    {
        if(!serial.enabled)
        {
            return false;
        }

        // Something here?

        return true;
    }

    function keypress_handler(e)
    {
        if(!may_handle(e))
        {
            return;
        }

        var chr = e.keyCode;

        send_char(chr);
        e.preventDefault();
    }

    function keydown_handler(e)
    {
        var chr = e.keyCode;

        if(chr === 8)
        {
            // supress backspace
            send_char(127);
            e.preventDefault();
        }
    }

    function paste_handler(e)
    {
        //console.log(e.clipboardData.getData('text/plain'));

        var data = e.clipboardData.getData('text/plain');

        for(var i = 0; i < data.length; i++)
        {
            send_char(data.charCodeAt(i));
        }

        e.preventDefault();
    }
}

@@ -818,27 +818,21 @@
 element.
-textContent
+value
 ;%0A      
@@ -845,27 +845,21 @@
 element.
-textContent
+value
  = text.
@@ -888,32 +888,109 @@
 - 1);%0A        %7D%0A
+        else if(str === %22%5Cr%22)%0A        %7B%0A            // do nothing %0A        %7D%0A
         else%0A   
@@ -1020,19 +1020,13 @@
 ent.
-textContent
+value
  += 

Use textarea.value over textContent