forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsStyleUtil.cpp
254 lines (223 loc) · 7.95 KB
/
nsStyleUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <math.h>
#include "mozilla/Util.h"
#include "nsStyleUtil.h"
#include "nsCRT.h"
#include "nsStyleConsts.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
#include "nsINameSpaceManager.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsReadableUtils.h"
#include "nsTextFormatter.h"
#include "nsCSSProps.h"
#include "nsRuleNode.h"
using namespace mozilla;
//------------------------------------------------------------------------------
// Font Algorithm Code
//------------------------------------------------------------------------------
// Compare two language strings
bool nsStyleUtil::DashMatchCompare(const nsAString& aAttributeValue,
const nsAString& aSelectorValue,
const nsStringComparator& aComparator)
{
bool result;
uint32_t selectorLen = aSelectorValue.Length();
uint32_t attributeLen = aAttributeValue.Length();
if (selectorLen > attributeLen) {
result = false;
}
else {
nsAString::const_iterator iter;
if (selectorLen != attributeLen &&
*aAttributeValue.BeginReading(iter).advance(selectorLen) !=
PRUnichar('-')) {
// to match, the aAttributeValue must have a dash after the end of
// the aSelectorValue's text (unless the aSelectorValue and the
// aAttributeValue have the same text)
result = false;
}
else {
result = StringBeginsWith(aAttributeValue, aSelectorValue, aComparator);
}
}
return result;
}
void nsStyleUtil::AppendEscapedCSSString(const nsAString& aString,
nsAString& aReturn,
PRUnichar quoteChar)
{
NS_PRECONDITION(quoteChar == '\'' || quoteChar == '"',
"CSS strings must be quoted with ' or \"");
aReturn.Append(quoteChar);
const PRUnichar* in = aString.BeginReading();
const PRUnichar* const end = aString.EndReading();
for (; in != end; in++) {
if (*in < 0x20 || (*in >= 0x7F && *in < 0xA0)) {
// Escape U+0000 through U+001F and U+007F through U+009F numerically.
aReturn.AppendPrintf("\\%hX ", *in);
} else {
if (*in == '"' || *in == '\'' || *in == '\\') {
// Escape backslash and quote characters symbolically.
// It's not technically necessary to escape the quote
// character that isn't being used to delimit the string,
// but we do it anyway because that makes testing simpler.
aReturn.Append(PRUnichar('\\'));
}
aReturn.Append(*in);
}
}
aReturn.Append(quoteChar);
}
/* static */ void
nsStyleUtil::AppendEscapedCSSIdent(const nsAString& aIdent, nsAString& aReturn)
{
// The relevant parts of the CSS grammar are:
// ident [-]?{nmstart}{nmchar}*
// nmstart [_a-z]|{nonascii}|{escape}
// nmchar [_a-z0-9-]|{nonascii}|{escape}
// nonascii [^\0-\177]
// escape {unicode}|\\[^\n\r\f0-9a-f]
// unicode \\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?
// from http://www.w3.org/TR/CSS21/syndata.html#tokenization
const PRUnichar* in = aIdent.BeginReading();
const PRUnichar* const end = aIdent.EndReading();
if (in == end)
return;
// A leading dash does not need to be escaped as long as it is not the
// *only* character in the identifier.
if (in + 1 != end && *in == '-') {
aReturn.Append(PRUnichar('-'));
++in;
}
// Escape a digit at the start (including after a dash),
// numerically. If we didn't escape it numerically, it would get
// interpreted as a numeric escape for the wrong character.
// A second dash immediately after a leading dash must also be
// escaped, but this may be done symbolically.
if (in != end && (*in == '-' ||
('0' <= *in && *in <= '9'))) {
if (*in == '-') {
aReturn.Append(PRUnichar('\\'));
aReturn.Append(PRUnichar('-'));
} else {
aReturn.AppendPrintf("\\%hX ", *in);
}
++in;
}
for (; in != end; ++in) {
PRUnichar ch = *in;
if (ch < 0x20 || (0x7F <= ch && ch < 0xA0)) {
// Escape U+0000 through U+001F and U+007F through U+009F numerically.
aReturn.AppendPrintf("\\%hX ", *in);
} else {
// Escape ASCII non-identifier printables as a backslash plus
// the character.
if (ch < 0x7F &&
ch != '_' && ch != '-' &&
(ch < '0' || '9' < ch) &&
(ch < 'A' || 'Z' < ch) &&
(ch < 'a' || 'z' < ch)) {
aReturn.Append(PRUnichar('\\'));
}
aReturn.Append(ch);
}
}
}
/* static */ void
nsStyleUtil::AppendBitmaskCSSValue(nsCSSProperty aProperty,
int32_t aMaskedValue,
int32_t aFirstMask,
int32_t aLastMask,
nsAString& aResult)
{
for (int32_t mask = aFirstMask; mask <= aLastMask; mask <<= 1) {
if (mask & aMaskedValue) {
AppendASCIItoUTF16(nsCSSProps::LookupPropertyValue(aProperty, mask),
aResult);
aMaskedValue &= ~mask;
if (aMaskedValue) { // more left
aResult.Append(PRUnichar(' '));
}
}
}
NS_ABORT_IF_FALSE(aMaskedValue == 0, "unexpected bit remaining in bitfield");
}
/* static */ void
nsStyleUtil::AppendFontFeatureSettings(const nsTArray<gfxFontFeature>& aFeatures,
nsAString& aResult)
{
for (uint32_t i = 0, numFeat = aFeatures.Length(); i < numFeat; i++) {
const gfxFontFeature& feat = aFeatures[i];
if (i != 0) {
aResult.AppendLiteral(", ");
}
// output tag
char tag[7];
tag[0] = '"';
tag[1] = (feat.mTag >> 24) & 0xff;
tag[2] = (feat.mTag >> 16) & 0xff;
tag[3] = (feat.mTag >> 8) & 0xff;
tag[4] = feat.mTag & 0xff;
tag[5] = '"';
tag[6] = 0;
aResult.AppendASCII(tag);
// output value, if necessary
if (feat.mValue == 0) {
// 0 ==> off
aResult.AppendLiteral(" off");
} else if (feat.mValue > 1) {
aResult.AppendLiteral(" ");
aResult.AppendInt(feat.mValue);
}
// else, omit value if 1, implied by default
}
}
/* static */ void
nsStyleUtil::AppendFontFeatureSettings(const nsCSSValue& aSrc,
nsAString& aResult)
{
nsCSSUnit unit = aSrc.GetUnit();
if (unit == eCSSUnit_Normal) {
aResult.AppendLiteral("normal");
return;
}
NS_PRECONDITION(unit == eCSSUnit_PairList || unit == eCSSUnit_PairListDep,
"improper value unit for font-feature-settings:");
nsTArray<gfxFontFeature> featureSettings;
nsRuleNode::ComputeFontFeatures(aSrc.GetPairListValue(), featureSettings);
AppendFontFeatureSettings(featureSettings, aResult);
}
/* static */ float
nsStyleUtil::ColorComponentToFloat(uint8_t aAlpha)
{
// Alpha values are expressed as decimals, so we should convert
// back, using as few decimal places as possible for
// round-tripping.
// First try two decimal places:
float rounded = NS_roundf(float(aAlpha) * 100.0f / 255.0f) / 100.0f;
if (FloatToColorComponent(rounded) != aAlpha) {
// Use three decimal places.
rounded = NS_roundf(float(aAlpha) * 1000.0f / 255.0f) / 1000.0f;
}
return rounded;
}
/* static */ bool
nsStyleUtil::IsSignificantChild(nsIContent* aChild, bool aTextIsSignificant,
bool aWhitespaceIsSignificant)
{
NS_ASSERTION(!aWhitespaceIsSignificant || aTextIsSignificant,
"Nonsensical arguments");
bool isText = aChild->IsNodeOfType(nsINode::eTEXT);
if (!isText && !aChild->IsNodeOfType(nsINode::eCOMMENT) &&
!aChild->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION)) {
return true;
}
return aTextIsSignificant && isText && aChild->TextLength() != 0 &&
(aWhitespaceIsSignificant ||
!aChild->TextIsOnlyWhitespace());
}