HTMLTableColElement.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/HTMLTableColElement.h"
  6. #include "nsMappedAttributes.h"
  7. #include "nsAttrValueInlines.h"
  8. #include "nsRuleData.h"
  9. #include "mozilla/dom/HTMLTableColElementBinding.h"
  10. NS_IMPL_NS_NEW_HTML_ELEMENT(TableCol)
  11. namespace mozilla {
  12. namespace dom {
  13. // use the same protection as ancient code did
  14. // http://lxr.mozilla.org/classic/source/lib/layout/laytable.c#46
  15. #define MAX_COLSPAN 1000
  16. HTMLTableColElement::~HTMLTableColElement()
  17. {
  18. }
  19. JSObject*
  20. HTMLTableColElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  21. {
  22. return HTMLTableColElementBinding::Wrap(aCx, this, aGivenProto);
  23. }
  24. NS_IMPL_ELEMENT_CLONE(HTMLTableColElement)
  25. bool
  26. HTMLTableColElement::ParseAttribute(int32_t aNamespaceID,
  27. nsIAtom* aAttribute,
  28. const nsAString& aValue,
  29. nsAttrValue& aResult)
  30. {
  31. if (aNamespaceID == kNameSpaceID_None) {
  32. /* ignore these attributes, stored simply as strings ch */
  33. if (aAttribute == nsGkAtoms::charoff) {
  34. return aResult.ParseSpecialIntValue(aValue);
  35. }
  36. if (aAttribute == nsGkAtoms::span) {
  37. /* protection from unrealistic large colspan values */
  38. aResult.ParseIntWithFallback(aValue, 1, MAX_COLSPAN);
  39. return true;
  40. }
  41. if (aAttribute == nsGkAtoms::width) {
  42. return aResult.ParseSpecialIntValue(aValue);
  43. }
  44. if (aAttribute == nsGkAtoms::align) {
  45. return ParseTableCellHAlignValue(aValue, aResult);
  46. }
  47. if (aAttribute == nsGkAtoms::valign) {
  48. return ParseTableVAlignValue(aValue, aResult);
  49. }
  50. }
  51. return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
  52. aResult);
  53. }
  54. void
  55. HTMLTableColElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
  56. nsRuleData* aData)
  57. {
  58. if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Table)) {
  59. nsCSSValue *span = aData->ValueForSpan();
  60. if (span->GetUnit() == eCSSUnit_Null) {
  61. // span: int
  62. const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::span);
  63. if (value && value->Type() == nsAttrValue::eInteger) {
  64. int32_t val = value->GetIntegerValue();
  65. // Note: Do NOT use this code for table cells! The value "0"
  66. // means something special for colspan and rowspan, but for <col
  67. // span> and <colgroup span> it's just disallowed.
  68. if (val > 0) {
  69. span->SetIntValue(value->GetIntegerValue(), eCSSUnit_Integer);
  70. }
  71. }
  72. }
  73. }
  74. if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Position)) {
  75. nsCSSValue* width = aData->ValueForWidth();
  76. if (width->GetUnit() == eCSSUnit_Null) {
  77. // width
  78. const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::width);
  79. if (value) {
  80. switch (value->Type()) {
  81. case nsAttrValue::ePercent: {
  82. width->SetPercentValue(value->GetPercentValue());
  83. break;
  84. }
  85. case nsAttrValue::eInteger: {
  86. width->SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);
  87. break;
  88. }
  89. default:
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Text)) {
  96. nsCSSValue* textAlign = aData->ValueForTextAlign();
  97. if (textAlign->GetUnit() == eCSSUnit_Null) {
  98. // align: enum
  99. const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::align);
  100. if (value && value->Type() == nsAttrValue::eEnum)
  101. textAlign->SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
  102. }
  103. }
  104. if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Display)) {
  105. nsCSSValue* verticalAlign = aData->ValueForVerticalAlign();
  106. if (verticalAlign->GetUnit() == eCSSUnit_Null) {
  107. // valign: enum
  108. const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::valign);
  109. if (value && value->Type() == nsAttrValue::eEnum)
  110. verticalAlign->SetIntValue(value->GetEnumValue(), eCSSUnit_Enumerated);
  111. }
  112. }
  113. nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
  114. }
  115. NS_IMETHODIMP_(bool)
  116. HTMLTableColElement::IsAttributeMapped(const nsIAtom* aAttribute) const
  117. {
  118. static const MappedAttributeEntry attributes[] = {
  119. { &nsGkAtoms::width },
  120. { &nsGkAtoms::align },
  121. { &nsGkAtoms::valign },
  122. { &nsGkAtoms::span },
  123. { nullptr }
  124. };
  125. static const MappedAttributeEntry* const map[] = {
  126. attributes,
  127. sCommonAttributeMap,
  128. };
  129. return FindAttributeDependence(aAttribute, map);
  130. }
  131. nsMapRuleToAttributesFunc
  132. HTMLTableColElement::GetAttributeMappingFunction() const
  133. {
  134. return &MapAttributesIntoRule;
  135. }
  136. } // namespace dom
  137. } // namespace mozilla