Bow Tie Konverter

by Ryan Perez

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<form id="area">
		<textarea id="input-text" ></textarea>
		<button type="button" id="convert" >Convert</button>
		<textarea id="oput-text" autocomplete="off" wrap="logical" spellcheck="false"></textarea>
	</form>
	<div id="temp" style="display:none;"></div>

CSS

#area {
			width:"800";
			text-align:center;
		}
		#input-text{
			width:80%;
			height:300px;
		}
		#oput-text{
			width:80%;
			height:300px;
		}
		#convert{
			width:30%;
		}

JavaScript

var convertarray = [
			"&nbsp",  
			"&hellip;",
			"&ndash;",
			"&quot;",
			"&rdquo;",
			'&ldquo;',
			'&rsquo;',
			'&lsquo;',
			'&amp;',
			'\u00BD',
			"\u2019",
			'\u2026',
			'\u201C',
			'\u201D',
			'<b>',
			'<i>',
	
		];
		var resultarray = [
			' ',  
			'...',
			'-',
			"'",
			'"',
			'"',
			'"',
			'"',
			'&',
			'1/2',
			"'",
			'...',
			'"',
			'"',
			'<strong>',
			'<em>',
	
		];

		$(document).ready(function(){

			var result;

			function getElem(value){
				var addEndTag = false;
				if(value.nodeType == 1){
					if(value.childNodes.length == 0 ||
						value.childNodes.length ==1 && value.childNodes[0].nodeType == 3 && value.childNodes[0].data.replace(/ /g, '').length == 0)
						;
					else {
						result += "<" + value.nodeName.toLowerCase() + ">";
						addEndTag = true;
						for(var i = 0; i < value.childNodes.length; i++){
							getElem(value.childNodes[i]);
						}
					}
				}
				if(value.nodeType == 3){
					result += value.data;
				}
				if(addEndTag){
					result += "</" + value.nodeName.toLowerCase() + ">";
				}
			}

			$("#convert").click(function(){
				
				var input="<div>"+$('#input-text').val()+"</div>";
								
				for (var i=0; i < convertarray.length; i++) {
				  	var find = convertarray[i];
				  	var re = new RegExp(find,'g');
				  	input = input.replace(re,resultarray[i]);
				} 

				result = "";

				$(input).each(function() {

					console.log(this);
				    getElem(this);
				});

				if (result=='')
					result = input;

			 
				  result = result.replace(/ +/g, ' '); //renive susccessive space
				  result = result.replace(/<[^\/>][^>]*><\/[^>]+>/g,""); //remove empty tag

				  result = result.substr(5,result.length-11);

				  $("#oput-text").val(result); 


				// $('#oput-text').val(inputtext);

			});

   		});