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 :
38 : #include "mozilla/Util.h"
39 :
40 : #include "nsGenericHTMLElement.h"
41 : #include "nsIDOMHTMLHRElement.h"
42 :
43 : #include "nsIDOMEventTarget.h"
44 : #include "nsGkAtoms.h"
45 : #include "nsStyleConsts.h"
46 : #include "nsPresContext.h"
47 : #include "nsMappedAttributes.h"
48 : #include "nsRuleData.h"
49 : #include "nsCSSProps.h"
50 :
51 : using namespace mozilla;
52 :
53 : class nsHTMLHRElement : public nsGenericHTMLElement,
54 : public nsIDOMHTMLHRElement
55 : {
56 : public:
57 : nsHTMLHRElement(already_AddRefed<nsINodeInfo> aNodeInfo);
58 : virtual ~nsHTMLHRElement();
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 : // nsIDOMHTMLHRElement
73 : NS_DECL_NSIDOMHTMLHRELEMENT
74 :
75 : virtual bool ParseAttribute(PRInt32 aNamespaceID,
76 : nsIAtom* aAttribute,
77 : const nsAString& aValue,
78 : nsAttrValue& aResult);
79 : NS_IMETHOD_(bool) IsAttributeMapped(const nsIAtom* aAttribute) const;
80 : virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
81 : virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const;
82 : virtual nsXPCClassInfo* GetClassInfo();
83 : };
84 :
85 :
86 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(HR)
87 :
88 :
89 0 : nsHTMLHRElement::nsHTMLHRElement(already_AddRefed<nsINodeInfo> aNodeInfo)
90 0 : : nsGenericHTMLElement(aNodeInfo)
91 : {
92 0 : }
93 :
94 0 : nsHTMLHRElement::~nsHTMLHRElement()
95 : {
96 0 : }
97 :
98 :
99 0 : NS_IMPL_ADDREF_INHERITED(nsHTMLHRElement, nsGenericElement)
100 0 : NS_IMPL_RELEASE_INHERITED(nsHTMLHRElement, nsGenericElement)
101 :
102 :
103 0 : DOMCI_NODE_DATA(HTMLHRElement, nsHTMLHRElement)
104 :
105 : // QueryInterface implementation for nsHTMLHRElement
106 0 : NS_INTERFACE_TABLE_HEAD(nsHTMLHRElement)
107 0 : NS_HTML_CONTENT_INTERFACE_TABLE1(nsHTMLHRElement,
108 : nsIDOMHTMLHRElement)
109 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TO_MAP_SEGUE(nsHTMLHRElement,
110 : nsGenericHTMLElement)
111 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TAIL_CLASSINFO(HTMLHRElement)
112 :
113 :
114 0 : NS_IMPL_ELEMENT_CLONE(nsHTMLHRElement)
115 :
116 :
117 0 : NS_IMPL_STRING_ATTR(nsHTMLHRElement, Align, align)
118 0 : NS_IMPL_BOOL_ATTR(nsHTMLHRElement, NoShade, noshade)
119 0 : NS_IMPL_STRING_ATTR(nsHTMLHRElement, Size, size)
120 0 : NS_IMPL_STRING_ATTR(nsHTMLHRElement, Width, width)
121 0 : NS_IMPL_STRING_ATTR(nsHTMLHRElement, Color, color)
122 :
123 : static const nsAttrValue::EnumTable kAlignTable[] = {
124 : { "left", NS_STYLE_TEXT_ALIGN_LEFT },
125 : { "right", NS_STYLE_TEXT_ALIGN_RIGHT },
126 : { "center", NS_STYLE_TEXT_ALIGN_CENTER },
127 : { 0 }
128 : };
129 :
130 : bool
131 0 : nsHTMLHRElement::ParseAttribute(PRInt32 aNamespaceID,
132 : nsIAtom* aAttribute,
133 : const nsAString& aValue,
134 : nsAttrValue& aResult)
135 : {
136 0 : if (aNamespaceID == kNameSpaceID_None) {
137 0 : if (aAttribute == nsGkAtoms::width) {
138 0 : return aResult.ParseSpecialIntValue(aValue);
139 : }
140 0 : if (aAttribute == nsGkAtoms::size) {
141 0 : return aResult.ParseIntWithBounds(aValue, 1, 1000);
142 : }
143 0 : if (aAttribute == nsGkAtoms::align) {
144 0 : return aResult.ParseEnumValue(aValue, kAlignTable, false);
145 : }
146 0 : if (aAttribute == nsGkAtoms::color) {
147 0 : return aResult.ParseColor(aValue);
148 : }
149 : }
150 :
151 : return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
152 0 : aResult);
153 : }
154 :
155 : static void
156 0 : MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
157 : nsRuleData* aData)
158 : {
159 0 : bool noshade = false;
160 :
161 0 : const nsAttrValue* colorValue = aAttributes->GetAttr(nsGkAtoms::color);
162 : nscolor color;
163 0 : bool colorIsSet = colorValue && colorValue->GetColorValue(color);
164 :
165 0 : if (aData->mSIDs & (NS_STYLE_INHERIT_BIT(Position) |
166 : NS_STYLE_INHERIT_BIT(Border))) {
167 0 : if (colorIsSet) {
168 0 : noshade = true;
169 : } else {
170 0 : noshade = !!aAttributes->GetAttr(nsGkAtoms::noshade);
171 : }
172 : }
173 :
174 0 : if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Margin)) {
175 : // align: enum
176 0 : const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::align);
177 0 : if (value && value->Type() == nsAttrValue::eEnum) {
178 : // Map align attribute into auto side margins
179 0 : nsCSSValue* marginLeft = aData->ValueForMarginLeftValue();
180 0 : nsCSSValue* marginRight = aData->ValueForMarginRightValue();
181 0 : switch (value->GetEnumValue()) {
182 : case NS_STYLE_TEXT_ALIGN_LEFT:
183 0 : if (marginLeft->GetUnit() == eCSSUnit_Null)
184 0 : marginLeft->SetFloatValue(0.0f, eCSSUnit_Pixel);
185 0 : if (marginRight->GetUnit() == eCSSUnit_Null)
186 0 : marginRight->SetAutoValue();
187 0 : break;
188 : case NS_STYLE_TEXT_ALIGN_RIGHT:
189 0 : if (marginLeft->GetUnit() == eCSSUnit_Null)
190 0 : marginLeft->SetAutoValue();
191 0 : if (marginRight->GetUnit() == eCSSUnit_Null)
192 0 : marginRight->SetFloatValue(0.0f, eCSSUnit_Pixel);
193 0 : break;
194 : case NS_STYLE_TEXT_ALIGN_CENTER:
195 0 : if (marginLeft->GetUnit() == eCSSUnit_Null)
196 0 : marginLeft->SetAutoValue();
197 0 : if (marginRight->GetUnit() == eCSSUnit_Null)
198 0 : marginRight->SetAutoValue();
199 0 : break;
200 : }
201 : }
202 : }
203 0 : if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Position)) {
204 : // width: integer, percent
205 0 : nsCSSValue* width = aData->ValueForWidth();
206 0 : if (width->GetUnit() == eCSSUnit_Null) {
207 0 : const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::width);
208 0 : if (value && value->Type() == nsAttrValue::eInteger) {
209 0 : width->SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);
210 0 : } else if (value && value->Type() == nsAttrValue::ePercent) {
211 0 : width->SetPercentValue(value->GetPercentValue());
212 : }
213 : }
214 :
215 0 : nsCSSValue* height = aData->ValueForHeight();
216 0 : if (height->GetUnit() == eCSSUnit_Null) {
217 : // size: integer
218 0 : if (noshade) {
219 : // noshade case: size is set using the border
220 0 : height->SetAutoValue();
221 : } else {
222 : // normal case
223 : // the height includes the top and bottom borders that are initially 1px.
224 : // for size=1, html.css has a special case rule that makes this work by
225 : // removing all but the top border.
226 0 : const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::size);
227 0 : if (value && value->Type() == nsAttrValue::eInteger) {
228 0 : height->SetFloatValue((float)value->GetIntegerValue(), eCSSUnit_Pixel);
229 : } // else use default value from html.css
230 : }
231 : }
232 : }
233 0 : if ((aData->mSIDs & NS_STYLE_INHERIT_BIT(Border)) && noshade) { // if not noshade, border styles are dealt with by html.css
234 : // size: integer
235 : // if a size is set, use half of it per side, otherwise, use 1px per side
236 : float sizePerSide;
237 0 : bool allSides = true;
238 0 : const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::size);
239 0 : if (value && value->Type() == nsAttrValue::eInteger) {
240 0 : sizePerSide = (float)value->GetIntegerValue() / 2.0f;
241 0 : if (sizePerSide < 1.0f) {
242 : // XXX When the pixel bug is fixed, all the special casing for
243 : // subpixel borders should be removed.
244 : // In the meantime, this makes http://www.microsoft.com/ look right.
245 0 : sizePerSide = 1.0f;
246 0 : allSides = false;
247 : }
248 : } else {
249 0 : sizePerSide = 1.0f; // default to a 2px high line
250 : }
251 0 : nsCSSValue* borderTopWidth = aData->ValueForBorderTopWidth();
252 0 : if (borderTopWidth->GetUnit() == eCSSUnit_Null) {
253 0 : borderTopWidth->SetFloatValue(sizePerSide, eCSSUnit_Pixel);
254 : }
255 0 : if (allSides) {
256 0 : nsCSSValue* borderRightWidth = aData->ValueForBorderRightWidthValue();
257 0 : if (borderRightWidth->GetUnit() == eCSSUnit_Null) {
258 0 : borderRightWidth->SetFloatValue(sizePerSide, eCSSUnit_Pixel);
259 : }
260 0 : nsCSSValue* borderBottomWidth = aData->ValueForBorderBottomWidth();
261 0 : if (borderBottomWidth->GetUnit() == eCSSUnit_Null) {
262 0 : borderBottomWidth->SetFloatValue(sizePerSide, eCSSUnit_Pixel);
263 : }
264 0 : nsCSSValue* borderLeftWidth = aData->ValueForBorderLeftWidthValue();
265 0 : if (borderLeftWidth->GetUnit() == eCSSUnit_Null) {
266 0 : borderLeftWidth->SetFloatValue(sizePerSide, eCSSUnit_Pixel);
267 : }
268 : }
269 :
270 0 : nsCSSValue* borderTopStyle = aData->ValueForBorderTopStyle();
271 0 : if (borderTopStyle->GetUnit() == eCSSUnit_Null) {
272 : borderTopStyle->SetIntValue(NS_STYLE_BORDER_STYLE_SOLID,
273 0 : eCSSUnit_Enumerated);
274 : }
275 0 : if (allSides) {
276 0 : nsCSSValue* borderRightStyle = aData->ValueForBorderRightStyleValue();
277 0 : if (borderRightStyle->GetUnit() == eCSSUnit_Null) {
278 : borderRightStyle->SetIntValue(NS_STYLE_BORDER_STYLE_SOLID,
279 0 : eCSSUnit_Enumerated);
280 : }
281 0 : nsCSSValue* borderBottomStyle = aData->ValueForBorderBottomStyle();
282 0 : if (borderBottomStyle->GetUnit() == eCSSUnit_Null) {
283 : borderBottomStyle->SetIntValue(NS_STYLE_BORDER_STYLE_SOLID,
284 0 : eCSSUnit_Enumerated);
285 : }
286 0 : nsCSSValue* borderLeftStyle = aData->ValueForBorderLeftStyleValue();
287 0 : if (borderLeftStyle->GetUnit() == eCSSUnit_Null) {
288 : borderLeftStyle->SetIntValue(NS_STYLE_BORDER_STYLE_SOLID,
289 0 : eCSSUnit_Enumerated);
290 : }
291 :
292 : // If it would be noticeable, set the border radius to
293 : // 10000px on all corners; this triggers the clamping to make
294 : // circular ends. This assumes the <hr> isn't larger than
295 : // that in *both* dimensions.
296 0 : for (const nsCSSProperty* props =
297 0 : nsCSSProps::SubpropertyEntryFor(eCSSProperty_border_radius);
298 : *props != eCSSProperty_UNKNOWN; ++props) {
299 0 : nsCSSValue* dimen = aData->ValueFor(*props);
300 0 : if (dimen->GetUnit() == eCSSUnit_Null) {
301 0 : dimen->SetFloatValue(10000.0f, eCSSUnit_Pixel);
302 : }
303 : }
304 : }
305 : }
306 0 : if (aData->mSIDs & NS_STYLE_INHERIT_BIT(Color)) {
307 : // color: a color
308 : // (we got the color attribute earlier)
309 0 : nsCSSValue* colorValue = aData->ValueForColor();
310 0 : if (colorIsSet &&
311 0 : colorValue->GetUnit() == eCSSUnit_Null &&
312 0 : aData->mPresContext->UseDocumentColors()) {
313 0 : colorValue->SetColorValue(color);
314 : }
315 : }
316 :
317 0 : nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
318 0 : }
319 :
320 : NS_IMETHODIMP_(bool)
321 0 : nsHTMLHRElement::IsAttributeMapped(const nsIAtom* aAttribute) const
322 : {
323 : static const MappedAttributeEntry attributes[] = {
324 : { &nsGkAtoms::align },
325 : { &nsGkAtoms::width },
326 : { &nsGkAtoms::size },
327 : { &nsGkAtoms::color },
328 : { &nsGkAtoms::noshade },
329 : { nsnull },
330 : };
331 :
332 : static const MappedAttributeEntry* const map[] = {
333 : attributes,
334 : sCommonAttributeMap,
335 : };
336 :
337 0 : return FindAttributeDependence(aAttribute, map);
338 : }
339 :
340 :
341 : nsMapRuleToAttributesFunc
342 0 : nsHTMLHRElement::GetAttributeMappingFunction() const
343 : {
344 0 : return &MapAttributesIntoRule;
345 : }
|