HTMLTableSectionElement.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/HTMLTableSectionElement.h"
  6. #include "nsMappedAttributes.h"
  7. #include "nsAttrValueInlines.h"
  8. #include "nsRuleData.h"
  9. #include "mozilla/dom/BindingUtils.h"
  10. #include "mozilla/dom/HTMLTableSectionElementBinding.h"
  11. #include "nsContentUtils.h"
  12. NS_IMPL_NS_NEW_HTML_ELEMENT(TableSection)
  13. namespace mozilla {
  14. namespace dom {
  15. // you will see the phrases "rowgroup" and "section" used interchangably
  16. HTMLTableSectionElement::~HTMLTableSectionElement()
  17. {
  18. }
  19. JSObject*
  20. HTMLTableSectionElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  21. {
  22. return HTMLTableSectionElementBinding::Wrap(aCx, this, aGivenProto);
  23. }
  24. NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLTableSectionElement)
  25. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLTableSectionElement,
  26. nsGenericHTMLElement)
  27. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRows)
  28. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  29. NS_IMPL_ADDREF_INHERITED(HTMLTableSectionElement, Element)
  30. NS_IMPL_RELEASE_INHERITED(HTMLTableSectionElement, Element)
  31. // QueryInterface implementation for HTMLTableSectionElement
  32. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(HTMLTableSectionElement)
  33. NS_INTERFACE_MAP_END_INHERITING(nsGenericHTMLElement)
  34. NS_IMPL_ELEMENT_CLONE(HTMLTableSectionElement)
  35. nsIHTMLCollection*
  36. HTMLTableSectionElement::Rows()
  37. {
  38. if (!mRows) {
  39. mRows = new nsContentList(this,
  40. mNodeInfo->NamespaceID(),
  41. nsGkAtoms::tr,
  42. nsGkAtoms::tr,
  43. false);
  44. }
  45. return mRows;
  46. }
  47. already_AddRefed<nsGenericHTMLElement>
  48. HTMLTableSectionElement::InsertRow(int32_t aIndex, ErrorResult& aError)
  49. {
  50. if (aIndex < -1) {
  51. aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
  52. return nullptr;
  53. }
  54. nsIHTMLCollection* rows = Rows();
  55. uint32_t rowCount = rows->Length();
  56. if (aIndex > (int32_t)rowCount) {
  57. aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
  58. return nullptr;
  59. }
  60. bool doInsert = (aIndex < int32_t(rowCount)) && (aIndex != -1);
  61. // create the row
  62. RefPtr<mozilla::dom::NodeInfo> nodeInfo;
  63. nsContentUtils::NameChanged(mNodeInfo, nsGkAtoms::tr,
  64. getter_AddRefs(nodeInfo));
  65. RefPtr<nsGenericHTMLElement> rowContent =
  66. NS_NewHTMLTableRowElement(nodeInfo.forget());
  67. if (!rowContent) {
  68. aError.Throw(NS_ERROR_OUT_OF_MEMORY);
  69. return nullptr;
  70. }
  71. if (doInsert) {
  72. nsCOMPtr<nsINode> refNode = rows->Item(aIndex);
  73. nsINode::InsertBefore(*rowContent, refNode, aError);
  74. } else {
  75. nsINode::AppendChild(*rowContent, aError);
  76. }
  77. return rowContent.forget();
  78. }
  79. void
  80. HTMLTableSectionElement::DeleteRow(int32_t aValue, ErrorResult& aError)
  81. {
  82. if (aValue < -1) {
  83. aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
  84. return;
  85. }
  86. nsIHTMLCollection* rows = Rows();
  87. uint32_t refIndex;
  88. if (aValue == -1) {
  89. refIndex = rows->Length();
  90. if (refIndex == 0) {
  91. return;
  92. }
  93. --refIndex;
  94. }
  95. else {
  96. refIndex = (uint32_t)aValue;
  97. }
  98. nsCOMPtr<nsINode> row = rows->Item(refIndex);
  99. if (!row) {
  100. aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
  101. return;
  102. }
  103. nsINode::RemoveChild(*row, aError);
  104. }
  105. bool
  106. HTMLTableSectionElement::ParseAttribute(int32_t aNamespaceID,
  107. nsIAtom* aAttribute,
  108. const nsAString& aValue,
  109. nsAttrValue& aResult)
  110. {
  111. if (aNamespaceID == kNameSpaceID_None) {
  112. /* ignore these attributes, stored simply as strings
  113. ch
  114. */
  115. if (aAttribute == nsGkAtoms::charoff) {
  116. return aResult.ParseIntWithBounds(aValue, 0);
  117. }
  118. if (aAttribute == nsGkAtoms::height) {
  119. return aResult.ParseSpecialIntValue(aValue);
  120. }
  121. if (aAttribute == nsGkAtoms::align) {
  122. return ParseTableCellHAlignValue(aValue, aResult);
  123. }
  124. if (aAttribute == nsGkAtoms::bgcolor) {
  125. return aResult.ParseColor(aValue);
  126. }
  127. if (aAttribute == nsGkAtoms::valign) {
  128. return ParseTableVAlignValue(aValue, aResult);
  129. }
  130. }
  131. return nsGenericHTMLElement::ParseBackgroundAttribute(aNamespaceID,
  132. aAttribute, aValue,
  133. aResult) ||
  134. nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
  135. aResult);
  136. }
  137. void
  138. HTMLTableSectionElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
  139. nsRuleData* aData)
  140. {
  141. if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Position)) {
  142. // height: value
  143. nsCSSValue* height = aData->ValueForHeight();
  144. if (height->GetUnit() == eCSSUnit_Null) {
  145. const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::height);
  146. if (value && value->Type() == nsAttrValue::eInteger)
  147. height->SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);
  148. }
  149. }
  150. if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Text)) {
  151. nsCSSValue* textAlign = aData->ValueForTextAlign();
  152. if (textAlign->GetUnit() == eCSSUnit_Null) {
  153. // align: enum
  154. const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::align);
  155. if (value && value->Type() == nsAttrValue::eEnum)
  156. textAlign->SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
  157. }
  158. }
  159. if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Display)) {
  160. nsCSSValue* verticalAlign = aData->ValueForVerticalAlign();
  161. if (verticalAlign->GetUnit() == eCSSUnit_Null) {
  162. // valign: enum
  163. const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::valign);
  164. if (value && value->Type() == nsAttrValue::eEnum)
  165. verticalAlign->SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
  166. }
  167. }
  168. nsGenericHTMLElement::MapBackgroundAttributesInto(aAttributes, aData);
  169. nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
  170. }
  171. NS_IMETHODIMP_(bool)
  172. HTMLTableSectionElement::IsAttributeMapped(const nsIAtom* aAttribute) const
  173. {
  174. static const MappedAttributeEntry attributes[] = {
  175. { &nsGkAtoms::align },
  176. { &nsGkAtoms::valign },
  177. { &nsGkAtoms::height },
  178. { nullptr }
  179. };
  180. static const MappedAttributeEntry* const map[] = {
  181. attributes,
  182. sCommonAttributeMap,
  183. sBackgroundAttributeMap,
  184. };
  185. return FindAttributeDependence(aAttribute, map);
  186. }
  187. nsMapRuleToAttributesFunc
  188. HTMLTableSectionElement::GetAttributeMappingFunction() const
  189. {
  190. return &MapAttributesIntoRule;
  191. }
  192. } // namespace dom
  193. } // namespace mozilla