1/*
2 * Copyright (C) 2011 Google Inc.  All rights reserved.
3 * Copyright (C) 2007, 2008, 2013 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 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
32WebInspector.displayNameForNode = function(node)
33{
34    var title = node.nodeNameInCorrectCase();
35
36    var idAttribute = node.getAttribute("id");
37    if (idAttribute) {
38        if (/[\s'"]/.test(idAttribute)) {
39            idAttribute = idAttribute.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"");
40            title += "[id=\"" + idAttribute + "\"]";
41        } else
42            title += "#" + idAttribute;
43    }
44
45    var classAttribute = node.getAttribute("class");
46    if (classAttribute) {
47        var classes = classAttribute.trim().split(/\s+/);
48        var foundClasses = {};
49
50        for (var i = 0; i < classes.length; ++i) {
51            var className = classes[i];
52            if (className && !(className in foundClasses)) {
53                title += "." + className;
54                foundClasses[className] = true;
55            }
56        }
57    }
58
59    return title;
60}
61
62WebInspector.roleSelectorForNode = function(node)
63{
64    // This is proposed syntax for CSS 4 computed role selector :role(foo) and subject to change.
65    // See http://lists.w3.org/Archives/Public/www-style/2013Jul/0104.html
66    var title = "";
67    var role = node.computedRole();
68    if (role)
69        title = ":role(" + role + ")";
70    return title;
71}
72
73WebInspector.linkifyAccessibilityNodeReference = function(node)
74{
75    if (!node)
76        return null;
77    // Same as linkifyNodeReference except the link text has the classnames removed...
78    // ...for list brevity, and both text and title have roleSelectorForNode appended.
79    var link = WebInspector.linkifyNodeReference(node);
80    var tagIdSelector = link.title;
81    var classSelectorIndex = tagIdSelector.indexOf(".");
82    if (classSelectorIndex > -1)
83        tagIdSelector = tagIdSelector.substring(0, classSelectorIndex);
84    var roleSelector = WebInspector.roleSelectorForNode(node);
85    link.textContent = tagIdSelector + roleSelector;
86    link.title += roleSelector;
87    return link;
88}
89
90WebInspector.linkifyNodeReference = function(node)
91{
92    var displayName = WebInspector.displayNameForNode(node);
93
94    var link = document.createElement("span");
95    link.appendChild(document.createTextNode(displayName));
96    link.setAttribute("role", "link");
97    link.className = "node-link";
98    link.title = displayName;
99
100    link.addEventListener("click", WebInspector.domTreeManager.inspectElement.bind(WebInspector.domTreeManager, node.id));
101    link.addEventListener("mouseover", WebInspector.domTreeManager.highlightDOMNode.bind(WebInspector.domTreeManager, node.id, ""));
102    link.addEventListener("mouseout", WebInspector.domTreeManager.hideDOMNodeHighlight.bind(WebInspector.domTreeManager));
103
104    return link;
105}
106