Package tdi :: Package tools :: Module javascript
[frames] | no frames]

Module javascript

source code

Javascript Tools

Javascript Tools.


Copyright: Copyright 2006 - 2015 André Malo or his licensors, as applicable

License:

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Author: André Malo

Classes
  LiteralJSON
Literal JSON container for use with replace or fill
  SimpleJSON
JSON generator for use with replace or fill
  JSInlineFilter
TDI filter for modifying inline javascript
Functions
basestring
escape_inlined(toescape, encoding=None)
Escape value for inlining
source code
str
escape_string(toescape, inlined=True, encoding=None)
Escape a string for JS output (to be inserted into a JS string)
source code
basestring
replace(script, holders, pattern=None, as_json=True, inlined=True, encoding=None)
Replace javascript values
source code
 
fill(node, holders, pattern=None, as_json=True)
Replace javascript values in a script node
source code
 
fill_attr(node, attr, holders, pattern=None, as_json=True)
Replace javascript values in a script attribute
source code
basestring
cleanup(script, encoding=None)
Cleanup single JS buffer
source code
basestring
cdata(script, encoding=None)
Add a failsafe CDATA container around a script
source code
basestring
minify(script, encoding=None)
Minify a script (using rjsmin)
source code
 
MinifyFilter(builder, minifier=None, standalone=False)
TDI Filter for minifying inline javascript
source code
 
CDATAFilter(builder, standalone=False)
TDI filter for adding failsafe CDATA containers around scripts
source code
Variables
  __doc__ = __doc__.encode('ascii').decode('unicode_escape')
  __package__ = 'tdi.tools'
Function Details

escape_inlined(toescape, encoding=None)

source code 
Escape value for inlining
Parameters:
  • toescape (basestring) - The value to escape
  • encoding (str) - Encoding in case that toescape is a str. If omitted or None, no encoding is applied and toescape is expected to be ASCII compatible.
Returns: basestring
The escaped value, typed as input

escape_string(toescape, inlined=True, encoding=None)

source code 

Escape a string for JS output (to be inserted into a JS string)

This function is one of the building blocks of the tdi.tools.javascript.replace function. You probably shouldn't use it directly in your rendering code.

Parameters:
  • toescape (basestring) - The string to escape
  • inlined (bool) - Do additional escapings (possibly needed for inlining the script within a HTML page)?
  • encoding (str) - Encoding in case that toescape is a str. If omitted or None, no encoding is applied and toescape is expected to be ASCII compatible.
Returns: str
The escaped string (ascii)

replace(script, holders, pattern=None, as_json=True, inlined=True, encoding=None)

source code 

Replace javascript values

See fill and fill_attr for more specific usage.

This functions provides safe (single pass) javascript value replacement:

filled = javascript.replace(script_template, dict(
    a=10,
    b=u'Andr\xe9',
    c=javascript.SimpleJSON(dict(foo='bar')),
))

Where script_template is something like:

// more script...
var count = __a__;
var name = '__b__';
var param = __c__;
// more script...
Parameters:
  • script (basestring) - Script content to modify
  • holders (dict) - Placeholders mappings (name -> value). If a placeholder is found within the script which has no mapping, it's simply left as-is. If as_json is true, the values are checked if they have an as_json method. If they do have one, the method is called and the result (of type unicode) is used as replacement. Otherwise the mapped value is piped through the escape_string function and that result is used as replacement. as_json is passed a boolean inlined parameter which indicates whether the method should escape for inline usage or not.

    Use the LiteralJSON class for passing any JSON content literally to the script. There is also a SimpleJSON class for converting complex structures to JSON using the simplejson converter. You may pass your own classes as well, of course, as long as they provide a proper as_json() method.

  • pattern (unicode or compiled re object) - Placeholder name pattern. If omitted or None, the pattern is (simplified [1]): __(?P<name>.+)__, i.e. the placeholder name enclosed in double-underscores. The name group is expected.

    [1]The actual pattern is: __(?P<name>[^_]*(?:_[^_]+)*)__
  • as_json (bool) - Check the placeholder values for an as_json method? See the description of the holders parameter for details.
  • inlined (bool) - Escape simple content for being inlined (e.g. no CDATA endmarkers, </script>).
  • encoding (str) - Script encoding if script is a str. If omitted or None, the script is expected to be ASCII compatible.

    If script is of type unicode, the encoding is applied to as_json method return values. This is to make sure, the JSON stuff is encoded safely. If omitted or None, ASCII is assumed. JSON result characters not fitting into the this encoding are escaped (uxxxx).

Returns: basestring
The modified script, typed as input

See Also:

fill(node, holders, pattern=None, as_json=True)

source code 

Replace javascript values in a script node

This functions provides safe (single pass) javascript value replacement (utilizing the replace function):

javascript.fill(node, dict(
    a=10,
    b=u'Andr\xe9',
    c=javascript.SimpleJSON(dict(foo='bar')),
))

Where node is something like:

<script tdi="name">
    var count = __a__;
    var name = '__b__';
    var param = __c__;
</script>
Parameters:
  • node (TDI node) - The script node
  • holders (dict) - Placeholders mappings (name -> value). If a placeholder is found within the script which has no mapping, it's simply left as-is. If as_json is true, the values are checked if they have an as_json method. If they do have one, the method is called and the result (of type unicode) is used as replacement. Otherwise the mapped value is piped through the escape_string function and the result is used as replacement. as_json is passed a boolean inlined parameter which indicates whether the method should escape for inline usage or not.

    Use the LiteralJSON class for passing any JSON content literally to the script. There is also a SimpleJSON class for converting complex structures to JSON using the simplejson converter. You may pass your own classes as well, of course, as long as they provide a proper as_json() method.

  • pattern (unicode or compiled re object) - Placeholder name pattern. If omitted or None, the pattern is (simplified [1]): __(?P<name>.+)__, i.e. the placeholder name enclosed in double-underscores. The name group is expected.

    [1]The actual pattern is: __(?P<name>[^_]*(?:_[^_]+)*)__
  • as_json (bool) - Check the placeholder values for an as_json method? See the description of the holders parameter for details.

See Also:

fill_attr(node, attr, holders, pattern=None, as_json=True)

source code 

Replace javascript values in a script attribute

This functions provides safe (single pass) javascript value replacement (utilizing the replace function):

javascript.fill_attr(node, u'onclick', dict(
    a=10,
    b=u'Andr\xe9',
    c=javascript.SimpleJSON(dict(foo='bar')),
))

Where node is something like:

<div onclick="return foo(__a__)">...</div>
Parameters:
  • node (TDI node) - The script node
  • attr (basestring) - The name of the attribute
  • holders (dict) - Placeholders mappings (name -> value). If a placeholder is found within the script which has no mapping, it's simply left as-is. If as_json is true, the values are checked if they have an as_json method. If they do have one, the method is called and the result (of type unicode) is used as replacement. Otherwise the mapped value is piped through the escape_string function and that result is used as replacement. as_json is passed a boolean inlined parameter which indicates whether the method should escape for inline usage or not.

    Use the LiteralJSON class for passing any JSON content literally to the script. There is also a SimpleJSON class for converting complex structures to JSON using the simplejson converter. You may pass your own classes as well, of course, as long as they provide a proper as_json() method.

  • pattern (unicode or compiled re object) - Placeholder name pattern. If omitted or None, the pattern is (simplified [1]): __(?P<name>.+)__, i.e. the placeholder name enclosed in double-underscores. The name group is expected.

    [1]The actual pattern is: __(?P<name>[^_]*(?:_[^_]+)*)__
  • as_json (bool) - Check the placeholder values for an as_json method? See the description of the holders parameter for details.

See Also:

cleanup(script, encoding=None)

source code 

Cleanup single JS buffer

This method attempts to remove CDATA and starting/finishing comment containers.

Parameters:
  • script (basestring) - Buffer to cleanup
  • encoding (str) - Encoding in case that toescape is a str. If omitted or None, no encoding is applied and script is expected to be ASCII compatible.
Returns: basestring
The cleaned up buffer, typed as input

cdata(script, encoding=None)

source code 

Add a failsafe CDATA container around a script

See <http://lists.w3.org/Archives/Public/www-html/2002Apr/0053.html> for details.

Parameters:
  • script (basestring) - JS to contain
  • encoding (str) - Encoding in case that toescape is a str. If omitted or None, no encoding is applied and script is expected to be ASCII compatible.
Returns: basestring
The contained JS, typed as input

minify(script, encoding=None)

source code 

Minify a script (using rjsmin)

Parameters:
  • script (basestring) - JS to minify
  • encoding (str) - Encoding in case that toescape is a str. If omitted or None, no encoding is applied and script is expected to be ASCII compatible.
Returns: basestring
The minified JS, typed as input

MinifyFilter(builder, minifier=None, standalone=False)

source code 
TDI Filter for minifying inline javascript
Parameters:
  • minifier (callable) - Minifier function. If omitted or None, the builtin minifier (see minify) is applied.
  • standalone (bool) - Standalone context? In this case, we won't watch out for TDI attributes.

CDATAFilter(builder, standalone=False)

source code 

TDI filter for adding failsafe CDATA containers around scripts

See <http://lists.w3.org/Archives/Public/www-html/2002Apr/0053.html> for details.

Parameters:
  • standalone (bool) - Standalone context? In this case, we won't watch out for TDI attributes.

Variables Details

__doc__

Value:
__doc__.encode('ascii').decode('unicode_escape')

__package__

Value:
'tdi.tools'