1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/**
* Copyright (C) 2004 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
*
* 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_counter.h"
#include <memory>
#include "base/memory/ptr_util.h"
#include "base/numerics/clamped_math.h"
#include "third_party/blink/renderer/core/css/counter_style.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/element_traversal.h"
#include "third_party/blink/renderer/core/dom/flat_tree_traversal.h"
#include "third_party/blink/renderer/core/html/html_directory_element.h"
#include "third_party/blink/renderer/core/html/html_menu_element.h"
#include "third_party/blink/renderer/core/html/html_olist_element.h"
#include "third_party/blink/renderer/core/html/html_ulist_element.h"
#include "third_party/blink/renderer/core/html/list_item_ordinal.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#if DCHECK_IS_ON()
#include <stdio.h>
#endif
namespace blink {
namespace {
String CounterTextFromStyleAndValue(const CounterStyle* counter_style,
int value) {
if (!counter_style) {
return g_empty_string;
}
return counter_style->GenerateRepresentation(value);
}
} // namespace
LayoutCounter::LayoutCounter(Document& document,
const CounterContentData& counter)
: LayoutText(nullptr, StringImpl::empty_), counter_(counter) {
SetDocumentForAnonymous(&document);
View()->AddLayoutCounter();
}
LayoutCounter::~LayoutCounter() = default;
void LayoutCounter::Trace(Visitor* visitor) const {
visitor->Trace(counter_);
LayoutText::Trace(visitor);
}
void LayoutCounter::WillBeDestroyed() {
NOT_DESTROYED();
if (View()) {
View()->RemoveLayoutCounter();
}
LayoutText::WillBeDestroyed();
}
String LayoutCounter::GenerateCounterText(Vector<int> counter_values,
const CounterStyle* counter_style,
const AtomicString& separator) {
String text =
CounterTextFromStyleAndValue(counter_style, counter_values.front());
if (!separator.IsNull()) {
for (wtf_size_t i = 1u; i < counter_values.size(); ++i) {
text = CounterTextFromStyleAndValue(counter_style, counter_values[i]) +
separator + text;
}
}
return text;
}
void LayoutCounter::UpdateCounter(Vector<int> counter_values) {
NOT_DESTROYED();
SetTextIfNeeded(
GenerateCounterText(counter_values, NullableCounterStyle(), Separator()));
}
const CounterStyle* LayoutCounter::NullableCounterStyle() const {
NOT_DESTROYED();
// Note: CSS3 spec doesn't allow 'none' but CSS2.1 allows it. We currently
// allow it for backward compatibility.
// See https://github.com/w3c/csswg-drafts/issues/5795 for details.
if (counter_->ListStyle() == "none") {
return nullptr;
}
return &GetDocument().GetStyleEngine().FindCounterStyleAcrossScopes(
counter_->ListStyle(), counter_->GetTreeScope());
}
bool LayoutCounter::IsDirectionalSymbolMarker() const {
NOT_DESTROYED();
const auto* counter_style = NullableCounterStyle();
if (!counter_style || !counter_style->IsPredefinedSymbolMarker()) {
return false;
}
const AtomicString& list_style = counter_->ListStyle();
return list_style == keywords::kDisclosureOpen ||
list_style == keywords::kDisclosureClosed;
}
const AtomicString& LayoutCounter::Separator() const {
NOT_DESTROYED();
return counter_->Separator();
}
// static
const AtomicString& LayoutCounter::ListStyle(const LayoutObject* object,
const ComputedStyle& style) {
if (const auto* counter = DynamicTo<LayoutCounter>(object)) {
return counter->counter_->ListStyle();
}
return style.ListStyleType()->GetCounterStyleName();
}
} // namespace blink
|