Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
nsCSSLoader.cpp File Reference
#include "nsCSSLoader.h"
#include "nsIContent.h"
#include "nsIDOMNode.h"
#include "nsIDOMWindow.h"
#include "nsIDocument.h"
#include "nsIUnicharInputStream.h"
#include "nsIConverterInputStream.h"
#include "nsICharsetAlias.h"
#include "nsUnicharUtils.h"
#include "nsHashtable.h"
#include "nsIURI.h"
#include "nsIURL.h"
#include "nsIParser.h"
#include "nsIServiceManager.h"
#include "nsNetUtil.h"
#include "nsContentUtils.h"
#include "nsCRT.h"
#include "nsIScriptSecurityManager.h"
#include "nsContentPolicyUtils.h"
#include "nsIScriptGlobalObject.h"
#include "nsITimelineService.h"
#include "nsIHttpChannel.h"
#include "nsIScriptError.h"
#include "nsMimeTypes.h"
#include "nsIAtom.h"
#include "nsIDOM3Node.h"
#include "nsICSSStyleSheet.h"
#include "nsIStyleSheetLinkingElement.h"
#include "nsICSSLoaderObserver.h"
#include "nsICSSLoader.h"
#include "nsICSSParser.h"
#include "nsICSSImportRule.h"
#include "nsIMediaList.h"
#include "nsIDOMStyleSheet.h"
#include "nsIDOMCSSStyleSheet.h"
#include "nsIDOMCSSImportRule.h"
#include "prlog.h"

Go to the source code of this file.

Defines

#define LOG_FORCE(args)   PR_LOG(gLoaderLog, PR_LOG_ALWAYS, args)
#define LOG_ERROR(args)   PR_LOG(gLoaderLog, PR_LOG_ERROR, args)
#define LOG_WARN(args)   PR_LOG(gLoaderLog, PR_LOG_WARNING, args)
#define LOG_DEBUG(args)   PR_LOG(gLoaderLog, PR_LOG_DEBUG, args)
#define LOG(args)   LOG_DEBUG(args)
#define LOG_FORCE_ENABLED()   PR_LOG_TEST(gLoaderLog, PR_LOG_ALWAYS)
#define LOG_ERROR_ENABLED()   PR_LOG_TEST(gLoaderLog, PR_LOG_ERROR)
#define LOG_WARN_ENABLED()   PR_LOG_TEST(gLoaderLog, PR_LOG_WARNING)
#define LOG_DEBUG_ENABLED()   PR_LOG_TEST(gLoaderLog, PR_LOG_DEBUG)
#define LOG_ENABLED()   LOG_DEBUG_ENABLED()
#define LOG_URI(format, uri)

Functions

 StartAlternateLoads (nsIURI *aKey, SheetLoadData *&aData, void *aClosure)
 StartNonAlternates (nsIURI *aKey, SheetLoadData *&aData, void *aClosure)
static nsresult GetCharsetFromData (const unsigned char *aStyleSheetData, PRUint32 aDataLength, nsACString &aCharset)
nsresult NS_NewCSSLoader (nsIDocument *aDocument, nsICSSLoader **aLoader)
nsresult NS_NewCSSLoader (nsICSSLoader **aLoader)
 StopLoadingSheetCallback (nsIURI *aKey, SheetLoadData *&aData, void *aClosure)

Variables

static const char kCharsetSym [] = "@charset"

Define Documentation

#define LOG (   args)    LOG_DEBUG(args)

Definition at line 102 of file nsCSSLoader.cpp.

#define LOG_DEBUG (   args)    PR_LOG(gLoaderLog, PR_LOG_DEBUG, args)

Definition at line 101 of file nsCSSLoader.cpp.

Definition at line 107 of file nsCSSLoader.cpp.

Definition at line 108 of file nsCSSLoader.cpp.

#define LOG_ERROR (   args)    PR_LOG(gLoaderLog, PR_LOG_ERROR, args)

Definition at line 99 of file nsCSSLoader.cpp.

Definition at line 105 of file nsCSSLoader.cpp.

#define LOG_FORCE (   args)    PR_LOG(gLoaderLog, PR_LOG_ALWAYS, args)

Definition at line 98 of file nsCSSLoader.cpp.

Definition at line 104 of file nsCSSLoader.cpp.

#define LOG_URI (   format,
  uri 
)

Definition at line 121 of file nsCSSLoader.cpp.

#define LOG_WARN (   args)    PR_LOG(gLoaderLog, PR_LOG_WARNING, args)

Definition at line 100 of file nsCSSLoader.cpp.

Definition at line 106 of file nsCSSLoader.cpp.


Function Documentation

static nsresult GetCharsetFromData ( const unsigned char *  aStyleSheetData,
PRUint32  aDataLength,
nsACString &  aCharset 
) [static]

Definition at line 400 of file nsCSSLoader.cpp.

{
  aCharset.Truncate();
  if (aDataLength <= sizeof(kCharsetSym) - 1)
    return NS_ERROR_NOT_AVAILABLE;
  PRUint32 step = 1;
  PRUint32 pos = 0;
  // Determine the encoding type.  If we have a BOM, set aCharset to the
  // charset listed for that BOM in http://www.w3.org/TR/REC-xml#sec-guessing;
  // that way even if we don't have a valid @charset rule we can use the BOM to
  // get a reasonable charset.  If we do have an @charset rule, the string from
  // that will override this fallback setting of aCharset.
  if (*aStyleSheetData == 0x40 && *(aStyleSheetData+1) == 0x63 /* '@c' */ ) {
    // 1-byte ASCII-based encoding (ISO-8859-*, UTF-8, etc), no BOM
    step = 1;
    pos = 0;
  }
  else if (aStyleSheetData[0] == 0xEF &&
           aStyleSheetData[1] == 0xBB &&
           aStyleSheetData[2] == 0xBF) {
    // UTF-8 BOM
    step = 1;
    pos = 3;
    aCharset = "UTF-8";
  }
  // Check for a 4-byte encoding BOM before checking for a 2-byte one,
  // since the latter can be a proper subset of the former.
  else if (aStyleSheetData[0] == 0x00 &&
           aStyleSheetData[1] == 0x00 &&
           aStyleSheetData[2] == 0xFE &&
           aStyleSheetData[3] == 0xFF) {
    // big-endian 4-byte encoding BOM
    step = 4;
    pos = 7;
    aCharset = "UTF-32BE";
  }
  else if (aStyleSheetData[0] == 0xFF &&
           aStyleSheetData[1] == 0xFE &&
           aStyleSheetData[2] == 0x00 &&
           aStyleSheetData[3] == 0x00) {
    // little-endian 4-byte encoding BOM
    step = 4;
    pos = 4;
    aCharset = "UTF-32LE";
  }
  else if (aStyleSheetData[0] == 0x00 &&
           aStyleSheetData[1] == 0x00 &&
           aStyleSheetData[2] == 0xFF &&
           aStyleSheetData[3] == 0xFE) {
    // 4-byte encoding BOM in 2143 order
    NS_WARNING("Our unicode decoders aren't likely  to deal with this one");
    step = 4;
    pos = 6;
    aCharset = "UTF-32";
  }
  else if (aStyleSheetData[0] == 0xFE &&
           aStyleSheetData[1] == 0xFF &&
           aStyleSheetData[2] == 0x00 &&
           aStyleSheetData[3] == 0x00) {
    // 4-byte encoding BOM in 3412 order
    NS_WARNING("Our unicode decoders aren't likely  to deal with this one");
    step = 4;
    pos = 5;
    aCharset = "UTF-32";
  }
  else if (aStyleSheetData[0] == 0xFE && aStyleSheetData[1] == 0xFF) {
    // big-endian 2-byte encoding BOM
    step = 2;
    pos = 3;
    aCharset = "UTF-16BE";
  }
  else if (aStyleSheetData[0] == 0xFF && aStyleSheetData[1] == 0xFE) {
    // little-endian 2-byte encoding BOM
    step = 2;
    pos = 2;
    aCharset = "UTF-16LE";
  }
  else if (aStyleSheetData[0] == 0x00 &&
           aStyleSheetData[1] == 0x00 &&
           aStyleSheetData[2] == 0x00 &&
           aStyleSheetData[3] == 0x40) {
    // big-endian 4-byte encoding, no BOM
    step = 4;
    pos = 3;
  }
  else if (aStyleSheetData[0] == 0x40 &&
           aStyleSheetData[1] == 0x00 &&
           aStyleSheetData[2] == 0x00 &&
           aStyleSheetData[3] == 0x00) {
    // little-endian 4-byte encoding, no BOM
    step = 4;
    pos = 0;
  }
  else if (aStyleSheetData[0] == 0x00 &&
           aStyleSheetData[1] == 0x00 &&
           aStyleSheetData[2] == 0x40 &&
           aStyleSheetData[3] == 0x00) {
    // 4-byte encoding in 2143 order, no BOM
    step = 4;
    pos = 2;
  }
  else if (aStyleSheetData[0] == 0x00 &&
           aStyleSheetData[1] == 0x40 &&
           aStyleSheetData[2] == 0x00 &&
           aStyleSheetData[3] == 0x00) {
    // 4-byte encoding in 3412 order, no BOM
    step = 4;
    pos = 1;
  }
  else if (aStyleSheetData[0] == 0x00 &&
           aStyleSheetData[1] == 0x40 &&
           aStyleSheetData[2] == 0x00 &&
           aStyleSheetData[3] == 0x63) {
    // 2-byte big-endian encoding, no BOM
    step = 2;
    pos = 1;
  }
  else if (aStyleSheetData[0] == 0x40 &&
           aStyleSheetData[1] == 0x00 &&
           aStyleSheetData[2] == 0x63 &&
           aStyleSheetData[3] == 0x00) {
    // 2-byte little-endian encoding, no BOM
    step = 2;
    pos = 0;
  }
  else {
    // no clue what this is
    return NS_ERROR_UNEXPECTED;
  }

  PRUint32 index = 0;
  while (pos < aDataLength && index < sizeof(kCharsetSym) - 1) {
    if (aStyleSheetData[pos] != kCharsetSym[index]) {
      // If we have a guess as to the charset based on the BOM, then
      // we can just return NS_OK even if there is no valid @charset
      // rule.
      return aCharset.IsEmpty() ? NS_ERROR_NOT_AVAILABLE : NS_OK;
    }
    ++index;
    pos += step;
  }

  while (pos < aDataLength && nsCRT::IsAsciiSpace(aStyleSheetData[pos])) {
    pos += step;
  }

  if (pos >= aDataLength ||
      (aStyleSheetData[pos] != '"' && aStyleSheetData[pos] != '\'')) {
    return aCharset.IsEmpty() ? NS_ERROR_NOT_AVAILABLE : NS_OK;
  }

  char quote = aStyleSheetData[pos];
  pos += step;
  nsCAutoString charset;
  while (pos < aDataLength) {
    if (aStyleSheetData[pos] == '\\') {
      pos += step;
      if (pos >= aDataLength) {
        break;
      }          
    } else if (aStyleSheetData[pos] == quote) {
      break;
    }
    
    // casting to avoid ambiguities
    charset.Append(char(aStyleSheetData[pos]));
    pos += step;
  }

  // Check for the ending ';'
  pos += step;
  while (pos < aDataLength && nsCRT::IsAsciiSpace(aStyleSheetData[pos])) {
    pos += step;    
  }

  if (pos >= aDataLength || aStyleSheetData[pos] != ';') {
    return aCharset.IsEmpty() ? NS_ERROR_NOT_AVAILABLE : NS_OK;
  }

  aCharset = charset;
  return NS_OK;
}
nsresult NS_NewCSSLoader ( nsIDocument aDocument,
nsICSSLoader **  aLoader 
)

Definition at line 1883 of file nsCSSLoader.cpp.

{
  CSSLoaderImpl* it = new CSSLoaderImpl();

  NS_ENSURE_TRUE(it, NS_ERROR_OUT_OF_MEMORY);

  it->Init(aDocument);
  return CallQueryInterface(it, aLoader);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1893 of file nsCSSLoader.cpp.

Here is the call graph for this function:

StartAlternateLoads ( nsIURI aKey,
SheetLoadData *&  aData,
void aClosure 
)

Definition at line 281 of file nsCSSLoader.cpp.

Here is the caller graph for this function:

StartNonAlternates ( nsIURI aKey,
SheetLoadData *&  aData,
void aClosure 
)

Definition at line 314 of file nsCSSLoader.cpp.

{
  NS_PRECONDITION(aData, "Must have a data");
  NS_PRECONDITION(aClosure, "Must have a loader");
  
  CSSLoaderImpl* loader = NS_STATIC_CAST(CSSLoaderImpl*, aClosure);
  if (loader->IsAlternate(aData->mTitle)) {
    return PL_DHASH_NEXT;
  }

  // Need to start the load
  loader->LoadSheet(aData, eSheetNeedsParser);
  return PL_DHASH_REMOVE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

StopLoadingSheetCallback ( nsIURI aKey,
SheetLoadData *&  aData,
void aClosure 
)

Definition at line 1903 of file nsCSSLoader.cpp.

{
  NS_PRECONDITION(aData, "Must have a data!");
  NS_PRECONDITION(aClosure, "Must have a loader");

  aData->mIsLoading = PR_FALSE; // we will handle the removal right here
  aData->mIsCancelled = PR_TRUE;
  
  NS_STATIC_CAST(CSSLoaderImpl*,aClosure)->SheetComplete(aData, PR_FALSE);

  return PL_DHASH_REMOVE;
}

Here is the caller graph for this function:


Variable Documentation

const char kCharsetSym[] = "@charset" [static]

Definition at line 398 of file nsCSSLoader.cpp.