Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / layout / mathml / base / src / nsMathMLTokenFrame.cpp
blob5be14bb8243d56e7bdb82c219e0e2cffd77b8de4
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/
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.
15 * The Original Code is Mozilla MathML Project.
17 * The Initial Developer of the Original Code is
18 * The University of Queensland.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Roger B. Sidje <rbs@maths.uq.edu.au>
24 * Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsCOMPtr.h"
41 #include "nsFrame.h"
42 #include "nsPresContext.h"
43 #include "nsStyleContext.h"
44 #include "nsStyleConsts.h"
45 #include "nsIRenderingContext.h"
46 #include "nsIFontMetrics.h"
47 #include "nsContentUtils.h"
48 #include "nsCSSFrameConstructor.h"
49 #include "nsMathMLTokenFrame.h"
51 nsIFrame*
52 NS_NewMathMLTokenFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
54 return new (aPresShell) nsMathMLTokenFrame(aContext);
56 nsMathMLTokenFrame::~nsMathMLTokenFrame()
60 eMathMLFrameType
61 nsMathMLTokenFrame::GetMathMLFrameType()
63 // treat everything other than <mi> as ordinary...
64 if (mContent->Tag() != nsGkAtoms::mi_) {
65 return eMathMLFrameType_Ordinary;
68 // for <mi>, distinguish between italic and upright...
69 // Don't use nsMathMLFrame::GetAttribute for mathvariant or fontstyle as
70 // default values are not inherited.
71 nsAutoString style;
72 // mathvariant overrides fontstyle
73 // http://www.w3.org/TR/2003/REC-MathML2-20031021/chapter3.html#presm.deprecatt
74 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::MOZfontstyle, style) ||
75 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::mathvariant_, style) ||
76 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::fontstyle_, style);
78 if (style.EqualsLiteral("italic") || style.EqualsLiteral("bold-italic") ||
79 style.EqualsLiteral("script") || style.EqualsLiteral("bold-script") ||
80 style.EqualsLiteral("sans-serif-italic") ||
81 style.EqualsLiteral("sans-serif-bold-italic")) {
82 return eMathMLFrameType_ItalicIdentifier;
84 else if(style.EqualsLiteral("invariant")) {
85 nsAutoString data;
86 nsContentUtils::GetNodeTextContent(mContent, PR_FALSE, data);
87 eMATHVARIANT variant = nsMathMLOperators::LookupInvariantChar(data);
89 switch (variant) {
90 case eMATHVARIANT_italic:
91 case eMATHVARIANT_bold_italic:
92 case eMATHVARIANT_script:
93 case eMATHVARIANT_bold_script:
94 case eMATHVARIANT_sans_serif_italic:
95 case eMATHVARIANT_sans_serif_bold_italic:
96 return eMathMLFrameType_ItalicIdentifier;
97 default:
98 ; // fall through to upright
101 return eMathMLFrameType_UprightIdentifier;
104 static void
105 CompressWhitespace(nsIContent* aContent)
107 PRUint32 numKids = aContent->GetChildCount();
108 for (PRUint32 kid = 0; kid < numKids; kid++) {
109 nsIContent* cont = aContent->GetChildAt(kid);
110 if (cont && cont->IsNodeOfType(nsINode::eTEXT)) {
111 nsAutoString text;
112 cont->AppendTextTo(text);
113 text.CompressWhitespace();
114 cont->SetText(text, PR_FALSE); // not meant to be used if notify is needed
119 NS_IMETHODIMP
120 nsMathMLTokenFrame::Init(nsIContent* aContent,
121 nsIFrame* aParent,
122 nsIFrame* aPrevInFlow)
124 // leading and trailing whitespace doesn't count -- bug 15402
125 // brute force removal for people who do <mi> a </mi> instead of <mi>a</mi>
126 // XXX the best fix is to skip these in nsTextFrame
127 CompressWhitespace(aContent);
129 // let the base class do its Init()
130 return nsMathMLContainerFrame::Init(aContent, aParent, aPrevInFlow);
133 NS_IMETHODIMP
134 nsMathMLTokenFrame::SetInitialChildList(nsIAtom* aListName,
135 nsIFrame* aChildList)
137 // First, let the base class do its work
138 nsresult rv = nsMathMLContainerFrame::SetInitialChildList(aListName, aChildList);
139 if (NS_FAILED(rv))
140 return rv;
142 SetQuotes();
143 ProcessTextData();
144 return rv;
147 nsresult
148 nsMathMLTokenFrame::Reflow(nsPresContext* aPresContext,
149 nsHTMLReflowMetrics& aDesiredSize,
150 const nsHTMLReflowState& aReflowState,
151 nsReflowStatus& aStatus)
153 nsresult rv = NS_OK;
155 // initializations needed for empty markup like <mtag></mtag>
156 aDesiredSize.width = aDesiredSize.height = 0;
157 aDesiredSize.ascent = 0;
158 aDesiredSize.mBoundingMetrics.Clear();
160 nsSize availSize(aReflowState.ComputedWidth(), NS_UNCONSTRAINEDSIZE);
161 nsIFrame* childFrame = GetFirstChild(nsnull);
162 while (childFrame) {
163 // ask our children to compute their bounding metrics
164 nsHTMLReflowMetrics childDesiredSize(aDesiredSize.mFlags
165 | NS_REFLOW_CALC_BOUNDING_METRICS);
166 nsHTMLReflowState childReflowState(aPresContext, aReflowState,
167 childFrame, availSize);
168 rv = ReflowChild(childFrame, aPresContext, childDesiredSize,
169 childReflowState, aStatus);
170 //NS_ASSERTION(NS_FRAME_IS_COMPLETE(aStatus), "bad status");
171 if (NS_FAILED(rv)) {
172 // Call DidReflow() for the child frames we successfully did reflow.
173 DidReflowChildren(GetFirstChild(nsnull), childFrame);
174 return rv;
177 SaveReflowAndBoundingMetricsFor(childFrame, childDesiredSize,
178 childDesiredSize.mBoundingMetrics);
180 childFrame = childFrame->GetNextSibling();
184 // place and size children
185 FinalizeReflow(*aReflowState.rendContext, aDesiredSize);
187 aStatus = NS_FRAME_COMPLETE;
188 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
189 return NS_OK;
192 // For token elements, mBoundingMetrics is computed at the ReflowToken
193 // pass, it is not computed here because our children may be text frames
194 // that do not implement the GetBoundingMetrics() interface.
195 /* virtual */ nsresult
196 nsMathMLTokenFrame::Place(nsIRenderingContext& aRenderingContext,
197 PRBool aPlaceOrigin,
198 nsHTMLReflowMetrics& aDesiredSize)
200 mBoundingMetrics.Clear();
201 nsIFrame* childFrame = GetFirstChild(nsnull);
202 while (childFrame) {
203 nsHTMLReflowMetrics childSize;
204 GetReflowAndBoundingMetricsFor(childFrame, childSize,
205 childSize.mBoundingMetrics, nsnull);
206 // compute and cache the bounding metrics
207 mBoundingMetrics += childSize.mBoundingMetrics;
209 childFrame = childFrame->GetNextSibling();
212 nsCOMPtr<nsIFontMetrics> fm =
213 PresContext()->GetMetricsFor(GetStyleFont()->mFont);
214 nscoord ascent, descent;
215 fm->GetMaxAscent(ascent);
216 fm->GetMaxDescent(descent);
218 aDesiredSize.mBoundingMetrics = mBoundingMetrics;
219 aDesiredSize.width = mBoundingMetrics.width;
220 aDesiredSize.ascent = PR_MAX(mBoundingMetrics.ascent, ascent);
221 aDesiredSize.height = aDesiredSize.ascent +
222 PR_MAX(mBoundingMetrics.descent, descent);
224 if (aPlaceOrigin) {
225 nscoord dy, dx = 0;
226 nsIFrame* childFrame = GetFirstChild(nsnull);
227 while (childFrame) {
228 nsHTMLReflowMetrics childSize;
229 GetReflowAndBoundingMetricsFor(childFrame, childSize,
230 childSize.mBoundingMetrics);
232 // place and size the child; (dx,0) makes the caret happy - bug 188146
233 dy = childSize.height == 0 ? 0 : aDesiredSize.ascent - childSize.ascent;
234 FinishReflowChild(childFrame, PresContext(), nsnull, childSize, dx, dy, 0);
235 dx += childSize.width;
236 childFrame = childFrame->GetNextSibling();
240 SetReference(nsPoint(0, aDesiredSize.ascent));
242 return NS_OK;
245 /* virtual */ void
246 nsMathMLTokenFrame::MarkIntrinsicWidthsDirty()
248 // this could be called due to changes in the nsTextFrame beneath us
249 // when something changed in the text content. So re-process our text
250 ProcessTextData();
252 nsMathMLContainerFrame::MarkIntrinsicWidthsDirty();
255 NS_IMETHODIMP
256 nsMathMLTokenFrame::AttributeChanged(PRInt32 aNameSpaceID,
257 nsIAtom* aAttribute,
258 PRInt32 aModType)
260 if (nsGkAtoms::lquote_ == aAttribute ||
261 nsGkAtoms::rquote_ == aAttribute) {
262 SetQuotes();
265 return nsMathMLContainerFrame::
266 AttributeChanged(aNameSpaceID, aAttribute, aModType);
269 void
270 nsMathMLTokenFrame::ProcessTextData()
272 // see if the style changes from normal to italic or vice-versa
273 if (!SetTextStyle())
274 return;
276 // explicitly request a re-resolve to pick up the change of style
277 PresContext()->PresShell()->FrameConstructor()->
278 PostRestyleEvent(mContent, eReStyle_Self, NS_STYLE_HINT_NONE);
281 ///////////////////////////////////////////////////////////////////////////
282 // For <mi>, if the content is not a single character, turn the font to
283 // normal (this function will also query attributes from the mstyle hierarchy)
284 // Returns PR_TRUE if there is a style change.
286 // http://www.w3.org/TR/2003/REC-MathML2-20031021/chapter3.html#presm.commatt
288 // "It is important to note that only certain combinations of
289 // character data and mathvariant attribute values make sense.
290 // ...
291 // By design, the only cases that have an unambiguous
292 // interpretation are exactly the ones that correspond to SMP Math
293 // Alphanumeric Symbol characters, which are enumerated in Section
294 // 6.2.3 Mathematical Alphanumeric Symbols Characters. In all other
295 // cases, it is suggested that renderers ignore the value of the
296 // mathvariant attribute if it is present."
298 // There are no corresponding characters for mathvariant=normal, suggesting
299 // that this value should be ignored, but this (from the same section of
300 // Chapter 3) implies that font-style should not be inherited, but set to
301 // normal for mathvariant=normal:
303 // "In particular, inheritance of the mathvariant attribute does not follow
304 // the CSS model. The default value for this attribute is "normal"
305 // (non-slanted) for all tokens except mi. ... (The deprecated fontslant
306 // attribute also behaves this way.)"
308 PRBool
309 nsMathMLTokenFrame::SetTextStyle()
311 if (mContent->Tag() != nsGkAtoms::mi_)
312 return PR_FALSE;
314 if (!mFrames.FirstChild())
315 return PR_FALSE;
317 // Get the text content that we enclose and its length
318 nsAutoString data;
319 nsContentUtils::GetNodeTextContent(mContent, PR_FALSE, data);
320 PRInt32 length = data.Length();
321 if (!length)
322 return PR_FALSE;
324 nsAutoString fontstyle;
325 PRBool isSingleCharacter =
326 length == 1 ||
327 (length == 2 && NS_IS_HIGH_SURROGATE(data[0]));
328 if (isSingleCharacter &&
329 nsMathMLOperators::LookupInvariantChar(data) != eMATHVARIANT_NONE) {
330 // bug 65951 - a non-stylable character has its own intrinsic appearance
331 fontstyle.AssignLiteral("invariant");
333 else {
334 // Attributes override the default behavior.
335 if (!(mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::mathvariant_) ||
336 mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::fontstyle_))) {
337 if (!isSingleCharacter) {
338 fontstyle.AssignLiteral("normal");
340 else if (length == 1 && // BMP
341 !nsMathMLOperators::
342 TransformVariantChar(data[0], eMATHVARIANT_italic).
343 Equals(data)) {
344 // Transformation exists. Try to make the BMP character look like the
345 // styled character using the style system until bug 114365 is resolved.
346 fontstyle.AssignLiteral("italic");
348 // else single character but there is no corresponding Math Alphanumeric
349 // Symbol character: "ignore the value of the [default] mathvariant
350 // attribute".
354 // set the -moz-math-font-style attribute without notifying that we want a reflow
355 if (fontstyle.IsEmpty()) {
356 if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::MOZfontstyle)) {
357 mContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::MOZfontstyle, PR_FALSE);
358 return PR_TRUE;
361 else if (!mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::MOZfontstyle,
362 fontstyle, eCaseMatters)) {
363 mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::MOZfontstyle,
364 fontstyle, PR_FALSE);
365 return PR_TRUE;
368 return PR_FALSE;
371 ///////////////////////////////////////////////////////////////////////////
372 // For <ms>, it is assumed that the mathml.css file contains two rules:
373 // ms:before { content: open-quote; }
374 // ms:after { content: close-quote; }
375 // With these two rules, the frame construction code will
376 // create inline frames that contain text frames which themselves
377 // contain the text content of the quotes.
378 // So the main idea in this code is to see if there are lquote and
379 // rquote attributes. If these are there, we ovewrite the default
380 // quotes in the text frames.
382 // But what if the mathml.css file wasn't loaded?
383 // We also check that we are not relying on null pointers...
385 static void
386 SetQuote(nsIFrame* aFrame,
387 nsString& aValue)
389 nsIFrame* textFrame;
390 do {
391 // walk down the hierarchy of first children because they could be wrapped
392 textFrame = aFrame->GetFirstChild(nsnull);
393 if (textFrame) {
394 if (textFrame->GetType() == nsGkAtoms::textFrame)
395 break;
397 aFrame = textFrame;
398 } while (textFrame);
399 if (textFrame) {
400 nsIContent* quoteContent = textFrame->GetContent();
401 if (quoteContent && quoteContent->IsNodeOfType(nsINode::eTEXT)) {
402 quoteContent->SetText(aValue, PR_FALSE); // no notify since we don't want a reflow yet
407 void
408 nsMathMLTokenFrame::SetQuotes()
410 if (mContent->Tag() != nsGkAtoms::ms_)
411 return;
413 nsIFrame* rightFrame = nsnull;
414 nsIFrame* baseFrame = nsnull;
415 nsIFrame* leftFrame = mFrames.FirstChild();
416 if (leftFrame)
417 baseFrame = leftFrame->GetNextSibling();
418 if (baseFrame)
419 rightFrame = baseFrame->GetNextSibling();
420 if (!leftFrame || !baseFrame || !rightFrame)
421 return;
423 nsAutoString value;
424 // lquote
425 if (GetAttribute(mContent, mPresentationData.mstyle,
426 nsGkAtoms::lquote_, value)) {
427 SetQuote(leftFrame, value);
429 // rquote
430 if (GetAttribute(mContent, mPresentationData.mstyle,
431 nsGkAtoms::rquote_, value)) {
432 SetQuote(rightFrame, value);