Skip to content

Instantly share code, notes, and snippets.

@Marak
Forked from tmpvar/htmltojup_almost
Created June 10, 2010 01:39
Show Gist options
  • Select an option

  • Save Marak/432457 to your computer and use it in GitHub Desktop.

Select an option

Save Marak/432457 to your computer and use it in GitHub Desktop.
var intag = false, ret = [], tmpbuf = "", endtag = false;
for (var i=0; i<html.length; i++)
{
var char = html[i];
if (char === '<') {
intag = true;
if (tmpbuf.length > 0) {
ret.push(tmpbuf.replace("/",""));
tmpbuf = "";
}
} else if (char === '>') {
intag = false;
if (tmpbuf.length > 0) {
var explode = tmpbuf.split(" ");
var tag = explode.shift();
if (tag.indexOf("/") === -1) {
ret.push(tag);
var attrs ={}, attr, key, value;
for (var j = 0; j < explode.length; j++) {
attr = explode[j].split("=");
attrs[attr[0]] = attr[1];
}
ret.push(attrs);
}
tmpbuf="";
}
} else {
tmpbuf += char;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment