File: style-information.js

package info (click to toggle)
webdeveloper 1.2.5%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,364 kB
  • ctags: 1,608
  • sloc: makefile: 10
file content (319 lines) | stat: -rw-r--r-- 10,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
var WebDeveloper = WebDeveloper || {};

WebDeveloper.StyleInformation                = WebDeveloper.StyleInformation || {};
WebDeveloper.StyleInformation.currentElement = null;

// Handles the click event
WebDeveloper.StyleInformation.click = function(event)
{
  // If the click was not a right click
  if(event.button != 2)
  {
    var eventTarget = event.target;

    // If the event target is set
    if(eventTarget)
    {
      var tagName = eventTarget.tagName;

      // If the event target is not a scrollbar
      if(tagName && tagName.toLowerCase() != "scrollbar")
      {
        WebDeveloper.StyleInformation.displayStyleInformation(eventTarget);
      }

      event.stopPropagation();
      event.preventDefault();
    }
  }
};

// Handles the click event inside the output
WebDeveloper.StyleInformation.clickOutput = function(event)
{
  // If the click was not a right click
  if(event.button != 2)
  {
    var eventTarget = event.target;

    // If the event target is set
    if(eventTarget)
    {
      // If the event target is the copy ancestor path button
      if(eventTarget.hasAttribute("id") && eventTarget.getAttribute("id") == "web-developer-copy-ancestor-path")
      {
        WebDeveloper.StyleInformation.copyAncestorPath();
      }
      else
      {
        var tagName = eventTarget.tagName;

        // If the event target is a link
        if(tagName && tagName.toLowerCase() == "a")
        {
          var href = eventTarget.getAttribute("href");

          // If the href is set and is a hash
          if(href && href == "#")
          {
            WebDeveloper.StyleInformation.selectParentElement(eventTarget);
          }
          else
          {
            WebDeveloper.Common.openURL(href);
          }

          event.preventDefault();
        }
      }
    }
  }
};

// Copies the ancestor path
WebDeveloper.StyleInformation.copyAncestorPath = function()
{
  Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper).copyString(document.getElementById("web-developer-style-information-browser").contentDocument.defaultView.WebDeveloper.Dashboard.getAncestorPath());

  WebDeveloper.Common.displayNotification("ancestorPathCopied");
};

// Displays the style information for an element
WebDeveloper.StyleInformation.displayStyleInformation = function(element)
{
  var childElement       = null;
  var generatedDocument  = document.getElementById("web-developer-style-information-browser").contentDocument;
  var generatedContent   = generatedDocument.getElementById("content");
  var headingElement     = null;
  var noStyleInformation = true;
  var styleInformation   = generatedDocument.createDocumentFragment();
  var styleSheet         = null;
  var styleSheets        = WebDeveloper.StyleInformation.getStyleInformation(element);

  WebDeveloper.StyleInformation.currentElement = element;

  WebDeveloper.Common.empty(generatedContent);
  styleInformation.appendChild(WebDeveloper.ElementAncestors.generateAncestorInformation(element, generatedDocument));

  // Loop through the style sheets
  for(styleSheet in styleSheets)
  {
    childElement   = generatedDocument.createElement("i");
    headingElement = generatedDocument.createElement("h3");

    childElement.setAttribute("class", "icon-caret-down");
    headingElement.appendChild(childElement);

    childElement = generatedDocument.createElement("a");

    childElement.appendChild(generatedDocument.createTextNode(styleSheet));
    childElement.setAttribute("href", styleSheet);
    headingElement.appendChild(childElement);
    styleInformation.appendChild(headingElement);

    childElement = generatedDocument.createElement("pre");

    childElement.appendChild(generatedDocument.createTextNode(styleSheets[styleSheet].trim()));
    childElement.setAttribute("class", "web-developer-syntax-highlight");
    childElement.setAttribute("data-line-numbers", "false");
    childElement.setAttribute("data-type", "css");
    styleInformation.appendChild(childElement);

    noStyleInformation = false;
  }

  // If no style information was found
  if(noStyleInformation)
  {
    childElement = generatedDocument.createElement("p");

    childElement.appendChild(generatedDocument.createTextNode(WebDeveloper.Locales.getString("noStyleInformation")));
    childElement.setAttribute("class", "web-developer-information");
    styleInformation.appendChild(childElement);
  }

  generatedDocument.defaultView.WebDeveloper.Dashboard.setPosition(WebDeveloper.Preferences.getExtensionStringPreference("dashboard.position"));
  generatedDocument.defaultView.WebDeveloper.Dashboard.initialize(styleInformation, WebDeveloper.Preferences.getExtensionStringPreference("syntax.highlight.theme"));
};

// Formats a style
WebDeveloper.StyleInformation.formatStyle = function(property, value)
{
  return "  " + WebDeveloper.CSS.formatStyleProperty(property) + ": " + WebDeveloper.CSS.formatStyleValue(value) + ";\n";
};

// Gets the style information for an element
WebDeveloper.StyleInformation.getStyleInformation = function(element)
{
  var domUtils       = Components.classes["@mozilla.org/inspector/dom-utils;1"].getService(Components.interfaces.inIDOMUtils);
  var line           = null;
  var rule           = null;
  var rules          = domUtils.getCSSStyleRules(element);
  var ruleStyle      = null;
  var ruleStyles     = null;
  var styleSheet     = null;
  var styleSheetHref = null;
  var styleSheets    = [];
  var styleText      = null;

  // Loop through the element rules
  for(var i = 0, l = rules.Count(); i < l; i++)
  {
    rule = rules.GetElementAt(i).QueryInterface(Components.interfaces.nsIDOMCSSStyleRule);
    line = domUtils.getRuleLine(rule);

    // If there is a parent style sheet
    if(rule.parentStyleSheet)
    {
      styleSheet = rule.parentStyleSheet;
    }

    // If this is a valid style sheet
    if(WebDeveloper.CSS.isValidStyleSheet(styleSheet))
    {
      ruleStyles     = rule.style;
      styleSheetHref = styleSheet.href;
      styleText      = "/* " + WebDeveloper.Locales.getString("line") + " " + line + " */\n" + rule.selectorText + "\n{\n";

      // Loop through the style rules
      for(var j = 0, m = ruleStyles.length; j < m; j++)
      {
        ruleStyle = ruleStyles[j];

        // If this is a valid rule style
        if(WebDeveloper.CSS.isValidRuleStyle(ruleStyles, ruleStyle))
        {
          styleText += WebDeveloper.StyleInformation.formatStyle(ruleStyle, ruleStyles.getPropertyValue(ruleStyle));
        }
      }

      styleText += "}\n\n";

      // If this style sheet has rules already stored
      if(styleSheets[styleSheetHref])
      {
        styleSheets[styleSheetHref] += styleText;
      }
      else
      {
        styleSheets[styleSheetHref] = styleText;
      }
    }
  }

  // If the element has inline styles
  if(element.hasAttribute("style"))
  {
    var inlineStyle  = null;
    var inlineStyles = element.getAttribute("style").split(";");
    var property     = null;

    styleText = "";

    // If there are inline styles
    if(inlineStyles.length)
    {
      // Loop through the inline styles
      for(i = 0, l = inlineStyles.length; i < l; i++)
      {
        inlineStyle = inlineStyles[i];

        // If the inline style is set
        if(inlineStyle)
        {
          ruleStyle = inlineStyle.split(":");
          property  = ruleStyle[0].trim();

          // If the property is not an outline
          if(property != "outline")
          {
            styleText += WebDeveloper.StyleInformation.formatStyle(property, ruleStyle[1].trim());
          }
        }
      }

      // If the style text is set
      if(styleText)
      {
        styleSheets["/* " + WebDeveloper.Locales.getString("inlineStyles") + " */"] = "\n{\n" + styleText + "\n}\n";
      }
    }
  }

  return styleSheets;
};

// Initializes the style information dashboard
WebDeveloper.StyleInformation.initialize = function()
{
  var contentDocument = null;
  var documents       = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());

  // Loop through the documents
  for(var i = 0, l = documents.length; i < l; i++)
  {
    contentDocument = documents[i];

    contentDocument.addEventListener("click", WebDeveloper.StyleInformation.click, true);
    contentDocument.addEventListener("mouseover", WebDeveloper.ElementAncestors.mouseOver, false);

    WebDeveloper.Common.toggleStyleSheet("toolbar/style-sheets/element-ancestors.css", "web-developer-style-information-styles", contentDocument, false);
  }

  WebDeveloper.ElementAncestors.createToolbar();

  contentDocument                                                         = document.getElementById("web-developer-style-information-browser").contentDocument;
  contentDocument.querySelector(".web-developer-information").textContent = WebDeveloper.Locales.getString("selectAnElementDisplayStyles");

  contentDocument.addEventListener("click", WebDeveloper.StyleInformation.clickOutput, false);
};

// Handles a parent element being selected
WebDeveloper.StyleInformation.selectParentElement = function(eventTarget)
{
  var ancestorCount = 0;
  var element       = eventTarget.parentNode;
  var parentElement = WebDeveloper.StyleInformation.currentElement;

  // Loop through the next siblings
  while((element = element.nextSibling) !== null)
  {
    ancestorCount++;
  }

  // Loop through the ancestors
  for(var i = 0; i < ancestorCount; i++)
  {
    parentElement = parentElement.parentNode;
  }

  WebDeveloper.StyleInformation.displayStyleInformation(parentElement);
};

// Uninitializes the style information dashboard
WebDeveloper.StyleInformation.uninitialize = function()
{
  var contentDocument = document.getElementById("web-developer-style-information-browser").contentDocument;
  var documents       = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());

  contentDocument.removeEventListener("click", WebDeveloper.StyleInformation.clickOutput, false);

  // Loop through the documents
  for(var i = 0, l = documents.length; i < l; i++)
  {
    contentDocument = documents[i];

    contentDocument.removeEventListener("click", WebDeveloper.StyleInformation.click, true);
    contentDocument.removeEventListener("mouseover", WebDeveloper.ElementAncestors.mouseOver, false);

    WebDeveloper.ElementAncestors.removeOutline(contentDocument);
    WebDeveloper.Common.toggleStyleSheet("toolbar/style-sheets/element-ancestors.css", "web-developer-style-information-styles", contentDocument, false);
  }

  // If the element information is not also running
  if(!WebDeveloper.Common.getMainWindow().WebDeveloper.Dashboard.isOpenInDashboard(WebDeveloper.Locales.getString("elementInformation")))
  {
    WebDeveloper.ElementAncestors.removeToolbar();
  }
};