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