Home | History | Annotate | Download | only in front_end
      1 /*
      2  * Copyright (C) 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /**
     32  * @constructor
     33  */
     34 WebInspector.AuditFormatters = function()
     35 {
     36 }
     37 
     38 WebInspector.AuditFormatters.Registry = {
     39     text: function(text)
     40     {
     41         return document.createTextNode(text);
     42     },
     43 
     44     snippet: function(snippetText)
     45     {
     46         var div = document.createElement("div");
     47         div.textContent = snippetText;
     48         div.className = "source-code";
     49         return div;
     50     },
     51 
     52     concat: function()
     53     {
     54         var parent = document.createElement("span");
     55         for (var arg = 0; arg < arguments.length; ++arg)
     56             parent.appendChild(WebInspector.auditFormatters.apply(arguments[arg]));
     57         return parent;
     58     },
     59 
     60     url: function(url, displayText, allowExternalNavigation)
     61     {
     62         var a = document.createElement("a");
     63         a.href = sanitizeHref(url);
     64         a.title = url;
     65         a.textContent = displayText || url;
     66         if (allowExternalNavigation)
     67             a.target = "_blank";
     68         return a;
     69     },
     70 
     71     resourceLink: function(url, line)
     72     {
     73         // FIXME: use WebInspector.Linkifier
     74         return WebInspector.linkifyResourceAsNode(url, line, "console-message-url webkit-html-resource-link");
     75     }
     76 };
     77 
     78 WebInspector.AuditFormatters.prototype = {
     79     /**
     80      * @param {string|boolean|number|Object} value
     81      */
     82     apply: function(value)
     83     {
     84         var formatter;
     85         var type = typeof value;
     86         var args;
     87 
     88         switch (type) {
     89         case "string":
     90         case "boolean":
     91         case "number":
     92             formatter = WebInspector.AuditFormatters.Registry.text;
     93         args = [ value.toString() ];
     94         break;
     95 
     96         case "object":
     97             if (value instanceof Node)
     98                 return value;
     99             if (value instanceof Array) {
    100                 formatter = WebInspector.AuditFormatters.Registry.concat;
    101                 args = value;
    102             } else if (value.type && value.arguments) {
    103                 formatter = WebInspector.AuditFormatters.Registry[value.type];
    104                 args = value.arguments;
    105             }
    106         }
    107         if (!formatter)
    108             throw "Invalid value or formatter: " + type + JSON.stringify(value);
    109 
    110         return formatter.apply(null, args);
    111     },
    112 
    113     /**
    114      * @param {Object} formatters
    115      * @param {Object} thisArgument
    116      * @param {string|boolean|number|Object} value
    117      */
    118     partiallyApply: function(formatters, thisArgument, value)
    119     {
    120         if (value instanceof Array)
    121             return value.map(this.partiallyApply.bind(this, formatters, thisArgument));
    122         if (typeof value === "object" && typeof formatters[value.type] === "function" && value.arguments)
    123             return formatters[value.type].apply(thisArgument, value.arguments);
    124         return value;
    125     }
    126 }
    127 
    128 WebInspector.auditFormatters = new WebInspector.AuditFormatters();
    129