html5.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * @preserve HTML5 Shiv 3.7.3 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed
  3. */
  4. ;(function(window, document) {
  5. /*jshint evil:true */
  6. /** version */
  7. var version = '3.7.3';
  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. * Extends the built-in list of html5 elements
  71. * @memberOf html5
  72. * @param {String|Array} newElements whitespace separated list or array of new element names to shiv
  73. * @param {Document} ownerDocument The context document.
  74. */
  75. function addElements(newElements, ownerDocument) {
  76. var elements = html5.elements;
  77. if(typeof elements != 'string'){
  78. elements = elements.join(' ');
  79. }
  80. if(typeof newElements != 'string'){
  81. newElements = newElements.join(' ');
  82. }
  83. html5.elements = elements +' '+ newElements;
  84. shivDocument(ownerDocument);
  85. }
  86. /**
  87. * Returns the data associated to the given document
  88. * @private
  89. * @param {Document} ownerDocument The document.
  90. * @returns {Object} An object of data.
  91. */
  92. function getExpandoData(ownerDocument) {
  93. var data = expandoData[ownerDocument[expando]];
  94. if (!data) {
  95. data = {};
  96. expanID++;
  97. ownerDocument[expando] = expanID;
  98. expandoData[expanID] = data;
  99. }
  100. return data;
  101. }
  102. /**
  103. * returns a shived element for the given nodeName and document
  104. * @memberOf html5
  105. * @param {String} nodeName name of the element
  106. * @param {Document|DocumentFragment} ownerDocument The context document.
  107. * @returns {Object} The shived element.
  108. */
  109. function createElement(nodeName, ownerDocument, data){
  110. if (!ownerDocument) {
  111. ownerDocument = document;
  112. }
  113. if(supportsUnknownElements){
  114. return ownerDocument.createElement(nodeName);
  115. }
  116. if (!data) {
  117. data = getExpandoData(ownerDocument);
  118. }
  119. var node;
  120. if (data.cache[nodeName]) {
  121. node = data.cache[nodeName].cloneNode();
  122. } else if (saveClones.test(nodeName)) {
  123. node = (data.cache[nodeName] = data.createElem(nodeName)).cloneNode();
  124. } else {
  125. node = data.createElem(nodeName);
  126. }
  127. // Avoid adding some elements to fragments in IE < 9 because
  128. // * Attributes like `name` or `type` cannot be set/changed once an element
  129. // is inserted into a document/fragment
  130. // * Link elements with `src` attributes that are inaccessible, as with
  131. // a 403 response, will cause the tab/window to crash
  132. // * Script elements appended to fragments will execute when their `src`
  133. // or `text` property is set
  134. return node.canHaveChildren && !reSkip.test(nodeName) && !node.tagUrn ? data.frag.appendChild(node) : node;
  135. }
  136. /**
  137. * returns a shived DocumentFragment for the given document
  138. * @memberOf html5
  139. * @param {Document} ownerDocument The context document.
  140. * @returns {Object} The shived DocumentFragment.
  141. */
  142. function createDocumentFragment(ownerDocument, data){
  143. if (!ownerDocument) {
  144. ownerDocument = document;
  145. }
  146. if(supportsUnknownElements){
  147. return ownerDocument.createDocumentFragment();
  148. }
  149. data = data || getExpandoData(ownerDocument);
  150. var clone = data.frag.cloneNode(),
  151. i = 0,
  152. elems = getElements(),
  153. l = elems.length;
  154. for(;i<l;i++){
  155. clone.createElement(elems[i]);
  156. }
  157. return clone;
  158. }
  159. /**
  160. * Shivs the `createElement` and `createDocumentFragment` methods of the document.
  161. * @private
  162. * @param {Document|DocumentFragment} ownerDocument The document.
  163. * @param {Object} data of the document.
  164. */
  165. function shivMethods(ownerDocument, data) {
  166. if (!data.cache) {
  167. data.cache = {};
  168. data.createElem = ownerDocument.createElement;
  169. data.createFrag = ownerDocument.createDocumentFragment;
  170. data.frag = data.createFrag();
  171. }
  172. ownerDocument.createElement = function(nodeName) {
  173. //abort shiv
  174. if (!html5.shivMethods) {
  175. return data.createElem(nodeName);
  176. }
  177. return createElement(nodeName, ownerDocument, data);
  178. };
  179. ownerDocument.createDocumentFragment = Function('h,f', 'return function(){' +
  180. 'var n=f.cloneNode(),c=n.createElement;' +
  181. 'h.shivMethods&&(' +
  182. // unroll the `createElement` calls
  183. getElements().join().replace(/[\w\-:]+/g, function(nodeName) {
  184. data.createElem(nodeName);
  185. data.frag.createElement(nodeName);
  186. return 'c("' + nodeName + '")';
  187. }) +
  188. ');return n}'
  189. )(html5, data.frag);
  190. }
  191. /*--------------------------------------------------------------------------*/
  192. /**
  193. * Shivs the given document.
  194. * @memberOf html5
  195. * @param {Document} ownerDocument The document to shiv.
  196. * @returns {Document} The shived document.
  197. */
  198. function shivDocument(ownerDocument) {
  199. if (!ownerDocument) {
  200. ownerDocument = document;
  201. }
  202. var data = getExpandoData(ownerDocument);
  203. if (html5.shivCSS && !supportsHtml5Styles && !data.hasCSS) {
  204. data.hasCSS = !!addStyleSheet(ownerDocument,
  205. // corrects block display not defined in IE6/7/8/9
  206. 'article,aside,dialog,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}' +
  207. // adds styling not present in IE6/7/8/9
  208. 'mark{background:#FF0;color:#000}' +
  209. // hides non-rendered elements
  210. 'template{display:none}'
  211. );
  212. }
  213. if (!supportsUnknownElements) {
  214. shivMethods(ownerDocument, data);
  215. }
  216. return ownerDocument;
  217. }
  218. /*--------------------------------------------------------------------------*/
  219. /**
  220. * The `html5` object is exposed so that more elements can be shived and
  221. * existing shiving can be detected on iframes.
  222. * @type Object
  223. * @example
  224. *
  225. * // options can be changed before the script is included
  226. * html5 = { 'elements': 'mark section', 'shivCSS': false, 'shivMethods': false };
  227. */
  228. var html5 = {
  229. /**
  230. * An array or space separated string of node names of the elements to shiv.
  231. * @memberOf html5
  232. * @type Array|String
  233. */
  234. 'elements': options.elements || 'abbr article aside audio bdi canvas data datalist details dialog figcaption figure footer header hgroup main mark meter nav output picture progress section summary template time video',
  235. /**
  236. * current version of html5shiv
  237. */
  238. 'version': version,
  239. /**
  240. * A flag to indicate that the HTML5 style sheet should be inserted.
  241. * @memberOf html5
  242. * @type Boolean
  243. */
  244. 'shivCSS': (options.shivCSS !== false),
  245. /**
  246. * Is equal to true if a browser supports creating unknown/HTML5 elements
  247. * @memberOf html5
  248. * @type boolean
  249. */
  250. 'supportsUnknownElements': supportsUnknownElements,
  251. /**
  252. * A flag to indicate that the document's `createElement` and `createDocumentFragment`
  253. * methods should be overwritten.
  254. * @memberOf html5
  255. * @type Boolean
  256. */
  257. 'shivMethods': (options.shivMethods !== false),
  258. /**
  259. * A string to describe the type of `html5` object ("default" or "default print").
  260. * @memberOf html5
  261. * @type String
  262. */
  263. 'type': 'default',
  264. // shivs the document according to the specified `html5` object options
  265. 'shivDocument': shivDocument,
  266. //creates a shived element
  267. createElement: createElement,
  268. //creates a shived documentFragment
  269. createDocumentFragment: createDocumentFragment,
  270. //extends list of elements
  271. addElements: addElements
  272. };
  273. /*--------------------------------------------------------------------------*/
  274. // expose html5
  275. window.html5 = html5;
  276. // shiv the document
  277. shivDocument(document);
  278. if(typeof module == 'object' && module.exports){
  279. module.exports = html5;
  280. }
  281. }(typeof window !== "undefined" ? window : this, document));