yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 22 | #include "HTMLMeterElement.h" |
| 23 | |
darin@apple.com | 4e29cb2 | 2016-11-14 17:55:57 +0000 | [diff] [blame] | 24 | #if ENABLE(METER_ELEMENT) |
| 25 | |
weinig@apple.com | c360893 | 2010-05-19 17:48:06 +0000 | [diff] [blame] | 26 | #include "Attribute.h" |
antti@apple.com | 71ed935 | 2014-01-10 17:45:19 +0000 | [diff] [blame] | 27 | #include "ElementIterator.h" |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 28 | #include "HTMLDivElement.h" |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 29 | #include "HTMLFormElement.h" |
| 30 | #include "HTMLNames.h" |
darin@apple.com | 2f2a980 | 2010-09-13 23:42:02 +0000 | [diff] [blame] | 31 | #include "HTMLParserIdioms.h" |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 32 | #include "HTMLStyleElement.h" |
shinyak@chromium.org | 037f1cd | 2012-08-15 09:37:32 +0000 | [diff] [blame] | 33 | #include "Page.h" |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 34 | #include "RenderMeter.h" |
shinyak@chromium.org | 037f1cd | 2012-08-15 09:37:32 +0000 | [diff] [blame] | 35 | #include "RenderTheme.h" |
tkent@chromium.org | 553b172 | 2011-04-08 06:03:31 +0000 | [diff] [blame] | 36 | #include "ShadowRoot.h" |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 37 | #include "UserAgentStyleSheets.h" |
fpizlo@apple.com | 197cd32 | 2018-03-17 06:11:00 +0000 | [diff] [blame] | 38 | #include <wtf/IsoMallocInlines.h> |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 39 | |
| 40 | namespace WebCore { |
| 41 | |
fpizlo@apple.com | 197cd32 | 2018-03-17 06:11:00 +0000 | [diff] [blame] | 42 | WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLMeterElement); |
| 43 | |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 44 | using namespace HTMLNames; |
| 45 | |
weinig@apple.com | 4917883 | 2013-09-15 00:39:29 +0000 | [diff] [blame] | 46 | HTMLMeterElement::HTMLMeterElement(const QualifiedName& tagName, Document& document) |
commit-queue@webkit.org | a3eabcd | 2012-03-16 01:49:05 +0000 | [diff] [blame] | 47 | : LabelableElement(tagName, document) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 48 | { |
| 49 | ASSERT(hasTagName(meterTag)); |
| 50 | } |
| 51 | |
dbates@webkit.org | f21f3ae | 2017-10-19 23:48:45 +0000 | [diff] [blame] | 52 | HTMLMeterElement::~HTMLMeterElement() = default; |
morrita@google.com | ca4194d | 2011-04-05 02:01:56 +0000 | [diff] [blame] | 53 | |
weinig@apple.com | 02f433a | 2015-01-06 22:32:48 +0000 | [diff] [blame] | 54 | Ref<HTMLMeterElement> HTMLMeterElement::create(const QualifiedName& tagName, Document& document) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 55 | { |
weinig@apple.com | 02f433a | 2015-01-06 22:32:48 +0000 | [diff] [blame] | 56 | Ref<HTMLMeterElement> meter = adoptRef(*new HTMLMeterElement(tagName, document)); |
esprehn@chromium.org | 933723d | 2013-01-29 07:47:20 +0000 | [diff] [blame] | 57 | meter->ensureUserAgentShadowRoot(); |
morrita@google.com | ca4194d | 2011-04-05 02:01:56 +0000 | [diff] [blame] | 58 | return meter; |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 59 | } |
| 60 | |
antti@apple.com | 454418f | 2016-04-25 19:49:23 +0000 | [diff] [blame] | 61 | RenderPtr<RenderElement> HTMLMeterElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 62 | { |
antti@apple.com | 26b5c58 | 2017-05-16 05:35:04 +0000 | [diff] [blame] | 63 | if (!RenderTheme::singleton().supportsMeter(style.appearance())) |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 64 | return RenderElement::createFor(*this, WTFMove(style)); |
shinyak@chromium.org | 037f1cd | 2012-08-15 09:37:32 +0000 | [diff] [blame] | 65 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 66 | return createRenderer<RenderMeter>(*this, WTFMove(style)); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 67 | } |
| 68 | |
weinig@apple.com | da898c3 | 2013-11-11 04:02:09 +0000 | [diff] [blame] | 69 | bool HTMLMeterElement::childShouldCreateRenderer(const Node& child) const |
morrita@google.com | beaa664 | 2012-02-24 09:59:07 +0000 | [diff] [blame] | 70 | { |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 71 | return !is<RenderMeter>(renderer()) && HTMLElement::childShouldCreateRenderer(child); |
morrita@google.com | beaa664 | 2012-02-24 09:59:07 +0000 | [diff] [blame] | 72 | } |
| 73 | |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 74 | void HTMLMeterElement::parseAttribute(const QualifiedName& name, const AtomString& value) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 75 | { |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 76 | if (name == valueAttr || name == minAttr || name == maxAttr || name == lowAttr || name == highAttr || name == optimumAttr) |
morrita@google.com | ca4194d | 2011-04-05 02:01:56 +0000 | [diff] [blame] | 77 | didElementStateChange(); |
| 78 | else |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 79 | LabelableElement::parseAttribute(name, value); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | double HTMLMeterElement::min() const |
| 83 | { |
rniwa@webkit.org | e999a05 | 2016-07-16 15:21:55 +0000 | [diff] [blame] | 84 | return parseToDoubleForNumberType(attributeWithoutSynchronization(minAttr), 0); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 85 | } |
| 86 | |
cdumez@apple.com | bbe7d9a | 2016-09-21 23:50:24 +0000 | [diff] [blame] | 87 | void HTMLMeterElement::setMin(double min) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 88 | { |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 89 | setAttributeWithoutSynchronization(minAttr, AtomString::number(min)); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | double HTMLMeterElement::max() const |
| 93 | { |
rniwa@webkit.org | e999a05 | 2016-07-16 15:21:55 +0000 | [diff] [blame] | 94 | return std::max(parseToDoubleForNumberType(attributeWithoutSynchronization(maxAttr), std::max(1.0, min())), min()); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 95 | } |
| 96 | |
cdumez@apple.com | bbe7d9a | 2016-09-21 23:50:24 +0000 | [diff] [blame] | 97 | void HTMLMeterElement::setMax(double max) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 98 | { |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 99 | setAttributeWithoutSynchronization(maxAttr, AtomString::number(max)); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | double HTMLMeterElement::value() const |
| 103 | { |
rniwa@webkit.org | e999a05 | 2016-07-16 15:21:55 +0000 | [diff] [blame] | 104 | double value = parseToDoubleForNumberType(attributeWithoutSynchronization(valueAttr), 0); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 105 | return std::min(std::max(value, min()), max()); |
| 106 | } |
| 107 | |
cdumez@apple.com | bbe7d9a | 2016-09-21 23:50:24 +0000 | [diff] [blame] | 108 | void HTMLMeterElement::setValue(double value) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 109 | { |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 110 | setAttributeWithoutSynchronization(valueAttr, AtomString::number(value)); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | double HTMLMeterElement::low() const |
| 114 | { |
rniwa@webkit.org | e999a05 | 2016-07-16 15:21:55 +0000 | [diff] [blame] | 115 | double low = parseToDoubleForNumberType(attributeWithoutSynchronization(lowAttr), min()); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 116 | return std::min(std::max(low, min()), max()); |
| 117 | } |
| 118 | |
cdumez@apple.com | bbe7d9a | 2016-09-21 23:50:24 +0000 | [diff] [blame] | 119 | void HTMLMeterElement::setLow(double low) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 120 | { |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 121 | setAttributeWithoutSynchronization(lowAttr, AtomString::number(low)); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | double HTMLMeterElement::high() const |
| 125 | { |
rniwa@webkit.org | e999a05 | 2016-07-16 15:21:55 +0000 | [diff] [blame] | 126 | double high = parseToDoubleForNumberType(attributeWithoutSynchronization(highAttr), max()); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 127 | return std::min(std::max(high, low()), max()); |
| 128 | } |
| 129 | |
cdumez@apple.com | bbe7d9a | 2016-09-21 23:50:24 +0000 | [diff] [blame] | 130 | void HTMLMeterElement::setHigh(double high) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 131 | { |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 132 | setAttributeWithoutSynchronization(highAttr, AtomString::number(high)); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | double HTMLMeterElement::optimum() const |
| 136 | { |
rniwa@webkit.org | e999a05 | 2016-07-16 15:21:55 +0000 | [diff] [blame] | 137 | double optimum = parseToDoubleForNumberType(attributeWithoutSynchronization(optimumAttr), (max() + min()) / 2); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 138 | return std::min(std::max(optimum, min()), max()); |
| 139 | } |
| 140 | |
cdumez@apple.com | bbe7d9a | 2016-09-21 23:50:24 +0000 | [diff] [blame] | 141 | void HTMLMeterElement::setOptimum(double optimum) |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 142 | { |
darin@apple.com | 0ce67df | 2019-06-17 01:48:13 +0000 | [diff] [blame] | 143 | setAttributeWithoutSynchronization(optimumAttr, AtomString::number(optimum)); |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 144 | } |
| 145 | |
morrita@google.com | a70a8f4 | 2010-06-08 06:26:06 +0000 | [diff] [blame] | 146 | HTMLMeterElement::GaugeRegion HTMLMeterElement::gaugeRegion() const |
| 147 | { |
| 148 | double lowValue = low(); |
| 149 | double highValue = high(); |
| 150 | double theValue = value(); |
| 151 | double optimumValue = optimum(); |
| 152 | |
morrita@google.com | 2611bce | 2010-10-29 09:47:03 +0000 | [diff] [blame] | 153 | if (optimumValue < lowValue) { |
morrita@google.com | a70a8f4 | 2010-06-08 06:26:06 +0000 | [diff] [blame] | 154 | // The optimum range stays under low |
| 155 | if (theValue <= lowValue) |
| 156 | return GaugeRegionOptimum; |
| 157 | if (theValue <= highValue) |
| 158 | return GaugeRegionSuboptimal; |
| 159 | return GaugeRegionEvenLessGood; |
| 160 | } |
| 161 | |
morrita@google.com | 2611bce | 2010-10-29 09:47:03 +0000 | [diff] [blame] | 162 | if (highValue < optimumValue) { |
morrita@google.com | a70a8f4 | 2010-06-08 06:26:06 +0000 | [diff] [blame] | 163 | // The optimum range stays over high |
| 164 | if (highValue <= theValue) |
| 165 | return GaugeRegionOptimum; |
| 166 | if (lowValue <= theValue) |
| 167 | return GaugeRegionSuboptimal; |
| 168 | return GaugeRegionEvenLessGood; |
| 169 | } |
| 170 | |
morrita@google.com | 2611bce | 2010-10-29 09:47:03 +0000 | [diff] [blame] | 171 | // The optimum range stays between high and low. |
| 172 | // According to the standard, <meter> never show GaugeRegionEvenLessGood in this case |
| 173 | // because the value is never less or greater than min or max. |
| 174 | if (lowValue <= theValue && theValue <= highValue) |
morrita@google.com | a70a8f4 | 2010-06-08 06:26:06 +0000 | [diff] [blame] | 175 | return GaugeRegionOptimum; |
morrita@google.com | a70a8f4 | 2010-06-08 06:26:06 +0000 | [diff] [blame] | 176 | return GaugeRegionSuboptimal; |
| 177 | } |
| 178 | |
morrita@google.com | ca4194d | 2011-04-05 02:01:56 +0000 | [diff] [blame] | 179 | double HTMLMeterElement::valueRatio() const |
| 180 | { |
| 181 | double min = this->min(); |
| 182 | double max = this->max(); |
| 183 | double value = this->value(); |
| 184 | |
| 185 | if (max <= min) |
| 186 | return 0; |
| 187 | return (value - min) / (max - min); |
| 188 | } |
| 189 | |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 190 | static void setValueClass(HTMLElement& element, HTMLMeterElement::GaugeRegion gaugeRegion) |
| 191 | { |
| 192 | switch (gaugeRegion) { |
| 193 | case HTMLMeterElement::GaugeRegionOptimum: |
| 194 | element.setAttribute(HTMLNames::classAttr, "optimum"); |
| 195 | element.setPseudo("-webkit-meter-optimum-value"); |
| 196 | return; |
| 197 | case HTMLMeterElement::GaugeRegionSuboptimal: |
| 198 | element.setAttribute(HTMLNames::classAttr, "suboptimum"); |
| 199 | element.setPseudo("-webkit-meter-suboptimum-value"); |
| 200 | return; |
| 201 | case HTMLMeterElement::GaugeRegionEvenLessGood: |
| 202 | element.setAttribute(HTMLNames::classAttr, "even-less-good"); |
| 203 | element.setPseudo("-webkit-meter-even-less-good-value"); |
| 204 | return; |
| 205 | default: |
| 206 | ASSERT_NOT_REACHED(); |
| 207 | } |
| 208 | } |
| 209 | |
morrita@google.com | ca4194d | 2011-04-05 02:01:56 +0000 | [diff] [blame] | 210 | void HTMLMeterElement::didElementStateChange() |
| 211 | { |
simon.fraser@apple.com | 094a874 | 2019-11-13 04:41:17 +0000 | [diff] [blame] | 212 | m_value->setInlineStyleProperty(CSSPropertyWidth, valueRatio()*100, CSSUnitType::CSS_PERCENTAGE); |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 213 | setValueClass(*m_value, gaugeRegion()); |
| 214 | |
shinyak@chromium.org | 037f1cd | 2012-08-15 09:37:32 +0000 | [diff] [blame] | 215 | if (RenderMeter* render = renderMeter()) |
morrita@google.com | f198756 | 2011-09-21 05:41:27 +0000 | [diff] [blame] | 216 | render->updateFromElement(); |
morrita@google.com | ca4194d | 2011-04-05 02:01:56 +0000 | [diff] [blame] | 217 | } |
| 218 | |
shinyak@chromium.org | 037f1cd | 2012-08-15 09:37:32 +0000 | [diff] [blame] | 219 | RenderMeter* HTMLMeterElement::renderMeter() const |
| 220 | { |
cdumez@apple.com | 3abcc79 | 2014-10-20 03:42:03 +0000 | [diff] [blame] | 221 | if (is<RenderMeter>(renderer())) |
| 222 | return downcast<RenderMeter>(renderer()); |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 223 | return nullptr; |
shinyak@chromium.org | 037f1cd | 2012-08-15 09:37:32 +0000 | [diff] [blame] | 224 | } |
| 225 | |
dbates@webkit.org | 62df276 | 2017-11-02 04:13:18 +0000 | [diff] [blame] | 226 | void HTMLMeterElement::didAddUserAgentShadowRoot(ShadowRoot& root) |
morrita@google.com | ca4194d | 2011-04-05 02:01:56 +0000 | [diff] [blame] | 227 | { |
esprehn@chromium.org | 933723d | 2013-01-29 07:47:20 +0000 | [diff] [blame] | 228 | ASSERT(!m_value); |
shinyak@chromium.org | 037f1cd | 2012-08-15 09:37:32 +0000 | [diff] [blame] | 229 | |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 230 | static NeverDestroyed<String> shadowStyle(meterElementShadowUserAgentStyleSheet, String::ConstructFromLiteral); |
| 231 | |
| 232 | auto style = HTMLStyleElement::create(HTMLNames::styleTag, document(), false); |
darin@apple.com | 4a588ff | 2016-11-11 20:16:03 +0000 | [diff] [blame] | 233 | style->setTextContent(shadowStyle); |
dbates@webkit.org | 62df276 | 2017-11-02 04:13:18 +0000 | [diff] [blame] | 234 | root.appendChild(style); |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 235 | |
| 236 | // Pseudos are set to allow author styling. |
| 237 | auto inner = HTMLDivElement::create(document()); |
| 238 | inner->setIdAttribute("inner"); |
| 239 | inner->setPseudo("-webkit-meter-inner-element"); |
dbates@webkit.org | 62df276 | 2017-11-02 04:13:18 +0000 | [diff] [blame] | 240 | root.appendChild(inner); |
morrita@google.com | 6b18c3f | 2012-02-09 05:46:57 +0000 | [diff] [blame] | 241 | |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 242 | auto bar = HTMLDivElement::create(document()); |
| 243 | bar->setIdAttribute("bar"); |
| 244 | bar->setPseudo("-webkit-meter-bar"); |
darin@apple.com | 4a588ff | 2016-11-11 20:16:03 +0000 | [diff] [blame] | 245 | inner->appendChild(bar); |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 246 | |
| 247 | m_value = HTMLDivElement::create(document()); |
| 248 | m_value->setIdAttribute("value"); |
darin@apple.com | 4a588ff | 2016-11-11 20:16:03 +0000 | [diff] [blame] | 249 | bar->appendChild(*m_value); |
morrita@google.com | 6b18c3f | 2012-02-09 05:46:57 +0000 | [diff] [blame] | 250 | |
antti@apple.com | b4a1f8c | 2016-10-13 09:22:38 +0000 | [diff] [blame] | 251 | didElementStateChange(); |
morrita@google.com | ca4194d | 2011-04-05 02:01:56 +0000 | [diff] [blame] | 252 | } |
| 253 | |
yael.aharon@nokia.com | 666f4b8 | 2010-05-15 16:41:06 +0000 | [diff] [blame] | 254 | } // namespace |
| 255 | #endif |