"use strict";
if(localStorage.length == 0){
	localStorage.setItem('todos','[]');
}

var savedTextContent = null;
var todos = JSON.parse(localStorage.todos);
var input = document.querySelector('.new-todo');
var ul = document.querySelector('.todo-list');
var li = null;
var span = null;
var button = null;

function addItem(textContent){
	todos.push(textContent);
	localStorage.todos = JSON.stringify(todos);
}

function editableItem(textContent){
	for(var i = 0; i < todos.length; i++){
		if(savedTextContent == todos[i]){
			todos.splice(i,1,textContent);
			localStorage.todos = JSON.stringify(todos);
		}
	}
}

function removeItem(textContent){
	for(var i = 0; i < todos.length; i++){
		if(textContent == todos[i]){
			todos.splice(i,1);
			localStorage.todos = JSON.stringify(todos);
		}
	}
}

function check(textContent){
	var duplicate = false;
	for(var i = 0; i <= todos.length; i++){
		if(textContent == todos[i] || textContent == ''){
			duplicate = true;
		}
	}
	return duplicate;
}

function addEvents(li, span, button){
	span.addEventListener('dblclick', function (){
		button.style.display = 'none';
		span.contentEditable = true;
		span.focus();
		savedTextContent = span.textContent;
	}, false);

	span.addEventListener('keypress', function (e){
		if(e.keyCode == 13 && !check(span.textContent)){
			button.style.display = 'block';
			span.contentEditable = false;
			if(span.textContent != savedTextContent){
				editableItem(span.textContent);
			}
		}
	}, false);

	span.addEventListener('blur', function (){
		if(!check(span.textContent)){
			button.style.display = 'block';
			span.contentEditable = false;
			if(span.textContent != savedTextContent){
				editableItem(span.textContent);
			}
		}
	}, false);
	
	button.addEventListener('click', function (){
		ul.removeChild(li);
		removeItem(span.textContent);
	}, false);
}

if(todos.length != 0){
	for(var i = 0; i < todos.length; i++){
		li = document.createElement('li');
		span = document.createElement('span');
		button = document.createElement('button');

		span.textContent = todos[i];
		addEvents(li, span, button);

		ul.appendChild(li);
		li.appendChild(span);
		li.appendChild(button);
	}
}

input.addEventListener('keypress', function (e){
	if(e.keyCode == 13 && !check(input.value)){
		li = document.createElement('li');
		span = document.createElement('span');
		button = document.createElement('button');

		span.textContent = input.value;
		addEvents(li, span, button);

		ul.appendChild(li);
		li.appendChild(span);
		li.appendChild(button);

		input.value = '';
		addItem(span.textContent);
	}
}, false);
@@ -1197,32 +1197,35 @@
 ;%0A%09%7D, false);%0A%0A%09
+// 
 span.addEventLis
@@ -1253,24 +1253,27 @@
 ction (e)%7B%0A%09
+// 
 %09if(e.keyCod
@@ -1311,170 +1311,391 @@
 ent)
-)%7B%0A%09%09%09button.style.display = 'block';%0A%09%09%09span.contentEditable = false;%0A%09%09%09if(span.textContent != savedTextContent)%7B%0A%09%09%09%09editableItem(span.textContent);%0A%09%09%09%7D%0A%09%09%7D%0A%09
+ && span.textContent != savedTextContent)%7B%0A%09// %09%09span.contentEditable = false;%0A%09// %09%09button.style.display = 'block';%0A%09// %09%09editableItem(span.textContent);%0A%09// %09%7D else if(e.keyCode == 13 && check(span.textContent) %7C%7C span.textContent == savedTextContent)%7B%0A%09// %09%09span.contentEditable = false;%0A%09// %09%09button.style.display = 'block';%0A%09// %09%09span.textContent = savedTextContent;%0A%09// %09%7D%0A%09// 
 %7D, f
@@ -1778,16 +1778,49 @@
 tent))%7B%0A
+%09%09%09span.contentEditable = false;%0A
 %09%09%09butto
@@ -1853,44 +1853,121 @@
 %0A%09%09%09
-span.contentEditable = false;%0A%09%09%09if(
+editableItem(span.textContent);%0A%09%09%7D else%7B%0A%09%09%09span.contentEditable = false;%0A%09%09%09button.style.display = 'block';%0A%09%09%09
 span
@@ -1971,33 +1971,32 @@
 pan.textContent 
-!
 = savedTextConte
@@ -2001,51 +2001,9 @@
 tent
-)%7B%0A%09%09%09%09editableItem(span.textContent);%0A%09%09%09%7D
+;
 %0A%09%09%7D

Patch todo list