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 "nsGenericHTMLElement.h"
13 #include "nsAttrValueInlines.h"
14 #include "nsGkAtoms.h"
15 #include "nsStyleConsts.h"
16 #include "nsMappedAttributes.h"
17 #include "nsRuleData.h"
18 
19 NS_IMPL_NS_NEW_HTML_ELEMENT(SharedList)
20 
21 namespace mozilla {
22 namespace dom {
23 
~HTMLSharedListElement()24 HTMLSharedListElement::~HTMLSharedListElement()
25 {
26 }
27 
28 NS_IMPL_ADDREF_INHERITED(HTMLSharedListElement, Element)
29 NS_IMPL_RELEASE_INHERITED(HTMLSharedListElement, Element)
30 
31 // QueryInterface implementation for nsHTMLSharedListElement
32 NS_INTERFACE_MAP_BEGIN(HTMLSharedListElement)
33   NS_INTERFACE_MAP_ENTRY_IF_TAG(nsIDOMHTMLOListElement, ol)
34   NS_INTERFACE_MAP_ENTRY_IF_TAG(nsIDOMHTMLUListElement, ul)
35 NS_INTERFACE_MAP_END_INHERITING(nsGenericHTMLElement)
36 
37 
38 NS_IMPL_ELEMENT_CLONE(HTMLSharedListElement)
39 
40 
41 NS_IMPL_BOOL_ATTR(HTMLSharedListElement, Compact, compact)
42 NS_IMPL_INT_ATTR_DEFAULT_VALUE(HTMLSharedListElement, Start, start, 1)
43 NS_IMPL_STRING_ATTR(HTMLSharedListElement, Type, type)
44 NS_IMPL_BOOL_ATTR(HTMLSharedListElement, Reversed, reversed)
45 
46 // Shared with nsHTMLSharedElement.cpp
47 nsAttrValue::EnumTable kListTypeTable[] = {
48   { "none", NS_STYLE_LIST_STYLE_NONE },
49   { "disc", NS_STYLE_LIST_STYLE_DISC },
50   { "circle", NS_STYLE_LIST_STYLE_CIRCLE },
51   { "round", NS_STYLE_LIST_STYLE_CIRCLE },
52   { "square", NS_STYLE_LIST_STYLE_SQUARE },
53   { "decimal", NS_STYLE_LIST_STYLE_DECIMAL },
54   { "lower-roman", NS_STYLE_LIST_STYLE_LOWER_ROMAN },
55   { "upper-roman", NS_STYLE_LIST_STYLE_UPPER_ROMAN },
56   { "lower-alpha", NS_STYLE_LIST_STYLE_LOWER_ALPHA },
57   { "upper-alpha", NS_STYLE_LIST_STYLE_UPPER_ALPHA },
58   { nullptr, 0 }
59 };
60 
61 static const nsAttrValue::EnumTable kOldListTypeTable[] = {
62   { "1", NS_STYLE_LIST_STYLE_DECIMAL },
63   { "A", NS_STYLE_LIST_STYLE_UPPER_ALPHA },
64   { "a", NS_STYLE_LIST_STYLE_LOWER_ALPHA },
65   { "I", NS_STYLE_LIST_STYLE_UPPER_ROMAN },
66   { "i", NS_STYLE_LIST_STYLE_LOWER_ROMAN },
67   { nullptr, 0 }
68 };
69 
70 bool
ParseAttribute(int32_t aNamespaceID,nsIAtom * aAttribute,const nsAString & aValue,nsAttrValue & aResult)71 HTMLSharedListElement::ParseAttribute(int32_t aNamespaceID,
72                                       nsIAtom* aAttribute,
73                                       const nsAString& aValue,
74                                       nsAttrValue& aResult)
75 {
76   if (aNamespaceID == kNameSpaceID_None) {
77     if (mNodeInfo->Equals(nsGkAtoms::ol) ||
78         mNodeInfo->Equals(nsGkAtoms::ul)) {
79       if (aAttribute == nsGkAtoms::type) {
80         return aResult.ParseEnumValue(aValue, kListTypeTable, false) ||
81                aResult.ParseEnumValue(aValue, kOldListTypeTable, true);
82       }
83       if (aAttribute == nsGkAtoms::start) {
84         return aResult.ParseIntValue(aValue);
85       }
86     }
87   }
88 
89   return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
90                                               aResult);
91 }
92 
93 void
MapAttributesIntoRule(const nsMappedAttributes * aAttributes,nsRuleData * aData)94 HTMLSharedListElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
95                                              nsRuleData* aData)
96 {
97   if (aData->mSIDs & NS_STYLE_INHERIT_BIT(List)) {
98     nsCSSValue* listStyleType = aData->ValueForListStyleType();
99     if (listStyleType->GetUnit() == eCSSUnit_Null) {
100       // type: enum
101       const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::type);
102       if (value && value->Type() == nsAttrValue::eEnum) {
103         listStyleType->SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
104       }
105     }
106   }
107 
108   nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
109 }
110 
NS_IMETHODIMP_(bool)111 NS_IMETHODIMP_(bool)
112 HTMLSharedListElement::IsAttributeMapped(const nsIAtom* aAttribute) const
113 {
114   if (mNodeInfo->Equals(nsGkAtoms::ol) ||
115       mNodeInfo->Equals(nsGkAtoms::ul)) {
116     static const MappedAttributeEntry attributes[] = {
117       { &nsGkAtoms::type },
118       { nullptr }
119     };
120 
121     static const MappedAttributeEntry* const map[] = {
122       attributes,
123       sCommonAttributeMap,
124     };
125 
126     return FindAttributeDependence(aAttribute, map);
127   }
128 
129   return nsGenericHTMLElement::IsAttributeMapped(aAttribute);
130 }
131 
132 nsMapRuleToAttributesFunc
GetAttributeMappingFunction() const133 HTMLSharedListElement::GetAttributeMappingFunction() const
134 {
135   if (mNodeInfo->Equals(nsGkAtoms::ol) ||
136       mNodeInfo->Equals(nsGkAtoms::ul)) {
137     return &MapAttributesIntoRule;
138   }
139 
140   return nsGenericHTMLElement::GetAttributeMappingFunction();
141 }
142 
143 JSObject*
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)144 HTMLSharedListElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
145 {
146   if (mNodeInfo->Equals(nsGkAtoms::ol)) {
147     return HTMLOListElementBinding::Wrap(aCx, this, aGivenProto);
148   }
149   if (mNodeInfo->Equals(nsGkAtoms::dl)) {
150     return HTMLDListElementBinding::Wrap(aCx, this, aGivenProto);
151   }
152   MOZ_ASSERT(mNodeInfo->Equals(nsGkAtoms::ul));
153   return HTMLUListElementBinding::Wrap(aCx, this, aGivenProto);
154 }
155 
156 } // namespace dom
157 } // namespace mozilla
158