Web Inspector: Line error widget showed in the wrong resource
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / IssueMessage.js
1 /*
2  * Copyright (C) 2013, 2016 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.IssueMessage = class IssueMessage extends WebInspector.Object
27 {
28     constructor(consoleMessage)
29     {
30         super();
31
32         console.assert(consoleMessage instanceof WebInspector.ConsoleMessage);
33
34         this._consoleMessage = consoleMessage;
35
36         this._text = this._issueText();
37
38         switch (this._consoleMessage.source) {
39         case "javascript":
40             // FIXME: It would be nice if we had this information (the specific type of JavaScript error)
41             // as part of the data passed from WebCore, instead of having to determine it ourselves.
42             var prefixRegex = /^([^:]+): (?:DOM Exception \d+: )?/;
43             var match = prefixRegex.exec(this._text);
44             if (match && match[1] in WebInspector.IssueMessage.Type._prefixTypeMap) {
45                 this._type = WebInspector.IssueMessage.Type._prefixTypeMap[match[1]];
46                 this._text = this._text.substring(match[0].length);
47             } else
48                 this._type = WebInspector.IssueMessage.Type.OtherIssue;
49             break;
50
51         case "css":
52         case "xml":
53             this._type = WebInspector.IssueMessage.Type.PageIssue;
54             break;
55
56         case "network":
57             this._type = WebInspector.IssueMessage.Type.NetworkIssue;
58             break;
59
60         case "security":
61             this._type = WebInspector.IssueMessage.Type.SecurityIssue;
62             break;
63
64         case "console-api":
65         case "storage":
66         case "appcache":
67         case "rendering":
68         case "other":
69             this._type = WebInspector.IssueMessage.Type.OtherIssue;
70             break;
71
72         default:
73             console.error("Unknown issue source:", source);
74             this._type = WebInspector.IssueMessage.Type.OtherIssue;
75         }
76
77         this._sourceCodeLocation = consoleMessage.sourceCodeLocation;
78         if (this._sourceCodeLocation)
79             this._sourceCodeLocation.addEventListener(WebInspector.SourceCodeLocation.Event.DisplayLocationChanged, this._sourceCodeLocationDisplayLocationChanged, this);
80     }
81
82     // Static
83
84     static displayName(type)
85     {
86         switch(type) {
87         case WebInspector.IssueMessage.Type.SemanticIssue:
88             return WebInspector.UIString("Semantic Issue");
89         case WebInspector.IssueMessage.Type.RangeIssue:
90             return WebInspector.UIString("Range Issue");
91         case WebInspector.IssueMessage.Type.ReferenceIssue:
92             return WebInspector.UIString("Reference Issue");
93         case WebInspector.IssueMessage.Type.TypeIssue:
94             return WebInspector.UIString("Type Issue");
95         case WebInspector.IssueMessage.Type.PageIssue:
96             return WebInspector.UIString("Page Issue");
97         case WebInspector.IssueMessage.Type.NetworkIssue:
98             return WebInspector.UIString("Network Issue");
99         case WebInspector.IssueMessage.Type.SecurityIssue:
100             return WebInspector.UIString("Security Issue");
101         case WebInspector.IssueMessage.Type.OtherIssue:
102             return WebInspector.UIString("Other Issue");
103         default:
104             console.error("Unknown issue message type:", type);
105             return WebInspector.UIString("Other Issue");
106         }
107     }
108
109     // Public
110
111     get text() { return this._text; }
112     get type() { return this._type; }
113     get level() { return this._consoleMessage.level; }
114     get source() { return this._consoleMessage.source; }
115     get url() { return this._consoleMessage.url; }
116     get sourceCodeLocation() { return this._sourceCodeLocation; }
117
118     // Protected
119
120     saveIdentityToCookie(cookie)
121     {
122         cookie[WebInspector.IssueMessage.URLCookieKey] = this.url;
123         cookie[WebInspector.IssueMessage.LineNumberCookieKey] = this._sourceCodeLocation ? this._sourceCodeLocation.lineNumber : 0;
124         cookie[WebInspector.IssueMessage.ColumnNumberCookieKey] = this._sourceCodeLocation ? this._sourceCodeLocation.columnNumber : 0;
125     }
126
127     // Private
128
129     _issueText()
130     {
131         let parameters = this._consoleMessage.parameters;
132         if (!parameters)
133             return this._consoleMessage.messageText;
134
135         if (WebInspector.RemoteObject.type(parameters[0]) !== "string")
136             return this._consoleMessage.messageText;
137
138         function valueFormatter(obj)
139         {
140             return obj.description;
141         }
142
143         let formatters = {};
144         formatters.o = valueFormatter;
145         formatters.s = valueFormatter;
146         formatters.f = valueFormatter;
147         formatters.i = valueFormatter;
148         formatters.d = valueFormatter;
149
150         function append(a, b)
151         {
152             a += b;
153             return a;
154         }
155
156         let result = String.format(parameters[0].description, parameters.slice(1), formatters, "", append);
157         let resultText = result.formattedResult;
158
159         for (let i = 0; i < result.unusedSubstitutions.length; ++i)
160             resultText += " " + result.unusedSubstitutions[i].description;
161
162         return resultText;
163     }
164
165     _sourceCodeLocationDisplayLocationChanged(event)
166     {
167         this.dispatchEventToListeners(WebInspector.IssueMessage.Event.DisplayLocationDidChange, event.data);
168     }
169 };
170
171 WebInspector.IssueMessage.Level = {
172     Error: "error",
173     Warning: "warning"
174 };
175
176 WebInspector.IssueMessage.Type = {
177     SemanticIssue: "issue-message-type-semantic-issue",
178     RangeIssue: "issue-message-type-range-issue",
179     ReferenceIssue: "issue-message-type-reference-issue",
180     TypeIssue: "issue-message-type-type-issue",
181     PageIssue: "issue-message-type-page-issue",
182     NetworkIssue: "issue-message-type-network-issue",
183     SecurityIssue: "issue-message-type-security-issue",
184     OtherIssue: "issue-message-type-other-issue"
185 };
186
187 WebInspector.IssueMessage.TypeIdentifier = "issue-message";
188 WebInspector.IssueMessage.URLCookieKey = "issue-message-url";
189 WebInspector.IssueMessage.LineNumberCookieKey = "issue-message-line-number";
190 WebInspector.IssueMessage.ColumnNumberCookieKey = "issue-message-column-number";
191
192 WebInspector.IssueMessage.Event = {
193     LocationDidChange: "issue-message-location-did-change",
194     DisplayLocationDidChange: "issue-message-display-location-did-change"
195 };
196
197 WebInspector.IssueMessage.Type._prefixTypeMap = {
198     "SyntaxError": WebInspector.IssueMessage.Type.SemanticIssue,
199     "URIError": WebInspector.IssueMessage.Type.SemanticIssue,
200     "EvalError": WebInspector.IssueMessage.Type.SemanticIssue,
201     "INVALID_CHARACTER_ERR": WebInspector.IssueMessage.Type.SemanticIssue,
202     "SYNTAX_ERR": WebInspector.IssueMessage.Type.SemanticIssue,
203
204     "RangeError": WebInspector.IssueMessage.Type.RangeIssue,
205     "INDEX_SIZE_ERR": WebInspector.IssueMessage.Type.RangeIssue,
206     "DOMSTRING_SIZE_ERR": WebInspector.IssueMessage.Type.RangeIssue,
207
208     "ReferenceError": WebInspector.IssueMessage.Type.ReferenceIssue,
209     "HIERARCHY_REQUEST_ERR": WebInspector.IssueMessage.Type.ReferenceIssue,
210     "INVALID_STATE_ERR": WebInspector.IssueMessage.Type.ReferenceIssue,
211     "NOT_FOUND_ERR": WebInspector.IssueMessage.Type.ReferenceIssue,
212     "WRONG_DOCUMENT_ERR": WebInspector.IssueMessage.Type.ReferenceIssue,
213
214     "TypeError": WebInspector.IssueMessage.Type.TypeIssue,
215     "INVALID_NODE_TYPE_ERR": WebInspector.IssueMessage.Type.TypeIssue,
216     "TYPE_MISMATCH_ERR": WebInspector.IssueMessage.Type.TypeIssue,
217
218     "SECURITY_ERR": WebInspector.IssueMessage.Type.SecurityIssue,
219
220     "NETWORK_ERR": WebInspector.IssueMessage.Type.NetworkIssue,
221
222     "ABORT_ERR": WebInspector.IssueMessage.Type.OtherIssue,
223     "DATA_CLONE_ERR": WebInspector.IssueMessage.Type.OtherIssue,
224     "INUSE_ATTRIBUTE_ERR": WebInspector.IssueMessage.Type.OtherIssue,
225     "INVALID_ACCESS_ERR": WebInspector.IssueMessage.Type.OtherIssue,
226     "INVALID_MODIFICATION_ERR": WebInspector.IssueMessage.Type.OtherIssue,
227     "NAMESPACE_ERR": WebInspector.IssueMessage.Type.OtherIssue,
228     "NOT_SUPPORTED_ERR": WebInspector.IssueMessage.Type.OtherIssue,
229     "NO_DATA_ALLOWED_ERR": WebInspector.IssueMessage.Type.OtherIssue,
230     "NO_MODIFICATION_ALLOWED_ERR": WebInspector.IssueMessage.Type.OtherIssue,
231     "QUOTA_EXCEEDED_ERR": WebInspector.IssueMessage.Type.OtherIssue,
232     "TIMEOUT_ERR": WebInspector.IssueMessage.Type.OtherIssue,
233     "URL_MISMATCH_ERR": WebInspector.IssueMessage.Type.OtherIssue,
234     "VALIDATION_ERR": WebInspector.IssueMessage.Type.OtherIssue
235 };