Escape inlined JSPreload JSON data.

This commit is contained in:
Max Goodman
2014-04-17 01:30:35 -07:00
parent 7744fc7283
commit 013a29e221
3 changed files with 27 additions and 3 deletions

View File

@@ -214,8 +214,9 @@ class DataSource(Source):
return self.wrap.format(content=json_data) + "\n"
def use(self):
from r2.lib.filters import SC_OFF, SC_ON
return (SC_OFF + inline_script_tag.format(content=self.get_source()) +
from r2.lib.filters import SC_OFF, SC_ON, websafe_json
escaped_json = websafe_json(self.get_source())
return (SC_OFF + inline_script_tag.format(content=escaped_json) +
SC_ON + "\n")
@property

View File

@@ -8,7 +8,14 @@ r.preload = {
},
set: function(data) {
_.extend(this.data, data)
var unescapedData = r.utils.structuredMap(data, function(val) {
if (_.isString(val)) {
return _.unescape(val)
} else {
return val
}
})
_.extend(this.data, unescapedData)
},
read: function(url) {

View File

@@ -31,6 +31,22 @@ r.utils = {
return list
},
structuredMap: function(obj, func) {
if (_.isArray(obj)) {
return _.map(obj, function(value) {
return r.utils.structuredMap(value, func)
})
} else if (_.isObject(obj)) {
var mapped = {}
_.each(obj, function(value, key) {
mapped[func(key, 'key')] = r.utils.structuredMap(value, func)
})
return mapped
} else {
return func(obj, 'value')
}
},
querySelectorFromEl: function(targetEl, selector) {
return $(targetEl).parents().andSelf()
.filter(selector || '*')