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/HTMLSharedListElement.h"
8 #include "mozilla/dom/HTMLDListElementBinding.h"
9 #include "mozilla/dom/HTMLOListElementBinding.h"
10 #include "mozilla/dom/HTMLUListElementBinding.h"
11 
12 #include "mozilla/GenericSpecifiedValuesInlines.h"
13 #include "nsGenericHTMLElement.h"
14 #include "nsAttrValueInlines.h"
15 #include "nsGkAtoms.h"
16 #include "nsStyleConsts.h"
17 #include "nsMappedAttributes.h"
18 
19 NS_IMPL_NS_NEW_HTML_ELEMENT(SharedList)
20 
21 namespace mozilla {
22 namespace dom {
23 
~HTMLSharedListElement()24 HTMLSharedListElement::~HTMLSharedListElement() {}
25 
26 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLSharedListElement,
27                                                nsGenericHTMLElement)
28 
29 NS_IMPL_ELEMENT_CLONE(HTMLSharedListElement)
30 
31 // Shared with nsHTMLSharedElement.cpp
32 nsAttrValue::EnumTable kListTypeTable[] = {
33     {"none", NS_STYLE_LIST_STYLE_NONE},
34     {"disc", NS_STYLE_LIST_STYLE_DISC},
35     {"circle", NS_STYLE_LIST_STYLE_CIRCLE},
36     {"round", NS_STYLE_LIST_STYLE_CIRCLE},
37     {"square", NS_STYLE_LIST_STYLE_SQUARE},
38     {"decimal", NS_STYLE_LIST_STYLE_DECIMAL},
39     {"lower-roman", NS_STYLE_LIST_STYLE_LOWER_ROMAN},
40     {"upper-roman", NS_STYLE_LIST_STYLE_UPPER_ROMAN},
41     {"lower-alpha", NS_STYLE_LIST_STYLE_LOWER_ALPHA},
42     {"upper-alpha", NS_STYLE_LIST_STYLE_UPPER_ALPHA},
43     {nullptr, 0}};
44 
45 static const nsAttrValue::EnumTable kOldListTypeTable[] = {
46     {"1", NS_STYLE_LIST_STYLE_DECIMAL},
47     {"A", NS_STYLE_LIST_STYLE_UPPER_ALPHA},
48     {"a", NS_STYLE_LIST_STYLE_LOWER_ALPHA},
49     {"I", NS_STYLE_LIST_STYLE_UPPER_ROMAN},
50     {"i", NS_STYLE_LIST_STYLE_LOWER_ROMAN},
51     {nullptr, 0}};
52 
ParseAttribute(int32_t aNamespaceID,nsAtom * aAttribute,const nsAString & aValue,nsIPrincipal * aMaybeScriptedPrincipal,nsAttrValue & aResult)53 bool HTMLSharedListElement::ParseAttribute(
54     int32_t aNamespaceID, nsAtom* aAttribute, const nsAString& aValue,
55     nsIPrincipal* aMaybeScriptedPrincipal, nsAttrValue& aResult) {
56   if (aNamespaceID == kNameSpaceID_None) {
57     if (mNodeInfo->Equals(nsGkAtoms::ol) || mNodeInfo->Equals(nsGkAtoms::ul)) {
58       if (aAttribute == nsGkAtoms::type) {
59         return aResult.ParseEnumValue(aValue, kListTypeTable, false) ||
60                aResult.ParseEnumValue(aValue, kOldListTypeTable, true);
61       }
62       if (aAttribute == nsGkAtoms::start) {
63         return aResult.ParseIntValue(aValue);
64       }
65     }
66   }
67 
68   return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
69                                               aMaybeScriptedPrincipal, aResult);
70 }
71 
MapAttributesIntoRule(const nsMappedAttributes * aAttributes,GenericSpecifiedValues * aData)72 void HTMLSharedListElement::MapAttributesIntoRule(
73     const nsMappedAttributes* aAttributes, GenericSpecifiedValues* aData) {
74   if (aData->ShouldComputeStyleStruct(NS_STYLE_INHERIT_BIT(List))) {
75     if (!aData->PropertyIsSet(eCSSProperty_list_style_type)) {
76       // type: enum
77       const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::type);
78       if (value && value->Type() == nsAttrValue::eEnum) {
79         aData->SetKeywordValue(eCSSProperty_list_style_type,
80                                value->GetEnumValue());
81       }
82     }
83   }
84 
85   nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
86 }
87 
NS_IMETHODIMP_(bool)88 NS_IMETHODIMP_(bool)
89 HTMLSharedListElement::IsAttributeMapped(const nsAtom* aAttribute) const {
90   if (mNodeInfo->Equals(nsGkAtoms::ol) || mNodeInfo->Equals(nsGkAtoms::ul)) {
91     static const MappedAttributeEntry attributes[] = {{&nsGkAtoms::type},
92                                                       {nullptr}};
93 
94     static const MappedAttributeEntry* const map[] = {
95         attributes,
96         sCommonAttributeMap,
97     };
98 
99     return FindAttributeDependence(aAttribute, map);
100   }
101 
102   return nsGenericHTMLElement::IsAttributeMapped(aAttribute);
103 }
104 
GetAttributeMappingFunction() const105 nsMapRuleToAttributesFunc HTMLSharedListElement::GetAttributeMappingFunction()
106     const {
107   if (mNodeInfo->Equals(nsGkAtoms::ol) || mNodeInfo->Equals(nsGkAtoms::ul)) {
108     return &MapAttributesIntoRule;
109   }
110 
111   return nsGenericHTMLElement::GetAttributeMappingFunction();
112 }
113 
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)114 JSObject* HTMLSharedListElement::WrapNode(JSContext* aCx,
115                                           JS::Handle<JSObject*> aGivenProto) {
116   if (mNodeInfo->Equals(nsGkAtoms::ol)) {
117     return HTMLOListElementBinding::Wrap(aCx, this, aGivenProto);
118   }
119   if (mNodeInfo->Equals(nsGkAtoms::dl)) {
120     return HTMLDListElementBinding::Wrap(aCx, this, aGivenProto);
121   }
122   MOZ_ASSERT(mNodeInfo->Equals(nsGkAtoms::ul));
123   return HTMLUListElementBinding::Wrap(aCx, this, aGivenProto);
124 }
125 
126 }  // namespace dom
127 }  // namespace mozilla
128