Created
July 28, 2013 05:42
-
-
Save aiba/6097544 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Functional HTML library. | |
* Requires $ and _. | |
*/ | |
(function(outerScopeObj) { | |
function toHTML(x) { | |
if (x.toHTML) { | |
return x.toHTML(); | |
} | |
if (typeof(x) == "string") { | |
return _.escape(x); | |
} | |
if (typeof(x) == "number") { | |
return (""+x); | |
} | |
if (Array.prototype.isPrototypeOf(x)) { | |
return _.map(x, function(y) { | |
return toHTML(y); | |
}).join(""); | |
} | |
return _.escape("<<?>>"); | |
} | |
//---------------------------------------------------------------- | |
// tags. | |
//---------------------------------------------------------------- | |
var neverSingletonsArray = [ | |
'TEXTAREA', 'SCRIPT', 'DIV', 'IFRAME', 'UL', 'TABLE' | |
]; | |
var neverSingletons = {}; | |
_.each(neverSingletonsArray, function(x) { | |
neverSingletons[x] = true; | |
}); | |
var neverSingletons = { | |
'TEXTAREA': true, | |
'SCRIPT': true, | |
'DIV': true, | |
'IFRAME': true, | |
'UL': true, | |
'TABLE': true | |
}; | |
function isAttrObj(o) { | |
return (o && | |
((typeof o) == "object") && | |
(! o.toHTML) && | |
(! Array.prototype.isPrototypeOf(o)) && | |
(! Date.prototype.isPrototypeOf(o))); | |
} | |
function makeTagFun(tagName) { | |
return function() { | |
var tag = []; | |
tag.name = tagName; | |
tag.attribs = {}; | |
var contents = Array.prototype.slice.call(arguments); | |
if (contents.length > 0) { | |
if (isAttrObj(contents[0])) { | |
tag.attribs = contents[0]; | |
contents.shift(); | |
} | |
_.each(contents, function(content) { | |
tag.push(content); | |
}); | |
} | |
// this behavior is relied on: | |
tag.toString = function() { | |
return this.toHTML(); | |
}; | |
tag.toHTML = function() { | |
var t = this; | |
var result = []; | |
result.add = function(x) { this.push(x); return this; }; | |
result.add('<').add(t.name); | |
if (t.attribs) { | |
_.each(t.attribs, function(v,k) { | |
if (k == "className") { k = "class"; } | |
if (k == "htmlFor") { k = "for"; } | |
if (!(v === undefined)) { | |
// escape quotes and newlines in values | |
v = String(v).replace(/\"/g, '\\"').replace(/\n/g, '\\n'); | |
result.add(' ').add(k).add('="').add(v).add('"'); | |
} | |
}); | |
} | |
if ((t.length < 1) && (!(t.name.toUpperCase() in neverSingletons))) { | |
result.add(' />'); | |
} else { | |
result.add('>'); | |
_.each(t, function(x) { | |
result.add(toHTML(x)); | |
}); | |
result.add('</').add(t.name).add('\n>'); | |
} | |
return result.join(""); | |
}; | |
return tag; | |
}; | |
}; | |
function importTags(scopeObj, tagArray) { | |
_.each(tagArray, function(arg) { | |
var funcName = arg.replace(/:/g, "_").replace(/-/g, "_"); | |
var tagName = arg.toLowerCase(); | |
scopeObj[funcName] = makeTagFun(tagName); | |
}); | |
} | |
var html_tags = | |
["A", "ABBR", "ACRONYM", "ADDRESS", "APPLET", "AREA", "B", | |
"BASE", "BASEFONT", "BDO", "BIG", "BLOCKQUOTE", "BODY", | |
"BR", "BUTTON", "CAPTION", "CENTER", "CITE", "CODE", "COL", | |
"COLGROUP", "DD", "DEL", "DIR", "DIV", "DFN", "DL", "DT", | |
"EM", "FIELDSET", "FONT", "FORM", "FRAME", "FRAMESET", | |
"H1", "H2", "H3", "H4", "H5", "H6", | |
"HEAD", "HR", "HTML", "I", "IFRAME", "IMG", "INPUT", | |
"INS", "ISINDEX", "KBD", "LABEL", "LEGEND", "LI", "LINK", | |
"MAP", "MENU", "META", "NOFRAMES", "NOSCRIPT", "OBJECT", | |
"OL", "OPTGROUP", "OPTION", "P", "PARAM", "PRE", "Q", "S", | |
"SAMP", "SCRIPT", "SELECT", "SMALL", "SPAN", "STRIKE", | |
"STRONG", "STYLE", "SUB", "SUP", "TABLE", "TBODY", "TD", | |
"TEXTAREA", "TFOOT", "TH", "THEAD", "TITLE", "TR", "TT", | |
"U", "UL", "VAR", "XMP"]; | |
importTags(outerScopeObj, html_tags); | |
})(window); | |
$.fn.funhtml = function() { | |
var args = Array.prototype.slice.call(arguments); | |
return $(this).html(args.join("")); | |
}; | |
function RAWHTML(x) { | |
return { | |
toHTML: function() { return x; } | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment