// Copyright (C) 2008 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * Supporting scripts for the cajoling testbed. * *
* This supports input forms that can be instantiated multiple times to * simulate multiple gadgets in the same frame. * Different forms are distinguished by a unique suffix, and use the following * identifiers: *
'cajolerForm' + uiSuffix
'messages' + uiSuffix
'output' + uiSuffix
'eval-results' + uiSuffix
'cajita-stacks' + uiSuffix
'cajita-stack' + uiSuffix
'caja-html' + uiSuffix
* Gadgets can exports their public API by attaching methods and fields to
* the exports
object. E.g. the 'module.1' module could do
* exports.doSomething = function () { ... }; ** which makes the
doSomething
function available to other modules
* via* loadModule('gadget.1').doSomething(); ** * @author mikesamuel@gmail.com * @overrides prettyPrint, prettyPrintOne * @provides BOGUS_PROXY_URL, cajole, gadgetPublicApis, getCajoler, getImports, * getUiSuffix, getTestbedServer, indentAndWrapCode, initTestbeds, * innerText, loadExampleInto, renderTemplate, registerTestbed, runPlain, * testbeds * @requires ___, HtmlEmitter, alert, attachDocumentStub, cajita, console, * document, eval, html, location, valijaMaker, window */ /** UI suffixes of all registered testbeds. */ var testbeds = []; /** URL to use when no proxy URL is provided */ var BOGUS_PROXY_URL = 'http://bogus-proxy.google.com'; /** A registry of the public APIs of each of the testbed applets. */ var gadgetPublicApis = { // Predefine a honeypot so we can try to exploit confused deputies 'keystoneKop': ___.primFreeze({ // Not marked simple. It is a breach if a gadget can get the container // to call this on their behalf. f: function() { alert('You get a cookie ' + [].join.call(arguments, ', ')); } }) }; if ('undefined' === typeof prettyPrintOne) { // So it works without prettyprinting when disconnected from the network. prettyPrintOne = function (html) { return html; }; prettyPrint = function () {}; } /** * Returns an instance of CajaApplet that exposes public methods as javascript * methods. * @see CajaApplet.java * @return {com.google.caja.demos.applet.CajaApplet} */ function getCajoler() { return document.applets.cajoler; } /** Get the protocol, host, and port of a bin/testbed-proxy.py. */ var getTestbedServer = (function () { /** Parses the URL to pick out CGI parameters. */ function getCgiParams() { var parts = (location.search || '').split(/[\?&]/g); var params = {}; for (var i = parts.length; --i >= 0;) { var part = parts[i]; var eq = part.indexOf('='); var key, val; if (eq < 0) { key = decodeURIComponent(part); val = ''; } else { key = decodeURIComponent(part.substring(0, eq)); val = decodeURIComponent(part.substring(eq + 1)); } (params[key] || (params[key] = [])).push(val); } return params; } var testbedServer; return function getTestbedServer() { if (testbedServer === void 0) { var backend = getCgiParams().backend; testbedServer = (backend && backend.length === 1) ? backend[0] : BOGUS_PROXY_URL; } return testbedServer; }; })(); /** * Computes the UI suffix given an arbitrary element in the template-generated * content. */ function getUiSuffix(element) { while (!element.hasAttribute('id')) { element = element.parentNode; } return element.id.replace(/^[^\.]+/, ''); } /** * Reads caja code and configuration from the testbed form, cajoles * it, runs it, and displays the output in the current HTML page. * @param {HTMLFormElement} form */ var cajole = (function () { /** * Extract cajoled ecmascript from DefaultGadgetRewriter's output. * This removes the envelope created by * DefaultGadgetRewriter.rewriteContent(String). * @param {string} htmlText from the cajoler * @param {string} uiSuffix suffix of testbed identifiers as described above. */ function loadCaja(htmlText, uiSuffix) { var splitPoint = htmlText.indexOf('