|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set expandtab shiftwidth=2 tabstop=2: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "StyleInfo.h" |
|
8 |
|
9 #include "mozilla/dom/Element.h" |
|
10 #include "nsComputedDOMStyle.h" |
|
11 #include "nsCSSProps.h" |
|
12 #include "nsIFrame.h" |
|
13 |
|
14 using namespace mozilla; |
|
15 using namespace mozilla::a11y; |
|
16 |
|
17 StyleInfo::StyleInfo(dom::Element* aElement, nsIPresShell* aPresShell) : |
|
18 mElement(aElement) |
|
19 { |
|
20 mStyleContext = |
|
21 nsComputedDOMStyle::GetStyleContextForElementNoFlush(aElement, |
|
22 nullptr, |
|
23 aPresShell); |
|
24 } |
|
25 |
|
26 void |
|
27 StyleInfo::Display(nsAString& aValue) |
|
28 { |
|
29 aValue.Truncate(); |
|
30 AppendASCIItoUTF16( |
|
31 nsCSSProps::ValueToKeyword(mStyleContext->StyleDisplay()->mDisplay, |
|
32 nsCSSProps::kDisplayKTable), aValue); |
|
33 } |
|
34 |
|
35 void |
|
36 StyleInfo::TextAlign(nsAString& aValue) |
|
37 { |
|
38 aValue.Truncate(); |
|
39 AppendASCIItoUTF16( |
|
40 nsCSSProps::ValueToKeyword(mStyleContext->StyleText()->mTextAlign, |
|
41 nsCSSProps::kTextAlignKTable), aValue); |
|
42 } |
|
43 |
|
44 void |
|
45 StyleInfo::TextIndent(nsAString& aValue) |
|
46 { |
|
47 aValue.Truncate(); |
|
48 |
|
49 const nsStyleCoord& styleCoord = |
|
50 mStyleContext->StyleText()->mTextIndent; |
|
51 |
|
52 nscoord coordVal = 0; |
|
53 switch (styleCoord.GetUnit()) { |
|
54 case eStyleUnit_Coord: |
|
55 coordVal = styleCoord.GetCoordValue(); |
|
56 break; |
|
57 |
|
58 case eStyleUnit_Percent: |
|
59 { |
|
60 nsIFrame* frame = mElement->GetPrimaryFrame(); |
|
61 nsIFrame* containerFrame = frame->GetContainingBlock(); |
|
62 nscoord percentageBase = containerFrame->GetContentRect().width; |
|
63 coordVal = NSCoordSaturatingMultiply(percentageBase, |
|
64 styleCoord.GetPercentValue()); |
|
65 break; |
|
66 } |
|
67 |
|
68 case eStyleUnit_Null: |
|
69 case eStyleUnit_Normal: |
|
70 case eStyleUnit_Auto: |
|
71 case eStyleUnit_None: |
|
72 case eStyleUnit_Factor: |
|
73 case eStyleUnit_Degree: |
|
74 case eStyleUnit_Grad: |
|
75 case eStyleUnit_Radian: |
|
76 case eStyleUnit_Turn: |
|
77 case eStyleUnit_FlexFraction: |
|
78 case eStyleUnit_Integer: |
|
79 case eStyleUnit_Enumerated: |
|
80 case eStyleUnit_Calc: |
|
81 break; |
|
82 } |
|
83 |
|
84 aValue.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(coordVal)); |
|
85 aValue.AppendLiteral("px"); |
|
86 } |
|
87 |
|
88 void |
|
89 StyleInfo::Margin(css::Side aSide, nsAString& aValue) |
|
90 { |
|
91 aValue.Truncate(); |
|
92 |
|
93 nscoord coordVal = mElement->GetPrimaryFrame()->GetUsedMargin().Side(aSide); |
|
94 aValue.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(coordVal)); |
|
95 aValue.AppendLiteral("px"); |
|
96 } |
|
97 |
|
98 void |
|
99 StyleInfo::FormatColor(const nscolor& aValue, nsString& aFormattedValue) |
|
100 { |
|
101 // Combine the string like rgb(R, G, B) from nscolor. |
|
102 aFormattedValue.AppendLiteral("rgb("); |
|
103 aFormattedValue.AppendInt(NS_GET_R(aValue)); |
|
104 aFormattedValue.AppendLiteral(", "); |
|
105 aFormattedValue.AppendInt(NS_GET_G(aValue)); |
|
106 aFormattedValue.AppendLiteral(", "); |
|
107 aFormattedValue.AppendInt(NS_GET_B(aValue)); |
|
108 aFormattedValue.Append(')'); |
|
109 } |
|
110 |
|
111 void |
|
112 StyleInfo::FormatFontStyle(const nscoord& aValue, nsAString& aFormattedValue) |
|
113 { |
|
114 nsCSSKeyword keyword = |
|
115 nsCSSProps::ValueToKeywordEnum(aValue, nsCSSProps::kFontStyleKTable); |
|
116 AppendUTF8toUTF16(nsCSSKeywords::GetStringValue(keyword), aFormattedValue); |
|
117 } |
|
118 |
|
119 void |
|
120 StyleInfo::FormatTextDecorationStyle(uint8_t aValue, nsAString& aFormattedValue) |
|
121 { |
|
122 nsCSSKeyword keyword = |
|
123 nsCSSProps::ValueToKeywordEnum(aValue, |
|
124 nsCSSProps::kTextDecorationStyleKTable); |
|
125 AppendUTF8toUTF16(nsCSSKeywords::GetStringValue(keyword), aFormattedValue); |
|
126 } |