html5shiv.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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,dialog,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 dialog 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. }(this, document));