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/. */
7 #include "mozilla/dom/HTMLTableSectionElement.h"
8 #include "mozilla/MappedDeclarationsBuilder.h"
9 #include "nsAttrValueInlines.h"
10 #include "mozilla/dom/BindingUtils.h"
11 #include "mozilla/dom/HTMLTableSectionElementBinding.h"
12 #include "nsContentUtils.h"
14 NS_IMPL_NS_NEW_HTML_ELEMENT(TableSection
)
16 namespace mozilla::dom
{
18 // you will see the phrases "rowgroup" and "section" used interchangably
20 HTMLTableSectionElement::~HTMLTableSectionElement() = default;
22 JSObject
* HTMLTableSectionElement::WrapNode(JSContext
* aCx
,
23 JS::Handle
<JSObject
*> aGivenProto
) {
24 return HTMLTableSectionElement_Binding::Wrap(aCx
, this, aGivenProto
);
27 NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLTableSectionElement
,
28 nsGenericHTMLElement
, mRows
)
30 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLTableSectionElement
,
33 NS_IMPL_ELEMENT_CLONE(HTMLTableSectionElement
)
35 nsIHTMLCollection
* HTMLTableSectionElement::Rows() {
37 mRows
= new nsContentList(this, mNodeInfo
->NamespaceID(), nsGkAtoms::tr
,
38 nsGkAtoms::tr
, false);
44 already_AddRefed
<nsGenericHTMLElement
> HTMLTableSectionElement::InsertRow(
45 int32_t aIndex
, ErrorResult
& aError
) {
47 aError
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
51 nsIHTMLCollection
* rows
= Rows();
53 uint32_t rowCount
= rows
->Length();
54 if (aIndex
> (int32_t)rowCount
) {
55 aError
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
59 bool doInsert
= (aIndex
< int32_t(rowCount
)) && (aIndex
!= -1);
62 RefPtr
<mozilla::dom::NodeInfo
> nodeInfo
;
63 nsContentUtils::QNameChanged(mNodeInfo
, nsGkAtoms::tr
,
64 getter_AddRefs(nodeInfo
));
66 RefPtr
<nsGenericHTMLElement
> rowContent
=
67 NS_NewHTMLTableRowElement(nodeInfo
.forget());
69 aError
.Throw(NS_ERROR_OUT_OF_MEMORY
);
74 nsCOMPtr
<nsINode
> refNode
= rows
->Item(aIndex
);
75 nsINode::InsertBefore(*rowContent
, refNode
, aError
);
77 nsINode::AppendChild(*rowContent
, aError
);
79 return rowContent
.forget();
82 void HTMLTableSectionElement::DeleteRow(int32_t aValue
, ErrorResult
& aError
) {
84 aError
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
88 nsIHTMLCollection
* rows
= Rows();
92 refIndex
= rows
->Length();
99 refIndex
= (uint32_t)aValue
;
102 nsCOMPtr
<nsINode
> row
= rows
->Item(refIndex
);
104 aError
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
108 nsINode::RemoveChild(*row
, aError
);
111 bool HTMLTableSectionElement::ParseAttribute(
112 int32_t aNamespaceID
, nsAtom
* aAttribute
, const nsAString
& aValue
,
113 nsIPrincipal
* aMaybeScriptedPrincipal
, nsAttrValue
& aResult
) {
114 if (aNamespaceID
== kNameSpaceID_None
) {
115 /* ignore these attributes, stored simply as strings
118 if (aAttribute
== nsGkAtoms::height
) {
119 // Per HTML spec there should be nothing special here, but all browsers
120 // implement height mapping to style. See
121 // <https://github.com/whatwg/html/issues/4718>. All browsers allow 0, so
123 return aResult
.ParseHTMLDimension(aValue
);
125 if (aAttribute
== nsGkAtoms::align
) {
126 return ParseTableCellHAlignValue(aValue
, aResult
);
128 if (aAttribute
== nsGkAtoms::bgcolor
) {
129 return aResult
.ParseColor(aValue
);
131 if (aAttribute
== nsGkAtoms::valign
) {
132 return ParseTableVAlignValue(aValue
, aResult
);
136 return nsGenericHTMLElement::ParseBackgroundAttribute(
137 aNamespaceID
, aAttribute
, aValue
, aResult
) ||
138 nsGenericHTMLElement::ParseAttribute(aNamespaceID
, aAttribute
, aValue
,
139 aMaybeScriptedPrincipal
, aResult
);
142 void HTMLTableSectionElement::MapAttributesIntoRule(
143 MappedDeclarationsBuilder
& aBuilder
) {
145 if (!aBuilder
.PropertyIsSet(eCSSProperty_height
)) {
146 const nsAttrValue
* value
= aBuilder
.GetAttr(nsGkAtoms::height
);
147 if (value
&& value
->Type() == nsAttrValue::eInteger
) {
148 aBuilder
.SetPixelValue(eCSSProperty_height
,
149 (float)value
->GetIntegerValue());
152 nsGenericHTMLElement::MapDivAlignAttributeInto(aBuilder
);
153 nsGenericHTMLElement::MapVAlignAttributeInto(aBuilder
);
154 nsGenericHTMLElement::MapBackgroundAttributesInto(aBuilder
);
155 nsGenericHTMLElement::MapCommonAttributesInto(aBuilder
);
159 HTMLTableSectionElement::IsAttributeMapped(const nsAtom
* aAttribute
) const {
160 static const MappedAttributeEntry attributes
[] = {
161 {nsGkAtoms::align
}, {nsGkAtoms::valign
}, {nsGkAtoms::height
}, {nullptr}};
163 static const MappedAttributeEntry
* const map
[] = {
166 sBackgroundAttributeMap
,
169 return FindAttributeDependence(aAttribute
, map
);
172 nsMapRuleToAttributesFunc
HTMLTableSectionElement::GetAttributeMappingFunction()
174 return &MapAttributesIntoRule
;
177 } // namespace mozilla::dom