mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
Better "return this" rewrite (#243)
server-side rewrite: js obj proxy: - rewrite 'return this' more generally, but not 'return this.', update tests
This commit is contained in:
parent
93921aadb7
commit
bbbb62ad52
@ -186,13 +186,13 @@ if (!self.__WB_pmw) {{ self.__WB_pmw = function(obj) {{ return obj; }} }}\n\
|
|||||||
return lambda x: x.replace('this', replacer)
|
return lambda x: x.replace('this', replacer)
|
||||||
|
|
||||||
def __init__(self, rewriter, rules=[]):
|
def __init__(self, rewriter, rules=[]):
|
||||||
func_rw = 'Function("return {0}")'.format(self.THIS_RW)
|
#func_rw = 'Function("return {0}")'.format(self.THIS_RW)
|
||||||
|
|
||||||
prop_str = '|'.join(self.local_objs)
|
prop_str = '|'.join(self.local_objs)
|
||||||
|
|
||||||
rules = rules + [
|
rules = rules + [
|
||||||
(r'(?<=\.)postMessage\b\(', self.add_prefix('__WB_pmw(self).'), 0),
|
(r'(?<=\.)postMessage\b\(', self.add_prefix('__WB_pmw(self).'), 0),
|
||||||
(r'Function\(["\']return this["\']\)', self.fixed(func_rw), 0),
|
#(r'Function\(["\']return this["\']\)', self.fixed(func_rw), 0),
|
||||||
|
(r'\breturn\s+this\b\s*(?![.$])', self.replace_str(self.THIS_RW), 0),
|
||||||
(r'(?<=[\n])\s*this\b(?=(?:\.(?:{0})\b))'.format(prop_str), self.replace_str(';' + self.THIS_RW), 0),
|
(r'(?<=[\n])\s*this\b(?=(?:\.(?:{0})\b))'.format(prop_str), self.replace_str(';' + self.THIS_RW), 0),
|
||||||
(r'(?<![$.])\s*this\b(?=(?:\.(?:{0})\b))'.format(prop_str), self.replace_str(self.THIS_RW), 0),
|
(r'(?<![$.])\s*this\b(?=(?:\.(?:{0})\b))'.format(prop_str), self.replace_str(self.THIS_RW), 0),
|
||||||
(r'(?<=[=])\s*this\b\s*(?![.$])', self.replace_str(self.THIS_RW), 0),
|
(r'(?<=[=])\s*this\b\s*(?![.$])', self.replace_str(self.THIS_RW), 0),
|
||||||
|
@ -148,6 +148,9 @@ r"""
|
|||||||
>>> _test_js_obj_proxy('func(Function("return this"));')
|
>>> _test_js_obj_proxy('func(Function("return this"));')
|
||||||
'func(Function("return (this && this._WB_wombat_obj_proxy || this)"));'
|
'func(Function("return (this && this._WB_wombat_obj_proxy || this)"));'
|
||||||
|
|
||||||
|
>>> _test_js_obj_proxy('A.call(function() { return this });')
|
||||||
|
'A.call(function() { return (this && this._WB_wombat_obj_proxy || this) });'
|
||||||
|
|
||||||
>>> _test_js_obj_proxy('this.document.location = foo')
|
>>> _test_js_obj_proxy('this.document.location = foo')
|
||||||
'(this && this._WB_wombat_obj_proxy || this).document.location = foo'
|
'(this && this._WB_wombat_obj_proxy || this).document.location = foo'
|
||||||
|
|
||||||
@ -167,6 +170,9 @@ r"""
|
|||||||
>>> _test_js_obj_proxy('|||this|||')
|
>>> _test_js_obj_proxy('|||this|||')
|
||||||
'|||this|||'
|
'|||this|||'
|
||||||
|
|
||||||
|
>>> _test_js_obj_proxy('return this.foo')
|
||||||
|
'return this.foo'
|
||||||
|
|
||||||
|
|
||||||
#=================================================================
|
#=================================================================
|
||||||
# XML Rewriting
|
# XML Rewriting
|
||||||
|
Loading…
x
Reference in New Issue
Block a user