This repository has been archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kup.js
130 lines (130 loc) · 4.55 KB
/
kup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Generated by CoffeeScript 1.9.1
(function() {
var Kup, contentEncodings, empty, fn, i, j, len, len1, regular, results, tag;
Kup = function() {
return this.htmlOut = '';
};
if (typeof window !== "undefined" && window !== null) {
window.Kup = Kup;
} else if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
module.exports = Kup;
} else {
throw new Error('either the `window` global or the `module.exports` global must be present');
}
contentEncodings = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': ''',
'/': '/'
};
Kup.prototype = {
_isAttrs: function(attrs) {
return 'object' === typeof attrs;
},
_encodeContent: function(content) {
return content.toString().replace(/[&<>"'\/]/g, function(char) {
return contentEncodings[char];
});
},
_encodeAttribute: function(value) {
return value.toString().replace(/"/g, '"');
},
_camelcaseToDashcase: function(string) {
return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
},
_attributeToString: function(key, value) {
if (key === 'style' && 'object' === typeof value) {
value = this._styleObjectToString(value);
}
return key + "=\"" + (this._encodeAttribute(value)) + "\"";
},
_styleObjectToString: function(styles) {
var key, parts, value;
parts = [];
for (key in styles) {
value = styles[key];
parts.push((this._camelcaseToDashcase(key)) + ": " + value + ";");
}
return parts.join(' ');
},
_openingTagUntilExceptClosingBracketToString: function(tag, attrs) {
var key, parts, value;
parts = ["<" + tag];
for (key in attrs) {
value = attrs[key];
if (value == null) {
throw new Error("value of attribute `" + key + "` in tag `" + tag + "` is undefined or null");
}
parts.push(this._attributeToString(key, value));
}
return parts.join(' ');
},
doctype: function() {
return this.htmlOut += '<!DOCTYPE html>';
},
_open: function(tag, attrs) {
return this.htmlOut += this._openingTagUntilExceptClosingBracketToString(tag, attrs) + '>';
},
_content: function(content) {
var stringContent, type;
type = typeof content;
if (type === 'function') {
return content();
} else if (content != null) {
stringContent = type !== 'string' ? content.toString() : content;
return this.htmlOut += this._encodeContent(stringContent);
}
},
_close: function(tag) {
return this.htmlOut += "</" + tag + ">";
},
tag: function(tag, attrs, content) {
if (!this._isAttrs(attrs)) {
content = attrs;
attrs = void 0;
}
this._open(tag, attrs);
this._content(content);
return this._close(tag);
},
empty: function(tag, attrs) {
return this.htmlOut += this._openingTagUntilExceptClosingBracketToString(tag, attrs) + ' />';
},
unsafe: function(string) {
return this.htmlOut += string;
},
safe: function(string) {
return this.htmlOut += this._encodeContent(string);
}
};
regular = 'a abbr address article aside audio b bdi bdo blockquote body button canvas caption cite code colgroup datalist dd del details dfn div dl dt em fieldset figcaption figure footer form frameset h1 h2 h3 h4 h5 h6 head header hgroup html i iframe ins kbd label legend li map mark menu meter nav noscript object ol optgroup option output p pre progress q rp rt ruby s samp script section select small span strong sub summary sup table tbody td textarea tfoot th thead time title tr u ul video style'.split(/[\n ]+/);
fn = function(tag) {
return Kup.prototype[tag] = function(attrs, content) {
return this.tag(tag, attrs, content);
};
};
for (i = 0, len = regular.length; i < len; i++) {
tag = regular[i];
fn(tag);
}
empty = 'area base br col command embed hr img input keygen link meta param source track wbr frame'.split(/[\n ]+/);
results = [];
for (j = 0, len1 = empty.length; j < len1; j++) {
tag = empty[j];
results.push((function(tag) {
return Kup.prototype[tag] = function(attrs, content) {
if (!this._isAttrs(attrs)) {
content = attrs;
attrs = void 0;
}
if (content != null) {
throw new Error("void tag `" + tag + "` accepts no content but content `" + content + "` was given");
}
return this.empty(tag, attrs);
};
})(tag));
}
return results;
})();