~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/content/html/content/src/nsHTMLFontElement.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
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
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
#include "nsCOMPtr.h"
 
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"
 
51
 
 
52
class nsHTMLFontElement : public nsGenericHTMLElement,
 
53
                          public nsIDOMHTMLFontElement
 
54
{
 
55
public:
 
56
  nsHTMLFontElement();
 
57
  virtual ~nsHTMLFontElement();
 
58
 
 
59
  // nsISupports
 
60
  NS_DECL_ISUPPORTS_INHERITED
 
61
 
 
62
  // nsIDOMNode
 
63
  NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLElement::)
 
64
 
 
65
  // nsIDOMElement
 
66
  NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLElement::)
 
67
 
 
68
  // nsIDOMHTMLElement
 
69
  NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLElement::)
 
70
 
 
71
  // nsIDOMHTMLFontElement
 
72
  NS_DECL_NSIDOMHTMLFONTELEMENT
 
73
 
 
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;
 
82
};
 
83
 
 
84
nsresult
 
85
NS_NewHTMLFontElement(nsIHTMLContent** aInstancePtrResult,
 
86
                      nsINodeInfo *aNodeInfo, PRBool aFromParser)
 
87
{
 
88
  NS_ENSURE_ARG_POINTER(aInstancePtrResult);
 
89
 
 
90
  nsHTMLFontElement* it = new nsHTMLFontElement();
 
91
 
 
92
  if (!it) {
 
93
    return NS_ERROR_OUT_OF_MEMORY;
 
94
  }
 
95
 
 
96
  nsresult rv = it->Init(aNodeInfo);
 
97
 
 
98
  if (NS_FAILED(rv)) {
 
99
    delete it;
 
100
 
 
101
    return rv;
 
102
  }
 
103
 
 
104
  *aInstancePtrResult = NS_STATIC_CAST(nsIHTMLContent *, it);
 
105
  NS_ADDREF(*aInstancePtrResult);
 
106
 
 
107
  return NS_OK;
 
108
}
 
109
 
 
110
 
 
111
nsHTMLFontElement::nsHTMLFontElement()
 
112
{
 
113
}
 
114
 
 
115
nsHTMLFontElement::~nsHTMLFontElement()
 
116
{
 
117
}
 
118
 
 
119
NS_IMPL_ADDREF_INHERITED(nsHTMLFontElement, nsGenericElement)
 
120
NS_IMPL_RELEASE_INHERITED(nsHTMLFontElement, nsGenericElement)
 
121
 
 
122
 
 
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
 
128
 
 
129
 
 
130
nsresult
 
131
nsHTMLFontElement::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
 
132
{
 
133
  NS_ENSURE_ARG_POINTER(aReturn);
 
134
  *aReturn = nsnull;
 
135
 
 
136
  nsHTMLFontElement* it = new nsHTMLFontElement();
 
137
 
 
138
  if (!it) {
 
139
    return NS_ERROR_OUT_OF_MEMORY;
 
140
  }
 
141
 
 
142
  nsCOMPtr<nsIDOMNode> kungFuDeathGrip(it);
 
143
 
 
144
  nsresult rv = it->Init(mNodeInfo);
 
145
 
 
146
  if (NS_FAILED(rv))
 
147
    return rv;
 
148
 
 
149
  CopyInnerTo(it, aDeep);
 
150
 
 
151
  *aReturn = NS_STATIC_CAST(nsIDOMNode *, it);
 
152
 
 
153
  NS_ADDREF(*aReturn);
 
154
 
 
155
  return NS_OK;
 
156
}
 
157
 
 
158
 
 
159
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Color, color)
 
160
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Face, face)
 
161
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Size, size)
 
162
 
 
163
 
 
164
PRBool
 
165
nsHTMLFontElement::ParseAttribute(nsIAtom* aAttribute,
 
166
                                  const nsAString& aValue,
 
167
                                  nsAttrValue& aResult)
 
168
{
 
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);
 
177
      return PR_TRUE;
 
178
    }
 
179
    return PR_FALSE;
 
180
  }
 
181
  if (aAttribute == nsHTMLAtoms::pointSize ||
 
182
      aAttribute == nsHTMLAtoms::fontWeight) {
 
183
    return aResult.ParseIntValue(aValue);
 
184
  }
 
185
  if (aAttribute == nsHTMLAtoms::color) {
 
186
    return aResult.ParseColor(aValue, nsGenericHTMLElement::GetOwnerDocument());
 
187
  }
 
188
 
 
189
  return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
 
190
}
 
191
 
 
192
NS_IMETHODIMP
 
193
nsHTMLFontElement::AttributeToString(nsIAtom* aAttribute,
 
194
                                     const nsHTMLValue& aValue,
 
195
                                     nsAString& aResult) const
 
196
{
 
197
  if ((aAttribute == nsHTMLAtoms::size) ||
 
198
      (aAttribute == nsHTMLAtoms::pointSize) ||
 
199
      (aAttribute == nsHTMLAtoms::fontWeight)) {
 
200
    if (aValue.GetUnit() == eHTMLUnit_Enumerated) {
 
201
      nsAutoString intVal;
 
202
      PRInt32 value = aValue.GetIntValue(); 
 
203
      intVal.AppendInt(value, 10);
 
204
      if (value >= 0) {
 
205
        aResult = NS_LITERAL_STRING("+") + intVal;
 
206
      }
 
207
      else {
 
208
        aResult = intVal;
 
209
      }
 
210
      return NS_CONTENT_ATTR_HAS_VALUE;
 
211
    }
 
212
 
 
213
    return NS_CONTENT_ATTR_NOT_THERE;
 
214
  }
 
215
 
 
216
  return nsGenericHTMLElement::AttributeToString(aAttribute, aValue, aResult);
 
217
}
 
218
 
 
219
static void
 
220
MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
 
221
                      nsRuleData* aData)
 
222
{
 
223
  if (aData->mSID == eStyleStruct_Font) {
 
224
    nsRuleDataFont& font = *(aData->mFontData);
 
225
    nsHTMLValue value;
 
226
    
 
227
    // face: string list
 
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;
 
237
        }
 
238
      }
 
239
    }
 
240
 
 
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);
 
248
      }
 
249
      else {
 
250
        // size: int, enum , 
 
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
 
257
                    
 
258
          size = ((0 < size) ? ((size < 8) ? size : 7) : 1); 
 
259
          font.mSize.SetIntValue(size, eCSSUnit_Enumerated);
 
260
        }
 
261
      }
 
262
    }
 
263
 
 
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);
 
271
    }
 
272
  }
 
273
  else if (aData->mSID == eStyleStruct_Color) {
 
274
    if (aData->mColorData->mColor.GetUnit() == eCSSUnit_Null) {
 
275
      // color: color
 
276
      nsHTMLValue value;
 
277
      nscolor color;
 
278
      if (NS_CONTENT_ATTR_NOT_THERE !=
 
279
          aAttributes->GetAttribute(nsHTMLAtoms::color, value) &&
 
280
          value.GetColorValue(color)) {
 
281
        aData->mColorData->mColor.SetColorValue(color);
 
282
      }
 
283
    }
 
284
  }
 
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.
 
289
    nsHTMLValue value;
 
290
    nscolor color;
 
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();
 
298
      }
 
299
      decoration.SetIntValue(newValue, eCSSUnit_Enumerated);
 
300
    }
 
301
  }
 
302
 
 
303
  nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
 
304
}
 
305
 
 
306
NS_IMETHODIMP_(PRBool)
 
307
nsHTMLFontElement::IsAttributeMapped(const nsIAtom* aAttribute) const
 
308
{
 
309
  static const MappedAttributeEntry attributes[] = {
 
310
    { &nsHTMLAtoms::face },
 
311
    { &nsHTMLAtoms::pointSize },
 
312
    { &nsHTMLAtoms::size },
 
313
    { &nsHTMLAtoms::fontWeight },
 
314
    { &nsHTMLAtoms::color },
 
315
    { nsnull }
 
316
  };
 
317
 
 
318
  static const MappedAttributeEntry* const map[] = {
 
319
    attributes,
 
320
    sCommonAttributeMap,
 
321
  };
 
322
 
 
323
  return FindAttributeDependence(aAttribute, map, NS_ARRAY_LENGTH(map));
 
324
}
 
325
 
 
326
 
 
327
NS_IMETHODIMP
 
328
nsHTMLFontElement::GetAttributeMappingFunction(nsMapRuleToAttributesFunc& aMapRuleFunc) const
 
329
{
 
330
  aMapRuleFunc = &MapAttributesIntoRule;
 
331
  return NS_OK;
 
332
}