Skip to content

Commit

Permalink
Merge pull request huginn#812 from younata/master
Browse files Browse the repository at this point in the history
Add 'unescapeHTML' functionality to the javascript agent.
  • Loading branch information
cantino committed May 9, 2015
2 parents cef7f1d + c6c5fed commit 1a43bd2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/models/agents/java_script_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class JavaScriptAgent < Agent
* `this.options(key)`
* `this.log(message)`
* `this.error(message)`
* `this.escapeHtml(htmlToEscape)`
* `this.unescapeHtml(htmlToUnescape)`
MD

def validate_options
Expand Down Expand Up @@ -102,6 +104,8 @@ def execute_js(js_function, incoming_events = [])
memory.to_json
end
end
context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) }
context["unescapeHtml"] = lambda { |a, x| CGI.unescapeHTML(x) }

context.eval(code)
context.eval("Agent.#{js_function}();")
Expand Down Expand Up @@ -158,6 +162,14 @@ def setup_javascript
doError(message);
}
Agent.escapeHtml = function(html) {
return escapeHtml(html);
}
Agent.unescapeHtml = function(html) {
return unescapeHtml(html);
}
Agent.check = function(){};
Agent.receive = function(){};
JS
Expand Down
14 changes: 14 additions & 0 deletions spec/models/agents/java_script_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@
end
end

describe "escaping and unescaping HTML" do
it "can escape and unescape html with this.escapeHtml and this.unescapeHtml in the javascript environment" do
@agent.options['code'] = 'Agent.check = function() { this.createEvent({ escaped: this.escapeHtml(\'test \"escaping\" <characters>\'), unescaped: this.unescapeHtml(\'test &quot;unescaping&quot; &lt;characters&gt;\')}); };'
@agent.save!
expect {
expect {
@agent.check
}.not_to change { AgentLog.count }
}.to change { Event.count}.by(1)
created_event = @agent.events.last
expect(created_event.payload).to eq({ 'escaped' => 'test &quot;escaping&quot; &lt;characters&gt;', 'unescaped' => 'test "unescaping" <characters>'})
end
end

describe "getting incoming events" do
it "can access incoming events in the JavaScript enviroment via this.incomingEvents" do
event = Event.new
Expand Down

0 comments on commit 1a43bd2

Please sign in to comment.