Page Speed Optimization Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Attributes | Friends | List of all members
net_instaweb::CssHierarchy Class Reference

#include "css_hierarchy.h"

Public Member Functions

 CssHierarchy (CssFilter *filter)
void InitializeRoot (const GoogleUrl &css_base_url, const GoogleUrl &css_trim_url, const StringPiece input_contents, bool has_unparseables, int64 flattened_result_limit, Css::Stylesheet *stylesheet, MessageHandler *message_handler)
bool NeedsRewriting () const
 A hierarchy needs rewriting only if it has an import to read and expand.
const StringPiece url () const
const StringPiece url_for_humans () const
const GoogleUrlcss_base_url () const
const GoogleUrlcss_trim_url () const
const GoogleUrlcss_resolution_base ()
const Css::Stylesheet * stylesheet () const
Css::Stylesheet * mutable_stylesheet ()
void set_stylesheet (Css::Stylesheet *stylesheet)
const StringPiece input_contents () const
void set_input_contents (const StringPiece input_contents)
GoogleStringinput_contents_backing_store ()
void set_input_contents_to_backing_store ()
const GoogleStringminified_contents () const
void set_minified_contents (const StringPiece minified_contents)
const GoogleStringcharset () const
GoogleStringmutable_charset ()
const GoogleStringcharset_source () const
const StringVector & media () const
StringVector * mutable_media ()
const std::vector
< CssHierarchy * > & 
children () const
 Intended for access to children; add new children using ExpandChildren.
std::vector< CssHierarchy * > & children ()
bool input_contents_resolved () const
void set_input_contents_resolved (bool x)
bool flattening_succeeded () const
void set_flattening_succeeded (bool ok)
const GoogleStringflattening_failure_reason () const
void AddFlatteningFailureReason (const GoogleString &reason)
bool unparseable_detected () const
void set_unparseable_detected (bool ok)
int64 flattened_result_limit () const
void set_flattened_result_limit (int64 x)
bool CheckCharsetOk (const ResourcePtr &resource, GoogleString *failure_reason)
bool Parse ()
bool ExpandChildren ()
void RollUpContents ()
bool RollUpStylesheets ()

Static Public Attributes

static const char kFailureReasonPrefix []


class CssHierarchyTest

Detailed Description

Representation of a CSS with all the information required for import flattening, image rewriting, and minifying. A flattened CSS has had all of its 's replaced with the contents of the 'd file (and each of those have had their 's replaced, and so on recursively).

Lifecycle: Processing: Construct + InitializeRoot if (ExpandChildren) <---------------—+ for each child | InitializeNested | set_input_contents | if (Parse) | if (CheckCharsetOk) | Kick off recursion from here –+ Harvesting (when all the children of a node have completed): if you need the rolled-up text form: RollUpContents Use minified_contents if you need the rolled-up parsed form: RollUpStylesheet Use stylesheet

Constructor & Destructor Documentation

net_instaweb::CssHierarchy::CssHierarchy ( CssFilter filter)

Initialized in an empty state, which is considered successful since it can be flattened into nothing.

Member Function Documentation

void net_instaweb::CssHierarchy::AddFlatteningFailureReason ( const GoogleString reason)

Do nothing if given an empty reason, otherwise if we don't have a failure reason yet, set it to the given one prepended with "Flattening failed: ", otherwise append the given one to what we have now, separated by " AND ". We also strip any leading "Flattening failed: " from the given reason, which can happen when rolling up hierarchies.

bool net_instaweb::CssHierarchy::CheckCharsetOk ( const ResourcePtr resource,
GoogleString failure_reason 

If we haven't already, determine the charset of this CSS, then check if it is compatible with the charset of its parent; currently they are compatible if they're exactly the same (ignoring case). The charset of this CSS is taken from resource's headers if specified, else from the rule in the parsed CSS, if any, else from the owning document (our parent). Returns true if the charsets are compatible, otherwise returns false and sets the failure reason. The charset is always determined and set regardless of the return value.

TODO(matterbury): A potential future enhancement is to allow 'compatible' charsets, like a US-ASCII child in a UTF-8 parent, since US-ASCII is a subset of UTF-8.
const GoogleUrl& net_instaweb::CssHierarchy::css_resolution_base ( )

If the input contents have been resolved return css_trim_url_ because that's what the contents have been resolved against, otherwise return css_base_url_ because that's what the paths in the CSS are relative to.

bool net_instaweb::CssHierarchy::ExpandChildren ( )

Expand the imports in our stylesheet, creating the next level of the hierarchy tree by creating a child hierarchy for each import. The expansion of a child can fail because of problems with the imported URL or because of import recursion, in which case the flattening_succeeded flag for that child is set to false. An expanded child might be empty because of disjoint media rules, in which case the child is un-initialized [for example, if a.css is imported with a media rule of 'print' and it imports b.css with a media rule of 'screen' there is no point in expanding b.css because none of it can apply to the 'print' medium]. Returns true if any children were expanded and need rewriting, which can be tested using NeedsRewriting() [it tests both that the child was expanded and that the expansion succeeded].

void net_instaweb::CssHierarchy::InitializeRoot ( const GoogleUrl css_base_url,
const GoogleUrl css_trim_url,
const StringPiece  input_contents,
bool  has_unparseables,
int64  flattened_result_limit,
Css::Stylesheet *  stylesheet,
MessageHandler message_handler 

Initialize the top-level hierarchy's state from the given values. A StringPiece reference to input_contents is made so it must remain valid for the life of this object.

bool net_instaweb::CssHierarchy::Parse ( )

Parse the input contents into a stylesheet iff it doesn't have one yet, and apply the media applicable to the whole CSS to each ruleset in the stylesheet and delete any rulesets that end up with no applicable media. Returns true if the input contents are successfully parsed, false if not. 'this' will be unchanged if false is returned.

void net_instaweb::CssHierarchy::RollUpContents ( )

Recursively roll up this CSS's textual form such that minified_contents() returns the flattened version of this CSS with 's replaced with the contents of the imported file, all rules removed, and the entire result minified. Intended for use by nested hierarchies that need to produce their flattened+minimized CSS for their parent to incorporate into their own flattened+minimized CSS. If anything goes wrong with the rolling up then the minified contents are set to the original contents. If the textual form hasn't yet been parsed this method will do so by invoking Parse, since the parsed form is required for minification. If rolling up succeeds, any charset and imports are removed from the parsed stylesheet, to match the flattened+minimized CSS for the input contents (without charset/imports), and to help speed up the ultimate call to RollUpStylesheets().

bool net_instaweb::CssHierarchy::RollUpStylesheets ( )

Recursively roll up this CSS's parsed form such that stylesheet() returns the flattened version of it, with child CSSs' rulesets merged into this one's and all imports and charsets removed. It is a pre-requisite that any children have had RollUpContents() invoked on them; it is not required that it has been invoked on 'this' but it is OK if it has. It is also a pre-requisite that if the CSS has not yet been parsed then it must not contain any rules, rather it must be the already-flattened CSS text, because we use the existence of rules to tell that we have already tried and failed to parse and flatten the CSS. This method is intended to be invoked only on the root CSS since there is no need to roll up intermediate/nested stylesheets; only their contents need to be rolled up. Returns false if the CSS was not already parsed and the call to Parse() failed, in which case rolling up has not been performed and 'this' is unchanged.

void net_instaweb::CssHierarchy::set_input_contents ( const StringPiece  input_contents)

A StringPiece reference to input_contents is made so it must remain valid for the life of this object.

The documentation for this class was generated from the following file: