Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / security / resources / libwrapjs.js
blob6d51ca19bea93f158cc4a0e298aa8dfc6abef746
1 // Library for wraping JavaScript code for different evaluation contexts.
3 var libwrapjs = {
4 transform_each_character: function(str, transform) {
5 var result = new Array();
6 for (var i=0; i < str.length; ++i)
7 result.push(transform(str.charAt(i)));
8 return result.join('');
9 },
11 escape_for_single_quote: function(str) {
12 function transform(ch) {
13 if (ch == "\\")
14 return "\\\\";
15 if (ch == "/")
16 return "\\/";
17 if (ch == "'")
18 return "\\'";
19 return ch;
21 return this.transform_each_character(str, transform);
24 escape_for_html: function(str) {
25 function transform(ch) {
26 if (ch == "<")
27 return "&lt;";
28 if (ch == ">")
29 return "&gt;";
30 if (ch == "&")
31 return "&amp;";
32 if (ch == "\n")
33 return "<br />"
34 if (ch == "\"")
35 return "&quot;";
36 return ch;
38 return this.transform_each_character(str, transform);
41 in_string: function(code) {
42 return "'" + this.escape_for_single_quote(code) + "'";
45 in_script_tag: function(code) {
46 return "<script>" + code + "</scr" + "ipt>";
49 in_document_write: function(code) {
50 return "document.write(" + this.in_string(this.in_script_tag(code)) + ")";
53 in_javascript_url: function(code) {
54 return this.in_string("javascript:void(" + code + ")");
57 in_javascript_document: function(code) {
58 return this.in_string("javascript:" +
59 this.in_string(this.in_script_tag(code)));