background.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var background_size_1 = require("../css/property-descriptors/background-size");
  4. var vector_1 = require("./vector");
  5. var background_repeat_1 = require("../css/property-descriptors/background-repeat");
  6. var length_percentage_1 = require("../css/types/length-percentage");
  7. var parser_1 = require("../css/syntax/parser");
  8. var box_sizing_1 = require("./box-sizing");
  9. var background_clip_1 = require("../css/property-descriptors/background-clip");
  10. exports.calculateBackgroundPositioningArea = function (backgroundOrigin, element) {
  11. if (backgroundOrigin === 0 /* BORDER_BOX */) {
  12. return element.bounds;
  13. }
  14. if (backgroundOrigin === 2 /* CONTENT_BOX */) {
  15. return box_sizing_1.contentBox(element);
  16. }
  17. return box_sizing_1.paddingBox(element);
  18. };
  19. exports.calculateBackgroundPaintingArea = function (backgroundClip, element) {
  20. if (backgroundClip === background_clip_1.BACKGROUND_CLIP.BORDER_BOX) {
  21. return element.bounds;
  22. }
  23. if (backgroundClip === background_clip_1.BACKGROUND_CLIP.CONTENT_BOX) {
  24. return box_sizing_1.contentBox(element);
  25. }
  26. return box_sizing_1.paddingBox(element);
  27. };
  28. exports.calculateBackgroundRendering = function (container, index, intrinsicSize) {
  29. var backgroundPositioningArea = exports.calculateBackgroundPositioningArea(exports.getBackgroundValueForIndex(container.styles.backgroundOrigin, index), container);
  30. var backgroundPaintingArea = exports.calculateBackgroundPaintingArea(exports.getBackgroundValueForIndex(container.styles.backgroundClip, index), container);
  31. var backgroundImageSize = exports.calculateBackgroundSize(exports.getBackgroundValueForIndex(container.styles.backgroundSize, index), intrinsicSize, backgroundPositioningArea);
  32. var sizeWidth = backgroundImageSize[0], sizeHeight = backgroundImageSize[1];
  33. var position = length_percentage_1.getAbsoluteValueForTuple(exports.getBackgroundValueForIndex(container.styles.backgroundPosition, index), backgroundPositioningArea.width - sizeWidth, backgroundPositioningArea.height - sizeHeight);
  34. var path = exports.calculateBackgroundRepeatPath(exports.getBackgroundValueForIndex(container.styles.backgroundRepeat, index), position, backgroundImageSize, backgroundPositioningArea, backgroundPaintingArea);
  35. var offsetX = Math.round(backgroundPositioningArea.left + position[0]);
  36. var offsetY = Math.round(backgroundPositioningArea.top + position[1]);
  37. return [path, offsetX, offsetY, sizeWidth, sizeHeight];
  38. };
  39. exports.isAuto = function (token) { return parser_1.isIdentToken(token) && token.value === background_size_1.BACKGROUND_SIZE.AUTO; };
  40. var hasIntrinsicValue = function (value) { return typeof value === 'number'; };
  41. exports.calculateBackgroundSize = function (size, _a, bounds) {
  42. var intrinsicWidth = _a[0], intrinsicHeight = _a[1], intrinsicProportion = _a[2];
  43. var first = size[0], second = size[1];
  44. if (length_percentage_1.isLengthPercentage(first) && second && length_percentage_1.isLengthPercentage(second)) {
  45. return [length_percentage_1.getAbsoluteValue(first, bounds.width), length_percentage_1.getAbsoluteValue(second, bounds.height)];
  46. }
  47. var hasIntrinsicProportion = hasIntrinsicValue(intrinsicProportion);
  48. if (parser_1.isIdentToken(first) && (first.value === background_size_1.BACKGROUND_SIZE.CONTAIN || first.value === background_size_1.BACKGROUND_SIZE.COVER)) {
  49. if (hasIntrinsicValue(intrinsicProportion)) {
  50. var targetRatio = bounds.width / bounds.height;
  51. return targetRatio < intrinsicProportion !== (first.value === background_size_1.BACKGROUND_SIZE.COVER)
  52. ? [bounds.width, bounds.width / intrinsicProportion]
  53. : [bounds.height * intrinsicProportion, bounds.height];
  54. }
  55. return [bounds.width, bounds.height];
  56. }
  57. var hasIntrinsicWidth = hasIntrinsicValue(intrinsicWidth);
  58. var hasIntrinsicHeight = hasIntrinsicValue(intrinsicHeight);
  59. var hasIntrinsicDimensions = hasIntrinsicWidth || hasIntrinsicHeight;
  60. // If the background-size is auto or auto auto:
  61. if (exports.isAuto(first) && (!second || exports.isAuto(second))) {
  62. // If the image has both horizontal and vertical intrinsic dimensions, it's rendered at that size.
  63. if (hasIntrinsicWidth && hasIntrinsicHeight) {
  64. return [intrinsicWidth, intrinsicHeight];
  65. }
  66. // If the image has no intrinsic dimensions and has no intrinsic proportions,
  67. // it's rendered at the size of the background positioning area.
  68. if (!hasIntrinsicProportion && !hasIntrinsicDimensions) {
  69. return [bounds.width, bounds.height];
  70. }
  71. // TODO If the image has no intrinsic dimensions but has intrinsic proportions, it's rendered as if contain had been specified instead.
  72. // If the image has only one intrinsic dimension and has intrinsic proportions, it's rendered at the size corresponding to that one dimension.
  73. // The other dimension is computed using the specified dimension and the intrinsic proportions.
  74. if (hasIntrinsicDimensions && hasIntrinsicProportion) {
  75. var width_1 = hasIntrinsicWidth
  76. ? intrinsicWidth
  77. : intrinsicHeight * intrinsicProportion;
  78. var height_1 = hasIntrinsicHeight
  79. ? intrinsicHeight
  80. : intrinsicWidth / intrinsicProportion;
  81. return [width_1, height_1];
  82. }
  83. // If the image has only one intrinsic dimension but has no intrinsic proportions,
  84. // it's rendered using the specified dimension and the other dimension of the background positioning area.
  85. var width_2 = hasIntrinsicWidth ? intrinsicWidth : bounds.width;
  86. var height_2 = hasIntrinsicHeight ? intrinsicHeight : bounds.height;
  87. return [width_2, height_2];
  88. }
  89. // If the image has intrinsic proportions, it's stretched to the specified dimension.
  90. // The unspecified dimension is computed using the specified dimension and the intrinsic proportions.
  91. if (hasIntrinsicProportion) {
  92. var width_3 = 0;
  93. var height_3 = 0;
  94. if (length_percentage_1.isLengthPercentage(first)) {
  95. width_3 = length_percentage_1.getAbsoluteValue(first, bounds.width);
  96. }
  97. else if (length_percentage_1.isLengthPercentage(second)) {
  98. height_3 = length_percentage_1.getAbsoluteValue(second, bounds.height);
  99. }
  100. if (exports.isAuto(first)) {
  101. width_3 = height_3 * intrinsicProportion;
  102. }
  103. else if (!second || exports.isAuto(second)) {
  104. height_3 = width_3 / intrinsicProportion;
  105. }
  106. return [width_3, height_3];
  107. }
  108. // If the image has no intrinsic proportions, it's stretched to the specified dimension.
  109. // The unspecified dimension is computed using the image's corresponding intrinsic dimension,
  110. // if there is one. If there is no such intrinsic dimension,
  111. // it becomes the corresponding dimension of the background positioning area.
  112. var width = null;
  113. var height = null;
  114. if (length_percentage_1.isLengthPercentage(first)) {
  115. width = length_percentage_1.getAbsoluteValue(first, bounds.width);
  116. }
  117. else if (second && length_percentage_1.isLengthPercentage(second)) {
  118. height = length_percentage_1.getAbsoluteValue(second, bounds.height);
  119. }
  120. if (width !== null && (!second || exports.isAuto(second))) {
  121. height =
  122. hasIntrinsicWidth && hasIntrinsicHeight
  123. ? (width / intrinsicWidth) * intrinsicHeight
  124. : bounds.height;
  125. }
  126. if (height !== null && exports.isAuto(first)) {
  127. width =
  128. hasIntrinsicWidth && hasIntrinsicHeight
  129. ? (height / intrinsicHeight) * intrinsicWidth
  130. : bounds.width;
  131. }
  132. if (width !== null && height !== null) {
  133. return [width, height];
  134. }
  135. throw new Error("Unable to calculate background-size for element");
  136. };
  137. exports.getBackgroundValueForIndex = function (values, index) {
  138. var value = values[index];
  139. if (typeof value === 'undefined') {
  140. return values[0];
  141. }
  142. return value;
  143. };
  144. exports.calculateBackgroundRepeatPath = function (repeat, _a, _b, backgroundPositioningArea, backgroundPaintingArea) {
  145. var x = _a[0], y = _a[1];
  146. var width = _b[0], height = _b[1];
  147. switch (repeat) {
  148. case background_repeat_1.BACKGROUND_REPEAT.REPEAT_X:
  149. return [
  150. new vector_1.Vector(Math.round(backgroundPositioningArea.left), Math.round(backgroundPositioningArea.top + y)),
  151. new vector_1.Vector(Math.round(backgroundPositioningArea.left + backgroundPositioningArea.width), Math.round(backgroundPositioningArea.top + y)),
  152. new vector_1.Vector(Math.round(backgroundPositioningArea.left + backgroundPositioningArea.width), Math.round(height + backgroundPositioningArea.top + y)),
  153. new vector_1.Vector(Math.round(backgroundPositioningArea.left), Math.round(height + backgroundPositioningArea.top + y))
  154. ];
  155. case background_repeat_1.BACKGROUND_REPEAT.REPEAT_Y:
  156. return [
  157. new vector_1.Vector(Math.round(backgroundPositioningArea.left + x), Math.round(backgroundPositioningArea.top)),
  158. new vector_1.Vector(Math.round(backgroundPositioningArea.left + x + width), Math.round(backgroundPositioningArea.top)),
  159. new vector_1.Vector(Math.round(backgroundPositioningArea.left + x + width), Math.round(backgroundPositioningArea.height + backgroundPositioningArea.top)),
  160. new vector_1.Vector(Math.round(backgroundPositioningArea.left + x), Math.round(backgroundPositioningArea.height + backgroundPositioningArea.top))
  161. ];
  162. case background_repeat_1.BACKGROUND_REPEAT.NO_REPEAT:
  163. return [
  164. new vector_1.Vector(Math.round(backgroundPositioningArea.left + x), Math.round(backgroundPositioningArea.top + y)),
  165. new vector_1.Vector(Math.round(backgroundPositioningArea.left + x + width), Math.round(backgroundPositioningArea.top + y)),
  166. new vector_1.Vector(Math.round(backgroundPositioningArea.left + x + width), Math.round(backgroundPositioningArea.top + y + height)),
  167. new vector_1.Vector(Math.round(backgroundPositioningArea.left + x), Math.round(backgroundPositioningArea.top + y + height))
  168. ];
  169. default:
  170. return [
  171. new vector_1.Vector(Math.round(backgroundPaintingArea.left), Math.round(backgroundPaintingArea.top)),
  172. new vector_1.Vector(Math.round(backgroundPaintingArea.left + backgroundPaintingArea.width), Math.round(backgroundPaintingArea.top)),
  173. new vector_1.Vector(Math.round(backgroundPaintingArea.left + backgroundPaintingArea.width), Math.round(backgroundPaintingArea.height + backgroundPaintingArea.top)),
  174. new vector_1.Vector(Math.round(backgroundPaintingArea.left), Math.round(backgroundPaintingArea.height + backgroundPaintingArea.top))
  175. ];
  176. }
  177. };
  178. //# sourceMappingURL=background.js.map