iehtmlshiv.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /**
  2. * @preserve HTML5 Shiv v3.6.2 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed
  3. */
  4. ;(function(window, document) {
  5. /*jshint evil:true */
  6. /** version */
  7. var version = '3.6.2';
  8. /** Preset options */
  9. var options = window.html5 || {};
  10. /** Used to skip problem elements */
  11. var reSkip = /^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i;
  12. /** Not all elements can be cloned in IE **/
  13. var saveClones = /^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i;
  14. /** Detect whether the browser supports default html5 styles */
  15. var supportsHtml5Styles;
  16. /** Name of the expando, to work with multiple documents or to re-shiv one document */
  17. var expando = '_html5shiv';
  18. /** The id for the the documents expando */
  19. var expanID = 0;
  20. /** Cached data for each document */
  21. var expandoData = {};
  22. /** Detect whether the browser supports unknown elements */
  23. var supportsUnknownElements;
  24. (function() {
  25. try {
  26. var a = document.createElement('a');
  27. a.innerHTML = '<xyz></xyz>';
  28. //if the hidden property is implemented we can assume, that the browser supports basic HTML5 Styles
  29. supportsHtml5Styles = ('hidden' in a);
  30. supportsUnknownElements = a.childNodes.length == 1 || (function() {
  31. // assign a false positive if unable to shiv
  32. (document.createElement)('a');
  33. var frag = document.createDocumentFragment();
  34. return (
  35. typeof frag.cloneNode == 'undefined' ||
  36. typeof frag.createDocumentFragment == 'undefined' ||
  37. typeof frag.createElement == 'undefined'
  38. );
  39. }());
  40. } catch(e) {
  41. // assign a false positive if detection fails => unable to shiv
  42. supportsHtml5Styles = true;
  43. supportsUnknownElements = true;
  44. }
  45. }());
  46. /*--------------------------------------------------------------------------*/
  47. /**
  48. * Creates a style sheet with the given CSS text and adds it to the document.
  49. * @private
  50. * @param {Document} ownerDocument The document.
  51. * @param {String} cssText The CSS text.
  52. * @returns {StyleSheet} The style element.
  53. */
  54. function addStyleSheet(ownerDocument, cssText) {
  55. var p = ownerDocument.createElement('p'),
  56. parent = ownerDocument.getElementsByTagName('head')[0] || ownerDocument.documentElement;
  57. p.innerHTML = 'x<style>' + cssText + '</style>';
  58. return parent.insertBefore(p.lastChild, parent.firstChild);
  59. }
  60. /**
  61. * Returns the value of `html5.elements` as an array.
  62. * @private
  63. * @returns {Array} An array of shived element node names.
  64. */
  65. function getElements() {
  66. var elements = html5.elements;
  67. return typeof elements == 'string' ? elements.split(' ') : elements;
  68. }
  69. /**
  70. * Returns the data associated to the given document
  71. * @private
  72. * @param {Document} ownerDocument The document.
  73. * @returns {Object} An object of data.
  74. */
  75. function getExpandoData(ownerDocument) {
  76. var data = expandoData[ownerDocument[expando]];
  77. if (!data) {
  78. data = {};
  79. expanID++;
  80. ownerDocument[expando] = expanID;
  81. expandoData[expanID] = data;
  82. }
  83. return data;
  84. }
  85. /**
  86. * returns a shived element for the given nodeName and document
  87. * @memberOf html5
  88. * @param {String} nodeName name of the element
  89. * @param {Document} ownerDocument The context document.
  90. * @returns {Object} The shived element.
  91. */
  92. function createElement(nodeName, ownerDocument, data){
  93. if (!ownerDocument) {
  94. ownerDocument = document;
  95. }
  96. if(supportsUnknownElements){
  97. return ownerDocument.createElement(nodeName);
  98. }
  99. if (!data) {
  100. data = getExpandoData(ownerDocument);
  101. }
  102. var node;
  103. if (data.cache[nodeName]) {
  104. node = data.cache[nodeName].cloneNode();
  105. } else if (saveClones.test(nodeName)) {
  106. node = (data.cache[nodeName] = data.createElem(nodeName)).cloneNode();
  107. } else {
  108. node = data.createElem(nodeName);
  109. }
  110. // Avoid adding some elements to fragments in IE < 9 because
  111. // * Attributes like `name` or `type` cannot be set/changed once an element
  112. // is inserted into a document/fragment
  113. // * Link elements with `src` attributes that are inaccessible, as with
  114. // a 403 response, will cause the tab/window to crash
  115. // * Script elements appended to fragments will execute when their `src`
  116. // or `text` property is set
  117. return node.canHaveChildren && !reSkip.test(nodeName) ? data.frag.appendChild(node) : node;
  118. }
  119. /**
  120. * returns a shived DocumentFragment for the given document
  121. * @memberOf html5
  122. * @param {Document} ownerDocument The context document.
  123. * @returns {Object} The shived DocumentFragment.
  124. */
  125. function createDocumentFragment(ownerDocument, data){
  126. if (!ownerDocument) {
  127. ownerDocument = document;
  128. }
  129. if(supportsUnknownElements){
  130. return ownerDocument.createDocumentFragment();
  131. }
  132. data = data || getExpandoData(ownerDocument);
  133. var clone = data.frag.cloneNode(),
  134. i = 0,
  135. elems = getElements(),
  136. l = elems.length;
  137. for(;i<l;i++){
  138. clone.createElement(elems[i]);
  139. }
  140. return clone;
  141. }
  142. /**
  143. * Shivs the `createElement` and `createDocumentFragment` methods of the document.
  144. * @private
  145. * @param {Document|DocumentFragment} ownerDocument The document.
  146. * @param {Object} data of the document.
  147. */
  148. function shivMethods(ownerDocument, data) {
  149. if (!data.cache) {
  150. data.cache = {};
  151. data.createElem = ownerDocument.createElement;
  152. data.createFrag = ownerDocument.createDocumentFragment;
  153. data.frag = data.createFrag();
  154. }
  155. ownerDocument.createElement = function(nodeName) {
  156. //abort shiv
  157. if (!html5.shivMethods) {
  158. return data.createElem(nodeName);
  159. }
  160. return createElement(nodeName, ownerDocument, data);
  161. };
  162. ownerDocument.createDocumentFragment = Function('h,f', 'return function(){' +
  163. 'var n=f.cloneNode(),c=n.createElement;' +
  164. 'h.shivMethods&&(' +
  165. // unroll the `createElement` calls
  166. getElements().join().replace(/\w+/g, function(nodeName) {
  167. data.createElem(nodeName);
  168. data.frag.createElement(nodeName);
  169. return 'c("' + nodeName + '")';
  170. }) +
  171. ');return n}'
  172. )(html5, data.frag);
  173. }
  174. /*--------------------------------------------------------------------------*/
  175. /**
  176. * Shivs the given document.
  177. * @memberOf html5
  178. * @param {Document} ownerDocument The document to shiv.
  179. * @returns {Document} The shived document.
  180. */
  181. function shivDocument(ownerDocument) {
  182. if (!ownerDocument) {
  183. ownerDocument = document;
  184. }
  185. var data = getExpandoData(ownerDocument);
  186. if (html5.shivCSS && !supportsHtml5Styles && !data.hasCSS) {
  187. data.hasCSS = !!addStyleSheet(ownerDocument,
  188. // corrects block display not defined in IE6/7/8/9
  189. 'article,aside,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}' +
  190. // adds styling not present in IE6/7/8/9
  191. 'mark{background:#FF0;color:#000}' +
  192. // hides non-rendered elements
  193. 'template{display:none}'
  194. );
  195. }
  196. if (!supportsUnknownElements) {
  197. shivMethods(ownerDocument, data);
  198. }
  199. return ownerDocument;
  200. }
  201. /*--------------------------------------------------------------------------*/
  202. /**
  203. * The `html5` object is exposed so that more elements can be shived and
  204. * existing shiving can be detected on iframes.
  205. * @type Object
  206. * @example
  207. *
  208. * // options can be changed before the script is included
  209. * html5 = { 'elements': 'mark section', 'shivCSS': false, 'shivMethods': false };
  210. */
  211. var html5 = {
  212. /**
  213. * An array or space separated string of node names of the elements to shiv.
  214. * @memberOf html5
  215. * @type Array|String
  216. */
  217. 'elements': options.elements || 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup main mark meter nav output progress section summary template time video',
  218. /**
  219. * current version of html5shiv
  220. */
  221. 'version': version,
  222. /**
  223. * A flag to indicate that the HTML5 style sheet should be inserted.
  224. * @memberOf html5
  225. * @type Boolean
  226. */
  227. 'shivCSS': (options.shivCSS !== false),
  228. /**
  229. * Is equal to true if a browser supports creating unknown/HTML5 elements
  230. * @memberOf html5
  231. * @type boolean
  232. */
  233. 'supportsUnknownElements': supportsUnknownElements,
  234. /**
  235. * A flag to indicate that the document's `createElement` and `createDocumentFragment`
  236. * methods should be overwritten.
  237. * @memberOf html5
  238. * @type Boolean
  239. */
  240. 'shivMethods': (options.shivMethods !== false),
  241. /**
  242. * A string to describe the type of `html5` object ("default" or "default print").
  243. * @memberOf html5
  244. * @type String
  245. */
  246. 'type': 'default',
  247. // shivs the document according to the specified `html5` object options
  248. 'shivDocument': shivDocument,
  249. //creates a shived element
  250. createElement: createElement,
  251. //creates a shived documentFragment
  252. createDocumentFragment: createDocumentFragment
  253. };
  254. /*--------------------------------------------------------------------------*/
  255. // expose html5
  256. window.html5 = html5;
  257. // shiv the document
  258. shivDocument(document);
  259. /*------------------------------- Print Shiv -------------------------------*/
  260. /** Used to filter media types */
  261. var reMedia = /^$|\b(?:all|print)\b/;
  262. /** Used to namespace printable elements */
  263. var shivNamespace = 'html5shiv';
  264. /** Detect whether the browser supports shivable style sheets */
  265. var supportsShivableSheets = !supportsUnknownElements && (function() {
  266. // assign a false negative if unable to shiv
  267. var docEl = document.documentElement;
  268. return !(
  269. typeof document.namespaces == 'undefined' ||
  270. typeof document.parentWindow == 'undefined' ||
  271. typeof docEl.applyElement == 'undefined' ||
  272. typeof docEl.removeNode == 'undefined' ||
  273. typeof window.attachEvent == 'undefined'
  274. );
  275. }());
  276. /*--------------------------------------------------------------------------*/
  277. /**
  278. * Wraps all HTML5 elements in the given document with printable elements.
  279. * (eg. the "header" element is wrapped with the "html5shiv:header" element)
  280. * @private
  281. * @param {Document} ownerDocument The document.
  282. * @returns {Array} An array wrappers added.
  283. */
  284. function addWrappers(ownerDocument) {
  285. var node,
  286. nodes = ownerDocument.getElementsByTagName('*'),
  287. index = nodes.length,
  288. reElements = RegExp('^(?:' + getElements().join('|') + ')$', 'i'),
  289. result = [];
  290. while (index--) {
  291. node = nodes[index];
  292. if (reElements.test(node.nodeName)) {
  293. result.push(node.applyElement(createWrapper(node)));
  294. }
  295. }
  296. return result;
  297. }
  298. /**
  299. * Creates a printable wrapper for the given element.
  300. * @private
  301. * @param {Element} element The element.
  302. * @returns {Element} The wrapper.
  303. */
  304. function createWrapper(element) {
  305. var node,
  306. nodes = element.attributes,
  307. index = nodes.length,
  308. wrapper = element.ownerDocument.createElement(shivNamespace + ':' + element.nodeName);
  309. // copy element attributes to the wrapper
  310. while (index--) {
  311. node = nodes[index];
  312. node.specified && wrapper.setAttribute(node.nodeName, node.nodeValue);
  313. }
  314. // copy element styles to the wrapper
  315. wrapper.style.cssText = element.style.cssText;
  316. return wrapper;
  317. }
  318. /**
  319. * Shivs the given CSS text.
  320. * (eg. header{} becomes html5shiv\:header{})
  321. * @private
  322. * @param {String} cssText The CSS text to shiv.
  323. * @returns {String} The shived CSS text.
  324. */
  325. function shivCssText(cssText) {
  326. var pair,
  327. parts = cssText.split('{'),
  328. index = parts.length,
  329. reElements = RegExp('(^|[\\s,>+~])(' + getElements().join('|') + ')(?=[[\\s,>+~#.:]|$)', 'gi'),
  330. replacement = '$1' + shivNamespace + '\\:$2';
  331. while (index--) {
  332. pair = parts[index] = parts[index].split('}');
  333. pair[pair.length - 1] = pair[pair.length - 1].replace(reElements, replacement);
  334. parts[index] = pair.join('}');
  335. }
  336. return parts.join('{');
  337. }
  338. /**
  339. * Removes the given wrappers, leaving the original elements.
  340. * @private
  341. * @params {Array} wrappers An array of printable wrappers.
  342. */
  343. function removeWrappers(wrappers) {
  344. var index = wrappers.length;
  345. while (index--) {
  346. wrappers[index].removeNode();
  347. }
  348. }
  349. /*--------------------------------------------------------------------------*/
  350. /**
  351. * Shivs the given document for print.
  352. * @memberOf html5
  353. * @param {Document} ownerDocument The document to shiv.
  354. * @returns {Document} The shived document.
  355. */
  356. function shivPrint(ownerDocument) {
  357. var shivedSheet,
  358. wrappers,
  359. data = getExpandoData(ownerDocument),
  360. namespaces = ownerDocument.namespaces,
  361. ownerWindow = ownerDocument.parentWindow;
  362. if (!supportsShivableSheets || ownerDocument.printShived) {
  363. return ownerDocument;
  364. }
  365. if (typeof namespaces[shivNamespace] == 'undefined') {
  366. namespaces.add(shivNamespace);
  367. }
  368. function removeSheet() {
  369. clearTimeout(data._removeSheetTimer);
  370. if (shivedSheet) {
  371. shivedSheet.removeNode(true);
  372. }
  373. shivedSheet= null;
  374. }
  375. ownerWindow.attachEvent('onbeforeprint', function() {
  376. removeSheet();
  377. var imports,
  378. length,
  379. sheet,
  380. collection = ownerDocument.styleSheets,
  381. cssText = [],
  382. index = collection.length,
  383. sheets = Array(index);
  384. // convert styleSheets collection to an array
  385. while (index--) {
  386. sheets[index] = collection[index];
  387. }
  388. // concat all style sheet CSS text
  389. while ((sheet = sheets.pop())) {
  390. // IE does not enforce a same origin policy for external style sheets...
  391. // but has trouble with some dynamically created stylesheets
  392. if (!sheet.disabled && reMedia.test(sheet.media)) {
  393. try {
  394. imports = sheet.imports;
  395. length = imports.length;
  396. } catch(er){
  397. length = 0;
  398. }
  399. for (index = 0; index < length; index++) {
  400. sheets.push(imports[index]);
  401. }
  402. try {
  403. cssText.push(sheet.cssText);
  404. } catch(er){}
  405. }
  406. }
  407. // wrap all HTML5 elements with printable elements and add the shived style sheet
  408. cssText = shivCssText(cssText.reverse().join(''));
  409. wrappers = addWrappers(ownerDocument);
  410. shivedSheet = addStyleSheet(ownerDocument, cssText);
  411. });
  412. ownerWindow.attachEvent('onafterprint', function() {
  413. // remove wrappers, leaving the original elements, and remove the shived style sheet
  414. removeWrappers(wrappers);
  415. clearTimeout(data._removeSheetTimer);
  416. data._removeSheetTimer = setTimeout(removeSheet, 500);
  417. });
  418. ownerDocument.printShived = true;
  419. return ownerDocument;
  420. }
  421. /*--------------------------------------------------------------------------*/
  422. // expose API
  423. html5.type += ' print';
  424. html5.shivPrint = shivPrint;
  425. // shiv for print
  426. shivPrint(document);
  427. }(this, document));