|
Line 0
a/Source/WebCore/html/shadow/DateTimeEditElement.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 |
* Copyright (C) 2020 Apple Inc. 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 |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include "DateTimeEditElement.h" |
| 29 |
|
| 30 |
#if ENABLE(DATE_AND_TIME_INPUT_TYPES) |
| 31 |
|
| 32 |
#include "DateComponents.h" |
| 33 |
#include "DateTimeFieldElements.h" |
| 34 |
#include "DateTimeFormat.h" |
| 35 |
#include "DateTimeSymbolicFieldElement.h" |
| 36 |
#include "HTMLNames.h" |
| 37 |
#include "PlatformLocale.h" |
| 38 |
#include "Text.h" |
| 39 |
#include <wtf/IsoMallocInlines.h> |
| 40 |
#include <wtf/text/StringBuilder.h> |
| 41 |
|
| 42 |
namespace WebCore { |
| 43 |
|
| 44 |
using namespace HTMLNames; |
| 45 |
|
| 46 |
WTF_MAKE_ISO_ALLOCATED_IMPL(DateTimeEditElement); |
| 47 |
|
| 48 |
class DateTimeEditBuilder : private DateTimeFormat::TokenHandler { |
| 49 |
WTF_MAKE_NONCOPYABLE(DateTimeEditBuilder); |
| 50 |
|
| 51 |
public: |
| 52 |
DateTimeEditBuilder(DateTimeEditElement&, const DateTimeEditElement::LayoutParameters&); |
| 53 |
|
| 54 |
bool build(const String&); |
| 55 |
|
| 56 |
private: |
| 57 |
// DateTimeFormat::TokenHandler functions: |
| 58 |
void visitField(DateTimeFormat::FieldType, int) final; |
| 59 |
void visitLiteral(const String&) final; |
| 60 |
|
| 61 |
DateTimeEditElement& m_editElement; |
| 62 |
const DateTimeEditElement::LayoutParameters& m_parameters; |
| 63 |
}; |
| 64 |
|
| 65 |
DateTimeEditBuilder::DateTimeEditBuilder(DateTimeEditElement& element, const DateTimeEditElement::LayoutParameters& layoutParameters) |
| 66 |
: m_editElement(element) |
| 67 |
, m_parameters(layoutParameters) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
bool DateTimeEditBuilder::build(const String& formatString) |
| 72 |
{ |
| 73 |
m_editElement.resetFields(); |
| 74 |
return DateTimeFormat::parse(formatString, *this); |
| 75 |
} |
| 76 |
|
| 77 |
void DateTimeEditBuilder::visitField(DateTimeFormat::FieldType fieldType, int count) |
| 78 |
{ |
| 79 |
constexpr int countForAbbreviatedMonth = 3; |
| 80 |
constexpr int countForFullMonth = 4; |
| 81 |
constexpr int countForNarrowMonth = 5; |
| 82 |
Document& document = m_editElement.document(); |
| 83 |
|
| 84 |
switch (fieldType) { |
| 85 |
case DateTimeFormat::FieldTypeDayOfMonth: { |
| 86 |
m_editElement.addField(DateTimeDayFieldElement::create(document, m_editElement)); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
case DateTimeFormat::FieldTypeMonth: |
| 91 |
case DateTimeFormat::FieldTypeMonthStandAlone: { |
| 92 |
switch (count) { |
| 93 |
case countForNarrowMonth: |
| 94 |
case countForAbbreviatedMonth: { |
| 95 |
auto field = DateTimeSymbolicMonthFieldElement::create(document, m_editElement, fieldType == DateTimeFormat::FieldTypeMonth ? m_parameters.locale.shortMonthLabels() : m_parameters.locale.shortStandAloneMonthLabels()); |
| 96 |
m_editElement.addField(field); |
| 97 |
break; |
| 98 |
} |
| 99 |
case countForFullMonth: { |
| 100 |
auto field = DateTimeSymbolicMonthFieldElement::create(document, m_editElement, fieldType == DateTimeFormat::FieldTypeMonth ? m_parameters.locale.monthLabels() : m_parameters.locale.standAloneMonthLabels()); |
| 101 |
m_editElement.addField(field); |
| 102 |
break; |
| 103 |
} |
| 104 |
default: { |
| 105 |
m_editElement.addField(DateTimeMonthFieldElement::create(document, m_editElement)); |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
case DateTimeFormat::FieldTypeYear: { |
| 113 |
m_editElement.addField(DateTimeYearFieldElement::create(document, m_editElement)); |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
default: |
| 118 |
return; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void DateTimeEditBuilder::visitLiteral(const String& text) |
| 123 |
{ |
| 124 |
ASSERT(text.length()); |
| 125 |
static MainThreadNeverDestroyed<const AtomString> textPseudoId("-webkit-datetime-edit-text", AtomString::ConstructFromLiteral); |
| 126 |
auto element = HTMLDivElement::create(m_editElement.document()); |
| 127 |
element->setPseudo(textPseudoId); |
| 128 |
element->appendChild(Text::create(m_editElement.document(), text)); |
| 129 |
m_editElement.fieldsWrapperElement()->appendChild(element); |
| 130 |
} |
| 131 |
|
| 132 |
DateTimeEditElement::EditControlOwner::~EditControlOwner() |
| 133 |
{ |
| 134 |
} |
| 135 |
|
| 136 |
DateTimeEditElement::DateTimeEditElement(Document& document, EditControlOwner& editControlOwner) |
| 137 |
: HTMLDivElement(divTag, document) |
| 138 |
, m_editControlOwner(&editControlOwner) |
| 139 |
{ |
| 140 |
static MainThreadNeverDestroyed<const AtomString> dateTimeEditPseudoId("-webkit-datetime-edit", AtomString::ConstructFromLiteral); |
| 141 |
setPseudo(dateTimeEditPseudoId); |
| 142 |
} |
| 143 |
|
| 144 |
DateTimeEditElement::~DateTimeEditElement() |
| 145 |
{ |
| 146 |
} |
| 147 |
|
| 148 |
inline Element* DateTimeEditElement::fieldsWrapperElement() const |
| 149 |
{ |
| 150 |
ASSERT(firstChild()); |
| 151 |
return downcast<Element>(firstChild()); |
| 152 |
} |
| 153 |
|
| 154 |
void DateTimeEditElement::addField(Ref<DateTimeFieldElement> field) |
| 155 |
{ |
| 156 |
if (m_fields.size() == m_fields.capacity()) |
| 157 |
return; |
| 158 |
m_fields.append(field); |
| 159 |
fieldsWrapperElement()->appendChild(field); |
| 160 |
} |
| 161 |
|
| 162 |
Ref<DateTimeEditElement> DateTimeEditElement::create(Document& document, EditControlOwner& editControlOwner) |
| 163 |
{ |
| 164 |
return adoptRef(*new DateTimeEditElement(document, editControlOwner)); |
| 165 |
} |
| 166 |
|
| 167 |
void DateTimeEditElement::layout(const LayoutParameters& layoutParameters) |
| 168 |
{ |
| 169 |
static MainThreadNeverDestroyed<const AtomString> fieldsWrapperPseudoId("-webkit-datetime-edit-fields-wrapper", AtomString::ConstructFromLiteral); |
| 170 |
|
| 171 |
if (!firstChild()) { |
| 172 |
auto element = HTMLDivElement::create(document()); |
| 173 |
element->setPseudo(fieldsWrapperPseudoId); |
| 174 |
appendChild(element); |
| 175 |
} |
| 176 |
|
| 177 |
Element* fieldsWrapper = fieldsWrapperElement(); |
| 178 |
|
| 179 |
DateTimeEditBuilder builder(*this, layoutParameters); |
| 180 |
Node* lastChildToBeRemoved = fieldsWrapper->lastChild(); |
| 181 |
if (!builder.build(layoutParameters.dateTimeFormat) || m_fields.isEmpty()) { |
| 182 |
lastChildToBeRemoved = fieldsWrapper->lastChild(); |
| 183 |
builder.build(layoutParameters.fallbackDateTimeFormat); |
| 184 |
} |
| 185 |
|
| 186 |
if (lastChildToBeRemoved) { |
| 187 |
for (Node* childNode = fieldsWrapper->firstChild(); childNode; childNode = fieldsWrapper->firstChild()) { |
| 188 |
fieldsWrapper->removeChild(*childNode); |
| 189 |
if (childNode == lastChildToBeRemoved) |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
AtomString DateTimeEditElement::localeIdentifier() const |
| 196 |
{ |
| 197 |
return m_editControlOwner ? m_editControlOwner->localeIdentifier() : nullAtom(); |
| 198 |
} |
| 199 |
|
| 200 |
void DateTimeEditElement::resetFields() |
| 201 |
{ |
| 202 |
m_fields.shrink(0); |
| 203 |
} |
| 204 |
|
| 205 |
void DateTimeEditElement::setValueAsDate(const LayoutParameters& layoutParameters, const DateComponents& date) |
| 206 |
{ |
| 207 |
layout(layoutParameters); |
| 208 |
for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) |
| 209 |
m_fields[fieldIndex]->setValueAsDate(date); |
| 210 |
} |
| 211 |
|
| 212 |
void DateTimeEditElement::setEmptyValue(const LayoutParameters& layoutParameters) |
| 213 |
{ |
| 214 |
layout(layoutParameters); |
| 215 |
for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) |
| 216 |
m_fields[fieldIndex]->setEmptyValue(); |
| 217 |
} |
| 218 |
|
| 219 |
String DateTimeEditElement::value() const |
| 220 |
{ |
| 221 |
if (!m_editControlOwner) |
| 222 |
return emptyString(); |
| 223 |
return m_editControlOwner->valueForEditControl(); |
| 224 |
} |
| 225 |
|
| 226 |
} // namespace WebCore |
| 227 |
|
| 228 |
#endif // ENABLE(DATE_AND_TIME_INPUT_TYPES) |