Mike Orr avatar Mike Orr committed b38af26

New helper ``js_obfuscate`` implements the old rails helper of the same name.

Comments (0)

Files changed (2)

 
 tip (development version)
 -------------------------
+* webhelpers.html.tools:
+  - New helper ``js_obfuscate`` implements the old rails helper of the same name.
 
 1.0b2 (2009-12-21)
 ------------------

webhelpers/html/tools.py

 __all__ = [
     'auto_link', 
     'button_to', 
+    'js_obfuscate',
     'highlight', 
     'mail_to',
     'strip_links',
     return HTML.form(method=form_method, action=url, class_="button-to",
                      c=[HTML.div(method_tag, HTML.input(**html_options))])
 
+def js_obfuscate(content):
+    """Obfuscate data in a Javascript tag.
+    
+    Example::
+        
+        >>> js_obfuscate("<input type='hidden' name='check' value='valid' />")
+        literal(u'<script type="text/javascript">\\n//<![CDATA[\\neval(unescape(\\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%69%6e%70%75%74%20%74%79%70%65%3d%27%68%69%64%64%65%6e%27%20%6e%61%6d%65%3d%27%63%68%65%63%6b%27%20%76%61%6c%75%65%3d%27%76%61%6c%69%64%27%20%2f%3e%27%29%3b\\'))\\n//]]>\\n</script>')
+        
+    """
+    doc_write = "document.write('%s');" % content
+    obfuscated = ''.join(['%%%x' % ord(x) for x in doc_write])
+    complete = "eval(unescape('%s'))" % obfuscated
+    cdata = HTML.cdata("\n", complete, "\n//")
+    return HTML.script("\n//", cdata, "\n", type="text/javascript")
+
 
 def mail_to(email_address, name=None, cc=None, bcc=None, subject=None, 
     body=None, replace_at=None, replace_dot=None, encode=None, **html_options):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.