2001-09-25 05:32:19 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1998-07-23 22:04:24 +04:00
|
|
|
|
2000-02-24 15:51:28 +03:00
|
|
|
#include <math.h>
|
2011-10-11 09:50:08 +04:00
|
|
|
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
1998-07-23 22:04:24 +04:00
|
|
|
#include "nsStyleUtil.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsStyleConsts.h"
|
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2007-02-09 03:15:14 +03:00
|
|
|
#include "nsIContent.h"
|
2000-07-28 03:17:53 +04:00
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetUtil.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2005-01-30 21:01:57 +03:00
|
|
|
#include "nsTextFormatter.h"
|
2009-10-22 01:57:57 +04:00
|
|
|
#include "nsCSSProps.h"
|
2012-04-26 10:24:26 +04:00
|
|
|
#include "nsRuleNode.h"
|
2000-02-24 15:51:28 +03:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2000-02-24 15:51:28 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2002-12-11 02:44:03 +03:00
|
|
|
// Font Algorithm Code
|
2000-02-24 15:51:28 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2004-08-24 01:10:39 +04:00
|
|
|
// Compare two language strings
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsStyleUtil::DashMatchCompare(const nsAString& aAttributeValue,
|
2004-08-24 01:10:39 +04:00
|
|
|
const nsAString& aSelectorValue,
|
|
|
|
const nsStringComparator& aComparator)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool result;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t selectorLen = aSelectorValue.Length();
|
|
|
|
uint32_t attributeLen = aAttributeValue.Length();
|
2004-08-24 01:10:39 +04:00
|
|
|
if (selectorLen > attributeLen) {
|
2011-10-17 18:59:28 +04:00
|
|
|
result = false;
|
2004-08-24 01:10:39 +04:00
|
|
|
}
|
|
|
|
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)
|
2011-10-17 18:59:28 +04:00
|
|
|
result = false;
|
2004-08-24 01:10:39 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = StringBeginsWith(aAttributeValue, aSelectorValue, aComparator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2005-01-30 21:01:57 +03:00
|
|
|
|
2012-11-17 01:59:38 +04:00
|
|
|
void nsStyleUtil::AppendEscapedCSSString(const nsString& aString,
|
|
|
|
nsAString& aReturn)
|
2005-01-30 21:01:57 +03:00
|
|
|
{
|
2012-11-17 01:59:38 +04:00
|
|
|
aReturn.Append(PRUnichar('"'));
|
|
|
|
|
|
|
|
const nsString::char_type* in = aString.get();
|
|
|
|
const nsString::char_type* const end = in + aString.Length();
|
|
|
|
for (; in != end; in++)
|
|
|
|
{
|
|
|
|
if (*in < 0x20)
|
|
|
|
{
|
|
|
|
// Escape all characters below 0x20 numerically.
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is the buffer into which snprintf should write. As the hex. value is,
|
|
|
|
for numbers below 0x20, max. 2 characters long, we don't need more than 5
|
|
|
|
characters ("\XX "+NUL).
|
|
|
|
*/
|
|
|
|
PRUnichar buf[5];
|
|
|
|
nsTextFormatter::snprintf(buf, ArrayLength(buf), NS_LITERAL_STRING("\\%hX ").get(), *in);
|
|
|
|
aReturn.Append(buf);
|
|
|
|
|
|
|
|
} else switch (*in) {
|
|
|
|
// Special characters which should be escaped: Quotes and backslash
|
|
|
|
case '\\':
|
|
|
|
case '\"':
|
|
|
|
case '\'':
|
|
|
|
aReturn.Append(PRUnichar('\\'));
|
|
|
|
// And now, after the eventual escaping character, the actual one.
|
|
|
|
default:
|
|
|
|
aReturn.Append(PRUnichar(*in));
|
2005-01-30 21:01:57 +03:00
|
|
|
}
|
|
|
|
}
|
2009-03-06 07:05:00 +03:00
|
|
|
|
2012-11-17 01:59:38 +04:00
|
|
|
aReturn.Append(PRUnichar('"'));
|
2005-01-30 21:01:57 +03:00
|
|
|
}
|
2007-03-08 21:44:45 +03:00
|
|
|
|
2010-02-04 23:49:29 +03:00
|
|
|
/* static */ void
|
2012-11-17 01:59:38 +04:00
|
|
|
nsStyleUtil::AppendEscapedCSSIdent(const nsString& aIdent, nsAString& aReturn)
|
2010-02-04 23:49:29 +03:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
|
2012-11-17 01:59:38 +04:00
|
|
|
const nsString::char_type* in = aIdent.get();
|
|
|
|
const nsString::char_type* const end = in + aIdent.Length();
|
2010-02-04 23:49:29 +03:00
|
|
|
|
2012-11-17 01:59:38 +04:00
|
|
|
// Deal with the leading dash separately so we don't need to
|
|
|
|
// unnecessarily escape digits.
|
|
|
|
if (in != end && *in == '-') {
|
2010-02-04 23:49:29 +03:00
|
|
|
aReturn.Append(PRUnichar('-'));
|
|
|
|
++in;
|
|
|
|
}
|
|
|
|
|
2012-11-17 01:59:38 +04:00
|
|
|
bool first = true;
|
|
|
|
for (; in != end; ++in, first = false)
|
|
|
|
{
|
|
|
|
if (*in < 0x20 || (first && '0' <= *in && *in <= '9'))
|
|
|
|
{
|
|
|
|
// Escape all characters below 0x20, and digits at the start
|
|
|
|
// (including after a dash), numerically. If we didn't escape
|
|
|
|
// digits numerically, they'd get interpreted as a numeric escape
|
|
|
|
// for the wrong character.
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is the buffer into which snprintf should write. As the hex.
|
|
|
|
value is, for numbers below 0x7F, max. 2 characters long, we
|
|
|
|
don't need more than 5 characters ("\XX "+NUL).
|
|
|
|
*/
|
|
|
|
PRUnichar buf[5];
|
|
|
|
nsTextFormatter::snprintf(buf, ArrayLength(buf),
|
|
|
|
NS_LITERAL_STRING("\\%hX ").get(), *in);
|
|
|
|
aReturn.Append(buf);
|
2010-02-04 23:49:29 +03:00
|
|
|
} else {
|
2012-11-17 01:59:38 +04:00
|
|
|
PRUnichar ch = *in;
|
|
|
|
if (!((ch == PRUnichar('_')) ||
|
|
|
|
(PRUnichar('A') <= ch && ch <= PRUnichar('Z')) ||
|
|
|
|
(PRUnichar('a') <= ch && ch <= PRUnichar('z')) ||
|
|
|
|
PRUnichar(0x80) <= ch ||
|
|
|
|
(!first && ch == PRUnichar('-')) ||
|
|
|
|
(PRUnichar('0') <= ch && ch <= PRUnichar('9')))) {
|
|
|
|
// Character needs to be escaped
|
2010-02-04 23:49:29 +03:00
|
|
|
aReturn.Append(PRUnichar('\\'));
|
|
|
|
}
|
|
|
|
aReturn.Append(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-22 01:57:57 +04:00
|
|
|
/* static */ void
|
|
|
|
nsStyleUtil::AppendBitmaskCSSValue(nsCSSProperty aProperty,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aMaskedValue,
|
|
|
|
int32_t aFirstMask,
|
|
|
|
int32_t aLastMask,
|
2009-10-22 01:57:57 +04:00
|
|
|
nsAString& aResult)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t mask = aFirstMask; mask <= aLastMask; mask <<= 1) {
|
2009-10-22 01:57:57 +04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2012-04-26 10:24:26 +04:00
|
|
|
/* static */ void
|
|
|
|
nsStyleUtil::AppendFontFeatureSettings(const nsTArray<gfxFontFeature>& aFeatures,
|
|
|
|
nsAString& aResult)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0, numFeat = aFeatures.Length(); i < numFeat; i++) {
|
2012-04-26 10:24:26 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-03-08 21:44:45 +03:00
|
|
|
/* static */ float
|
2012-08-22 19:56:38 +04:00
|
|
|
nsStyleUtil::ColorComponentToFloat(uint8_t aAlpha)
|
2007-03-08 21:44:45 +03:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
2008-02-19 09:17:07 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* static */ bool
|
|
|
|
nsStyleUtil::IsSignificantChild(nsIContent* aChild, bool aTextIsSignificant,
|
|
|
|
bool aWhitespaceIsSignificant)
|
2008-02-19 09:17:07 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aWhitespaceIsSignificant || aTextIsSignificant,
|
|
|
|
"Nonsensical arguments");
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isText = aChild->IsNodeOfType(nsINode::eTEXT);
|
2008-02-19 09:17:07 +03:00
|
|
|
|
|
|
|
if (!isText && !aChild->IsNodeOfType(nsINode::eCOMMENT) &&
|
|
|
|
!aChild->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-02-19 09:17:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return aTextIsSignificant && isText && aChild->TextLength() != 0 &&
|
|
|
|
(aWhitespaceIsSignificant ||
|
|
|
|
!aChild->TextIsOnlyWhitespace());
|
|
|
|
}
|
|
|
|
|