DOMPresentationUtils.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2011 Google Inc. All rights reserved.
  3. * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
  4. * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
  5. * Copyright (C) 2009 Joseph Pecoraro
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. WebInspector.DOMPresentationUtils = {}
  32. WebInspector.DOMPresentationUtils.decorateNodeLabel = function(node, parentElement)
  33. {
  34. var title = node.nodeNameInCorrectCase();
  35. var nameElement = document.createElement("span");
  36. nameElement.textContent = title;
  37. parentElement.appendChild(nameElement);
  38. var idAttribute = node.getAttribute("id");
  39. if (idAttribute) {
  40. var idElement = document.createElement("span");
  41. parentElement.appendChild(idElement);
  42. var part = "#" + idAttribute;
  43. title += part;
  44. idElement.appendChild(document.createTextNode(part));
  45. // Mark the name as extra, since the ID is more important.
  46. nameElement.className = "extra";
  47. }
  48. var classAttribute = node.getAttribute("class");
  49. if (classAttribute) {
  50. var classes = classAttribute.split(/\s+/);
  51. var foundClasses = {};
  52. if (classes.length) {
  53. var classesElement = document.createElement("span");
  54. classesElement.className = "extra";
  55. parentElement.appendChild(classesElement);
  56. for (var i = 0; i < classes.length; ++i) {
  57. var className = classes[i];
  58. if (className && !(className in foundClasses)) {
  59. var part = "." + className;
  60. title += part;
  61. classesElement.appendChild(document.createTextNode(part));
  62. foundClasses[className] = true;
  63. }
  64. }
  65. }
  66. }
  67. parentElement.title = title;
  68. }
  69. /**
  70. * @param {Element} container
  71. * @param {string} nodeTitle
  72. */
  73. WebInspector.DOMPresentationUtils.createSpansForNodeTitle = function(container, nodeTitle)
  74. {
  75. var match = nodeTitle.match(/([^#.]+)(#[^.]+)?(\..*)?/);
  76. container.createChild("span", "webkit-html-tag-name").textContent = match[1];
  77. if (match[2])
  78. container.createChild("span", "webkit-html-attribute-value").textContent = match[2];
  79. if (match[3])
  80. container.createChild("span", "webkit-html-attribute-name").textContent = match[3];
  81. }
  82. WebInspector.DOMPresentationUtils.linkifyNodeReference = function(node)
  83. {
  84. var link = document.createElement("span");
  85. link.className = "node-link";
  86. WebInspector.DOMPresentationUtils.decorateNodeLabel(node, link);
  87. link.addEventListener("click", WebInspector.domAgent.inspectElement.bind(WebInspector.domAgent, node.id), false);
  88. link.addEventListener("mouseover", WebInspector.domAgent.highlightDOMNode.bind(WebInspector.domAgent, node.id, "", undefined), false);
  89. link.addEventListener("mouseout", WebInspector.domAgent.hideDOMNodeHighlight.bind(WebInspector.domAgent), false);
  90. return link;
  91. }
  92. WebInspector.DOMPresentationUtils.linkifyNodeById = function(nodeId)
  93. {
  94. var node = WebInspector.domAgent.nodeForId(nodeId);
  95. if (!node)
  96. return document.createTextNode(WebInspector.UIString("<node>"));
  97. return WebInspector.DOMPresentationUtils.linkifyNodeReference(node);
  98. }
  99. /**
  100. * @param {string} imageURL
  101. * @param {boolean} showDimensions
  102. * @param {function(Element=)} userCallback
  103. * @param {Object=} precomputedDimensions
  104. */
  105. WebInspector.DOMPresentationUtils.buildImagePreviewContents = function(imageURL, showDimensions, userCallback, precomputedDimensions)
  106. {
  107. var resource = WebInspector.resourceTreeModel.resourceForURL(imageURL);
  108. if (!resource) {
  109. userCallback();
  110. return;
  111. }
  112. var imageElement = document.createElement("img");
  113. imageElement.addEventListener("load", buildContent, false);
  114. imageElement.addEventListener("error", errorCallback, false);
  115. resource.populateImageSource(imageElement);
  116. function errorCallback()
  117. {
  118. // Drop the event parameter when invoking userCallback.
  119. userCallback();
  120. }
  121. function buildContent()
  122. {
  123. var container = document.createElement("table");
  124. container.className = "image-preview-container";
  125. var naturalWidth = precomputedDimensions ? precomputedDimensions.naturalWidth : imageElement.naturalWidth;
  126. var naturalHeight = precomputedDimensions ? precomputedDimensions.naturalHeight : imageElement.naturalHeight;
  127. var offsetWidth = precomputedDimensions ? precomputedDimensions.offsetWidth : naturalWidth;
  128. var offsetHeight = precomputedDimensions ? precomputedDimensions.offsetHeight : naturalHeight;
  129. var description;
  130. if (showDimensions) {
  131. if (offsetHeight === naturalHeight && offsetWidth === naturalWidth)
  132. description = WebInspector.UIString("%d \xd7 %d pixels", offsetWidth, offsetHeight);
  133. else
  134. description = WebInspector.UIString("%d \xd7 %d pixels (Natural: %d \xd7 %d pixels)", offsetWidth, offsetHeight, naturalWidth, naturalHeight);
  135. }
  136. container.createChild("tr").createChild("td", "image-container").appendChild(imageElement);
  137. if (description)
  138. container.createChild("tr").createChild("td").createChild("span", "description").textContent = description;
  139. userCallback(container);
  140. }
  141. }