| /* |
| * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| * (C) 2000 Dirk Mueller (mueller@kde.org) |
| * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Library General Public |
| * License as published by the Free Software Foundation; either |
| * version 2 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Library General Public License for more details. |
| * |
| * You should have received a copy of the GNU Library General Public License |
| * along with this library; see the file COPYING.LIB. If not, write to |
| * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| * Boston, MA 02110-1301, USA. |
| * |
| */ |
| |
| #include "third_party/blink/renderer/core/layout/layout_fieldset.h" |
| |
| #include "third_party/blink/renderer/core/css/css_property_names.h" |
| #include "third_party/blink/renderer/core/html/forms/html_legend_element.h" |
| #include "third_party/blink/renderer/core/paint/fieldset_painter.h" |
| #include "third_party/blink/renderer/platform/runtime_enabled_features.h" |
| |
| namespace blink { |
| |
| LayoutFieldset::LayoutFieldset(Element* element) : LayoutBlockFlow(element) {} |
| |
| MinMaxSizes LayoutFieldset::PreferredLogicalWidths() const { |
| MinMaxSizes sizes = LayoutBlockFlow::PreferredLogicalWidths(); |
| // Size-contained elements don't consider their contents for preferred sizing. |
| if (ShouldApplySizeContainment()) |
| return sizes; |
| |
| if (LayoutBox* legend = FindInFlowLegend()) { |
| int legend_min_width = legend->PreferredLogicalWidths().min_size.ToInt(); |
| |
| const Length& legend_margin_left = legend->StyleRef().MarginLeft(); |
| const Length& legend_margin_right = legend->StyleRef().MarginRight(); |
| |
| if (legend_margin_left.IsFixed()) |
| legend_min_width += legend_margin_left.Value(); |
| |
| if (legend_margin_right.IsFixed()) |
| legend_min_width += legend_margin_right.Value(); |
| |
| sizes.min_size = |
| max(sizes.min_size, legend_min_width + BorderAndPaddingWidth()); |
| } |
| |
| return sizes; |
| } |
| |
| LayoutObject* LayoutFieldset::LayoutSpecialExcludedChild(bool relayout_children, |
| SubtreeLayoutScope&) { |
| LayoutBox* legend = FindInFlowLegend(); |
| if (legend) { |
| LayoutRect old_legend_frame_rect = legend->FrameRect(); |
| |
| if (relayout_children) { |
| legend->SetNeedsLayoutAndFullPaintInvalidation( |
| layout_invalidation_reason::kFieldsetChanged); |
| } |
| legend->LayoutIfNeeded(); |
| |
| LayoutUnit logical_left; |
| if (StyleRef().IsLeftToRightDirection()) { |
| switch (legend->StyleRef().GetTextAlign()) { |
| case ETextAlign::kCenter: |
| logical_left = (LogicalWidth() - LogicalWidthForChild(*legend)) / 2; |
| break; |
| case ETextAlign::kRight: |
| logical_left = LogicalWidth() - BorderEnd() - PaddingEnd() - |
| LogicalWidthForChild(*legend); |
| break; |
| default: |
| logical_left = |
| BorderStart() + PaddingStart() + MarginStartForChild(*legend); |
| break; |
| } |
| } else { |
| switch (legend->StyleRef().GetTextAlign()) { |
| case ETextAlign::kLeft: |
| logical_left = BorderStart() + PaddingStart(); |
| break; |
| case ETextAlign::kCenter: { |
| // Make sure that the extra pixel goes to the end side in RTL (since |
| // it went to the end side in LTR). |
| LayoutUnit centered_width = |
| LogicalWidth() - LogicalWidthForChild(*legend); |
| logical_left = centered_width - centered_width / 2; |
| break; |
| } |
| default: |
| logical_left = LogicalWidth() - BorderStart() - PaddingStart() - |
| MarginStartForChild(*legend) - |
| LogicalWidthForChild(*legend); |
| break; |
| } |
| } |
| |
| SetLogicalLeftForChild(*legend, logical_left); |
| |
| LayoutUnit fieldset_border_before = LayoutUnit(BorderBefore()); |
| LayoutUnit legend_logical_height = LogicalHeightForChild(*legend); |
| |
| LayoutUnit legend_logical_top; |
| LayoutUnit collapsed_legend_extent; |
| // FIXME: We need to account for the legend's margin before too. |
| if (fieldset_border_before > legend_logical_height) { |
| // The <legend> is smaller than the associated fieldset before border |
| // so the latter determines positioning of the <legend>. The sizing |
| // depends |
| // on the legend's margins as we want to still follow the author's cues. |
| // Firefox completely ignores the margins in this case which seems wrong. |
| legend_logical_top = (fieldset_border_before - legend_logical_height) / 2; |
| collapsed_legend_extent = max<LayoutUnit>( |
| fieldset_border_before, legend_logical_top + legend_logical_height + |
| MarginAfterForChild(*legend)); |
| } else { |
| collapsed_legend_extent = |
| legend_logical_height + MarginAfterForChild(*legend); |
| } |
| |
| SetLogicalTopForChild(*legend, legend_logical_top); |
| SetLogicalHeight(PaddingBefore() + collapsed_legend_extent); |
| |
| if (legend->FrameRect() != old_legend_frame_rect) { |
| // We need to invalidate the fieldset border if the legend's frame |
| // changed. |
| SetShouldDoFullPaintInvalidation(); |
| } |
| } |
| return legend; |
| } |
| |
| LayoutBox* LayoutFieldset::FindInFlowLegend(const LayoutBlock& fieldset) { |
| DCHECK(fieldset.IsFieldset() || fieldset.IsLayoutNGFieldset()); |
| const LayoutBlock* parent = &fieldset; |
| if (RuntimeEnabledFeatures::LayoutNGFieldsetEnabled()) { |
| if (fieldset.IsLayoutNGFieldset()) { |
| // If there is a rendered legend, it will be found inside the anonymous |
| // fieldset wrapper. |
| parent = To<LayoutBlock>(fieldset.FirstChild()); |
| if (!parent) |
| return nullptr; |
| // If the anonymous fieldset wrapper is a multi-column, the rendered |
| // legend will be found inside the multi-column flow thread. |
| if (parent->FirstChild() && parent->FirstChild()->IsLayoutFlowThread()) |
| parent = To<LayoutBlock>(parent->FirstChild()); |
| } |
| } |
| for (LayoutObject* legend = parent->FirstChild(); legend; |
| legend = legend->NextSibling()) { |
| if (legend->IsRenderedLegendCandidate()) |
| return ToLayoutBox(legend); |
| } |
| return nullptr; |
| } |
| |
| LayoutBlock* LayoutFieldset::FindLegendContainingBlock( |
| const LayoutBox& legend, |
| AncestorSkipInfo* skip_info) { |
| DCHECK(legend.IsRenderedLegend()); |
| LayoutObject* parent = legend.Parent(); |
| if (!parent->IsAnonymous()) |
| return To<LayoutBlock>(parent); |
| |
| if (skip_info) |
| skip_info->Update(*parent); |
| |
| // In LayoutNG all children of a fieldset are wrapped inside an anonymous |
| // block. This also includes the rendered legend, even if that one really |
| // belongs on the outside as a direct fieldset child. Skip the anonymous |
| // wrapper in such cases. |
| if (parent->IsLayoutFlowThread()) { |
| // If the fieldset also establishes a multicol container, we need to skip |
| // the flow thread as well. |
| parent = parent->Parent(); |
| if (skip_info) |
| skip_info->Update(*parent); |
| DCHECK(parent->IsAnonymous()); |
| } |
| DCHECK(parent->Parent()->IsLayoutNGFieldset()); |
| return To<LayoutBlock>(parent->Parent()); |
| } |
| |
| void LayoutFieldset::PaintBoxDecorationBackground( |
| const PaintInfo& paint_info, |
| const PhysicalOffset& paint_offset) const { |
| FieldsetPainter(*this).PaintBoxDecorationBackground(paint_info, paint_offset); |
| } |
| |
| void LayoutFieldset::PaintMask(const PaintInfo& paint_info, |
| const PhysicalOffset& paint_offset) const { |
| FieldsetPainter(*this).PaintMask(paint_info, paint_offset); |
| } |
| |
| bool LayoutFieldset::BackgroundIsKnownToBeOpaqueInRect( |
| const PhysicalRect& local_rect) const { |
| // If the field set has a legend, then it probably does not completely fill |
| // its background. |
| if (FindInFlowLegend()) |
| return false; |
| |
| return LayoutBlockFlow::BackgroundIsKnownToBeOpaqueInRect(local_rect); |
| } |
| |
| } // namespace blink |