1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
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
15
* The Original Code is Mozilla Communicator client code.
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.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#include "nsIDOMHTMLFontElement.h"
40
#include "nsIDOMEventReceiver.h"
41
#include "nsIHTMLContent.h"
42
#include "nsGenericHTMLElement.h"
43
#include "nsHTMLAtoms.h"
44
#include "nsIDeviceContext.h"
45
#include "nsStyleConsts.h"
46
#include "nsIPresContext.h"
47
#include "nsMappedAttributes.h"
48
#include "nsCSSStruct.h"
49
#include "nsRuleNode.h"
50
#include "nsIDocument.h"
52
class nsHTMLFontElement : public nsGenericHTMLElement,
53
public nsIDOMHTMLFontElement
57
virtual ~nsHTMLFontElement();
60
NS_DECL_ISUPPORTS_INHERITED
63
NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLElement::)
66
NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLElement::)
69
NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLElement::)
71
// nsIDOMHTMLFontElement
72
NS_DECL_NSIDOMHTMLFONTELEMENT
74
virtual PRBool ParseAttribute(nsIAtom* aAttribute,
75
const nsAString& aValue,
76
nsAttrValue& aResult);
77
NS_IMETHOD AttributeToString(nsIAtom* aAttribute,
78
const nsHTMLValue& aValue,
79
nsAString& aResult) const;
80
NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
81
NS_IMETHOD GetAttributeMappingFunction(nsMapRuleToAttributesFunc& aMapRuleFunc) const;
85
NS_NewHTMLFontElement(nsIHTMLContent** aInstancePtrResult,
86
nsINodeInfo *aNodeInfo, PRBool aFromParser)
88
NS_ENSURE_ARG_POINTER(aInstancePtrResult);
90
nsHTMLFontElement* it = new nsHTMLFontElement();
93
return NS_ERROR_OUT_OF_MEMORY;
96
nsresult rv = it->Init(aNodeInfo);
104
*aInstancePtrResult = NS_STATIC_CAST(nsIHTMLContent *, it);
105
NS_ADDREF(*aInstancePtrResult);
111
nsHTMLFontElement::nsHTMLFontElement()
115
nsHTMLFontElement::~nsHTMLFontElement()
119
NS_IMPL_ADDREF_INHERITED(nsHTMLFontElement, nsGenericElement)
120
NS_IMPL_RELEASE_INHERITED(nsHTMLFontElement, nsGenericElement)
123
// QueryInterface implementation for nsHTMLFontElement
124
NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLFontElement, nsGenericHTMLElement)
125
NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLFontElement)
126
NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLFontElement)
127
NS_HTML_CONTENT_INTERFACE_MAP_END
131
nsHTMLFontElement::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
133
NS_ENSURE_ARG_POINTER(aReturn);
136
nsHTMLFontElement* it = new nsHTMLFontElement();
139
return NS_ERROR_OUT_OF_MEMORY;
142
nsCOMPtr<nsIDOMNode> kungFuDeathGrip(it);
144
nsresult rv = it->Init(mNodeInfo);
149
CopyInnerTo(it, aDeep);
151
*aReturn = NS_STATIC_CAST(nsIDOMNode *, it);
159
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Color, color)
160
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Face, face)
161
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Size, size)
165
nsHTMLFontElement::ParseAttribute(nsIAtom* aAttribute,
166
const nsAString& aValue,
167
nsAttrValue& aResult)
169
if (aAttribute == nsHTMLAtoms::size) {
170
nsAutoString tmp(aValue);
171
PRInt32 ec, v = tmp.ToInteger(&ec);
172
if(NS_SUCCEEDED(ec)) {
173
tmp.CompressWhitespace(PR_TRUE, PR_FALSE);
174
PRUnichar ch = tmp.First();
175
aResult.SetTo(v, (ch == '+' || ch == '-') ?
176
nsAttrValue::eEnum : nsAttrValue::eInteger);
181
if (aAttribute == nsHTMLAtoms::pointSize ||
182
aAttribute == nsHTMLAtoms::fontWeight) {
183
return aResult.ParseIntValue(aValue);
185
if (aAttribute == nsHTMLAtoms::color) {
186
return aResult.ParseColor(aValue, nsGenericHTMLElement::GetOwnerDocument());
189
return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
193
nsHTMLFontElement::AttributeToString(nsIAtom* aAttribute,
194
const nsHTMLValue& aValue,
195
nsAString& aResult) const
197
if ((aAttribute == nsHTMLAtoms::size) ||
198
(aAttribute == nsHTMLAtoms::pointSize) ||
199
(aAttribute == nsHTMLAtoms::fontWeight)) {
200
if (aValue.GetUnit() == eHTMLUnit_Enumerated) {
202
PRInt32 value = aValue.GetIntValue();
203
intVal.AppendInt(value, 10);
205
aResult = NS_LITERAL_STRING("+") + intVal;
210
return NS_CONTENT_ATTR_HAS_VALUE;
213
return NS_CONTENT_ATTR_NOT_THERE;
216
return nsGenericHTMLElement::AttributeToString(aAttribute, aValue, aResult);
220
MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
223
if (aData->mSID == eStyleStruct_Font) {
224
nsRuleDataFont& font = *(aData->mFontData);
228
if (font.mFamily.GetUnit() == eCSSUnit_Null) {
229
if (aAttributes->GetAttribute(nsHTMLAtoms::face, value) !=
230
NS_CONTENT_ATTR_NOT_THERE &&
231
value.GetUnit() == eHTMLUnit_String) {
232
nsAutoString familyList;
233
value.GetStringValue(familyList);
234
if (!familyList.IsEmpty()) {
235
font.mFamily.SetStringValue(familyList, eCSSUnit_String);
236
font.mFamilyFromHTML = PR_TRUE;
241
// pointSize: int, enum
242
if (font.mSize.GetUnit() == eCSSUnit_Null) {
243
aAttributes->GetAttribute(nsHTMLAtoms::pointSize, value);
244
if (value.GetUnit() == eHTMLUnit_Integer ||
245
value.GetUnit() == eHTMLUnit_Enumerated) {
246
PRInt32 val = value.GetIntValue();
247
font.mSize.SetFloatValue((float)val, eCSSUnit_Point);
251
aAttributes->GetAttribute(nsHTMLAtoms::size, value);
252
nsHTMLUnit unit = value.GetUnit();
253
if (unit == eHTMLUnit_Integer || unit == eHTMLUnit_Enumerated) {
254
PRInt32 size = value.GetIntValue();
255
if (unit == eHTMLUnit_Enumerated) // int (+/-)
256
size += 3; // XXX should be BASEFONT, not three see bug 3875
258
size = ((0 < size) ? ((size < 8) ? size : 7) : 1);
259
font.mSize.SetIntValue(size, eCSSUnit_Enumerated);
264
// fontWeight: int, enum
265
if (font.mWeight.GetUnit() == eCSSUnit_Null) {
266
aAttributes->GetAttribute(nsHTMLAtoms::fontWeight, value);
267
if (value.GetUnit() == eHTMLUnit_Integer) // +/-
268
font.mWeight.SetIntValue(value.GetIntValue(), eCSSUnit_Integer);
269
else if (value.GetUnit() == eHTMLUnit_Enumerated)
270
font.mWeight.SetIntValue(value.GetIntValue(), eCSSUnit_Enumerated);
273
else if (aData->mSID == eStyleStruct_Color) {
274
if (aData->mColorData->mColor.GetUnit() == eCSSUnit_Null) {
278
if (NS_CONTENT_ATTR_NOT_THERE !=
279
aAttributes->GetAttribute(nsHTMLAtoms::color, value) &&
280
value.GetColorValue(color)) {
281
aData->mColorData->mColor.SetColorValue(color);
285
else if (aData->mSID == eStyleStruct_TextReset) {
286
// Make <a><font color="red">text</font></a> give the text a red underline
287
// in quirks mode. The NS_STYLE_TEXT_DECORATION_OVERRIDE_ALL flag only
288
// affects quirks mode rendering.
291
if (NS_CONTENT_ATTR_NOT_THERE !=
292
aAttributes->GetAttribute(nsHTMLAtoms::color, value) &&
293
value.GetColorValue(color)) {
294
nsCSSValue& decoration = aData->mTextData->mDecoration;
295
PRInt32 newValue = NS_STYLE_TEXT_DECORATION_OVERRIDE_ALL;
296
if (decoration.GetUnit() == eCSSUnit_Enumerated) {
297
newValue |= decoration.GetIntValue();
299
decoration.SetIntValue(newValue, eCSSUnit_Enumerated);
303
nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
306
NS_IMETHODIMP_(PRBool)
307
nsHTMLFontElement::IsAttributeMapped(const nsIAtom* aAttribute) const
309
static const MappedAttributeEntry attributes[] = {
310
{ &nsHTMLAtoms::face },
311
{ &nsHTMLAtoms::pointSize },
312
{ &nsHTMLAtoms::size },
313
{ &nsHTMLAtoms::fontWeight },
314
{ &nsHTMLAtoms::color },
318
static const MappedAttributeEntry* const map[] = {
323
return FindAttributeDependence(aAttribute, map, NS_ARRAY_LENGTH(map));
328
nsHTMLFontElement::GetAttributeMappingFunction(nsMapRuleToAttributesFunc& aMapRuleFunc) const
330
aMapRuleFunc = &MapAttributesIntoRule;