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/HTMLTableCaptionElement.h"
8 
9 #include "mozilla/MappedDeclarations.h"
10 #include "nsAttrValueInlines.h"
11 #include "nsMappedAttributes.h"
12 #include "nsStyleConsts.h"
13 #include "mozilla/dom/HTMLTableCaptionElementBinding.h"
14 
15 NS_IMPL_NS_NEW_HTML_ELEMENT(TableCaption)
16 
17 namespace mozilla::dom {
18 
19 HTMLTableCaptionElement::~HTMLTableCaptionElement() = default;
20 
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)21 JSObject* HTMLTableCaptionElement::WrapNode(JSContext* aCx,
22                                             JS::Handle<JSObject*> aGivenProto) {
23   return HTMLTableCaptionElement_Binding::Wrap(aCx, this, aGivenProto);
24 }
25 
26 NS_IMPL_ELEMENT_CLONE(HTMLTableCaptionElement)
27 
28 static const nsAttrValue::EnumTable kCaptionAlignTable[] = {
29     {"left", StyleCaptionSide::Left},
30     {"right", StyleCaptionSide::Right},
31     {"top", StyleCaptionSide::Top},
32     {"bottom", StyleCaptionSide::Bottom},
33     {nullptr, 0}};
34 
ParseAttribute(int32_t aNamespaceID,nsAtom * aAttribute,const nsAString & aValue,nsIPrincipal * aMaybeScriptedPrincipal,nsAttrValue & aResult)35 bool HTMLTableCaptionElement::ParseAttribute(
36     int32_t aNamespaceID, nsAtom* aAttribute, const nsAString& aValue,
37     nsIPrincipal* aMaybeScriptedPrincipal, nsAttrValue& aResult) {
38   if (aAttribute == nsGkAtoms::align && aNamespaceID == kNameSpaceID_None) {
39     return aResult.ParseEnumValue(aValue, kCaptionAlignTable, false);
40   }
41 
42   return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
43                                               aMaybeScriptedPrincipal, aResult);
44 }
45 
MapAttributesIntoRule(const nsMappedAttributes * aAttributes,MappedDeclarations & aDecls)46 void HTMLTableCaptionElement::MapAttributesIntoRule(
47     const nsMappedAttributes* aAttributes, MappedDeclarations& aDecls) {
48   if (!aDecls.PropertyIsSet(eCSSProperty_caption_side)) {
49     const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::align);
50     if (value && value->Type() == nsAttrValue::eEnum)
51       aDecls.SetKeywordValue(eCSSProperty_caption_side, value->GetEnumValue());
52   }
53 
54   nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aDecls);
55 }
56 
NS_IMETHODIMP_(bool)57 NS_IMETHODIMP_(bool)
58 HTMLTableCaptionElement::IsAttributeMapped(const nsAtom* aAttribute) const {
59   static const MappedAttributeEntry attributes[] = {{nsGkAtoms::align},
60                                                     {nullptr}};
61 
62   static const MappedAttributeEntry* const map[] = {
63       attributes,
64       sCommonAttributeMap,
65   };
66 
67   return FindAttributeDependence(aAttribute, map);
68 }
69 
GetAttributeMappingFunction() const70 nsMapRuleToAttributesFunc HTMLTableCaptionElement::GetAttributeMappingFunction()
71     const {
72   return &MapAttributesIntoRule;
73 }
74 
75 }  // namespace mozilla::dom
76