UNPKG

5.14 kBJavaScriptView Raw
1/**
2 * Require the given path.
3 *
4 * @param {String} path
5 * @return {Object} exports
6 * @api public
7 */
8
9function require(p, parent, orig){
10 var path = require.resolve(p)
11 , mod = require.modules[path];
12
13 // lookup failed
14 if (null == path) {
15 orig = orig || p;
16 parent = parent || 'root';
17 throw new Error('failed to require "' + orig + '" from "' + parent + '"');
18 }
19
20 // perform real require()
21 // by invoking the module's
22 // registered function
23 if (!mod.exports) {
24 mod.exports = {};
25 mod.client = mod.component = true;
26 mod.call(this, mod, mod.exports, require.relative(path));
27 }
28
29 return mod.exports;
30}
31
32/**
33 * Registered modules.
34 */
35
36require.modules = {};
37
38/**
39 * Registered aliases.
40 */
41
42require.aliases = {};
43
44/**
45 * Resolve `path`.
46 *
47 * Lookup:
48 *
49 * - PATH/index.js
50 * - PATH.js
51 * - PATH
52 *
53 * @param {String} path
54 * @return {String} path or null
55 * @api private
56 */
57
58require.resolve = function(path){
59 var orig = path
60 , reg = path + '.js'
61 , regJSON = path + '.json'
62 , index = path + '/index.js'
63 , indexJSON = path + '/index.json';
64
65 return require.modules[reg] && reg
66 || require.modules[regJSON] && regJSON
67 || require.modules[index] && index
68 || require.modules[indexJSON] && indexJSON
69 || require.modules[orig] && orig
70 || require.aliases[index];
71};
72
73/**
74 * Normalize `path` relative to the current path.
75 *
76 * @param {String} curr
77 * @param {String} path
78 * @return {String}
79 * @api private
80 */
81
82require.normalize = function(curr, path) {
83 var segs = [];
84
85 if ('.' != path.charAt(0)) return path;
86
87 curr = curr.split('/');
88 path = path.split('/');
89
90 for (var i = 0; i < path.length; ++i) {
91 if ('..' == path[i]) {
92 curr.pop();
93 } else if ('.' != path[i] && '' != path[i]) {
94 segs.push(path[i]);
95 }
96 }
97
98 return curr.concat(segs).join('/');
99};
100
101/**
102 * Register module at `path` with callback `fn`.
103 *
104 * @param {String} path
105 * @param {Function} fn
106 * @api private
107 */
108
109require.register = function(path, fn){
110 require.modules[path] = fn;
111};
112
113/**
114 * Alias a module definition.
115 *
116 * @param {String} from
117 * @param {String} to
118 * @api private
119 */
120
121require.alias = function(from, to){
122 var fn = require.modules[from];
123 if (!fn) throw new Error('failed to alias "' + from + '", it does not exist');
124 require.aliases[to] = from;
125};
126
127/**
128 * Return a require function relative to the `parent` path.
129 *
130 * @param {String} parent
131 * @return {Function}
132 * @api private
133 */
134
135require.relative = function(parent) {
136 var p = require.normalize(parent, '..');
137
138 /**
139 * lastIndexOf helper.
140 */
141
142 function lastIndexOf(arr, obj){
143 var i = arr.length;
144 while (i--) {
145 if (arr[i] === obj) return i;
146 }
147 return -1;
148 }
149
150 /**
151 * The relative require() itself.
152 */
153
154 function fn(path){
155 var orig = path;
156 path = fn.resolve(path);
157 return require(path, parent, orig);
158 }
159
160 /**
161 * Resolve relative to the parent.
162 */
163
164 fn.resolve = function(path){
165 // resolve deps by returning
166 // the dep in the nearest "deps"
167 // directory
168 if ('.' != path.charAt(0)) {
169 var segs = parent.split('/');
170 var i = lastIndexOf(segs, 'deps') + 1;
171 if (!i) i = 0;
172 path = segs.slice(0, i + 1).join('/') + '/deps/' + path;
173 return path;
174 }
175 return require.normalize(p, path);
176 };
177
178 /**
179 * Check if module is defined at `path`.
180 */
181
182 fn.exists = function(path){
183 return !! require.modules[fn.resolve(path)];
184 };
185
186 return fn;
187};require.register("domify/index.js", function(module, exports, require){
188
189/**
190 * Wrap map from jquery.
191 */
192
193var map = {
194 option: [1, '<select multiple="multiple">', '</select>'],
195 optgroup: [1, '<select multiple="multiple">', '</select>'],
196 legend: [1, '<fieldset>', '</fieldset>'],
197 thead: [1, '<table>', '</table>'],
198 tbody: [1, '<table>', '</table>'],
199 tfoot: [1, '<table>', '</table>'],
200 colgroup: [1, '<table>', '</table>'],
201 caption: [1, '<table>', '</table>'],
202 tr: [2, '<table><tbody>', '</tbody></table>'],
203 td: [3, '<table><tbody><tr>', '</tr></tbody></table>'],
204 th: [3, '<table><tbody><tr>', '</tr></tbody></table>'],
205 col: [2, '<table><tbody></tbody><colgroup>', '</colgroup></table>'],
206 _default: [0, '', '']
207};
208
209/**
210 * Convert the given `html` into DOM elements.
211 *
212 * @api public
213 */
214
215module.exports = function(html){
216 if ('string' != typeof html) throw new TypeError('String expected');
217
218 // tag name
219 var m = /<([\w:]+)/.exec(html);
220 if (!m) throw new Error('No elements were generated.');
221 var tag = m[1];
222
223 // body support
224 if (tag == 'body') {
225 var el = document.createElement('html');
226 el.innerHTML = html;
227 return el.removeChild(el.lastChild);
228 }
229
230 // wrap map
231 var wrap = map[tag] || map._default;
232 var depth = wrap[0];
233 var prefix = wrap[1];
234 var suffix = wrap[2];
235 var el = document.createElement('div');
236 el.innerHTML = prefix + html + suffix;
237 while (depth--) el = el.lastChild;
238
239 if (el.lastChild.nextElementSibling || el.lastChild.previousElementSibling) {
240 throw new Error('More than one element was generated.');
241 }
242
243 return el.removeChild(el.lastChild);
244};
245
246});