FormattingContext.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/Dump.h>
  27. #include <LibWeb/Layout/BlockBox.h>
  28. #include <LibWeb/Layout/BlockFormattingContext.h>
  29. #include <LibWeb/Layout/Box.h>
  30. #include <LibWeb/Layout/FormattingContext.h>
  31. #include <LibWeb/Layout/InlineFormattingContext.h>
  32. #include <LibWeb/Layout/ReplacedBox.h>
  33. #include <LibWeb/Layout/TableBox.h>
  34. #include <LibWeb/Layout/TableCellBox.h>
  35. #include <LibWeb/Layout/TableFormattingContext.h>
  36. #include <LibWeb/Layout/TableRowBox.h>
  37. namespace Web::Layout {
  38. FormattingContext::FormattingContext(Box& context_box, FormattingContext* parent)
  39. : m_parent(parent)
  40. , m_context_box(&context_box)
  41. {
  42. }
  43. FormattingContext::~FormattingContext()
  44. {
  45. }
  46. bool FormattingContext::creates_block_formatting_context(const Box& box)
  47. {
  48. if (box.is_root_element())
  49. return true;
  50. if (box.is_floating())
  51. return true;
  52. if (box.is_absolutely_positioned())
  53. return true;
  54. if (box.is_inline_block())
  55. return true;
  56. if (is<TableCellBox>(box))
  57. return true;
  58. // FIXME: table-caption
  59. // FIXME: anonymous table cells
  60. // FIXME: Block elements where overflow has a value other than visible and clip.
  61. // FIXME: display: flow-root
  62. // FIXME: Elements with contain: layout, content, or paint.
  63. // FIXME: flex
  64. // FIXME: grid
  65. // FIXME: multicol
  66. // FIXME: column-span: all
  67. return false;
  68. }
  69. void FormattingContext::layout_inside(Box& box, LayoutMode layout_mode)
  70. {
  71. if (creates_block_formatting_context(box)) {
  72. BlockFormattingContext context(box, this);
  73. context.run(box, layout_mode);
  74. return;
  75. }
  76. if (is<TableBox>(box)) {
  77. TableFormattingContext context(box, this);
  78. context.run(box, layout_mode);
  79. } else if (box.children_are_inline()) {
  80. InlineFormattingContext context(box, this);
  81. context.run(box, layout_mode);
  82. } else {
  83. // FIXME: This needs refactoring!
  84. ASSERT(is_block_formatting_context());
  85. run(box, layout_mode);
  86. }
  87. }
  88. static float greatest_child_width(const Box& box)
  89. {
  90. float max_width = 0;
  91. if (box.children_are_inline()) {
  92. for (auto& child : box.line_boxes()) {
  93. max_width = max(max_width, child.width());
  94. }
  95. } else {
  96. box.for_each_child_of_type<Box>([&](auto& child) {
  97. max_width = max(max_width, child.border_box_width());
  98. });
  99. }
  100. return max_width;
  101. }
  102. FormattingContext::ShrinkToFitResult FormattingContext::calculate_shrink_to_fit_widths(Box& box)
  103. {
  104. // Calculate the preferred width by formatting the content without breaking lines
  105. // other than where explicit line breaks occur.
  106. layout_inside(box, LayoutMode::OnlyRequiredLineBreaks);
  107. float preferred_width = greatest_child_width(box);
  108. // Also calculate the preferred minimum width, e.g., by trying all possible line breaks.
  109. // CSS 2.2 does not define the exact algorithm.
  110. layout_inside(box, LayoutMode::AllPossibleLineBreaks);
  111. float preferred_minimum_width = greatest_child_width(box);
  112. return { preferred_width, preferred_minimum_width };
  113. }
  114. static Gfx::FloatSize solve_replaced_size_constraint(float w, float h, const ReplacedBox& box)
  115. {
  116. // 10.4 Minimum and maximum widths: 'min-width' and 'max-width'
  117. auto& containing_block = *box.containing_block();
  118. auto specified_min_width = box.computed_values().min_width().resolved_or_zero(box, containing_block.width()).to_px(box);
  119. auto specified_max_width = box.computed_values().max_width().resolved(CSS::Length::make_px(w), box, containing_block.width()).to_px(box);
  120. auto specified_min_height = box.computed_values().min_height().resolved_or_auto(box, containing_block.height()).to_px(box);
  121. auto specified_max_height = box.computed_values().max_height().resolved(CSS::Length::make_px(h), box, containing_block.height()).to_px(box);
  122. auto min_width = min(specified_min_width, specified_max_width);
  123. auto max_width = max(specified_min_width, specified_max_width);
  124. auto min_height = min(specified_min_height, specified_max_height);
  125. auto max_height = max(specified_min_height, specified_max_height);
  126. if (w > max_width)
  127. return { w, max(max_width * h / w, min_height) };
  128. if (w < min_width)
  129. return { max_width, min(min_width * h / w, max_height) };
  130. if (h > max_height)
  131. return { max(max_height * w / h, min_width), max_height };
  132. if (h < min_height)
  133. return { min(min_height * w / h, max_width), min_height };
  134. if ((w > max_width && h > max_height) && (max_width / w < max_height / h))
  135. return { max_width, max(min_height, max_width * h / w) };
  136. if ((w > max_width && h > max_height) && (max_width / w > max_height / h))
  137. return { max(min_width, max_height * w / h), max_height };
  138. if ((w < min_width && h < min_height) && (min_width / w < min_height / h))
  139. return { min(max_width, min_height * w / h), min_height };
  140. if ((w < min_width && h < min_height) && (min_width / w > min_height / h))
  141. return { min_width, min(max_height, min_width * h / w) };
  142. if (w < min_width && h > max_height)
  143. return { min_width, max_height };
  144. if (w > max_width && h < min_height)
  145. return { max_width, min_height };
  146. return { w, h };
  147. }
  148. float FormattingContext::tentative_width_for_replaced_element(const ReplacedBox& box, const CSS::Length& width)
  149. {
  150. auto& containing_block = *box.containing_block();
  151. auto specified_height = box.computed_values().height().resolved_or_auto(box, containing_block.height());
  152. float used_width = width.to_px(box);
  153. // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
  154. // then that intrinsic width is the used value of 'width'.
  155. if (specified_height.is_auto() && width.is_auto() && box.has_intrinsic_width()) {
  156. used_width = box.intrinsic_width();
  157. }
  158. // If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
  159. // but does have an intrinsic height and intrinsic ratio;
  160. // or if 'width' has a computed value of 'auto',
  161. // 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
  162. //
  163. // (used height) * (intrinsic ratio)
  164. else if ((specified_height.is_auto() && width.is_auto() && !box.has_intrinsic_width() && box.has_intrinsic_height() && box.has_intrinsic_ratio()) || (width.is_auto() && box.has_intrinsic_ratio())) {
  165. used_width = compute_height_for_replaced_element(box) * box.intrinsic_ratio();
  166. }
  167. else if (width.is_auto() && box.has_intrinsic_width()) {
  168. used_width = box.intrinsic_width();
  169. }
  170. else if (width.is_auto()) {
  171. used_width = 300;
  172. }
  173. return used_width;
  174. }
  175. void FormattingContext::compute_width_for_absolutely_positioned_element(Box& box)
  176. {
  177. if (is<ReplacedBox>(box))
  178. compute_width_for_absolutely_positioned_replaced_element(downcast<ReplacedBox>(box));
  179. else
  180. compute_width_for_absolutely_positioned_non_replaced_element(box);
  181. }
  182. void FormattingContext::compute_height_for_absolutely_positioned_element(Box& box)
  183. {
  184. if (is<ReplacedBox>(box))
  185. compute_height_for_absolutely_positioned_replaced_element(downcast<ReplacedBox>(box));
  186. else
  187. compute_height_for_absolutely_positioned_non_replaced_element(box);
  188. }
  189. float FormattingContext::compute_width_for_replaced_element(const ReplacedBox& box)
  190. {
  191. // 10.3.4 Block-level, replaced elements in normal flow...
  192. // 10.3.2 Inline, replaced elements
  193. auto zero_value = CSS::Length::make_px(0);
  194. auto& containing_block = *box.containing_block();
  195. auto margin_left = box.computed_values().margin().left.resolved_or_zero(box, containing_block.width());
  196. auto margin_right = box.computed_values().margin().right.resolved_or_zero(box, containing_block.width());
  197. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  198. if (margin_left.is_auto())
  199. margin_left = zero_value;
  200. if (margin_right.is_auto())
  201. margin_right = zero_value;
  202. auto specified_width = box.computed_values().width().resolved_or_auto(box, containing_block.width());
  203. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  204. auto used_width = tentative_width_for_replaced_element(box, specified_width);
  205. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  206. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  207. auto specified_max_width = box.computed_values().max_width().resolved_or_auto(box, containing_block.width());
  208. if (!specified_max_width.is_auto()) {
  209. if (used_width > specified_max_width.to_px(box)) {
  210. used_width = tentative_width_for_replaced_element(box, specified_max_width);
  211. }
  212. }
  213. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  214. // but this time using the value of 'min-width' as the computed value for 'width'.
  215. auto specified_min_width = box.computed_values().min_width().resolved_or_auto(box, containing_block.width());
  216. if (!specified_min_width.is_auto()) {
  217. if (used_width < specified_min_width.to_px(box)) {
  218. used_width = tentative_width_for_replaced_element(box, specified_min_width);
  219. }
  220. }
  221. return used_width;
  222. }
  223. float FormattingContext::tentative_height_for_replaced_element(const ReplacedBox& box, const CSS::Length& height)
  224. {
  225. auto& containing_block = *box.containing_block();
  226. auto specified_width = box.computed_values().width().resolved_or_auto(box, containing_block.width());
  227. float used_height = height.to_px(box);
  228. // If 'height' and 'width' both have computed values of 'auto' and the element also has
  229. // an intrinsic height, then that intrinsic height is the used value of 'height'.
  230. if (specified_width.is_auto() && height.is_auto() && box.has_intrinsic_height())
  231. used_height = box.intrinsic_height();
  232. else if (height.is_auto() && box.has_intrinsic_ratio())
  233. used_height = compute_width_for_replaced_element(box) / box.intrinsic_ratio();
  234. else if (height.is_auto() && box.has_intrinsic_height())
  235. used_height = box.intrinsic_height();
  236. else if (height.is_auto())
  237. used_height = 150;
  238. return used_height;
  239. }
  240. float FormattingContext::compute_height_for_replaced_element(const ReplacedBox& box)
  241. {
  242. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow,
  243. // 'inline-block' replaced elements in normal flow and floating replaced elements
  244. auto& containing_block = *box.containing_block();
  245. auto specified_width = box.computed_values().width().resolved_or_auto(box, containing_block.width());
  246. auto specified_height = box.computed_values().height().resolved_or_auto(box, containing_block.height());
  247. float used_height = tentative_height_for_replaced_element(box, specified_height);
  248. if (specified_width.is_auto() && specified_height.is_auto() && box.has_intrinsic_ratio()) {
  249. float w = tentative_width_for_replaced_element(box, specified_width);
  250. float h = used_height;
  251. used_height = solve_replaced_size_constraint(w, h, box).height();
  252. }
  253. return used_height;
  254. }
  255. void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_element(Box& box)
  256. {
  257. auto& containing_block = *box.containing_block();
  258. auto& computed_values = box.computed_values();
  259. auto zero_value = CSS::Length::make_px(0);
  260. auto margin_left = CSS::Length::make_auto();
  261. auto margin_right = CSS::Length::make_auto();
  262. const auto border_left = computed_values.border_left().width;
  263. const auto border_right = computed_values.border_right().width;
  264. const auto padding_left = computed_values.padding().left.resolved_or_zero(box, containing_block.width());
  265. const auto padding_right = computed_values.padding().right.resolved_or_zero(box, containing_block.width());
  266. auto try_compute_width = [&](const auto& a_width) {
  267. margin_left = computed_values.margin().left.resolved_or_zero(box, containing_block.width());
  268. margin_right = computed_values.margin().right.resolved_or_zero(box, containing_block.width());
  269. auto left = computed_values.offset().left.resolved_or_auto(box, containing_block.width());
  270. auto right = computed_values.offset().right.resolved_or_auto(box, containing_block.width());
  271. auto width = a_width;
  272. auto solve_for_left = [&] {
  273. return CSS::Length(containing_block.width() - margin_left.to_px(box) - border_left - padding_left.to_px(box) - width.to_px(box) - padding_right.to_px(box) - border_right - margin_right.to_px(box) - right.to_px(box), CSS::Length::Type::Px);
  274. };
  275. auto solve_for_width = [&] {
  276. return CSS::Length(containing_block.width() - left.to_px(box) - margin_left.to_px(box) - border_left - padding_left.to_px(box) - padding_right.to_px(box) - border_right - margin_right.to_px(box) - right.to_px(box), CSS::Length::Type::Px);
  277. };
  278. auto solve_for_right = [&] {
  279. return CSS::Length(containing_block.width() - left.to_px(box) - margin_left.to_px(box) - border_left - padding_left.to_px(box) - width.to_px(box) - padding_right.to_px(box) - border_right - margin_right.to_px(box), CSS::Length::Type::Px);
  280. };
  281. // If all three of 'left', 'width', and 'right' are 'auto':
  282. if (left.is_auto() && width.is_auto() && right.is_auto()) {
  283. // First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
  284. if (margin_left.is_auto())
  285. margin_left = CSS::Length::make_px(0);
  286. if (margin_right.is_auto())
  287. margin_right = CSS::Length::make_px(0);
  288. // Then, if the 'direction' property of the element establishing the static-position containing block
  289. // is 'ltr' set 'left' to the static position and apply rule number three below;
  290. // otherwise, set 'right' to the static position and apply rule number one below.
  291. // FIXME: This is very hackish.
  292. left = CSS::Length::make_px(0);
  293. goto Rule3;
  294. }
  295. if (!left.is_auto() && !width.is_auto() && !right.is_auto()) {
  296. // FIXME: This should be solved in a more complicated way.
  297. return width;
  298. }
  299. if (margin_left.is_auto())
  300. margin_left = CSS::Length::make_px(0);
  301. if (margin_right.is_auto())
  302. margin_right = CSS::Length::make_px(0);
  303. // 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
  304. // then the width is shrink-to-fit. Then solve for 'left'
  305. if (left.is_auto() && width.is_auto() && !right.is_auto()) {
  306. auto result = calculate_shrink_to_fit_widths(box);
  307. solve_for_left();
  308. auto available_width = solve_for_width();
  309. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  310. }
  311. // 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
  312. // then if the 'direction' property of the element establishing
  313. // the static-position containing block is 'ltr' set 'left'
  314. // to the static position, otherwise set 'right' to the static position.
  315. // Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
  316. else if (left.is_auto() && right.is_auto() && !width.is_auto()) {
  317. // FIXME: Check direction
  318. // FIXME: Use the static-position containing block
  319. left = zero_value;
  320. right = solve_for_right();
  321. }
  322. // 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
  323. // then the width is shrink-to-fit. Then solve for 'right'
  324. else if (width.is_auto() && right.is_auto() && !left.is_auto()) {
  325. Rule3:
  326. auto result = calculate_shrink_to_fit_widths(box);
  327. auto available_width = solve_for_width();
  328. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  329. right = solve_for_right();
  330. }
  331. // 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
  332. else if (left.is_auto() && !width.is_auto() && !right.is_auto()) {
  333. left = solve_for_left();
  334. }
  335. // 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
  336. else if (width.is_auto() && !left.is_auto() && !right.is_auto()) {
  337. width = solve_for_width();
  338. }
  339. // 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
  340. else if (right.is_auto() && !left.is_auto() && !width.is_auto()) {
  341. right = solve_for_right();
  342. }
  343. return width;
  344. };
  345. auto specified_width = computed_values.width().resolved_or_auto(box, containing_block.width());
  346. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  347. auto used_width = try_compute_width(specified_width);
  348. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  349. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  350. auto specified_max_width = computed_values.max_width().resolved_or_auto(box, containing_block.width());
  351. if (!specified_max_width.is_auto()) {
  352. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  353. used_width = try_compute_width(specified_max_width);
  354. }
  355. }
  356. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  357. // but this time using the value of 'min-width' as the computed value for 'width'.
  358. auto specified_min_width = computed_values.min_width().resolved_or_auto(box, containing_block.width());
  359. if (!specified_min_width.is_auto()) {
  360. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  361. used_width = try_compute_width(specified_min_width);
  362. }
  363. }
  364. box.set_width(used_width.to_px(box));
  365. box.box_model().margin.left = margin_left.to_px(box);
  366. box.box_model().margin.right = margin_right.to_px(box);
  367. box.box_model().border.left = border_left;
  368. box.box_model().border.right = border_right;
  369. box.box_model().padding.left = padding_left.to_px(box);
  370. box.box_model().padding.right = padding_right.to_px(box);
  371. }
  372. void FormattingContext::compute_width_for_absolutely_positioned_replaced_element(ReplacedBox& box)
  373. {
  374. // FIXME: Implement this.
  375. return compute_width_for_absolutely_positioned_non_replaced_element(box);
  376. }
  377. void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_element(Box& box)
  378. {
  379. auto& computed_values = box.computed_values();
  380. auto& containing_block = *box.containing_block();
  381. CSS::Length specified_height;
  382. if (computed_values.height().is_percentage() && !containing_block.computed_values().height().is_absolute()) {
  383. specified_height = CSS::Length::make_auto();
  384. } else {
  385. specified_height = computed_values.height().resolved_or_auto(box, containing_block.height());
  386. }
  387. auto specified_max_height = computed_values.max_height().resolved_or_auto(box, containing_block.height());
  388. box.box_model().margin.top = computed_values.margin().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  389. box.box_model().margin.bottom = computed_values.margin().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  390. box.box_model().border.top = computed_values.border_top().width;
  391. box.box_model().border.bottom = computed_values.border_bottom().width;
  392. box.box_model().padding.top = computed_values.padding().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  393. box.box_model().padding.bottom = computed_values.padding().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  394. if (!specified_height.is_auto()) {
  395. float used_height = specified_height.to_px(box);
  396. if (!specified_max_height.is_auto())
  397. used_height = min(used_height, specified_max_height.to_px(box));
  398. box.set_height(used_height);
  399. }
  400. }
  401. void FormattingContext::layout_absolutely_positioned_element(Box& box)
  402. {
  403. auto& containing_block = context_box();
  404. auto& box_model = box.box_model();
  405. auto specified_width = box.computed_values().width().resolved_or_auto(box, containing_block.width());
  406. compute_width_for_absolutely_positioned_element(box);
  407. layout_inside(box, LayoutMode::Default);
  408. compute_height_for_absolutely_positioned_element(box);
  409. box_model.margin.left = box.computed_values().margin().left.resolved_or_auto(box, containing_block.width()).to_px(box);
  410. box_model.margin.top = box.computed_values().margin().top.resolved_or_auto(box, containing_block.height()).to_px(box);
  411. box_model.margin.right = box.computed_values().margin().right.resolved_or_auto(box, containing_block.width()).to_px(box);
  412. box_model.margin.bottom = box.computed_values().margin().bottom.resolved_or_auto(box, containing_block.height()).to_px(box);
  413. box_model.border.left = box.computed_values().border_left().width;
  414. box_model.border.right = box.computed_values().border_right().width;
  415. box_model.border.top = box.computed_values().border_top().width;
  416. box_model.border.bottom = box.computed_values().border_bottom().width;
  417. box_model.offset.left = box.computed_values().offset().left.resolved_or_auto(box, containing_block.width()).to_px(box);
  418. box_model.offset.top = box.computed_values().offset().top.resolved_or_auto(box, containing_block.height()).to_px(box);
  419. box_model.offset.right = box.computed_values().offset().right.resolved_or_auto(box, containing_block.width()).to_px(box);
  420. box_model.offset.bottom = box.computed_values().offset().bottom.resolved_or_auto(box, containing_block.height()).to_px(box);
  421. if (box.computed_values().offset().left.is_auto() && specified_width.is_auto() && box.computed_values().offset().right.is_auto()) {
  422. if (box.computed_values().margin().left.is_auto())
  423. box_model.margin.left = 0;
  424. if (box.computed_values().margin().right.is_auto())
  425. box_model.margin.right = 0;
  426. }
  427. Gfx::FloatPoint used_offset;
  428. if (!box.computed_values().offset().left.is_auto()) {
  429. float x_offset = box_model.offset.left
  430. + box_model.border_box().left;
  431. used_offset.set_x(x_offset + box_model.margin.left);
  432. } else if (!box.computed_values().offset().right.is_auto()) {
  433. float x_offset = 0
  434. - box_model.offset.right
  435. - box_model.border_box().right;
  436. used_offset.set_x(containing_block.width() + x_offset - box.width() - box_model.margin.right);
  437. } else {
  438. float x_offset = box_model.margin_box().left;
  439. used_offset.set_x(x_offset);
  440. }
  441. if (!box.computed_values().offset().top.is_auto()) {
  442. float y_offset = box_model.offset.top
  443. + box_model.border_box().top;
  444. used_offset.set_y(y_offset + box_model.margin.top);
  445. } else if (!box.computed_values().offset().bottom.is_auto()) {
  446. float y_offset = 0
  447. - box_model.offset.bottom
  448. - box_model.border_box().bottom;
  449. used_offset.set_y(containing_block.height() + y_offset - box.height() - box_model.margin.bottom);
  450. } else {
  451. float y_offset = box_model.margin_box().top;
  452. used_offset.set_y(y_offset);
  453. }
  454. box.set_offset(used_offset);
  455. }
  456. void FormattingContext::compute_height_for_absolutely_positioned_replaced_element(ReplacedBox& box)
  457. {
  458. // FIXME: Implement this.
  459. return compute_height_for_absolutely_positioned_non_replaced_element(box);
  460. }
  461. }