polyfills.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //https://github.com/inexorabletash/polyfill/blob/master/web.js
  2. if (!document.querySelectorAll) {
  3. document.querySelectorAll = function (selectors) {
  4. var style = document.createElement('style'), elements = [], element;
  5. document.documentElement.firstChild.appendChild(style);
  6. document._qsa = [];
  7. style.styleSheet.cssText = selectors + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
  8. window.scrollBy(0, 0);
  9. style.parentNode.removeChild(style);
  10. while (document._qsa.length) {
  11. element = document._qsa.shift();
  12. element.style.removeAttribute('x-qsa');
  13. elements.push(element);
  14. }
  15. document._qsa = null;
  16. return elements;
  17. };
  18. }
  19. if (!document.querySelector) {
  20. document.querySelector = function (selectors) {
  21. var elements = document.querySelectorAll(selectors);
  22. return (elements.length) ? elements[0] : null;
  23. };
  24. }
  25. if (!document.getElementsByClassName) {
  26. document.getElementsByClassName = function (classNames) {
  27. classNames = String(classNames).replace(/^|\s+/g, '.');
  28. return document.querySelectorAll(classNames);
  29. };
  30. }
  31. //https://github.com/inexorabletash/polyfill
  32. // ES5 15.2.3.14 Object.keys ( O )
  33. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
  34. if (!Object.keys) {
  35. Object.keys = function (o) {
  36. if (o !== Object(o)) { throw TypeError('Object.keys called on non-object'); }
  37. var ret = [], p;
  38. for (p in o) {
  39. if (Object.prototype.hasOwnProperty.call(o, p)) {
  40. ret.push(p);
  41. }
  42. }
  43. return ret;
  44. };
  45. }
  46. //https://github.com/inexorabletash/polyfill/blob/master/web.js
  47. (function (global) {
  48. var B64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  49. global.atob = global.atob || function (input) {
  50. input = String(input);
  51. var position = 0,
  52. output = [],
  53. buffer = 0, bits = 0, n;
  54. input = input.replace(/\s/g, '');
  55. if ((input.length % 4) === 0) { input = input.replace(/=+$/, ''); }
  56. if ((input.length % 4) === 1) { throw Error("InvalidCharacterError"); }
  57. if (/[^+/0-9A-Za-z]/.test(input)) { throw Error("InvalidCharacterError"); }
  58. while (position < input.length) {
  59. n = B64_ALPHABET.indexOf(input.charAt(position));
  60. buffer = (buffer << 6) | n;
  61. bits += 6;
  62. if (bits === 24) {
  63. output.push(String.fromCharCode((buffer >> 16) & 0xFF));
  64. output.push(String.fromCharCode((buffer >> 8) & 0xFF));
  65. output.push(String.fromCharCode(buffer & 0xFF));
  66. bits = 0;
  67. buffer = 0;
  68. }
  69. position += 1;
  70. }
  71. if (bits === 12) {
  72. buffer = buffer >> 4;
  73. output.push(String.fromCharCode(buffer & 0xFF));
  74. } else if (bits === 18) {
  75. buffer = buffer >> 2;
  76. output.push(String.fromCharCode((buffer >> 8) & 0xFF));
  77. output.push(String.fromCharCode(buffer & 0xFF));
  78. }
  79. return output.join('');
  80. };
  81. global.btoa = global.btoa || function (input) {
  82. input = String(input);
  83. var position = 0,
  84. out = [],
  85. o1, o2, o3,
  86. e1, e2, e3, e4;
  87. if (/[^\x00-\xFF]/.test(input)) { throw Error("InvalidCharacterError"); }
  88. while (position < input.length) {
  89. o1 = input.charCodeAt(position++);
  90. o2 = input.charCodeAt(position++);
  91. o3 = input.charCodeAt(position++);
  92. // 111111 112222 222233 333333
  93. e1 = o1 >> 2;
  94. e2 = ((o1 & 0x3) << 4) | (o2 >> 4);
  95. e3 = ((o2 & 0xf) << 2) | (o3 >> 6);
  96. e4 = o3 & 0x3f;
  97. if (position === input.length + 2) {
  98. e3 = 64; e4 = 64;
  99. }
  100. else if (position === input.length + 1) {
  101. e4 = 64;
  102. }
  103. out.push(B64_ALPHABET.charAt(e1),
  104. B64_ALPHABET.charAt(e2),
  105. B64_ALPHABET.charAt(e3),
  106. B64_ALPHABET.charAt(e4));
  107. }
  108. return out.join('');
  109. };
  110. }(this));
  111. //https://github.com/jonathantneal/polyfill/blob/master/source/Window.prototype.getComputedStyle.ie8.js
  112. (function () {
  113. if(window.getComputedStyle) return; //Add an exit if already defined
  114. function getComputedStylePixel(element, property, fontSize) {
  115. element.document; // Internet Explorer sometimes struggles to read currentStyle until the element's document is accessed.
  116. var
  117. value = element.currentStyle[property].match(/([\d\.]+)(%|cm|em|in|mm|pc|pt|)/) || [0, 0, ''],
  118. size = value[1],
  119. suffix = value[2],
  120. rootSize;
  121. fontSize = !fontSize ? fontSize : /%|em/.test(suffix) && element.parentElement ? getComputedStylePixel(element.parentElement, 'fontSize', null) : 16;
  122. rootSize = property == 'fontSize' ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight;
  123. return suffix == '%' ? size / 100 * rootSize :
  124. suffix == 'cm' ? size * 0.3937 * 96 :
  125. suffix == 'em' ? size * fontSize :
  126. suffix == 'in' ? size * 96 :
  127. suffix == 'mm' ? size * 0.3937 * 96 / 10 :
  128. suffix == 'pc' ? size * 12 * 96 / 72 :
  129. suffix == 'pt' ? size * 96 / 72 :
  130. size;
  131. }
  132. function setShortStyleProperty(style, property) {
  133. var
  134. borderSuffix = property == 'border' ? 'Width' : '',
  135. t = property + 'Top' + borderSuffix,
  136. r = property + 'Right' + borderSuffix,
  137. b = property + 'Bottom' + borderSuffix,
  138. l = property + 'Left' + borderSuffix;
  139. style[property] = (style[t] == style[r] && style[t] == style[b] && style[t] == style[l] ? [ style[t] ] :
  140. style[t] == style[b] && style[l] == style[r] ? [ style[t], style[r] ] :
  141. style[l] == style[r] ? [ style[t], style[r], style[b] ] :
  142. [ style[t], style[r], style[b], style[l] ]).join(' ');
  143. }
  144. // <CSSStyleDeclaration>
  145. function CSSStyleDeclaration(element) {
  146. var
  147. style = this,
  148. currentStyle = element.currentStyle,
  149. fontSize = getComputedStylePixel(element, 'fontSize'),
  150. unCamelCase = function (match) {
  151. return '-' + match.toLowerCase();
  152. },
  153. property;
  154. for (property in currentStyle) {
  155. Array.prototype.push.call(style, property == 'styleFloat' ? 'float' : property.replace(/[A-Z]/, unCamelCase));
  156. if (property == 'width') {
  157. style[property] = element.offsetWidth + 'px';
  158. } else if (property == 'height') {
  159. style[property] = element.offsetHeight + 'px';
  160. } else if (property == 'styleFloat') {
  161. style.float = currentStyle[property];
  162. } else if (/margin.|padding.|border.+W/.test(property) && style[property] != 'auto') {
  163. style[property] = Math.round(getComputedStylePixel(element, property, fontSize)) + 'px';
  164. } else if (/^outline/.test(property)) {
  165. // errors on checking outline
  166. try {
  167. style[property] = currentStyle[property];
  168. } catch (error) {
  169. style.outlineColor = currentStyle.color;
  170. style.outlineStyle = style.outlineStyle || 'none';
  171. style.outlineWidth = style.outlineWidth || '0px';
  172. style.outline = [style.outlineColor, style.outlineWidth, style.outlineStyle].join(' ');
  173. }
  174. } else {
  175. style[property] = currentStyle[property];
  176. }
  177. }
  178. setShortStyleProperty(style, 'margin');
  179. setShortStyleProperty(style, 'padding');
  180. setShortStyleProperty(style, 'border');
  181. style.fontSize = Math.round(fontSize) + 'px';
  182. }
  183. CSSStyleDeclaration.prototype = {
  184. constructor: CSSStyleDeclaration,
  185. // <CSSStyleDeclaration>.getPropertyPriority
  186. getPropertyPriority: function () {
  187. throw new Error('NotSupportedError: DOM Exception 9');
  188. },
  189. // <CSSStyleDeclaration>.getPropertyValue
  190. getPropertyValue: function(property) {
  191. var lookup = property.replace(/-([a-z])/g, function(match) {
  192. match = match.charAt ? match.split('') : match;
  193. return match[1].toUpperCase();
  194. });
  195. var ret = this[lookup];
  196. return ret;
  197. },
  198. // <CSSStyleDeclaration>.item
  199. item: function (index) {
  200. return this[index];
  201. },
  202. // <CSSStyleDeclaration>.removeProperty
  203. removeProperty: function () {
  204. throw new Error('NoModificationAllowedError: DOM Exception 7');
  205. },
  206. // <CSSStyleDeclaration>.setProperty
  207. setProperty: function () {
  208. throw new Error('NoModificationAllowedError: DOM Exception 7');
  209. },
  210. // <CSSStyleDeclaration>.getPropertyCSSValue
  211. getPropertyCSSValue: function () {
  212. throw new Error('NotSupportedError: DOM Exception 9');
  213. }
  214. };
  215. // <window>.getComputedStyle
  216. window.getComputedStyle = function (element) {
  217. return new CSSStyleDeclaration(element);
  218. };
  219. })();
  220. //https://gist.github.com/jimeh/332357
  221. if (!Object.prototype.hasOwnProperty){
  222. /*jshint -W001, -W103 */
  223. Object.prototype.hasOwnProperty = function(prop) {
  224. var proto = this.__proto__ || this.constructor.prototype;
  225. return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]);
  226. }
  227. /*jshint +W001, +W103 */
  228. }