Page Speed Optimization Libraries  1.8.31.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Static Public Member Functions
net_instaweb::JavascriptCodeBlock Class Reference

#include "javascript_code_block.h"

List of all members.

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 std::vector
< source_map::Mapping > & 
SourceMappings () const
void AppendSourceMapUrl (StringPiece url)
StringPiece ComputeJavascriptLibrary () const
void SwapRewrittenString (GoogleString *other)
const GoogleString & message_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)

Detailed Description

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

Todo:
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

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.

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 
) [inline, static]

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 '$'.

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 [inline]

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

const std::vector<source_map::Mapping>& net_instaweb::JavascriptCodeBlock::SourceMappings ( ) const [inline]

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

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

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 
) [inline, static]

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) [static]

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


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines