yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +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 | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 22 | #include "HTMLProgressElement.h" |
| 23 | |
antti@apple.com | 71ed935 | 2014-01-10 17:45:19 +0000 | [diff] [blame] | 24 | #include "ElementIterator.h" |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 25 | #include "HTMLNames.h" |
darin@apple.com | 2f2a980 | 2010-09-13 23:42:02 +0000 | [diff] [blame] | 26 | #include "HTMLParserIdioms.h" |
morrita@google.com | bcbb208 | 2011-04-06 17:03:26 +0000 | [diff] [blame] | 27 | #include "ProgressShadowElement.h" |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 28 | #include "RenderProgress.h" |
tkent@chromium.org | 553b172 | 2011-04-08 06:03:31 +0000 | [diff] [blame] | 29 | #include "ShadowRoot.h" |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | using namespace HTMLNames; |
| 34 | |
morrita@google.com | cd7fb9d | 2011-04-18 17:14:03 +0000 | [diff] [blame] | 35 | const double HTMLProgressElement::IndeterminatePosition = -1; |
| 36 | const double HTMLProgressElement::InvalidPosition = -2; |
| 37 | |
weinig@apple.com | 4917883 | 2013-09-15 00:39:29 +0000 | [diff] [blame] | 38 | HTMLProgressElement::HTMLProgressElement(const QualifiedName& tagName, Document& document) |
commit-queue@webkit.org | d18af50 | 2012-03-16 15:01:16 +0000 | [diff] [blame] | 39 | : LabelableElement(tagName, document) |
haraken@chromium.org | 3a27df5 | 2012-08-20 01:31:23 +0000 | [diff] [blame] | 40 | , m_value(0) |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 41 | { |
| 42 | ASSERT(hasTagName(progressTag)); |
antti@apple.com | 0f2ac5b | 2013-08-18 20:01:20 +0000 | [diff] [blame] | 43 | setHasCustomStyleResolveCallbacks(); |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 44 | } |
| 45 | |
dbates@webkit.org | f21f3ae | 2017-10-19 23:48:45 +0000 | [diff] [blame] | 46 | HTMLProgressElement::~HTMLProgressElement() = default; |
morrita@google.com | bcbb208 | 2011-04-06 17:03:26 +0000 | [diff] [blame] | 47 | |
weinig@apple.com | 02f433a | 2015-01-06 22:32:48 +0000 | [diff] [blame] | 48 | Ref<HTMLProgressElement> HTMLProgressElement::create(const QualifiedName& tagName, Document& document) |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 49 | { |
weinig@apple.com | 02f433a | 2015-01-06 22:32:48 +0000 | [diff] [blame] | 50 | Ref<HTMLProgressElement> progress = adoptRef(*new HTMLProgressElement(tagName, document)); |
esprehn@chromium.org | 933723d | 2013-01-29 07:47:20 +0000 | [diff] [blame] | 51 | progress->ensureUserAgentShadowRoot(); |
akling@apple.com | dbdca2f | 2014-11-22 09:12:01 +0000 | [diff] [blame] | 52 | return progress; |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 53 | } |
| 54 | |
antti@apple.com | 454418f | 2016-04-25 19:49:23 +0000 | [diff] [blame] | 55 | RenderPtr<RenderElement> HTMLProgressElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&) |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 56 | { |
antti@apple.com | 454418f | 2016-04-25 19:49:23 +0000 | [diff] [blame] | 57 | if (!style.hasAppearance()) |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 58 | return RenderElement::createFor(*this, WTFMove(style)); |
shinyak@chromium.org | c44aafa | 2012-08-06 12:00:14 +0000 | [diff] [blame] | 59 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 60 | return createRenderer<RenderProgress>(*this, WTFMove(style)); |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 61 | } |
| 62 | |
weinig@apple.com | da898c3 | 2013-11-11 04:02:09 +0000 | [diff] [blame] | 63 | bool HTMLProgressElement::childShouldCreateRenderer(const Node& child) const |
morrita@google.com | beaa664 | 2012-02-24 09:59:07 +0000 | [diff] [blame] | 64 | { |
antti@apple.com | dacd6de | 2013-08-20 22:52:55 +0000 | [diff] [blame] | 65 | return hasShadowRootParent(child) && HTMLElement::childShouldCreateRenderer(child); |
morrita@google.com | beaa664 | 2012-02-24 09:59:07 +0000 | [diff] [blame] | 66 | } |
| 67 | |
shinyak@chromium.org | c44aafa | 2012-08-06 12:00:14 +0000 | [diff] [blame] | 68 | RenderProgress* HTMLProgressElement::renderProgress() const |
| 69 | { |
cdumez@apple.com | 3abcc79 | 2014-10-20 03:42:03 +0000 | [diff] [blame] | 70 | if (is<RenderProgress>(renderer())) |
| 71 | return downcast<RenderProgress>(renderer()); |
| 72 | return downcast<RenderProgress>(descendantsOfType<Element>(*userAgentShadowRoot()).first()->renderer()); |
shinyak@chromium.org | c44aafa | 2012-08-06 12:00:14 +0000 | [diff] [blame] | 73 | } |
| 74 | |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 75 | void HTMLProgressElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 76 | { |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 77 | if (name == valueAttr) |
morrita@google.com | 3e2d400 | 2011-02-01 04:05:52 +0000 | [diff] [blame] | 78 | didElementStateChange(); |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 79 | else if (name == maxAttr) |
morrita@google.com | 3e2d400 | 2011-02-01 04:05:52 +0000 | [diff] [blame] | 80 | didElementStateChange(); |
| 81 | else |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 82 | LabelableElement::parseAttribute(name, value); |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 83 | } |
| 84 | |
antti@apple.com | 0f2ac5b | 2013-08-18 20:01:20 +0000 | [diff] [blame] | 85 | void HTMLProgressElement::didAttachRenderers() |
morrita@google.com | b172c47a | 2010-12-08 04:21:20 +0000 | [diff] [blame] | 86 | { |
tasak@google.com | 5f12a09 | 2012-11-01 04:34:52 +0000 | [diff] [blame] | 87 | if (RenderProgress* render = renderProgress()) |
| 88 | render->updateFromElement(); |
morrita@google.com | b172c47a | 2010-12-08 04:21:20 +0000 | [diff] [blame] | 89 | } |
| 90 | |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 91 | double HTMLProgressElement::value() const |
| 92 | { |
rniwa@webkit.org | e999a05 | 2016-07-16 15:21:55 +0000 | [diff] [blame] | 93 | double value = parseToDoubleForNumberType(attributeWithoutSynchronization(valueAttr)); |
zandobersek@gmail.com | 8c24b7a | 2013-02-18 17:13:23 +0000 | [diff] [blame] | 94 | return !std::isfinite(value) || value < 0 ? 0 : std::min(value, max()); |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 95 | } |
| 96 | |
darin@apple.com | 66d4118 | 2016-10-29 02:32:20 +0000 | [diff] [blame] | 97 | void HTMLProgressElement::setValue(double value) |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 98 | { |
darin@apple.com | 1d17a8f | 2017-10-08 22:17:46 +0000 | [diff] [blame] | 99 | setAttributeWithoutSynchronization(valueAttr, AtomicString::number(value)); |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | double HTMLProgressElement::max() const |
| 103 | { |
rniwa@webkit.org | e999a05 | 2016-07-16 15:21:55 +0000 | [diff] [blame] | 104 | double max = parseToDoubleForNumberType(attributeWithoutSynchronization(maxAttr)); |
zandobersek@gmail.com | 8c24b7a | 2013-02-18 17:13:23 +0000 | [diff] [blame] | 105 | return !std::isfinite(max) || max <= 0 ? 1 : max; |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 106 | } |
| 107 | |
darin@apple.com | 66d4118 | 2016-10-29 02:32:20 +0000 | [diff] [blame] | 108 | void HTMLProgressElement::setMax(double max) |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 109 | { |
darin@apple.com | 1d17a8f | 2017-10-08 22:17:46 +0000 | [diff] [blame] | 110 | if (max > 0) |
| 111 | setAttributeWithoutSynchronization(maxAttr, AtomicString::number(max)); |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | double HTMLProgressElement::position() const |
| 115 | { |
dominicc@chromium.org | cc5eb84 | 2011-07-05 04:21:24 +0000 | [diff] [blame] | 116 | if (!isDeterminate()) |
morrita@google.com | cd7fb9d | 2011-04-18 17:14:03 +0000 | [diff] [blame] | 117 | return HTMLProgressElement::IndeterminatePosition; |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 118 | return value() / max(); |
| 119 | } |
| 120 | |
dominicc@chromium.org | cc5eb84 | 2011-07-05 04:21:24 +0000 | [diff] [blame] | 121 | bool HTMLProgressElement::isDeterminate() const |
yael.aharon@nokia.com | 4cdb6b8 | 2011-06-14 13:22:18 +0000 | [diff] [blame] | 122 | { |
rniwa@webkit.org | bda54a0 | 2016-07-18 00:39:37 +0000 | [diff] [blame] | 123 | return hasAttributeWithoutSynchronization(valueAttr); |
yael.aharon@nokia.com | 4cdb6b8 | 2011-06-14 13:22:18 +0000 | [diff] [blame] | 124 | } |
| 125 | |
morrita@google.com | 3e2d400 | 2011-02-01 04:05:52 +0000 | [diff] [blame] | 126 | void HTMLProgressElement::didElementStateChange() |
| 127 | { |
dominicc@chromium.org | cc5eb84 | 2011-07-05 04:21:24 +0000 | [diff] [blame] | 128 | m_value->setWidthPercentage(position() * 100); |
shinyak@chromium.org | c44aafa | 2012-08-06 12:00:14 +0000 | [diff] [blame] | 129 | if (RenderProgress* render = renderProgress()) { |
yael.aharon@nokia.com | 4cdb6b8 | 2011-06-14 13:22:18 +0000 | [diff] [blame] | 130 | bool wasDeterminate = render->isDeterminate(); |
shinyak@chromium.org | c44aafa | 2012-08-06 12:00:14 +0000 | [diff] [blame] | 131 | render->updateFromElement(); |
morrita@google.com | 1531baa | 2013-01-08 17:29:32 +0000 | [diff] [blame] | 132 | if (wasDeterminate != isDeterminate()) |
antti@apple.com | 1c45583 | 2016-10-18 12:28:55 +0000 | [diff] [blame] | 133 | invalidateStyleForSubtree(); |
yael.aharon@nokia.com | 4cdb6b8 | 2011-06-14 13:22:18 +0000 | [diff] [blame] | 134 | } |
morrita@google.com | 3e2d400 | 2011-02-01 04:05:52 +0000 | [diff] [blame] | 135 | } |
| 136 | |
dbates@webkit.org | 62df276 | 2017-11-02 04:13:18 +0000 | [diff] [blame^] | 137 | void HTMLProgressElement::didAddUserAgentShadowRoot(ShadowRoot& root) |
morrita@google.com | 3e2d400 | 2011-02-01 04:05:52 +0000 | [diff] [blame] | 138 | { |
haraken@chromium.org | 3a27df5 | 2012-08-20 01:31:23 +0000 | [diff] [blame] | 139 | ASSERT(!m_value); |
shinyak@chromium.org | c44aafa | 2012-08-06 12:00:14 +0000 | [diff] [blame] | 140 | |
cdumez@apple.com | f45d2b5 | 2016-05-11 19:02:09 +0000 | [diff] [blame] | 141 | auto inner = ProgressInnerElement::create(document()); |
dbates@webkit.org | 62df276 | 2017-11-02 04:13:18 +0000 | [diff] [blame^] | 142 | root.appendChild(inner); |
morrita@google.com | 6b18c3f | 2012-02-09 05:46:57 +0000 | [diff] [blame] | 143 | |
cdumez@apple.com | f45d2b5 | 2016-05-11 19:02:09 +0000 | [diff] [blame] | 144 | auto bar = ProgressBarElement::create(document()); |
| 145 | auto value = ProgressValueElement::create(document()); |
cdumez@apple.com | c2ad848 | 2015-09-10 18:02:15 +0000 | [diff] [blame] | 146 | m_value = value.ptr(); |
tasak@google.com | 5f12a09 | 2012-11-01 04:34:52 +0000 | [diff] [blame] | 147 | m_value->setWidthPercentage(HTMLProgressElement::IndeterminatePosition * 100); |
darin@apple.com | 4a588ff | 2016-11-11 20:16:03 +0000 | [diff] [blame] | 148 | bar->appendChild(value); |
morrita@google.com | 6b18c3f | 2012-02-09 05:46:57 +0000 | [diff] [blame] | 149 | |
darin@apple.com | 4a588ff | 2016-11-11 20:16:03 +0000 | [diff] [blame] | 150 | inner->appendChild(bar); |
morrita@google.com | 3e2d400 | 2011-02-01 04:05:52 +0000 | [diff] [blame] | 151 | } |
| 152 | |
tkent@chromium.org | 7548f3e | 2013-03-26 08:26:04 +0000 | [diff] [blame] | 153 | bool HTMLProgressElement::shouldAppearIndeterminate() const |
| 154 | { |
| 155 | return !isDeterminate(); |
| 156 | } |
| 157 | |
yael.aharon@nokia.com | 93e750a | 2010-03-15 01:00:36 +0000 | [diff] [blame] | 158 | } // namespace |