1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 : #include "nsIDOMHTMLOptGroupElement.h"
38 : #include "nsIDOMEventTarget.h"
39 : #include "nsGenericHTMLElement.h"
40 : #include "nsGkAtoms.h"
41 : #include "nsStyleConsts.h"
42 : #include "nsIFrame.h"
43 : #include "nsIFormControlFrame.h"
44 : #include "nsEventStates.h"
45 : #include "nsIDocument.h"
46 :
47 : #include "nsEventDispatcher.h"
48 : #include "nsHTMLSelectElement.h"
49 :
50 : /**
51 : * The implementation of <optgroup>
52 : */
53 : class nsHTMLOptGroupElement : public nsGenericHTMLElement,
54 : public nsIDOMHTMLOptGroupElement
55 : {
56 : public:
57 : nsHTMLOptGroupElement(already_AddRefed<nsINodeInfo> aNodeInfo);
58 : virtual ~nsHTMLOptGroupElement();
59 :
60 : // nsISupports
61 : NS_DECL_ISUPPORTS_INHERITED
62 :
63 : // nsIDOMNode
64 0 : NS_FORWARD_NSIDOMNODE(nsGenericHTMLElement::)
65 :
66 : // nsIDOMElement
67 0 : NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLElement::)
68 :
69 : // nsIDOMHTMLElement
70 0 : NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLElement::)
71 :
72 : // nsIDOMHTMLOptGroupElement
73 : NS_DECL_NSIDOMHTMLOPTGROUPELEMENT
74 :
75 : // nsGenericElement
76 : virtual nsresult InsertChildAt(nsIContent* aKid, PRUint32 aIndex,
77 : bool aNotify);
78 : virtual nsresult RemoveChildAt(PRUint32 aIndex, bool aNotify);
79 :
80 : // nsIContent
81 : virtual nsresult PreHandleEvent(nsEventChainPreVisitor& aVisitor);
82 :
83 : virtual nsEventStates IntrinsicState() const;
84 :
85 : virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const;
86 :
87 : virtual nsXPCClassInfo* GetClassInfo();
88 :
89 0 : virtual bool IsDisabled() const {
90 0 : return HasAttr(kNameSpaceID_None, nsGkAtoms::disabled);
91 : }
92 : protected:
93 :
94 : /**
95 : * Get the select content element that contains this option
96 : * @param aSelectElement the select element [OUT]
97 : */
98 : nsIContent* GetSelect();
99 : };
100 :
101 :
102 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(OptGroup)
103 :
104 :
105 0 : nsHTMLOptGroupElement::nsHTMLOptGroupElement(already_AddRefed<nsINodeInfo> aNodeInfo)
106 0 : : nsGenericHTMLElement(aNodeInfo)
107 : {
108 : // We start off enabled
109 0 : AddStatesSilently(NS_EVENT_STATE_ENABLED);
110 0 : }
111 :
112 0 : nsHTMLOptGroupElement::~nsHTMLOptGroupElement()
113 : {
114 0 : }
115 :
116 :
117 0 : NS_IMPL_ADDREF_INHERITED(nsHTMLOptGroupElement, nsGenericElement)
118 0 : NS_IMPL_RELEASE_INHERITED(nsHTMLOptGroupElement, nsGenericElement)
119 :
120 :
121 0 : DOMCI_NODE_DATA(HTMLOptGroupElement, nsHTMLOptGroupElement)
122 :
123 : // QueryInterface implementation for nsHTMLOptGroupElement
124 0 : NS_INTERFACE_TABLE_HEAD(nsHTMLOptGroupElement)
125 0 : NS_HTML_CONTENT_INTERFACE_TABLE1(nsHTMLOptGroupElement,
126 : nsIDOMHTMLOptGroupElement)
127 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TO_MAP_SEGUE(nsHTMLOptGroupElement,
128 : nsGenericHTMLElement)
129 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TAIL_CLASSINFO(HTMLOptGroupElement)
130 :
131 :
132 0 : NS_IMPL_ELEMENT_CLONE(nsHTMLOptGroupElement)
133 :
134 :
135 0 : NS_IMPL_BOOL_ATTR(nsHTMLOptGroupElement, Disabled, disabled)
136 0 : NS_IMPL_STRING_ATTR(nsHTMLOptGroupElement, Label, label)
137 :
138 :
139 : nsresult
140 0 : nsHTMLOptGroupElement::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
141 : {
142 0 : aVisitor.mCanHandle = false;
143 : // Do not process any DOM events if the element is disabled
144 : // XXXsmaug This is not the right thing to do. But what is?
145 0 : if (HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) {
146 0 : return NS_OK;
147 : }
148 :
149 0 : nsIFrame* frame = GetPrimaryFrame();
150 0 : if (frame) {
151 0 : const nsStyleUserInterface* uiStyle = frame->GetStyleUserInterface();
152 0 : if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE ||
153 : uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED) {
154 0 : return NS_OK;
155 : }
156 : }
157 :
158 0 : return nsGenericHTMLElement::PreHandleEvent(aVisitor);
159 : }
160 :
161 : nsIContent*
162 0 : nsHTMLOptGroupElement::GetSelect()
163 : {
164 0 : nsIContent* parent = this;
165 0 : while ((parent = parent->GetParent()) && parent->IsHTML()) {
166 0 : if (parent->Tag() == nsGkAtoms::select) {
167 0 : return parent;
168 : }
169 0 : if (parent->Tag() != nsGkAtoms::optgroup) {
170 0 : break;
171 : }
172 : }
173 :
174 0 : return nsnull;
175 : }
176 :
177 : nsresult
178 0 : nsHTMLOptGroupElement::InsertChildAt(nsIContent* aKid,
179 : PRUint32 aIndex,
180 : bool aNotify)
181 : {
182 0 : nsSafeOptionListMutation safeMutation(GetSelect(), this, aKid, aIndex, aNotify);
183 0 : nsresult rv = nsGenericHTMLElement::InsertChildAt(aKid, aIndex, aNotify);
184 0 : if (NS_FAILED(rv)) {
185 0 : safeMutation.MutationFailed();
186 : }
187 0 : return rv;
188 : }
189 :
190 : nsresult
191 0 : nsHTMLOptGroupElement::RemoveChildAt(PRUint32 aIndex, bool aNotify)
192 : {
193 : nsSafeOptionListMutation safeMutation(GetSelect(), this, nsnull, aIndex,
194 0 : aNotify);
195 0 : nsresult rv = nsGenericHTMLElement::RemoveChildAt(aIndex, aNotify);
196 0 : if (NS_FAILED(rv)) {
197 0 : safeMutation.MutationFailed();
198 : }
199 0 : return rv;
200 : }
201 :
202 : nsEventStates
203 0 : nsHTMLOptGroupElement::IntrinsicState() const
204 : {
205 0 : nsEventStates state = nsGenericHTMLElement::IntrinsicState();
206 :
207 0 : if (HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) {
208 0 : state |= NS_EVENT_STATE_DISABLED;
209 0 : state &= ~NS_EVENT_STATE_ENABLED;
210 : } else {
211 0 : state &= ~NS_EVENT_STATE_DISABLED;
212 0 : state |= NS_EVENT_STATE_ENABLED;
213 : }
214 :
215 : return state;
216 : }
|