1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2006, 2007, 2010 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "HTMLLIElement.h"
25
26#include "Attribute.h"
27#include "CSSPropertyNames.h"
28#include "CSSValueKeywords.h"
29#include "ElementAncestorIterator.h"
30#include "HTMLNames.h"
31#include "HTMLOListElement.h"
32#include "HTMLUListElement.h"
33#include "RenderListItem.h"
34#include <wtf/IsoMallocInlines.h>
35
36namespace WebCore {
37
38WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLLIElement);
39
40using namespace HTMLNames;
41
42HTMLLIElement::HTMLLIElement(const QualifiedName& tagName, Document& document)
43 : HTMLElement(tagName, document)
44{
45 ASSERT(hasTagName(liTag));
46 setHasCustomStyleResolveCallbacks();
47}
48
49Ref<HTMLLIElement> HTMLLIElement::create(Document& document)
50{
51 return adoptRef(*new HTMLLIElement(liTag, document));
52}
53
54Ref<HTMLLIElement> HTMLLIElement::create(const QualifiedName& tagName, Document& document)
55{
56 return adoptRef(*new HTMLLIElement(tagName, document));
57}
58
59bool HTMLLIElement::isPresentationAttribute(const QualifiedName& name) const
60{
61 if (name == typeAttr)
62 return true;
63 return HTMLElement::isPresentationAttribute(name);
64}
65
66void HTMLLIElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStyleProperties& style)
67{
68 if (name == typeAttr) {
69 if (value == "a")
70 addPropertyToPresentationAttributeStyle(style, CSSPropertyListStyleType, CSSValueLowerAlpha);
71 else if (value == "A")
72 addPropertyToPresentationAttributeStyle(style, CSSPropertyListStyleType, CSSValueUpperAlpha);
73 else if (value == "i")
74 addPropertyToPresentationAttributeStyle(style, CSSPropertyListStyleType, CSSValueLowerRoman);
75 else if (value == "I")
76 addPropertyToPresentationAttributeStyle(style, CSSPropertyListStyleType, CSSValueUpperRoman);
77 else if (value == "1")
78 addPropertyToPresentationAttributeStyle(style, CSSPropertyListStyleType, CSSValueDecimal);
79 else
80 addPropertyToPresentationAttributeStyle(style, CSSPropertyListStyleType, value);
81 } else
82 HTMLElement::collectStyleForPresentationAttribute(name, value, style);
83}
84
85void HTMLLIElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
86{
87 if (name == valueAttr) {
88 if (renderer() && renderer()->isListItem())
89 parseValue(value);
90 } else
91 HTMLElement::parseAttribute(name, value);
92}
93
94void HTMLLIElement::didAttachRenderers()
95{
96 if (!is<RenderListItem>(renderer()))
97 return;
98 auto& listItemRenderer = downcast<RenderListItem>(*renderer());
99
100 // Check if there is an enclosing list.
101 bool isInList = false;
102 for (auto& ancestor : ancestorsOfType<HTMLElement>(*this)) {
103 if (is<HTMLUListElement>(ancestor) || is<HTMLOListElement>(ancestor)) {
104 isInList = true;
105 break;
106 }
107 }
108
109 // If we are not in a list, tell the renderer so it can position us inside.
110 // We don't want to change our style to say "inside" since that would affect nested nodes.
111 if (!isInList)
112 listItemRenderer.setNotInList(true);
113
114 parseValue(attributeWithoutSynchronization(valueAttr));
115}
116
117inline void HTMLLIElement::parseValue(const AtomicString& value)
118{
119 ASSERT(renderer());
120
121 bool valueOK;
122 int requestedValue = value.toInt(&valueOK);
123 if (valueOK)
124 downcast<RenderListItem>(*renderer()).setExplicitValue(requestedValue);
125 else
126 downcast<RenderListItem>(*renderer()).setExplicitValue(WTF::nullopt);
127}
128
129}
130