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/HTMLTableRowElement.h"
8 #include "mozilla/dom/HTMLTableElement.h"
9 #include "mozilla/MappedDeclarations.h"
10 #include "nsMappedAttributes.h"
11 #include "nsAttrValueInlines.h"
12 #include "mozilla/dom/BindingUtils.h"
13 #include "mozilla/dom/HTMLTableRowElementBinding.h"
14 #include "nsContentList.h"
15 #include "nsContentUtils.h"
17 NS_IMPL_NS_NEW_HTML_ELEMENT(TableRow
)
19 namespace mozilla::dom
{
21 HTMLTableRowElement::~HTMLTableRowElement() = default;
23 JSObject
* HTMLTableRowElement::WrapNode(JSContext
* aCx
,
24 JS::Handle
<JSObject
*> aGivenProto
) {
25 return HTMLTableRowElement_Binding::Wrap(aCx
, this, aGivenProto
);
28 NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLTableRowElement
, nsGenericHTMLElement
,
31 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLTableRowElement
,
34 NS_IMPL_ELEMENT_CLONE(HTMLTableRowElement
)
37 HTMLTableSectionElement
* HTMLTableRowElement::GetSection() const {
38 nsIContent
* parent
= GetParent();
39 if (parent
&& parent
->IsAnyOfHTMLElements(nsGkAtoms::thead
, nsGkAtoms::tbody
,
41 return static_cast<HTMLTableSectionElement
*>(parent
);
47 HTMLTableElement
* HTMLTableRowElement::GetTable() const {
48 nsIContent
* parent
= GetParent();
53 // We may not be in a section
54 HTMLTableElement
* table
= HTMLTableElement::FromNode(parent
);
59 return HTMLTableElement::FromNodeOrNull(parent
->GetParent());
62 int32_t HTMLTableRowElement::RowIndex() const {
63 HTMLTableElement
* table
= GetTable();
68 nsIHTMLCollection
* rows
= table
->Rows();
70 uint32_t numRows
= rows
->Length();
72 for (uint32_t i
= 0; i
< numRows
; i
++) {
73 if (rows
->GetElementAt(i
) == this) {
81 int32_t HTMLTableRowElement::SectionRowIndex() const {
82 HTMLTableSectionElement
* section
= GetSection();
87 nsCOMPtr
<nsIHTMLCollection
> coll
= section
->Rows();
88 uint32_t numRows
= coll
->Length();
89 for (uint32_t i
= 0; i
< numRows
; i
++) {
90 if (coll
->GetElementAt(i
) == this) {
98 static bool IsCell(Element
* aElement
, int32_t aNamespaceID
, nsAtom
* aAtom
,
100 return aElement
->IsAnyOfHTMLElements(nsGkAtoms::td
, nsGkAtoms::th
);
103 nsIHTMLCollection
* HTMLTableRowElement::Cells() {
105 mCells
= new nsContentList(this, IsCell
,
106 nullptr, // destroy func
107 nullptr, // closure data
108 false, nullptr, kNameSpaceID_XHTML
, false);
114 already_AddRefed
<nsGenericHTMLElement
> HTMLTableRowElement::InsertCell(
115 int32_t aIndex
, ErrorResult
& aError
) {
117 aError
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
121 // Make sure mCells is initialized.
122 nsIHTMLCollection
* cells
= Cells();
124 NS_ASSERTION(mCells
, "How did that happen?");
126 nsCOMPtr
<nsINode
> nextSibling
;
127 // -1 means append, so should use null nextSibling
129 nextSibling
= cells
->Item(aIndex
);
130 // Check whether we're inserting past end of list. We want to avoid doing
131 // this unless we really have to, since this has to walk all our kids. If
132 // we have a nextSibling, we're clearly not past end of list.
134 uint32_t cellCount
= cells
->Length();
135 if (aIndex
> int32_t(cellCount
)) {
136 aError
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
143 RefPtr
<mozilla::dom::NodeInfo
> nodeInfo
;
144 nsContentUtils::QNameChanged(mNodeInfo
, nsGkAtoms::td
,
145 getter_AddRefs(nodeInfo
));
147 RefPtr
<nsGenericHTMLElement
> cell
=
148 NS_NewHTMLTableCellElement(nodeInfo
.forget());
150 aError
.Throw(NS_ERROR_OUT_OF_MEMORY
);
154 nsINode::InsertBefore(*cell
, nextSibling
, aError
);
156 return cell
.forget();
159 void HTMLTableRowElement::DeleteCell(int32_t aValue
, ErrorResult
& aError
) {
161 aError
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
165 nsIHTMLCollection
* cells
= Cells();
169 refIndex
= cells
->Length();
176 refIndex
= (uint32_t)aValue
;
179 nsCOMPtr
<nsINode
> cell
= cells
->Item(refIndex
);
181 aError
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
185 nsINode::RemoveChild(*cell
, aError
);
188 bool HTMLTableRowElement::ParseAttribute(int32_t aNamespaceID
,
190 const nsAString
& aValue
,
191 nsIPrincipal
* aMaybeScriptedPrincipal
,
192 nsAttrValue
& aResult
) {
194 * ignore these attributes, stored simply as strings
199 if (aNamespaceID
== kNameSpaceID_None
) {
200 if (aAttribute
== nsGkAtoms::height
) {
201 // Per spec should be ParseNonzeroHTMLDimension, but no browsers do that.
202 // See https://github.com/whatwg/html/issues/4716
203 return aResult
.ParseHTMLDimension(aValue
);
205 if (aAttribute
== nsGkAtoms::align
) {
206 return ParseTableCellHAlignValue(aValue
, aResult
);
208 if (aAttribute
== nsGkAtoms::bgcolor
) {
209 return aResult
.ParseColor(aValue
);
211 if (aAttribute
== nsGkAtoms::valign
) {
212 return ParseTableVAlignValue(aValue
, aResult
);
216 return nsGenericHTMLElement::ParseBackgroundAttribute(
217 aNamespaceID
, aAttribute
, aValue
, aResult
) ||
218 nsGenericHTMLElement::ParseAttribute(aNamespaceID
, aAttribute
, aValue
,
219 aMaybeScriptedPrincipal
, aResult
);
222 void HTMLTableRowElement::MapAttributesIntoRule(
223 const nsMappedAttributes
* aAttributes
, MappedDeclarations
& aDecls
) {
224 nsGenericHTMLElement::MapHeightAttributeInto(aAttributes
, aDecls
);
225 nsGenericHTMLElement::MapDivAlignAttributeInto(aAttributes
, aDecls
);
226 nsGenericHTMLElement::MapVAlignAttributeInto(aAttributes
, aDecls
);
227 nsGenericHTMLElement::MapBackgroundAttributesInto(aAttributes
, aDecls
);
228 nsGenericHTMLElement::MapCommonAttributesInto(aAttributes
, aDecls
);
232 HTMLTableRowElement::IsAttributeMapped(const nsAtom
* aAttribute
) const {
233 static const MappedAttributeEntry attributes
[] = {
234 {nsGkAtoms::align
}, {nsGkAtoms::valign
}, {nsGkAtoms::height
}, {nullptr}};
236 static const MappedAttributeEntry
* const map
[] = {
239 sBackgroundAttributeMap
,
242 return FindAttributeDependence(aAttribute
, map
);
245 nsMapRuleToAttributesFunc
HTMLTableRowElement::GetAttributeMappingFunction()
247 return &MapAttributesIntoRule
;
250 } // namespace mozilla::dom