1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "mozilla/dom/HTMLLIElement.h"
8 #include "mozilla/dom/HTMLLIElementBinding.h"
9
10 #include "mozilla/MappedDeclarations.h"
11 #include "nsAttrValueInlines.h"
12 #include "nsGkAtoms.h"
13 #include "nsStyleConsts.h"
14 #include "nsMappedAttributes.h"
15
16 NS_IMPL_NS_NEW_HTML_ELEMENT(LI)
17
18 namespace mozilla::dom {
19
20 HTMLLIElement::~HTMLLIElement() = default;
21
22 NS_IMPL_ELEMENT_CLONE(HTMLLIElement)
23
24 // values that are handled case-insensitively
25 static const nsAttrValue::EnumTable kUnorderedListItemTypeTable[] = {
26 {"disc", NS_STYLE_LIST_STYLE_DISC},
27 {"circle", NS_STYLE_LIST_STYLE_CIRCLE},
28 {"round", NS_STYLE_LIST_STYLE_CIRCLE},
29 {"square", NS_STYLE_LIST_STYLE_SQUARE},
30 {nullptr, 0}};
31
32 // values that are handled case-sensitively
33 static const nsAttrValue::EnumTable kOrderedListItemTypeTable[] = {
34 {"A", NS_STYLE_LIST_STYLE_UPPER_ALPHA},
35 {"a", NS_STYLE_LIST_STYLE_LOWER_ALPHA},
36 {"I", NS_STYLE_LIST_STYLE_UPPER_ROMAN},
37 {"i", NS_STYLE_LIST_STYLE_LOWER_ROMAN},
38 {"1", NS_STYLE_LIST_STYLE_DECIMAL},
39 {nullptr, 0}};
40
ParseAttribute(int32_t aNamespaceID,nsAtom * aAttribute,const nsAString & aValue,nsIPrincipal * aMaybeScriptedPrincipal,nsAttrValue & aResult)41 bool HTMLLIElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
42 const nsAString& aValue,
43 nsIPrincipal* aMaybeScriptedPrincipal,
44 nsAttrValue& aResult) {
45 if (aNamespaceID == kNameSpaceID_None) {
46 if (aAttribute == nsGkAtoms::type) {
47 return aResult.ParseEnumValue(aValue, kOrderedListItemTypeTable, true) ||
48 aResult.ParseEnumValue(aValue, kUnorderedListItemTypeTable, false);
49 }
50 if (aAttribute == nsGkAtoms::value) {
51 return aResult.ParseIntValue(aValue);
52 }
53 }
54
55 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
56 aMaybeScriptedPrincipal, aResult);
57 }
58
MapAttributesIntoRule(const nsMappedAttributes * aAttributes,MappedDeclarations & aDecls)59 void HTMLLIElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
60 MappedDeclarations& aDecls) {
61 if (!aDecls.PropertyIsSet(eCSSProperty_list_style_type)) {
62 // type: enum
63 const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::type);
64 if (value && value->Type() == nsAttrValue::eEnum)
65 aDecls.SetKeywordValue(eCSSProperty_list_style_type,
66 value->GetEnumValue());
67 }
68
69 // Map <li value=INTEGER> to 'counter-set: list-item INTEGER'.
70 const nsAttrValue* attrVal = aAttributes->GetAttr(nsGkAtoms::value);
71 if (attrVal && attrVal->Type() == nsAttrValue::eInteger) {
72 if (!aDecls.PropertyIsSet(eCSSProperty_counter_set)) {
73 aDecls.SetCounterSetListItem(attrVal->GetIntegerValue());
74 }
75 }
76
77 nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aDecls);
78 }
79
NS_IMETHODIMP_(bool)80 NS_IMETHODIMP_(bool)
81 HTMLLIElement::IsAttributeMapped(const nsAtom* aAttribute) const {
82 static const MappedAttributeEntry attributes[] = {
83 {nsGkAtoms::type},
84 {nsGkAtoms::value},
85 {nullptr},
86 };
87
88 static const MappedAttributeEntry* const map[] = {
89 attributes,
90 sCommonAttributeMap,
91 };
92
93 return FindAttributeDependence(aAttribute, map);
94 }
95
GetAttributeMappingFunction() const96 nsMapRuleToAttributesFunc HTMLLIElement::GetAttributeMappingFunction() const {
97 return &MapAttributesIntoRule;
98 }
99
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)100 JSObject* HTMLLIElement::WrapNode(JSContext* aCx,
101 JS::Handle<JSObject*> aGivenProto) {
102 return HTMLLIElement_Binding::Wrap(aCx, this, aGivenProto);
103 }
104
105 } // namespace mozilla::dom
106