1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-15 00:03:28 +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:
Ilya Kreymer 2017-09-22 12:36:02 -07:00 committed by GitHub
parent 93921aadb7
commit bbbb62ad52
2 changed files with 9 additions and 3 deletions

View File

@ -186,13 +186,13 @@ if (!self.__WB_pmw) {{ self.__WB_pmw = function(obj) {{ return obj; }} }}\n\
return lambda x: x.replace('this', replacer)
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)
rules = rules + [
(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'(?<![$.])\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),

View File

@ -148,6 +148,9 @@ r"""
>>> _test_js_obj_proxy('func(Function("return 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')
'(this && this._WB_wombat_obj_proxy || this).document.location = foo'
@ -167,6 +170,9 @@ r"""
>>> _test_js_obj_proxy('|||this|||')
'|||this|||'
>>> _test_js_obj_proxy('return this.foo')
'return this.foo'
#=================================================================
# XML Rewriting