Bug 1841281 - Disable test_basics.html on mac debug and windows for frequent failures...
[gecko.git] / dom / html / HTMLTableCellElement.cpp
blob4c2e33cb3ac31f8084c4ad74d393256c073ccb0c
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/HTMLTableCellElement.h"
8 #include "mozilla/dom/Document.h"
9 #include "mozilla/dom/HTMLTableElement.h"
10 #include "mozilla/dom/HTMLTableRowElement.h"
11 #include "mozilla/MappedDeclarationsBuilder.h"
12 #include "nsAttrValueInlines.h"
13 #include "celldata.h"
14 #include "mozilla/dom/HTMLTableCellElementBinding.h"
16 namespace {
17 enum class StyleCellScope : uint8_t { Row, Col, Rowgroup, Colgroup };
18 } // namespace
20 NS_IMPL_NS_NEW_HTML_ELEMENT(TableCell)
22 namespace mozilla::dom {
24 HTMLTableCellElement::~HTMLTableCellElement() = default;
26 JSObject* HTMLTableCellElement::WrapNode(JSContext* aCx,
27 JS::Handle<JSObject*> aGivenProto) {
28 return HTMLTableCellElement_Binding::Wrap(aCx, this, aGivenProto);
31 NS_IMPL_ELEMENT_CLONE(HTMLTableCellElement)
33 // protected method
34 HTMLTableRowElement* HTMLTableCellElement::GetRow() const {
35 return HTMLTableRowElement::FromNodeOrNull(GetParent());
38 // protected method
39 HTMLTableElement* HTMLTableCellElement::GetTable() const {
40 nsIContent* parent = GetParent();
41 if (!parent) {
42 return nullptr;
45 // parent should be a row.
46 nsIContent* section = parent->GetParent();
47 if (!section) {
48 return nullptr;
51 if (section->IsHTMLElement(nsGkAtoms::table)) {
52 // XHTML, without a row group.
53 return static_cast<HTMLTableElement*>(section);
56 // We have a row group.
57 nsIContent* result = section->GetParent();
58 if (result && result->IsHTMLElement(nsGkAtoms::table)) {
59 return static_cast<HTMLTableElement*>(result);
62 return nullptr;
65 int32_t HTMLTableCellElement::CellIndex() const {
66 HTMLTableRowElement* row = GetRow();
67 if (!row) {
68 return -1;
71 nsIHTMLCollection* cells = row->Cells();
72 if (!cells) {
73 return -1;
76 uint32_t numCells = cells->Length();
77 for (uint32_t i = 0; i < numCells; i++) {
78 if (cells->Item(i) == this) {
79 return i;
83 return -1;
86 StyleLockedDeclarationBlock*
87 HTMLTableCellElement::GetMappedAttributesInheritedFromTable() const {
88 if (HTMLTableElement* table = GetTable()) {
89 return table->GetAttributesMappedForCell();
91 return nullptr;
94 void HTMLTableCellElement::GetAlign(DOMString& aValue) {
95 if (!GetAttr(nsGkAtoms::align, aValue)) {
96 // There's no align attribute, ask the row for the alignment.
97 HTMLTableRowElement* row = GetRow();
98 if (row) {
99 row->GetAlign(aValue);
104 static const nsAttrValue::EnumTable kCellScopeTable[] = {
105 {"row", StyleCellScope::Row},
106 {"col", StyleCellScope::Col},
107 {"rowgroup", StyleCellScope::Rowgroup},
108 {"colgroup", StyleCellScope::Colgroup},
109 {nullptr, 0}};
111 void HTMLTableCellElement::GetScope(DOMString& aScope) {
112 GetEnumAttr(nsGkAtoms::scope, nullptr, aScope);
115 bool HTMLTableCellElement::ParseAttribute(int32_t aNamespaceID,
116 nsAtom* aAttribute,
117 const nsAString& aValue,
118 nsIPrincipal* aMaybeScriptedPrincipal,
119 nsAttrValue& aResult) {
120 if (aNamespaceID == kNameSpaceID_None) {
121 /* ignore these attributes, stored simply as strings
122 abbr, axis, ch, headers
124 if (aAttribute == nsGkAtoms::colspan) {
125 aResult.ParseClampedNonNegativeInt(aValue, 1, 1, MAX_COLSPAN);
126 return true;
128 if (aAttribute == nsGkAtoms::rowspan) {
129 aResult.ParseClampedNonNegativeInt(aValue, 1, 0, MAX_ROWSPAN);
130 // quirks mode does not honor the special html 4 value of 0
131 if (aResult.GetIntegerValue() == 0 && InNavQuirksMode(OwnerDoc())) {
132 aResult.SetTo(1, &aValue);
134 return true;
136 if (aAttribute == nsGkAtoms::height) {
137 return aResult.ParseNonzeroHTMLDimension(aValue);
139 if (aAttribute == nsGkAtoms::width) {
140 return aResult.ParseNonzeroHTMLDimension(aValue);
142 if (aAttribute == nsGkAtoms::align) {
143 return ParseTableCellHAlignValue(aValue, aResult);
145 if (aAttribute == nsGkAtoms::bgcolor) {
146 return aResult.ParseColor(aValue);
148 if (aAttribute == nsGkAtoms::scope) {
149 return aResult.ParseEnumValue(aValue, kCellScopeTable, false);
151 if (aAttribute == nsGkAtoms::valign) {
152 return ParseTableVAlignValue(aValue, aResult);
156 return nsGenericHTMLElement::ParseBackgroundAttribute(
157 aNamespaceID, aAttribute, aValue, aResult) ||
158 nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
159 aMaybeScriptedPrincipal, aResult);
162 void HTMLTableCellElement::MapAttributesIntoRule(
163 MappedDeclarationsBuilder& aBuilder) {
164 MapImageSizeAttributesInto(aBuilder);
166 if (!aBuilder.PropertyIsSet(eCSSProperty_white_space)) {
167 // nowrap: enum
168 if (aBuilder.GetAttr(nsGkAtoms::nowrap)) {
169 // See if our width is not a nonzero integer width.
170 const nsAttrValue* value = aBuilder.GetAttr(nsGkAtoms::width);
171 nsCompatibility mode = aBuilder.Document().GetCompatibilityMode();
172 if (!value || value->Type() != nsAttrValue::eInteger ||
173 value->GetIntegerValue() == 0 || eCompatibility_NavQuirks != mode) {
174 aBuilder.SetKeywordValue(eCSSProperty_white_space,
175 StyleWhiteSpace::Nowrap);
180 nsGenericHTMLElement::MapDivAlignAttributeInto(aBuilder);
181 nsGenericHTMLElement::MapVAlignAttributeInto(aBuilder);
182 nsGenericHTMLElement::MapBackgroundAttributesInto(aBuilder);
183 nsGenericHTMLElement::MapCommonAttributesInto(aBuilder);
186 NS_IMETHODIMP_(bool)
187 HTMLTableCellElement::IsAttributeMapped(const nsAtom* aAttribute) const {
188 static const MappedAttributeEntry attributes[] = {
189 {nsGkAtoms::align},
190 {nsGkAtoms::valign},
191 {nsGkAtoms::nowrap},
192 #if 0
193 // XXXldb If these are implemented, they might need to move to
194 // GetAttributeChangeHint (depending on how, and preferably not).
195 { nsGkAtoms::abbr },
196 { nsGkAtoms::axis },
197 { nsGkAtoms::headers },
198 { nsGkAtoms::scope },
199 #endif
200 {nsGkAtoms::width},
201 {nsGkAtoms::height},
202 {nullptr}
205 static const MappedAttributeEntry* const map[] = {
206 attributes,
207 sCommonAttributeMap,
208 sBackgroundAttributeMap,
211 return FindAttributeDependence(aAttribute, map);
214 nsMapRuleToAttributesFunc HTMLTableCellElement::GetAttributeMappingFunction()
215 const {
216 return &MapAttributesIntoRule;
219 } // namespace mozilla::dom