Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / audits / AuditFormatters.js
blob908b1cc9b45b9974fac8972e3acfe1a8c1b434ad
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 /**
32 * @constructor
34 WebInspector.AuditFormatters = function()
38 WebInspector.AuditFormatters.Registry = {
40 /**
41 * @param {string} text
42 * @return {!Text}
44 text: function(text)
46 return createTextNode(text);
49 /**
50 * @param {string} snippetText
51 * @return {!Element}
53 snippet: function(snippetText)
55 var div = createElement("div");
56 div.textContent = snippetText;
57 div.className = "source-code";
58 return div;
61 /**
62 * @return {!Element}
64 concat: function()
66 var parent = createElement("span");
67 for (var arg = 0; arg < arguments.length; ++arg)
68 parent.appendChild(WebInspector.auditFormatters.apply(arguments[arg]));
69 return parent;
72 /**
73 * @param {string} url
74 * @param {string=} displayText
75 * @return {!Element}
77 url: function(url, displayText)
79 return WebInspector.linkifyURLAsNode(url, displayText, undefined, true);
82 /**
83 * @param {string} url
84 * @param {number=} line
85 * @return {!Element}
87 resourceLink: function(url, line)
89 // FIXME: use WebInspector.Linkifier
90 return WebInspector.linkifyResourceAsNode(url, line, "resource-url webkit-html-resource-link");
94 WebInspector.AuditFormatters.prototype = {
95 /**
96 * @param {string|boolean|number|!Object} value
97 * @return {!Node}
99 apply: function(value)
101 var formatter;
102 var type = typeof value;
103 var args;
105 switch (type) {
106 case "string":
107 case "boolean":
108 case "number":
109 formatter = WebInspector.AuditFormatters.Registry.text;
110 args = [value.toString()];
111 break;
113 case "object":
114 if (value instanceof Node)
115 return value;
116 if (Array.isArray(value)) {
117 formatter = WebInspector.AuditFormatters.Registry.concat;
118 args = value;
119 } else if (value.type && value.arguments) {
120 formatter = WebInspector.AuditFormatters.Registry[value.type];
121 args = value.arguments;
124 if (!formatter)
125 throw "Invalid value or formatter: " + type + JSON.stringify(value);
127 return formatter.apply(null, args);
131 * @param {!Object} formatters
132 * @param {?Object} thisArgument
133 * @param {string|boolean|number|!Object} value
134 * @return {*}
136 partiallyApply: function(formatters, thisArgument, value)
138 if (Array.isArray(value))
139 return value.map(this.partiallyApply.bind(this, formatters, thisArgument));
140 if (typeof value === "object" && typeof formatters[value.type] === "function" && value.arguments)
141 return formatters[value.type].apply(thisArgument, value.arguments);
142 return value;
146 WebInspector.auditFormatters = new WebInspector.AuditFormatters();