Home | History | Annotate | Download | only in audits
      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 
     40     /**
     41      * @param {string} text
     42      * @return {!Text}
     43      */
     44     text: function(text)
     45     {
     46         return document.createTextNode(text);
     47     },
     48 
     49     /**
     50      * @param {string} snippetText
     51      * @return {!Element}
     52      */
     53     snippet: function(snippetText)
     54     {
     55         var div = document.createElement("div");
     56         div.textContent = snippetText;
     57         div.className = "source-code";
     58         return div;
     59     },
     60 
     61     /**
     62      * @return {!Element}
     63      */
     64     concat: function()
     65     {
     66         var parent = document.createElement("span");
     67         for (var arg = 0; arg < arguments.length; ++arg)
     68             parent.appendChild(WebInspector.auditFormatters.apply(arguments[arg]));
     69         return parent;
     70     },
     71 
     72     /**
     73      * @param {string} url
     74      * @param {string=} displayText
     75      * @param {boolean=} allowExternalNavigation
     76      * @return {!Element}
     77      */
     78     url: function(url, displayText, allowExternalNavigation)
     79     {
     80         var a = document.createElement("a");
     81         a.href = sanitizeHref(url);
     82         a.title = url;
     83         a.textContent = displayText || url;
     84         if (allowExternalNavigation)
     85             a.target = "_blank";
     86         return a;
     87     },
     88 
     89     /**
     90      * @param {string} url
     91      * @param {number=} line
     92      * @return {!Element}
     93      */
     94     resourceLink: function(url, line)
     95     {
     96         // FIXME: use WebInspector.Linkifier
     97         return WebInspector.linkifyResourceAsNode(url, line, "console-message-url webkit-html-resource-link");
     98     }
     99 };
    100 
    101 WebInspector.AuditFormatters.prototype = {
    102     /**
    103      * @param {string|boolean|number|!Object} value
    104      * @return {!Node}
    105      */
    106     apply: function(value)
    107     {
    108         var formatter;
    109         var type = typeof value;
    110         var args;
    111 
    112         switch (type) {
    113         case "string":
    114         case "boolean":
    115         case "number":
    116             formatter = WebInspector.AuditFormatters.Registry.text;
    117         args = [ value.toString() ];
    118         break;
    119 
    120         case "object":
    121             if (value instanceof Node)
    122                 return value;
    123             if (value instanceof Array) {
    124                 formatter = WebInspector.AuditFormatters.Registry.concat;
    125                 args = value;
    126             } else if (value.type && value.arguments) {
    127                 formatter = WebInspector.AuditFormatters.Registry[value.type];
    128                 args = value.arguments;
    129             }
    130         }
    131         if (!formatter)
    132             throw "Invalid value or formatter: " + type + JSON.stringify(value);
    133 
    134         return formatter.apply(null, args);
    135     },
    136 
    137     /**
    138      * @param {!Object} formatters
    139      * @param {?Object} thisArgument
    140      * @param {string|boolean|number|!Object} value
    141      * @return {*}
    142      */
    143     partiallyApply: function(formatters, thisArgument, value)
    144     {
    145         if (value instanceof Array)
    146             return value.map(this.partiallyApply.bind(this, formatters, thisArgument));
    147         if (typeof value === "object" && typeof formatters[value.type] === "function" && value.arguments)
    148             return formatters[value.type].apply(thisArgument, value.arguments);
    149         return value;
    150     }
    151 }
    152 
    153 WebInspector.auditFormatters = new WebInspector.AuditFormatters();
    154