Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / html / HTMLTableSectionElement.cpp
blob358657b8d4585c3be50ba20e86cc2981a530f24a
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,
31 nsGenericHTMLElement)
33 NS_IMPL_ELEMENT_CLONE(HTMLTableSectionElement)
35 nsIHTMLCollection* HTMLTableSectionElement::Rows() {
36 if (!mRows) {
37 mRows = new nsContentList(this, mNodeInfo->NamespaceID(), nsGkAtoms::tr,
38 nsGkAtoms::tr, false);
41 return mRows;
44 already_AddRefed<nsGenericHTMLElement> HTMLTableSectionElement::InsertRow(
45 int32_t aIndex, ErrorResult& aError) {
46 if (aIndex < -1) {
47 aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
48 return nullptr;
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);
56 return nullptr;
59 bool doInsert = (aIndex < int32_t(rowCount)) && (aIndex != -1);
61 // create the row
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());
68 if (!rowContent) {
69 aError.Throw(NS_ERROR_OUT_OF_MEMORY);
70 return nullptr;
73 if (doInsert) {
74 nsCOMPtr<nsINode> refNode = rows->Item(aIndex);
75 nsINode::InsertBefore(*rowContent, refNode, aError);
76 } else {
77 nsINode::AppendChild(*rowContent, aError);
79 return rowContent.forget();
82 void HTMLTableSectionElement::DeleteRow(int32_t aValue, ErrorResult& aError) {
83 if (aValue < -1) {
84 aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
85 return;
88 nsIHTMLCollection* rows = Rows();
90 uint32_t refIndex;
91 if (aValue == -1) {
92 refIndex = rows->Length();
93 if (refIndex == 0) {
94 return;
97 --refIndex;
98 } else {
99 refIndex = (uint32_t)aValue;
102 nsCOMPtr<nsINode> row = rows->Item(refIndex);
103 if (!row) {
104 aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
105 return;
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
122 // keep doing that.
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) {
144 // height: value
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);
158 NS_IMETHODIMP_(bool)
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[] = {
164 attributes,
165 sCommonAttributeMap,
166 sBackgroundAttributeMap,
169 return FindAttributeDependence(aAttribute, map);
172 nsMapRuleToAttributesFunc HTMLTableSectionElement::GetAttributeMappingFunction()
173 const {
174 return &MapAttributesIntoRule;
177 } // namespace mozilla::dom