AuditFormatters.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @constructor
  32. */
  33. WebInspector.AuditFormatters = function()
  34. {
  35. }
  36. WebInspector.AuditFormatters.Registry = {
  37. text: function(text)
  38. {
  39. return document.createTextNode(text);
  40. },
  41. snippet: function(snippetText)
  42. {
  43. var div = document.createElement("div");
  44. div.textContent = snippetText;
  45. div.className = "source-code";
  46. return div;
  47. },
  48. concat: function()
  49. {
  50. var parent = document.createElement("span");
  51. for (var arg = 0; arg < arguments.length; ++arg)
  52. parent.appendChild(WebInspector.auditFormatters.apply(arguments[arg]));
  53. return parent;
  54. },
  55. url: function(url, displayText, allowExternalNavigation)
  56. {
  57. var a = document.createElement("a");
  58. a.href = sanitizeHref(url);
  59. a.title = url;
  60. a.textContent = displayText || url;
  61. if (allowExternalNavigation)
  62. a.target = "_blank";
  63. return a;
  64. },
  65. resourceLink: function(url, line)
  66. {
  67. // FIXME: use WebInspector.Linkifier
  68. return WebInspector.linkifyResourceAsNode(url, line, "console-message-url webkit-html-resource-link");
  69. }
  70. };
  71. WebInspector.AuditFormatters.prototype = {
  72. /**
  73. * @param {string|boolean|number|Object} value
  74. */
  75. apply: function(value)
  76. {
  77. var formatter;
  78. var type = typeof value;
  79. var args;
  80. switch (type) {
  81. case "string":
  82. case "boolean":
  83. case "number":
  84. formatter = WebInspector.AuditFormatters.Registry.text;
  85. args = [ value.toString() ];
  86. break;
  87. case "object":
  88. if (value instanceof Node)
  89. return value;
  90. if (value instanceof Array) {
  91. formatter = WebInspector.AuditFormatters.Registry.concat;
  92. args = value;
  93. } else if (value.type && value.arguments) {
  94. formatter = WebInspector.AuditFormatters.Registry[value.type];
  95. args = value.arguments;
  96. }
  97. }
  98. if (!formatter)
  99. throw "Invalid value or formatter: " + type + JSON.stringify(value);
  100. return formatter.apply(null, args);
  101. },
  102. /**
  103. * @param {Object} formatters
  104. * @param {Object} thisArgument
  105. * @param {string|boolean|number|Object} value
  106. */
  107. partiallyApply: function(formatters, thisArgument, value)
  108. {
  109. if (value instanceof Array)
  110. return value.map(this.partiallyApply.bind(this, formatters, thisArgument));
  111. if (typeof value === "object" && typeof formatters[value.type] === "function" && value.arguments)
  112. return formatters[value.type].apply(thisArgument, value.arguments);
  113. return value;
  114. }
  115. }
  116. WebInspector.auditFormatters = new WebInspector.AuditFormatters();