summaryrefslogtreecommitdiffstats
path: root/layout/style/nsStyleUtil.cpp
blob: 65ef9035fa1646a0da53aedfb354db1c7ca780fc (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "nsStyleUtil.h"
#include "nsStyleConsts.h"

#include "mozilla/ExpandedPrincipal.h"
#include "mozilla/FontPropertyTypes.h"
#include "nsIContent.h"
#include "nsCSSProps.h"
#include "nsContentUtils.h"
#include "nsROCSSPrimitiveValue.h"
#include "nsStyleStruct.h"
#include "nsIContentPolicy.h"
#include "nsIContentSecurityPolicy.h"
#include "nsLayoutUtils.h"
#include "nsPrintfCString.h"
#include <cctype>

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) !=
            char16_t('-')) {
      // 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;
}

bool nsStyleUtil::ValueIncludes(const nsAString& aValueList,
                                const nsAString& aValue,
                                const nsStringComparator& aComparator) {
  const char16_t *p = aValueList.BeginReading(),
                 *p_end = aValueList.EndReading();

  while (p < p_end) {
    // skip leading space
    while (p != p_end && nsContentUtils::IsHTMLWhitespace(*p)) ++p;

    const char16_t* val_start = p;

    // look for space or end
    while (p != p_end && !nsContentUtils::IsHTMLWhitespace(*p)) ++p;

    const char16_t* val_end = p;

    if (val_start < val_end &&
        aValue.Equals(Substring(val_start, val_end), aComparator))
      return true;

    ++p;  // we know the next character is not whitespace
  }
  return false;
}

void nsStyleUtil::AppendEscapedCSSString(const nsAString& aString,
                                         nsAString& aReturn,
                                         char16_t quoteChar) {
  MOZ_ASSERT(quoteChar == '\'' || quoteChar == '"',
             "CSS strings must be quoted with ' or \"");

  aReturn.Append(quoteChar);

  const char16_t* in = aString.BeginReading();
  const char16_t* const end = aString.EndReading();
  for (; in != end; in++) {
    if (*in < 0x20 || *in == 0x7F) {
      // Escape U+0000 through U+001F and U+007F numerically.
      aReturn.AppendPrintf("\\%x ", *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(char16_t('\\'));
      }
      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 but
  // modified for idents by
  // http://dev.w3.org/csswg/cssom/#serialize-an-identifier and
  // http://dev.w3.org/csswg/css-syntax/#would-start-an-identifier

  const char16_t* in = aIdent.BeginReading();
  const char16_t* 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 == '-') {
    if (in + 1 == end) {
      aReturn.Append(char16_t('\\'));
      aReturn.Append(char16_t('-'));
      return;
    }

    aReturn.Append(char16_t('-'));
    ++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.
  if (in != end && ('0' <= *in && *in <= '9')) {
    aReturn.AppendPrintf("\\%x ", *in);
    ++in;
  }

  for (; in != end; ++in) {
    char16_t ch = *in;
    if (ch == 0x00) {
      aReturn.Append(char16_t(0xFFFD));
    } else if (ch < 0x20 || 0x7F == ch) {
      // Escape U+0000 through U+001F and U+007F numerically.
      aReturn.AppendPrintf("\\%x ", *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(char16_t('\\'));
      }
      aReturn.Append(ch);
    }
  }
}

/* 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 */
void nsStyleUtil::GetSerializedColorValue(nscolor aColor,
                                          nsAString& aSerializedColor) {
  MOZ_ASSERT(aSerializedColor.IsEmpty());

  const bool hasAlpha = NS_GET_A(aColor) != 255;
  if (hasAlpha) {
    aSerializedColor.AppendLiteral("rgba(");
  } else {
    aSerializedColor.AppendLiteral("rgb(");
  }
  aSerializedColor.AppendInt(NS_GET_R(aColor));
  aSerializedColor.AppendLiteral(", ");
  aSerializedColor.AppendInt(NS_GET_G(aColor));
  aSerializedColor.AppendLiteral(", ");
  aSerializedColor.AppendInt(NS_GET_B(aColor));
  if (hasAlpha) {
    aSerializedColor.AppendLiteral(", ");
    float alpha = nsStyleUtil::ColorComponentToFloat(NS_GET_A(aColor));
    nsStyleUtil::AppendCSSNumber(alpha, aSerializedColor);
  }
  aSerializedColor.AppendLiteral(")");
}

/* static */
bool nsStyleUtil::IsSignificantChild(nsIContent* aChild,
                                     bool aWhitespaceIsSignificant) {
  bool isText = aChild->IsText();

  if (!isText && !aChild->IsComment() && !aChild->IsProcessingInstruction()) {
    return true;
  }

  return isText && aChild->TextLength() != 0 &&
         (aWhitespaceIsSignificant || !aChild->TextIsOnlyWhitespace());
}

/* static */
bool nsStyleUtil::ThreadSafeIsSignificantChild(const nsIContent* aChild,
                                               bool aWhitespaceIsSignificant) {
  bool isText = aChild->IsText();

  if (!isText && !aChild->IsComment() && !aChild->IsProcessingInstruction()) {
    return true;
  }

  return isText && aChild->TextLength() != 0 &&
         (aWhitespaceIsSignificant ||
          !aChild->ThreadSafeTextIsOnlyWhitespace());
}

// For a replaced element whose concrete object size is no larger than the
// element's content-box, this method checks whether the given
// "object-position" coordinate might cause overflow in its dimension.
static bool ObjectPositionCoordMightCauseOverflow(
    const LengthPercentage& aCoord) {
  // Any nonzero length in "object-position" can push us to overflow
  // (particularly if our concrete object size is exactly the same size as the
  // replaced element's content-box).
  if (!aCoord.ConvertsToPercentage()) {
    return !aCoord.ConvertsToLength() || aCoord.ToLengthInCSSPixels() != 0.0f;
  }

  // Percentages are interpreted as a fraction of the extra space. So,
  // percentages in the 0-100% range are safe, but values outside of that
  // range could cause overflow.
  float percentage = aCoord.ToPercentage();
  return percentage < 0.0f || percentage > 1.0f;
}

/* static */
bool nsStyleUtil::ObjectPropsMightCauseOverflow(
    const nsStylePosition* aStylePos) {
  auto objectFit = aStylePos->mObjectFit;

  // "object-fit: cover" & "object-fit: none" can give us a render rect that's
  // larger than our container element's content-box.
  if (objectFit == StyleObjectFit::Cover || objectFit == StyleObjectFit::None) {
    return true;
  }
  // (All other object-fit values produce a concrete object size that's no
  // larger than the constraint region.)

  // Check each of our "object-position" coords to see if it could cause
  // overflow in its dimension:
  const Position& objectPosistion = aStylePos->mObjectPosition;
  if (ObjectPositionCoordMightCauseOverflow(objectPosistion.horizontal) ||
      ObjectPositionCoordMightCauseOverflow(objectPosistion.vertical)) {
    return true;
  }

  return false;
}

/* static */
bool nsStyleUtil::CSPAllowsInlineStyle(
    dom::Element* aElement, dom::Document* aDocument,
    nsIPrincipal* aTriggeringPrincipal, uint32_t aLineNumber,
    uint32_t aColumnNumber, const nsAString& aStyleText, nsresult* aRv) {
  nsresult rv;

  if (aRv) {
    *aRv = NS_OK;
  }

  nsCOMPtr<nsIContentSecurityPolicy> csp;
  if (aTriggeringPrincipal && BasePrincipal::Cast(aTriggeringPrincipal)
                                  ->OverridesCSP(aDocument->NodePrincipal())) {
    nsCOMPtr<nsIExpandedPrincipal> ep = do_QueryInterface(aTriggeringPrincipal);
    if (ep) {
      csp = ep->GetCsp();
    }
  } else {
    csp = aDocument->GetCsp();
  }

  if (!csp) {
    // No CSP --> the style is allowed
    return true;
  }

  // Hack to allow Devtools to edit inline styles
  if (csp->GetSkipAllowInlineStyleCheck()) {
    return true;
  }

  // query the nonce
  nsAutoString nonce;
  if (aElement && aElement->NodeInfo()->NameAtom() == nsGkAtoms::style) {
    nsString* cspNonce =
        static_cast<nsString*>(aElement->GetProperty(nsGkAtoms::nonce));
    if (cspNonce) {
      nonce = *cspNonce;
    }
  }

  bool allowInlineStyle = true;
  rv = csp->GetAllowsInline(
      nsIContentSecurityPolicy::STYLE_SRC_DIRECTIVE, nonce,
      false,              // aParserCreated only applies to scripts
      aElement, nullptr,  // nsICSPEventListener
      aStyleText, aLineNumber, aColumnNumber, &allowInlineStyle);
  NS_ENSURE_SUCCESS(rv, false);

  return allowInlineStyle;
}

void nsStyleUtil::AppendFontSlantStyle(const FontSlantStyle& aStyle,
                                       nsAString& aOut) {
  if (aStyle.IsNormal()) {
    aOut.AppendLiteral("normal");
  } else if (aStyle.IsItalic()) {
    aOut.AppendLiteral("italic");
  } else {
    aOut.AppendLiteral("oblique");
    auto angle = aStyle.ObliqueAngle();
    if (angle != FontSlantStyle::kDefaultAngle) {
      aOut.AppendLiteral(" ");
      AppendCSSNumber(angle, aOut);
      aOut.AppendLiteral("deg");
    }
  }
}