HTMLSharedListElement.cpp (5102B)
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 9 #include "mozilla/MappedDeclarationsBuilder.h" 10 #include "mozilla/dom/HTMLDListElementBinding.h" 11 #include "mozilla/dom/HTMLLIElement.h" 12 #include "mozilla/dom/HTMLOListElementBinding.h" 13 #include "mozilla/dom/HTMLUListElementBinding.h" 14 #include "nsAttrValueInlines.h" 15 #include "nsGenericHTMLElement.h" 16 #include "nsGkAtoms.h" 17 #include "nsStyleConsts.h" 18 19 NS_IMPL_NS_NEW_HTML_ELEMENT(SharedList) 20 21 namespace mozilla::dom { 22 23 HTMLSharedListElement::~HTMLSharedListElement() = default; 24 25 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLSharedListElement, 26 nsGenericHTMLElement) 27 28 NS_IMPL_ELEMENT_CLONE(HTMLSharedListElement) 29 30 bool HTMLSharedListElement::ParseAttribute( 31 int32_t aNamespaceID, nsAtom* aAttribute, const nsAString& aValue, 32 nsIPrincipal* aMaybeScriptedPrincipal, nsAttrValue& aResult) { 33 if (aNamespaceID == kNameSpaceID_None) { 34 if (mNodeInfo->Equals(nsGkAtoms::ul)) { 35 if (aAttribute == nsGkAtoms::type) { 36 return aResult.ParseEnumValue(aValue, HTMLLIElement::kULTypeTable, 37 false); 38 } 39 } 40 if (mNodeInfo->Equals(nsGkAtoms::ol)) { 41 if (aAttribute == nsGkAtoms::type) { 42 return aResult.ParseEnumValue(aValue, HTMLLIElement::kOLTypeTable, 43 true); 44 } 45 if (aAttribute == nsGkAtoms::start) { 46 return aResult.ParseIntValue(aValue); 47 } 48 } 49 } 50 51 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue, 52 aMaybeScriptedPrincipal, aResult); 53 } 54 55 void HTMLSharedListElement::MapAttributesIntoRule( 56 MappedDeclarationsBuilder& aBuilder) { 57 if (!aBuilder.PropertyIsSet(eCSSProperty_list_style_type)) { 58 const nsAttrValue* value = aBuilder.GetAttr(nsGkAtoms::type); 59 if (value && value->Type() == nsAttrValue::eEnum) { 60 aBuilder.SetKeywordValue(eCSSProperty_list_style_type, 61 value->GetEnumValue()); 62 } 63 } 64 65 nsGenericHTMLElement::MapCommonAttributesInto(aBuilder); 66 } 67 68 void HTMLSharedListElement::MapOLAttributesIntoRule( 69 MappedDeclarationsBuilder& aBuilder) { 70 if (!aBuilder.PropertyIsSet(eCSSProperty_counter_reset)) { 71 const nsAttrValue* startAttr = aBuilder.GetAttr(nsGkAtoms::start); 72 bool haveStart = startAttr && startAttr->Type() == nsAttrValue::eInteger; 73 int32_t start = 0; 74 if (haveStart) { 75 start = startAttr->GetIntegerValue() - 1; 76 } 77 bool haveReversed = !!aBuilder.GetAttr(nsGkAtoms::reversed); 78 if (haveReversed) { 79 if (haveStart) { 80 start += 2; // i.e. the attr value + 1 81 } else { 82 start = std::numeric_limits<int32_t>::min(); 83 } 84 } 85 if (haveStart || haveReversed) { 86 aBuilder.SetCounterResetListItem(start, haveReversed); 87 } 88 } 89 90 HTMLSharedListElement::MapAttributesIntoRule(aBuilder); 91 } 92 93 NS_IMETHODIMP_(bool) 94 HTMLSharedListElement::IsAttributeMapped(const nsAtom* aAttribute) const { 95 if (mNodeInfo->Equals(nsGkAtoms::ul)) { 96 static const MappedAttributeEntry attributes[] = {{nsGkAtoms::type}, 97 {nullptr}}; 98 99 static const MappedAttributeEntry* const map[] = { 100 attributes, 101 sCommonAttributeMap, 102 }; 103 104 return FindAttributeDependence(aAttribute, map); 105 } 106 107 if (mNodeInfo->Equals(nsGkAtoms::ol)) { 108 static const MappedAttributeEntry attributes[] = {{nsGkAtoms::type}, 109 {nsGkAtoms::start}, 110 {nsGkAtoms::reversed}, 111 {nullptr}}; 112 113 static const MappedAttributeEntry* const map[] = { 114 attributes, 115 sCommonAttributeMap, 116 }; 117 118 return FindAttributeDependence(aAttribute, map); 119 } 120 121 return nsGenericHTMLElement::IsAttributeMapped(aAttribute); 122 } 123 124 nsMapRuleToAttributesFunc HTMLSharedListElement::GetAttributeMappingFunction() 125 const { 126 if (mNodeInfo->Equals(nsGkAtoms::ul)) { 127 return &MapAttributesIntoRule; 128 } 129 if (mNodeInfo->Equals(nsGkAtoms::ol)) { 130 return &MapOLAttributesIntoRule; 131 } 132 133 return nsGenericHTMLElement::GetAttributeMappingFunction(); 134 } 135 136 JSObject* HTMLSharedListElement::WrapNode(JSContext* aCx, 137 JS::Handle<JSObject*> aGivenProto) { 138 if (mNodeInfo->Equals(nsGkAtoms::ol)) { 139 return HTMLOListElement_Binding::Wrap(aCx, this, aGivenProto); 140 } 141 if (mNodeInfo->Equals(nsGkAtoms::dl)) { 142 return HTMLDListElement_Binding::Wrap(aCx, this, aGivenProto); 143 } 144 MOZ_ASSERT(mNodeInfo->Equals(nsGkAtoms::ul)); 145 return HTMLUListElement_Binding::Wrap(aCx, this, aGivenProto); 146 } 147 148 } // namespace mozilla::dom