forked from craigbeck/html5.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
html5.noprint.js
545 lines (487 loc) · 16.5 KB
/
html5.noprint.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*!
* HTML5.js v1.0.0-rc
* Copyright 2012 John-David Dalton <http://allyoucanleet.com/>
* Based on HTML5 Shiv vpre3.3 | @afarkas @jon_neal @rem | MIT/GPL2 Licensed
* Available under MIT/GPL2 license
*/
;(function(window, document) {
'use strict';
/** Preset for the install/uninstall methods */
var allOptions = { 'methods': true, 'styles': true };
/** Cache of created elements, document methods, and install state */
var html5Cache = {};
/** Previous `html5` object */
var old = window.html5;
/** List of HTML5 node names to install support for */
var nodeNames = [
'abbr', 'article', 'aside', 'audio', 'bdi', 'canvas', 'data', 'datalist',
'details', 'figcaption', 'figure', 'footer', 'header', 'hgroup', 'main',
'mark', 'meter', 'nav', 'output', 'progress', 'section', 'summary', 'time',
'video'
];
/** Used to namespace printable elements and define `expando` */
var namespace = 'html5js';
/** Used to store an elements `uid` if `element.uniqueNumber` is not supported */
var expando = namespace + /\d+$/.exec(Math.random());
/** Used to filter media types */
var reMedia = /^$|\b(?:all|print)\b/;
/**
* Used to skip elements with type attributes because in IE they cannot be
* set/changed once an element is inserted into a document/fragment.
* http://msdn.microsoft.com/en-us/library/ie/ms534700(v=vs.85).aspx
*/
var reSkip = /^(?:button|select)$/i;
/** Used to detect elements that cannot be cloned correctly */
var reUnclonable = /^<\?/;
/** Used as a fallback for `element.uniqueNumber` */
var uid = 1;
/** Cache of unclonable element node names */
var unclonables = {};
/**
* An object used to flag features.
*
* @static
* @memberOf html5
* @type Object
*/
var support = {};
(function() {
var p,
parent,
sandbox;
// create a new document used to get untainted styles
try {
// avoid https: protocol issues with IE
sandbox = new ActiveXObject(location.protocol == 'https:' && 'htmlfile');
} catch(e) {
// http://xkr.us/articles/dom/iframe-document/
(sandbox = document.createElement('iframe')).name = expando;
sandbox.frameBorder = sandbox.height = sandbox.width = 0;
parent = document.body || document.documentElement;
parent.insertBefore(sandbox, parent.firstChild);
sandbox = (sandbox = sandbox.contentWindow || sandbox.contentDocument || frames[expando]).document || sandbox;
}
sandbox.write('<!doctype html><title></title><body><script>document.w = this<\/script>');
sandbox.close();
p = sandbox.body.appendChild(sandbox.createElement('p'));
p.innerHTML = '<nav/>';
/**
* Detect whether the browser supports default HTML5 styles.
* @memberOf html5.support
* @type Boolean
*/
support.html5Styles = !!p.firstChild &&
(p.firstChild.currentStyle || sandbox.w.getComputedStyle(p.firstChild, null)).display == 'block';
/**
* Detect whether the browser supports unknown elements.
*
* @memberOf html5.support
* @type Boolean
*/
support.unknownElements = p.childNodes.length == 1 || (function() {
// assign a false positive if unable to install
try {
(document.createElement)('p');
} catch(e) {
return true;
}
var frag = document.createDocumentFragment();
return (
typeof frag.createElement == 'undefined' ||
typeof p.uniqueNumber == 'undefined'
);
}());
parent && destroyElement(sandbox.w.frameElement);
}());
/*--------------------------------------------------------------------------*/
/**
* Creates a style sheet with the given CSS text and adds it to the document.
*
* @private
* @param {Document} ownerDocument The document.
* @param {String} cssText The CSS text.
* @returns {StyleSheet} The style sheet.
*/
function addStyleSheet(ownerDocument, cssText) {
// IE8 only respects namespace prefixs when created with `innerHTML`
var p = ownerDocument.createElement('p'),
parent = ownerDocument.getElementsByTagName('head')[0] || ownerDocument.documentElement;
p.innerHTML = 'x<style>' + cssText + '</style>';
return parent.insertBefore(p.lastChild, parent.firstChild);
}
/**
* Creates HTML5 elements using the given document enabling the document to
* parse them correctly.
*
* @private
* @param {Document|Fragment} ownerDocument The document.
* @returns {Document|Fragment} The document.
*/
function createElements(ownerDocument) {
var index = nodeNames.length,
create = ownerDocument.createElement;
while (index--) {
create(nodeNames[index]);
}
return ownerDocument;
}
/**
* Destroys the given element.
*
* @private
* @param {Element} element The element to destroy.
* @param {Object} [cache] The cache object.
*/
function destroyElement(element, cache) {
var trash = (cache || getCache(element.ownerDocument)).trash;
trash.appendChild(element);
trash.innerHTML = '';
}
/**
* Gets the cache object for the given document.
*
* @private
* @param {Document} ownerDocument The document.
* @returns {Object} The cache object.
*/
function getCache(ownerDocument) {
var docEl = ownerDocument.documentElement,
id = docEl.uniqueNumber || docEl[expando] || (docEl[expando] = uid++),
skip = support.unknownElements;
return html5Cache[id] || (html5Cache[id] = {
'frag': !skip && createElements(ownerDocument.createDocumentFragment()),
'nativeCreateElement': !skip && createElements(ownerDocument).createElement,
'nativeCreateFragment': !skip && ownerDocument.createDocumentFragment,
'nodes': {},
'trash': ownerDocument.createElement('div')
});
}
/**
* Resolves an options object from the given value.
*
* @private
* @param {Mixed} value The value to convert to an options object.
* @returns {Object} The options object.
*/
function resolveOptions(value) {
var key;
value = value ? (value === 'all' || value.all ? allOptions : value) : {};
if (typeof value == 'string') {
var object = {};
value = value.split(/[, ]+/);
while ((key = value.pop())) {
object[key] = true;
}
value = object;
}
return value;
}
/**
* Overwrites the document's `createElement` and `createDocumentFragment` methods
* with `html5.createElement` and `html5.createDocumentFragment` equivalents.
*
* @private
* @param {Document} ownerDocument The document.
*/
function setMethods(ownerDocument) {
var cache = getCache(ownerDocument),
create = cache.nativeCreateElement,
frag = cache.frag,
nodes = cache.nodes;
// allow a small amount of repeated code for better performance
ownerDocument.createElement = function(nodeName) {
var cached = nodes[nodeName],
node = cached ? cached.cloneNode() : create(nodeName);
if (!cached && !unclonables[nodeName] &&
!(unclonables[nodeName] = reUnclonable.test(node.outerHTML))) {
node = (nodes[nodeName] = node).cloneNode();
}
return node.canHaveChildren && !reSkip.test(nodeName) ? frag.appendChild(node) : node;
};
// compile unrolled `createElement` calls for better performance
ownerDocument.createDocumentFragment = Function('frag',
'return function() {\n' +
' var node = frag.cloneNode(), create = node.createElement;\n' +
(nodeNames + '').replace(/\w+/g, 'create("$&")\n') + ';\n' +
' return node\n' +
'}'
)(frag);
}
/**
* Adds minimal default HTML5 element styles to the given document.
*
* @private
* @param {Document} ownerDocument The document.
* @param {Object} options Options object.
*/
function setStyles(ownerDocument, options) {
// for additional default and normalized HTML5 element styles checkout
// https://github.com/necolas/normalize.css
getCache(ownerDocument).sheet = addStyleSheet(ownerDocument,
// corrects block display not defined in IE6/7/8/9 and Firefox 3
'article, aside, figcaption, figure, footer, header, hgroup, main, nav, section {' +
' display: block' +
'}' +
// adds styling not present in IE6/7/8/9
'mark {' +
' background: #ff0;' +
' color: #000' +
'}'
);
}
/**
* Restores the document's original `createElement` and `createDocumentFragment` methods.
*
* @private
* @param {Document} ownerDocument The document.
*/
function unsetMethods(ownerDocument) {
var cache = getCache(ownerDocument),
fn = cache.nativeCreateElement;
if (ownerDocument.createElement != fn) {
ownerDocument.createElement = fn;
}
if (ownerDocument.createDocumentFragment != (fn = cache.nativeCreateFragment)) {
ownerDocument.createDocumentFragment = fn;
}
}
/**
* Removes default HTML5 element styles.
*
* @private
* @param {Document} ownerDocument The document.
* @param {Object} options Options object.
*/
function unsetStyles(ownerDocument, options) {
var cache = getCache(ownerDocument),
sheet = cache.sheet;
if (sheet) {
cache.sheet = null;
destroyElement(sheet, cache);
}
}
/*--------------------------------------------------------------------------*/
/**
* Creates a shimmed element of the given node name.
*
* @memberOf html5
* @param {Document} [ownerDocument=document] The context document.
* @param {String} nodeName The node name of the element to create.
* @returns {Element} The created element.
* @example
*
* // basic usage
* html5.createElement('div');
*
* // from a child iframe
* parent.html5.createElement(document, 'div');
*/
function createElement(ownerDocument, nodeName) {
// juggle arguments
ownerDocument || (ownerDocument = document);
if (ownerDocument && !ownerDocument.nodeType) {
nodeName = ownerDocument;
ownerDocument = document;
}
if (support.unknownElements) {
return ownerDocument.createElement(nodeName);
}
// Avoid adding some elements to fragments in IE because
// * attributes like `type` cannot be set/changed once an element is inserted
// into a document/fragment
// * link elements with `src` attributes that are inaccessible, as with
// a 403 response, will cause the tab/window to crash
// * script elements appended to fragments will execute when their `src`
// or `text` property is set
var cache = getCache(ownerDocument),
nodes = cache.nodes,
cached = nodes[nodeName],
node = cached ? cached.cloneNode() : cache.nativeCreateElement(nodeName);
// IE < 9 doesn't clone unknown elements correctly
if (!cached && !unclonables[nodeName] &&
!(unclonables[nodeName] = reUnclonable.test(node.outerHTML))) {
node = (nodes[nodeName] = node).cloneNode();
}
return node.canHaveChildren && !reSkip.test(nodeName) ? cache.frag.appendChild(node) : node;
}
/**
* Creates a shimmed document fragment.
*
* @memberOf html5
* @param {Document} [ownerDocument=document] The context document.
* @returns {Fragment} The created document fragment.
* @example
*
* // basic usage
* html5.createDocumentFragment();
*
* // from a child iframe
* parent.html5.createDocumentFragment(document);
*/
function createDocumentFragment(ownerDocument) {
ownerDocument || (ownerDocument = document);
return support.unknownElements
? ownerDocument.createDocumentFragment()
: createElements(getCache(ownerDocument).frag.cloneNode());
}
/**
* Installs shims according to the specified options.
*
* @memberOf html5
* @param {Document} [ownerDocument=document] The document.
* @param {Object} [options={}] Options object.
* @returns {Document} The document.
* @example
*
* // basic usage
* // autmatically called on the primary document to allow IE < 9 to
* // parse HTML5 elements correctly
* html5.install();
*
* // from a child iframe
* parent.html5.install(document);
*
* // with an options object
* html5.install({
*
* // overwrite the document's `createElement` and `createDocumentFragment`
* // methods with `html5.createElement` and `html5.createDocumentFragment` equivalents.
* 'methods': true,
*
* // add support for printing HTML5 elements
* 'print': true,
*
* // add minimal default HTML5 element styles
* 'styles': true
* });
*
* // with an options string
* html5.install('print styles');
*
* // from a child iframe with options
* parent.html5.install(document, options);
*
* // using a shortcut to install all support extensions
* html5.install('all');
*/
function install(ownerDocument, options) {
ownerDocument || (ownerDocument = document);
if (ownerDocument && !ownerDocument.nodeType) {
options = ownerDocument;
ownerDocument = document;
}
options = resolveOptions(options);
uninstall(ownerDocument, {
'methods': options.methods,
'print': options.print,
'styles': options.styles
});
if (!support.html5Styles && options.styles) {
setStyles(ownerDocument, options);
}
if (!support.unknownElements) {
// if not installing methods then init cache and install support
// for basic HTML5 element parsing
options.methods ? setMethods(ownerDocument) : getCache(ownerDocument);
}
return ownerDocument;
}
/**
* Restores a previously overwritten `html5` object.
* @memberOf html5
* @returns {Object} The current `html5` object.
*/
function noConflict() {
window.html5 = old;
return this;
}
/**
* Uninstalls shims according to the specified options.
*
* @memberOf html5
* @param {Document} [ownerDocument=document] The document.
* @param {Object} [options={}] Options object.
* @returns {Document} The document.
* @example
*
* // basic usage with an options object
* html5.uninstall({
*
* // restore the document's original `createElement`
* // and `createDocumentFragment` methods.
* 'methods': true,
*
* // remove support for printing HTML5 elements
* 'print': true,
*
* // remove minimal default HTML5 element styles
* 'styles': true
* });
*
* // with an options string
* html5.uninstall('print styles');
*
* // from a child iframe with options
* parent.html5.uninstall(document, options);
*
* // using a shortcut to uninstall all support extensions
* html5.uninstall('all');
*/
function uninstall(ownerDocument, options) {
ownerDocument || (ownerDocument = document);
if (ownerDocument && !ownerDocument.nodeType) {
options = ownerDocument;
ownerDocument = document;
}
options = resolveOptions(options);
if (!support.unknownElements && options.methods) {
unsetMethods(ownerDocument);
}
if (!support.html5Printing && options.print) {
unsetPrintSupport(ownerDocument);
}
if (!support.html5Styles && options.styles) {
unsetStyles(ownerDocument, options);
}
return ownerDocument;
}
/*--------------------------------------------------------------------------*/
/**
* The `html5` object.
* @type Object
*/
var html5 = {
/**
* The semantic version number.
* @static
* @memberOf html5
* @type String
*/
'version': '1.0.0-rc',
// an object of feature detection flags
'support': support,
// creates shimmed document fragments
'createDocumentFragment': createDocumentFragment,
// creates shimmed elements
'createElement': createElement,
// installs support extensions
'install': install,
// avoid `html5` object conflicts
'noConflict': noConflict,
// uninstalls support extensions
'uninstall': uninstall
};
/*--------------------------------------------------------------------------*/
// Expose the `html5` object to the global object even when an AMD loader is
// present in case html5.js was injected by a third-party script and not
// intended to be loaded as a module. The global assignment can be reverted in
// the `html5` module via its `noConflict()` method.
window.html5 = html5;
// some AMD build optimizers, like r.js, check for specific condition patterns like the following:
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
// define as an anonymous module so, through path mapping, it can be aliased
define(function() {
return html5;
});
}
}(this, document));