javascript - Logic Flow Flaw in custom html builder -



javascript - Logic Flow Flaw in custom html builder -

i set out create simple html builder myself works this:

my syntax follows:

id1,id2 <- comma separated produce 2 div groups

<div id="id1"> <div id="id1inner"></div> </div> <div id="id2"> <div id="id2inner"></div> </div>

you can inner classes clearfix added inner parent such: (* denotes class, ! denotes id)

id1*class1*class2,id2!id1!id2

<div id="id1"> <div id="id1inner" class="clearfix"> <div class="class1"></div> <div class="class2"></div> </div> </div> <div id="id2"> <div id="id2inner" class="clearfix"> <div id="id1"></div> <div id="id2"></div> </div> </div>

the problem happens when seek mix adding inner classes (*) , ids (!) such this:

id1*class1!id1

this should create next block - @ loss how there.

<div id="id1"> <div id="id1inner" class="clearfix"> <div class="class1"></div> <div id="id1"></div> </div> </div>

live preview script: http://labs.remixdesigninc.com/builder/index.html

code reference:

$(document).ready(function(){ window.buildit = function(htmlitems){ count = htmlitems.length; // first character remove special characters (allow letters, number) (i=0; i<count; i++){ htmlitems[i] = htmlitems[i].replace(/^[^a-z]/i, ""); } // build html (x=0; x<count; x++){ var currentitemclass = htmlitems[x].indexof("*"); var currentitemid = htmlitems[x].indexof("!"); // check sub class or sub id if ((currentitemclass >= 0) || (currentitemid >= 0)){ // check sub class if (currentitemclass >= 0){ // classes var splitsubclass = htmlitems[x].split('*'); makestring = '<div id="' + splitsubclass[0].tolowercase() + '">\n\t<div id="' + splitsubclass[0].tolowercase() + 'inner" class="clearfix">\n'; (y=1; y<splitsubclass.length; y++){ makestring += '\t\t<div class="' + splitsubclass[y].tolowercase() + '"></div>\n'; } makestring += '\t</div>\n\</div>\n\n'; $("code").append(document.createtextnode(makestring)); } // check sub id if (currentitemid >= 0){ // ids var splitsubid = htmlitems[x].split('!'); makestring = '<div id="' + splitsubid[0].tolowercase() + '">\n\t<div id="' + splitsubid[0].tolowercase() + 'inner" class="clearfix">\n'; (z=1; z<splitsubid.length; z++){ makestring += '\t\t<div id="' + splitsubid[z].tolowercase() + '"></div>\n'; } makestring += '\t</div>\n\</div>\n\n'; $("code").append(document.createtextnode(makestring)); } } else { alert(htmlitems[x] + " not have * in it"); // no sub kid makestring = '<div id="' + htmlitems[x].tolowercase() + '">\n\t<div id="' + htmlitems[x].tolowercase() + 'inner">\n\t</div>\n </div>\n\n'; $("code").append(document.createtextnode(makestring)); } } } $("#btncreate").click(function(){ $("code").html(""); var htmlitems = $("#text").val(); if (htmlitems){ htmlitems = htmlitems.replace(/ /g, ""); //remove spaces htmlitems = htmlitems.replace(/[^a-z0-9,\-_*!]/gi, ""); //remove special characters (allow letters, numbers, , _ -) htmlitems = htmlitems.split(','); //create array out of items using comma separator if(htmlitems != ""){ buildit(htmlitems); } else { alert("nothing create."); } $("#text").val(htmlitems); } else { alert("nothing create."); } homecoming false; }); });

the problem having comes fact 1 time identify subitem class or id, never take business relationship there mixed classes , ids in same div. improve off finding outer id consuming subitem string piece @ time create each kid item. seek this:

window.buildit = function(htmlitems){ count = htmlitems.length; // first character remove special characters (allow letters, number) (i=0; i<count; i++){ htmlitems[i] = htmlitems[i].replace(/^[^a-z]/i, ""); } // build html (x=0; x<count; x++){ var item = htmlitems[x]; var firstsubclasspos = item.indexof("*"); var firstsubidpos = item.indexof("!"); if (firstsubclasspos < 0) { firstsubclasspos = item.length; } if (firstsubidpos < 0) { firstsubidpos = item.length; } var subitemstart = firstsubclasspos < firstsubidpos ? firstsubclasspos : firstsubidpos; var outerid = item.substring(0, subitemstart); //make outer divs var makestring = '<div id="' + outerid.tolowercase() + '">\n\t'; var subitems = item.substring(subitemstart, item.length); if (subitems.length > 0) { makestring += '<div id="' + outerid.tolowercase() + 'inner" class="clearfix">\n'; } else { makestring += '<div id="' + outerid.tolowercase() + 'inner">\n'; } while(subitems.length > 0) { var isclass = subitems[0] == '*'; subitems = subitems.substring(1, subitems.length); var nextclasspos = subitems.indexof('*'); var nextidpos = subitems.indexof('!'); if (nextclasspos < 0) { nextclasspos = subitems.length; } if (nextidpos < 0) { nextidpos = subitems.length; } var nextdelimiterpos = nextclasspos < nextidpos ? nextclasspos : nextidpos; var marker = subitems.substring(0, nextdelimiterpos); var markertype = isclass ? 'class' : 'id'; makestring += '\t\t<div ' + markertype + '="' + marker + '"></div>\n'; subitems = subitems.substring(nextdelimiterpos, subitems.length); } //close our outer divs makestring += '\t</div>\n</div>\n\n'; $("code").append(document.createtextnode(makestring)); } }

fiddle

javascript jquery html

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -