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

#include "javascript_code_block.h"

Public Member Functions

 JavascriptCodeBlock (const StringPiece &original_code, JavascriptRewriteConfig *config, const StringPiece &message_id, MessageHandler *handler)
bool Rewrite ()
bool successfully_rewritten () const
StringPiece rewritten_code () const
const source_map::MappingVector & SourceMappings () const
void AppendSourceMapUrl (StringPiece url)
StringPiece ComputeJavascriptLibrary () const
void SwapRewrittenString (GoogleString *other)
const GoogleStringmessage_id () const
 Get message id passed in at creation time, for external diagnostics.

Static Public Member Functions

static bool UnsafeToRename (const StringPiece &script)
static void ToJsStringLiteral (const StringPiece &original, GoogleString *escaped)
static GoogleString JsUrlHash (const GoogleString &url, Hasher *hasher)

Static Public Attributes

static const char kIntrospectionComment []

Detailed Description

Object representing a block of Javascript code that might be a candidate for rewriting.

TODO(jmaessen): Does this architecture make sense when we have multiple scripts on a page and the ability to move code around a bunch? How do we maintain JS context in that setting?

For now, we're content just being able to pull data in and parse it at all.

Member Function Documentation

void net_instaweb::JavascriptCodeBlock::AppendSourceMapUrl ( StringPiece  url)

Annotate rewritten_code() with a source map URL.

Call this after Rewrite() and before rewritten_code() if you want to append a comment to the minified JS indicating the URL for the source map. Note: Source map URL may not be appended if url is unsanitary, but this probably shouldn't happen in practice.

StringPiece net_instaweb::JavascriptCodeBlock::ComputeJavascriptLibrary ( ) const

Is the current block a JS library that can be redirected to a canonical URL? If so, return that canonical URL (storage owned by the underlying config object passed in at construction), otherwise return an empty StringPiece.

PRECONDITION: Rewrite() must have been called first.

static GoogleString net_instaweb::JavascriptCodeBlock::JsUrlHash ( const GoogleString url,
Hasher hasher 

Generates a hash of a URL escaped to be safe to use in a Javascript identifier, so that variable names can be safely created that won't collide with other local Javascript.

Hashes may contain '-', which isn't valid in a JavaScript name, so replace every '-' with '$'.

bool net_instaweb::JavascriptCodeBlock::Rewrite ( )

Attempt to rewrite the file. Returns true if we should use the rewritten version. Must be called before successfully_rewritten(), rewritten_code() and ComputeJavascriptLibrary().

StringPiece net_instaweb::JavascriptCodeBlock::rewritten_code ( ) const

PRECONDITION: Rewrite() must have been called first and successfully_rewritten() must be true.

const source_map::MappingVector& net_instaweb::JavascriptCodeBlock::SourceMappings ( ) const

Returns the contents of a source map from original to rewritten. PRECONDITION: Rewrite() must have been called first and successfully_rewritten() must be true.

bool net_instaweb::JavascriptCodeBlock::successfully_rewritten ( ) const

Should we use the rewritten version? PRECONDITION: Rewrite() must have been called first.

void net_instaweb::JavascriptCodeBlock::SwapRewrittenString ( GoogleString other)

Swaps rewritten_code_ into *other. Afterward the JavascriptCodeBlock will be cleared and unusable. PRECONDITION: Rewrite() must have been called first and successfully_rewritten() must be true.

static void net_instaweb::JavascriptCodeBlock::ToJsStringLiteral ( const StringPiece &  original,
GoogleString escaped 

Converts a regular string to what can be used in Javascript directly. Note that output also contains starting and ending quotes, to facilitate embedding.

add quotes

static bool net_instaweb::JavascriptCodeBlock::UnsafeToRename ( const StringPiece &  script)

Determines whether the javascript is brittle and will likely break if we alter its URL.

Member Data Documentation

const char net_instaweb::JavascriptCodeBlock::kIntrospectionComment[]

If debug_filter and AvoidRenamingIntrospectiveJavascript option are turned on, this comment will be injected right after the introspective Javascript context for debugging.

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