diff --git a/bbcode/bbcode.js b/bbcode/bbcode.js index 51a7cb74adabda3e111b314df2ff8463e888e9d9..6096a709a9c90838b71df218323286cda54c226f 100644 --- a/bbcode/bbcode.js +++ b/bbcode/bbcode.js @@ -1,29 +1,31 @@ +/* Element containing the BB-code */ let el = document.getElementsByClassName("nfo")[0].children[0] let str = el.innerText var r = /\[(\/)?(\w{0,5})(=([^\]]*))?\]/g -let result; let stk = []; let result_str = ""; -let last_idx = 0; -let other; +/* Index of first unhandled character in input string + * Should guarantee that nothing gets parsed twice */ +let last_idx = 0; +let result; while ((result = r.exec(str)) !== null) { index = result.index var [matched, end, tag, _, param] = result if (! end) { /* if start tag */ - o = Object() - o.tag = tag - o.str = "" - o.index = index + matched.length - o.param = param + o = { tag: tag + , str: "" + , index: index + matched.length + , param: param + } if (stk.length === 0) { result_str += str.substring(last_idx, index); } else { - other = stk.last(); + var other = stk.last(); other.str += str.substring(other.index, index); } last_idx = o.index; @@ -40,10 +42,11 @@ while ((result = r.exec(str)) !== null) { var s; switch (parent.tag) { case "url": + s = "<a href='" if (typeof parent.param === "undefined") { - s = "<a href='" + parent.str; + s += parent.str; } else { - s = "<a href='" + parent.param; + s += parent.param; } s += "'>" + parent.str + "</a>"; break;