features.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var testRangeBounds = function (document) {
  4. var TEST_HEIGHT = 123;
  5. if (document.createRange) {
  6. var range = document.createRange();
  7. if (range.getBoundingClientRect) {
  8. var testElement = document.createElement('boundtest');
  9. testElement.style.height = TEST_HEIGHT + "px";
  10. testElement.style.display = 'block';
  11. document.body.appendChild(testElement);
  12. range.selectNode(testElement);
  13. var rangeBounds = range.getBoundingClientRect();
  14. var rangeHeight = Math.round(rangeBounds.height);
  15. document.body.removeChild(testElement);
  16. if (rangeHeight === TEST_HEIGHT) {
  17. return true;
  18. }
  19. }
  20. }
  21. return false;
  22. };
  23. var testCORS = function () { return typeof new Image().crossOrigin !== 'undefined'; };
  24. var testResponseType = function () { return typeof new XMLHttpRequest().responseType === 'string'; };
  25. var testSVG = function (document) {
  26. var img = new Image();
  27. var canvas = document.createElement('canvas');
  28. var ctx = canvas.getContext('2d');
  29. if (!ctx) {
  30. return false;
  31. }
  32. img.src = "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'></svg>";
  33. try {
  34. ctx.drawImage(img, 0, 0);
  35. canvas.toDataURL();
  36. }
  37. catch (e) {
  38. return false;
  39. }
  40. return true;
  41. };
  42. var isGreenPixel = function (data) {
  43. return data[0] === 0 && data[1] === 255 && data[2] === 0 && data[3] === 255;
  44. };
  45. var testForeignObject = function (document) {
  46. var canvas = document.createElement('canvas');
  47. var size = 100;
  48. canvas.width = size;
  49. canvas.height = size;
  50. var ctx = canvas.getContext('2d');
  51. if (!ctx) {
  52. return Promise.reject(false);
  53. }
  54. ctx.fillStyle = 'rgb(0, 255, 0)';
  55. ctx.fillRect(0, 0, size, size);
  56. var img = new Image();
  57. var greenImageSrc = canvas.toDataURL();
  58. img.src = greenImageSrc;
  59. var svg = exports.createForeignObjectSVG(size, size, 0, 0, img);
  60. ctx.fillStyle = 'red';
  61. ctx.fillRect(0, 0, size, size);
  62. return exports.loadSerializedSVG(svg)
  63. .then(function (img) {
  64. ctx.drawImage(img, 0, 0);
  65. var data = ctx.getImageData(0, 0, size, size).data;
  66. ctx.fillStyle = 'red';
  67. ctx.fillRect(0, 0, size, size);
  68. var node = document.createElement('div');
  69. node.style.backgroundImage = "url(" + greenImageSrc + ")";
  70. node.style.height = size + "px";
  71. // Firefox 55 does not render inline <img /> tags
  72. return isGreenPixel(data)
  73. ? exports.loadSerializedSVG(exports.createForeignObjectSVG(size, size, 0, 0, node))
  74. : Promise.reject(false);
  75. })
  76. .then(function (img) {
  77. ctx.drawImage(img, 0, 0);
  78. // Edge does not render background-images
  79. return isGreenPixel(ctx.getImageData(0, 0, size, size).data);
  80. })
  81. .catch(function () { return false; });
  82. };
  83. exports.createForeignObjectSVG = function (width, height, x, y, node) {
  84. var xmlns = 'http://www.w3.org/2000/svg';
  85. var svg = document.createElementNS(xmlns, 'svg');
  86. var foreignObject = document.createElementNS(xmlns, 'foreignObject');
  87. svg.setAttributeNS(null, 'width', width.toString());
  88. svg.setAttributeNS(null, 'height', height.toString());
  89. foreignObject.setAttributeNS(null, 'width', '100%');
  90. foreignObject.setAttributeNS(null, 'height', '100%');
  91. foreignObject.setAttributeNS(null, 'x', x.toString());
  92. foreignObject.setAttributeNS(null, 'y', y.toString());
  93. foreignObject.setAttributeNS(null, 'externalResourcesRequired', 'true');
  94. svg.appendChild(foreignObject);
  95. foreignObject.appendChild(node);
  96. return svg;
  97. };
  98. exports.loadSerializedSVG = function (svg) {
  99. return new Promise(function (resolve, reject) {
  100. var img = new Image();
  101. img.onload = function () { return resolve(img); };
  102. img.onerror = reject;
  103. img.src = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(new XMLSerializer().serializeToString(svg));
  104. });
  105. };
  106. exports.FEATURES = {
  107. get SUPPORT_RANGE_BOUNDS() {
  108. 'use strict';
  109. var value = testRangeBounds(document);
  110. Object.defineProperty(exports.FEATURES, 'SUPPORT_RANGE_BOUNDS', { value: value });
  111. return value;
  112. },
  113. get SUPPORT_SVG_DRAWING() {
  114. 'use strict';
  115. var value = testSVG(document);
  116. Object.defineProperty(exports.FEATURES, 'SUPPORT_SVG_DRAWING', { value: value });
  117. return value;
  118. },
  119. get SUPPORT_FOREIGNOBJECT_DRAWING() {
  120. 'use strict';
  121. var value = typeof Array.from === 'function' && typeof window.fetch === 'function'
  122. ? testForeignObject(document)
  123. : Promise.resolve(false);
  124. Object.defineProperty(exports.FEATURES, 'SUPPORT_FOREIGNOBJECT_DRAWING', { value: value });
  125. return value;
  126. },
  127. get SUPPORT_CORS_IMAGES() {
  128. 'use strict';
  129. var value = testCORS();
  130. Object.defineProperty(exports.FEATURES, 'SUPPORT_CORS_IMAGES', { value: value });
  131. return value;
  132. },
  133. get SUPPORT_RESPONSE_TYPE() {
  134. 'use strict';
  135. var value = testResponseType();
  136. Object.defineProperty(exports.FEATURES, 'SUPPORT_RESPONSE_TYPE', { value: value });
  137. return value;
  138. },
  139. get SUPPORT_CORS_XHR() {
  140. 'use strict';
  141. var value = 'withCredentials' in new XMLHttpRequest();
  142. Object.defineProperty(exports.FEATURES, 'SUPPORT_CORS_XHR', { value: value });
  143. return value;
  144. }
  145. };
  146. //# sourceMappingURL=features.js.map