JavaScriptでSXMLを使う(__noSuchMethod__)
neat sxmlを、JavaScriptでやってみようとした。
with({__noSuchMethod__:function(){}}){ x(); // ReferenceError: x is not defined }
がうまくいかなかったので、見た目が汚い。
function sxml2dom(list) { var tagName = list[0], childs = list.slice(1); var elem = document.createElement(tagName); for (var i = 0;i < childs.length;i++) { for (var j = 0;j < childs[i].length;j++) { var child = childs[i][j]; if (child[0][0] == "$"){ var str = child[0].slice(1); var prop = str == "className" ? "class" : str.replace(/[A-Z]/g,function(c) "-" + c.toLowerCase()); elem.setAttribute(prop, child[1]); } else { if (typeof child == "string") elem.appendChild(document.createTextNode(child)); else elem.appendChild(sxml2dom(child)); } } } return elem; } var _ = { __noSuchMethod__: function(id, args) [id, args] }; sxml2dom( _.html( _.head(_.title("x")), _.body(_.$id("wrapper"), _.div(_.$className("content"), _.a(_.$style("color:blue"), _.$href("http://google.co.jp/"), "Google"))))); sxml2dom( _.head( _.meta(_.$httpEnv("Content-Type"), _.$content("text/css")) ) );