HTMLTableElement.h (7094B)
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/. */ 6 #ifndef mozilla_dom_HTMLTableElement_h 7 #define mozilla_dom_HTMLTableElement_h 8 9 #include "mozilla/dom/HTMLTableCaptionElement.h" 10 #include "mozilla/dom/HTMLTableSectionElement.h" 11 #include "nsGenericHTMLElement.h" 12 13 namespace mozilla::dom { 14 15 class TableRowsCollection; 16 17 class HTMLTableElement final : public nsGenericHTMLElement { 18 public: 19 explicit HTMLTableElement( 20 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo); 21 22 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLTableElement, table) 23 24 // nsISupports 25 NS_DECL_ISUPPORTS_INHERITED 26 27 HTMLTableCaptionElement* GetCaption() const { 28 return static_cast<HTMLTableCaptionElement*>(GetChild(nsGkAtoms::caption)); 29 } 30 void SetCaption(HTMLTableCaptionElement* aCaption, ErrorResult& aError) { 31 DeleteCaption(); 32 if (aCaption) { 33 nsCOMPtr<nsINode> firstChild = nsINode::GetFirstChild(); 34 nsINode::InsertBefore(*aCaption, firstChild, aError); 35 } 36 } 37 38 void DeleteTFoot(); 39 40 already_AddRefed<nsGenericHTMLElement> CreateCaption(); 41 42 void DeleteCaption(); 43 44 HTMLTableSectionElement* GetTHead() const { 45 return static_cast<HTMLTableSectionElement*>(GetChild(nsGkAtoms::thead)); 46 } 47 void SetTHead(HTMLTableSectionElement* aTHead, ErrorResult& aError) { 48 if (aTHead && !aTHead->IsHTMLElement(nsGkAtoms::thead)) { 49 aError.ThrowHierarchyRequestError("New value must be a thead element."); 50 return; 51 } 52 53 DeleteTHead(); 54 if (aTHead) { 55 nsCOMPtr<nsIContent> refNode = nullptr; 56 for (refNode = nsINode::GetFirstChild(); refNode; 57 refNode = refNode->GetNextSibling()) { 58 if (refNode->IsHTMLElement() && 59 !refNode->IsHTMLElement(nsGkAtoms::caption) && 60 !refNode->IsHTMLElement(nsGkAtoms::colgroup)) { 61 break; 62 } 63 } 64 65 nsINode::InsertBefore(*aTHead, refNode, aError); 66 } 67 } 68 already_AddRefed<nsGenericHTMLElement> CreateTHead(); 69 70 void DeleteTHead(); 71 72 HTMLTableSectionElement* GetTFoot() const { 73 return static_cast<HTMLTableSectionElement*>(GetChild(nsGkAtoms::tfoot)); 74 } 75 void SetTFoot(HTMLTableSectionElement* aTFoot, ErrorResult& aError) { 76 if (aTFoot && !aTFoot->IsHTMLElement(nsGkAtoms::tfoot)) { 77 aError.ThrowHierarchyRequestError("New value must be a tfoot element."); 78 return; 79 } 80 81 DeleteTFoot(); 82 if (aTFoot) { 83 nsINode::AppendChild(*aTFoot, aError); 84 } 85 } 86 already_AddRefed<nsGenericHTMLElement> CreateTFoot(); 87 88 nsIHTMLCollection* TBodies(); 89 90 already_AddRefed<nsGenericHTMLElement> CreateTBody(); 91 92 nsIHTMLCollection* Rows(); 93 94 already_AddRefed<nsGenericHTMLElement> InsertRow(int32_t aIndex, 95 ErrorResult& aError); 96 void DeleteRow(int32_t aIndex, ErrorResult& aError); 97 98 void GetAlign(DOMString& aAlign) { GetHTMLAttr(nsGkAtoms::align, aAlign); } 99 void SetAlign(const nsAString& aAlign, ErrorResult& aError) { 100 SetHTMLAttr(nsGkAtoms::align, aAlign, aError); 101 } 102 void GetBorder(DOMString& aBorder) { 103 GetHTMLAttr(nsGkAtoms::border, aBorder); 104 } 105 void SetBorder(const nsAString& aBorder, ErrorResult& aError) { 106 SetHTMLAttr(nsGkAtoms::border, aBorder, aError); 107 } 108 void GetFrame(DOMString& aFrame) { GetHTMLAttr(nsGkAtoms::frame, aFrame); } 109 void SetFrame(const nsAString& aFrame, ErrorResult& aError) { 110 SetHTMLAttr(nsGkAtoms::frame, aFrame, aError); 111 } 112 void GetRules(DOMString& aRules) { GetHTMLAttr(nsGkAtoms::rules, aRules); } 113 void SetRules(const nsAString& aRules, ErrorResult& aError) { 114 SetHTMLAttr(nsGkAtoms::rules, aRules, aError); 115 } 116 void GetSummary(nsString& aSummary) { 117 GetHTMLAttr(nsGkAtoms::summary, aSummary); 118 } 119 void GetSummary(DOMString& aSummary) { 120 GetHTMLAttr(nsGkAtoms::summary, aSummary); 121 } 122 void SetSummary(const nsAString& aSummary, ErrorResult& aError) { 123 SetHTMLAttr(nsGkAtoms::summary, aSummary, aError); 124 } 125 void GetWidth(DOMString& aWidth) { GetHTMLAttr(nsGkAtoms::width, aWidth); } 126 void SetWidth(const nsAString& aWidth, ErrorResult& aError) { 127 SetHTMLAttr(nsGkAtoms::width, aWidth, aError); 128 } 129 void GetBgColor(DOMString& aBgColor) { 130 GetHTMLAttr(nsGkAtoms::bgcolor, aBgColor); 131 } 132 void SetBgColor(const nsAString& aBgColor, ErrorResult& aError) { 133 SetHTMLAttr(nsGkAtoms::bgcolor, aBgColor, aError); 134 } 135 void GetCellPadding(DOMString& aCellPadding) { 136 GetHTMLAttr(nsGkAtoms::cellpadding, aCellPadding); 137 } 138 void SetCellPadding(const nsAString& aCellPadding, ErrorResult& aError) { 139 SetHTMLAttr(nsGkAtoms::cellpadding, aCellPadding, aError); 140 } 141 void GetCellSpacing(DOMString& aCellSpacing) { 142 GetHTMLAttr(nsGkAtoms::cellspacing, aCellSpacing); 143 } 144 void SetCellSpacing(const nsAString& aCellSpacing, ErrorResult& aError) { 145 SetHTMLAttr(nsGkAtoms::cellspacing, aCellSpacing, aError); 146 } 147 148 bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute, 149 const nsAString& aValue, 150 nsIPrincipal* aMaybeScriptedPrincipal, 151 nsAttrValue& aResult) override; 152 nsMapRuleToAttributesFunc GetAttributeMappingFunction() const override; 153 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override; 154 155 nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override; 156 157 nsresult BindToTree(BindContext&, nsINode& aParent) override; 158 void UnbindFromTree(UnbindContext&) override; 159 /** 160 * Called when an attribute is about to be changed 161 */ 162 void BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName, 163 const nsAttrValue* aValue, bool aNotify) override; 164 /** 165 * Called when an attribute has just been changed 166 */ 167 void AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, 168 const nsAttrValue* aValue, const nsAttrValue* aOldValue, 169 nsIPrincipal* aSubjectPrincipal, bool aNotify) override; 170 171 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLTableElement, 172 nsGenericHTMLElement) 173 StyleLockedDeclarationBlock* GetAttributesMappedForCell() const { 174 return mTableInheritedAttributes; 175 } 176 177 protected: 178 virtual ~HTMLTableElement(); 179 180 JSObject* WrapNode(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 181 182 nsIContent* GetChild(nsAtom* aTag) const { 183 for (nsIContent* cur = nsINode::GetFirstChild(); cur; 184 cur = cur->GetNextSibling()) { 185 if (cur->IsHTMLElement(aTag)) { 186 return cur; 187 } 188 } 189 return nullptr; 190 } 191 192 RefPtr<nsContentList> mTBodies; 193 RefPtr<TableRowsCollection> mRows; 194 RefPtr<StyleLockedDeclarationBlock> mTableInheritedAttributes; 195 void BuildInheritedAttributes(); 196 void ReleaseInheritedAttributes(); 197 198 private: 199 static void MapAttributesIntoRule(MappedDeclarationsBuilder&); 200 }; 201 202 } // namespace mozilla::dom 203 204 #endif /* mozilla_dom_HTMLTableElement_h */