Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created June 10, 2010 01:57
Show Gist options
  • Save indexzero/432469 to your computer and use it in GitHub Desktop.
Save indexzero/432469 to your computer and use it in GitHub Desktop.
var html = "<div><div>foo</div></div>"
var split = html.split(/(<[^<\>]*>)/);
var jup = "";
// Simple state machine:
// 'start' : Last Saw Start Tag
// 'content': Last Saw Content
// 'end' : Last Saw End
// 'unit' : Unit Tag
var state = "start";
var validators = [
function(str) {
return str.indexOf("/>") !== -1 ? "unit" : "continue";
},
function(str) {
return str.indexOf("</") !== -1 ? "end" : "continue";
},
function(str) {
return str.indexOf("<") !== -1 ? "start" : "continue";
},
function(str) {
return "content";
}
];
for(slice in split) {
if(split[slice] && split[slice].length > 0) {
var tagType = "continue";
for(i=0;tagType == "continue";i++) {
tagType = validators[i](split[slice]);
}
switch(tagType) {
case "unit":
jup += "[\"" + split[slice].replace("<","").replace("/>","") + "\"],";
break;
case "start":
jup += "[\"" + split[slice].replace(/(<|>)/g, "") + "\",";
break;
case "content":
jup += "\"" + split[slice] + "\"";
break;
case "end":
jup += "]";
break;
}
state = tagType;
}
}
console.log(jup);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment