forked from firebug/firebug
/
cssPanel.js
2957 lines (2551 loc) · 97.9 KB
/
cssPanel.js
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* See license.txt for terms of usage */
define([
"firebug/lib/object",
"firebug/firebug",
"firebug/lib/domplate",
"firebug/chrome/reps",
"firebug/lib/locale",
"firebug/lib/events",
"firebug/lib/url",
"firebug/js/sourceLink",
"firebug/lib/css",
"firebug/lib/dom",
"firebug/chrome/window",
"firebug/lib/search",
"firebug/lib/string",
"firebug/lib/array",
"firebug/lib/fonts",
"firebug/lib/xml",
"firebug/lib/persist",
"firebug/lib/system",
"firebug/chrome/menu",
"firebug/lib/options",
"firebug/css/cssModule",
"firebug/css/cssReps",
"firebug/editor/editor",
"firebug/editor/editorSelector",
"firebug/chrome/searchBox"
],
function(Obj, Firebug, Domplate, FirebugReps, Locale, Events, Url, SourceLink, Css, Dom, Win,
Search, Str, Arr, Fonts, Xml, Persist, System, Menu, Options, CSSModule, CSSInfoTip) {
with (Domplate) {
// ********************************************************************************************* //
// Constants
const Cc = Components.classes;
const Ci = Components.interfaces;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
var CSSDomplateBase =
{
isEditable: function(rule)
{
return !rule.isSystemSheet && !rule.isNotEditable;
},
isSelectorEditable: function(rule)
{
return rule.isSelectorEditable && this.isEditable(rule);
},
getPropertyValue: function(prop)
{
var limit = Options.get("stringCropLength");
if (limit > 0)
return Str.cropString(prop.value, limit);
return prop.value;
}
};
var CSSPropTag = domplate(CSSDomplateBase,
{
tag:
DIV({"class": "cssProp focusRow", $disabledStyle: "$prop.disabled",
$editGroup: "$rule|isEditable",
$cssOverridden: "$prop.overridden",
role: "option"},
// Use spaces for indent to make "copy to clipboard" nice.
SPAN(" "),
SPAN({"class": "cssPropName", $editable: "$rule|isEditable"},
"$prop.name"
),
// Use a space here, so that "copy to clipboard" has it (issue 3266).
SPAN({"class": "cssColon"}, ": "),
SPAN({"class": "cssPropValue", $editable: "$rule|isEditable",
_repObject: "$prop.value$prop.important"},"$prop|getPropertyValue$prop.important"
),
SPAN({"class": "cssSemi"}, ";"
)
)
});
var CSSRuleTag =
TAG("$rule.tag", {rule: "$rule"});
var CSSImportRuleTag = domplate(CSSDomplateBase,
{
tag:
DIV({"class": "cssRule insertInto focusRow importRule", _repObject: "$rule.rule"},
"@import "",
A({"class": "objectLink", _repObject: "$rule.rule.styleSheet"}, "$rule.rule.href"),
""",
SPAN({"class": "separator"}, "$rule.rule|getSeparator"),
SPAN({"class": "cssMediaQuery", $editable: "$rule|isEditable"},
"$rule.rule.media.mediaText"),
";"
),
getSeparator: function(rule)
{
return rule.media.mediaText == "" ? "" : " ";
}
});
var CSSCharsetRuleTag = domplate(CSSDomplateBase,
{
tag:
DIV({"class": "cssRule focusRow cssCharsetRule", _repObject: "$rule.rule"},
SPAN({"class": "cssRuleName"}, "@charset"),
" "",
SPAN({"class": "cssRuleValue", $editable: "$rule|isEditable"}, "$rule.rule.encoding"),
"";"
)
});
var CSSNamespaceRuleTag = domplate(CSSDomplateBase,
{
tag:
DIV({"class": "cssRule focusRow cssNamespaceRule", _repObject: "$rule.rule"},
SPAN({"class": "cssRuleName"}, "@namespace"),
SPAN({"class": "separator"}, "$rule.prefix|getSeparator"),
SPAN({"class": "cssNamespacePrefix", $editable: "$rule|isEditable"}, "$rule.prefix"),
" "",
SPAN({"class": "cssNamespaceName", $editable: "$rule|isEditable"}, "$rule.name"),
"";"
),
getSeparator: function(prefix)
{
return prefix == "" ? "" : " ";
}
});
var CSSFontFaceRuleTag = domplate(CSSDomplateBase,
{
tag:
DIV({"class": "cssRule cssFontFaceRule",
$cssEditableRule: "$rule|isEditable",
$insertInto: "$rule|isEditable",
_repObject: "$rule.rule",
role : 'presentation'},
DIV({"class": "cssHead focusRow", role : "listitem"}, "@font-face {"),
DIV({role : "group"},
DIV({"class": "cssPropertyListBox", role: "listbox"},
FOR("prop", "$rule.props",
TAG(CSSPropTag.tag, {rule: "$rule", prop: "$prop"})
)
)
),
DIV({$editable: "$rule|isEditable", $insertBefore:"$rule|isEditable",
role:"presentation"},
"}"
)
)
});
var CSSStyleRuleTag = domplate(CSSDomplateBase,
{
tag:
DIV({"class": "cssRule",
$cssEditableRule: "$rule|isEditable",
$insertInto: "$rule|isEditable",
$editGroup: "$rule|isSelectorEditable",
_repObject: "$rule.rule",
role: "presentation"},
DIV({"class": "cssHead focusRow", role: "listitem"},
SPAN({"class": "cssSelector", $editable: "$rule|isSelectorEditable"},
"$rule.selector"),
" {"
),
DIV({role: "group"},
DIV({"class": "cssPropertyListBox", _rule: "$rule", role: "listbox"},
FOR("prop", "$rule.props",
TAG(CSSPropTag.tag, {rule: "$rule", prop: "$prop"})
)
)
),
DIV({$editable: "$rule|isEditable", $insertBefore: "$rule|isEditable",
role:"presentation"},
"}"
)
)
});
Firebug.CSSStyleRuleTag = CSSStyleRuleTag;
// ********************************************************************************************* //
const reSplitCSS = /(url\("?[^"\)]+?"?\))|(rgba?\([^)]*\)?)|(hsla?\([^)]*\)?)|(#[\dA-Fa-f]+)|(-?\d+(\.\d+)?(%|[a-z]{1,4})?)|"([^"]*)"?|'([^']*)'?|([^,\s\/!\(\)]+)|(!(.*)?)/;
const reURL = /url\("?([^"\)]+)?"?\)/;
const reRepeat = /no-repeat|repeat-x|repeat-y|repeat/;
const reSelectorChar = /[-_0-9a-zA-Z]/;
// ********************************************************************************************* //
// CSS Module
Firebug.CSSStyleSheetPanel = function() {};
Firebug.CSSStyleSheetPanel.prototype = Obj.extend(Firebug.Panel,
{
template: domplate(
{
tag:
DIV({"class": "cssSheet insertInto a11yCSSView"},
FOR("rule", "$rules",
CSSRuleTag
),
DIV({"class": "cssSheet editable insertBefore"}, ""
)
)
}),
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
refresh: function()
{
if (this.location)
this.updateLocation(this.location);
else if (this.selection)
this.updateSelection(this.selection);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// CSS Editing
startBuiltInEditing: function(css)
{
if (FBTrace.DBG_CSS)
FBTrace.sysout("CSSStyleSheetPanel.startBuiltInEditing", css);
if (!this.stylesheetEditor)
this.stylesheetEditor = new StyleSheetEditor(this.document);
var styleSheet = this.location.editStyleSheet
? this.location.editStyleSheet.sheet
: this.location;
this.stylesheetEditor.styleSheet = this.location;
Firebug.Editor.startEditing(this.panelNode, css, this.stylesheetEditor);
//this.stylesheetEditor.scrollToLine(topmost.line, topmost.offset);
this.stylesheetEditor.input.scrollTop = this.panelNode.scrollTop;
},
startLiveEditing: function(styleSheet, context)
{
var css = getStyleSheetCSS(styleSheet, context);
this.startBuiltInEditing(css);
},
startSourceEditing: function(styleSheet, context)
{
if (Firebug.CSSDirtyListener.isDirty(styleSheet, context))
{
var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"].
getService(Ci.nsIPromptService);
var proceedToEdit = prompts.confirm(null, Locale.$STR("Firebug"),
Locale.$STR("confirmation.Edit_CSS_Source"));
if (!proceedToEdit)
{
this.stopEditing();
return;
}
}
var css = getOriginalStyleSheetCSS(styleSheet, context);
this.startBuiltInEditing(css);
},
stopEditing: function()
{
if (FBTrace.DBG_CSS)
FBTrace.sysout("CSSStyleSheetPanel.stopEditing");
if (this.currentCSSEditor)
{
this.currentCSSEditor.stopEditing();
delete this.currentCSSEditor;
}
else
{
Firebug.Editor.stopEditing();
}
},
toggleEditing: function()
{
if (this.editing)
{
this.stopEditing();
Events.dispatch(this.fbListeners, "onStopCSSEditing", [this.context]);
}
else
{
if (!this.location)
return;
var styleSheet = this.location.editStyleSheet
? this.location.editStyleSheet.sheet
: this.location;
this.currentCSSEditor = CSSModule.getCurrentEditor();
try
{
this.currentCSSEditor.startEditing(styleSheet, this.context, this);
Events.dispatch(this.fbListeners, "onStartCSSEditing", [styleSheet, this.context]);
}
catch(exc)
{
var mode = CSSModule.getCurrentEditorName();
if (FBTrace.DBG_ERRORS)
FBTrace.sysout("editor.startEditing ERROR "+exc, {exc: exc, name: mode,
currentEditor: this.currentCSSEditor, styleSheet: styleSheet,
CSSModule: CSSModule});
}
}
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
loadOriginalSource: function()
{
if (!this.location)
return;
var styleSheet = this.location;
var css = getOriginalStyleSheetCSS(styleSheet, this.context);
this.stylesheetEditor.setValue(css);
this.stylesheetEditor.saveEdit(null, css);
//styleSheet.editStyleSheet.showUnformated = true;
},
getStylesheetURL: function(rule, getBaseUri)
{
if (this.location.href)
return this.location.href;
else if (getBaseUri)
return this.context.window.document.baseURI;
else
return this.context.window.location.href;
},
getRuleByLine: function(styleSheet, line)
{
if (!Dom.domUtils)
return null;
var cssRules = styleSheet.cssRules;
for (var i = 0; i < cssRules.length; ++i)
{
var rule = cssRules[i];
var previousRule;
if (rule instanceof window.CSSStyleRule)
{
var selectorLine = Dom.domUtils.getRuleLine(rule);
// The declarations are on lines equal or greater than the selectorLine
if (selectorLine === line) // then the line requested is a selector line
return rule;
if (selectorLine > line) // then we passed the rule for the requested line
return previousRule;
// else the requested line is still ahead
previousRule = rule;
}
}
},
highlightRule: function(rule)
{
var ruleElement = Firebug.getElementByRepObject(this.panelNode.firstChild, rule);
if (ruleElement)
{
Dom.scrollIntoCenterView(ruleElement, this.panelNode);
Css.setClassTimed(ruleElement, "jumpHighlight", this.context);
}
},
getStyleSheetRules: function(context, styleSheet)
{
if (!styleSheet)
return [];
var isSystemSheet = Url.isSystemStyleSheet(styleSheet);
var rules = [];
var appendRules = function(cssRules)
{
var i, props;
if (!cssRules)
return;
for (i=0; i<cssRules.length; ++i)
{
var rule = cssRules[i];
if (rule instanceof window.CSSStyleRule)
{
props = this.getRuleProperties(context, rule);
rules.push({
tag: CSSStyleRuleTag.tag,
rule: rule,
selector: rule.selectorText.replace(/ :/g, " *:"), // (issue 3683)
props: props,
isSystemSheet: isSystemSheet,
isSelectorEditable: true
});
}
else if (rule instanceof window.CSSImportRule)
{
rules.push({tag: CSSImportRuleTag.tag, rule: rule});
}
else if (rule instanceof window.CSSCharsetRule)
{
rules.push({tag: CSSCharsetRuleTag.tag, rule: rule});
}
else if (rule instanceof window.CSSMediaRule ||
rule instanceof window.CSSMozDocumentRule)
{
appendRules(Css.safeGetCSSRules(rule));
}
else if (rule instanceof window.CSSFontFaceRule)
{
props = this.parseCSSProps(rule.style);
this.sortProperties(props);
rules.push({
tag: CSSFontFaceRuleTag.tag, rule: rule,
props: props, isSystemSheet: isSystemSheet,
isNotEditable: true
});
}
else if (rule instanceof window.CSSNameSpaceRule &&
!(rule instanceof window.MozCSSKeyframesRule ||
rule instanceof window.MozCSSKeyframeRule))
{
// Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=754772
// MozCSSKeyframesRules and MozCSSKeyframeRules are recognized as
// CSSNameSpaceRules, so explicitly check whether the rule is not a
// MozCSSKeyframesRule or a MozCSSKeyframeRule
var reNamespace = /^@namespace ((.+) )?url\("(.*?)"\);$/;
var namespace = rule.cssText.match(reNamespace);
var prefix = namespace[2] || "";
var name = namespace[3];
rules.push({tag: CSSNamespaceRuleTag.tag, rule: rule, prefix: prefix,
name: name, isNotEditable: true});
}
else
{
if (FBTrace.DBG_ERRORS && FBTrace.DBG_CSS)
FBTrace.sysout("css getStyleSheetRules failed to classify a rule ", rule);
}
}
}.bind(this);
appendRules(Css.safeGetCSSRules(styleSheet));
return rules;
},
parseCSSProps: function(style, inheritMode)
{
var m;
var props = [];
if (Firebug.expandShorthandProps)
{
var count = style.length-1;
var index = style.length;
while (index--)
{
var propName = style.item(count - index);
this.addProperty(propName, style.getPropertyValue(propName),
!!style.getPropertyPriority(propName), false, inheritMode, props);
}
}
else
{
var lines = style.cssText.match(/(?:[^;\(]*(?:\([^\)]*?\))?[^;\(]*)*;?/g);
var propRE = /\s*([^:\s]*)\s*:\s*(.*?)\s*(! important)?;?$/;
var line;
var i=0;
while(line = lines[i++])
{
m = propRE.exec(line);
if(!m)
continue;
//var name = m[1], value = m[2], important = !!m[3];
if (m[2])
this.addProperty(m[1], m[2], !!m[3], false, inheritMode, props);
}
}
return props;
},
sortProperties: function(props)
{
props.sort(function(a, b)
{
return a.name > b.name ? 1 : -1;
});
},
getRuleProperties: function(context, rule, inheritMode)
{
var props = this.parseCSSProps(rule.style, inheritMode);
this.addDisabledProperties(context, rule, inheritMode, props);
this.sortProperties(props);
return props;
},
addDisabledProperties: function(context, rule, inheritMode, props)
{
var disabledMap = this.getDisabledMap(context);
var moreProps = disabledMap.get(rule);
if (moreProps)
{
var propMap = {};
for (var i = 0; i < props.length; ++i)
propMap[props[i].name] = true;
for (var i = 0; i < moreProps.length; ++i)
{
var prop = moreProps[i];
if (propMap.hasOwnProperty(prop.name))
{
// A (probably enabled) property with the same name as this
// disabled one has appeared - remove this one entirely.
moreProps.splice(i, 1);
--i;
continue;
}
propMap[prop.name] = true;
this.addProperty(prop.name, prop.value, prop.important, true, inheritMode, props);
}
}
},
addProperty: function(name, value, important, disabled, inheritMode, props)
{
if (inheritMode && !Dom.domUtils.isInheritedProperty(name))
return;
name = this.translateName(name, value);
if (name)
{
if (Options.get("colorDisplay") == "hex")
value = Css.rgbToHex(value);
else if (Options.get("colorDisplay") == "hsl")
value = Css.rgbToHSL(value);
value = Css.stripUnits(value);
important = important ? " !important" : "";
var prop = {name: name, value: value, important: important, disabled: disabled};
props.push(prop);
}
},
translateName: function(name, value)
{
// Don't show these proprietary Mozilla properties
if ((value == "-moz-initial"
&& (name == "-moz-background-clip" || name == "-moz-background-origin"
|| name == "-moz-background-inline-policy"))
|| (value == "physical"
&& (name == "margin-left-ltr-source" || name == "margin-left-rtl-source"
|| name == "margin-right-ltr-source" || name == "margin-right-rtl-source"))
|| (value == "physical"
&& (name == "padding-left-ltr-source" || name == "padding-left-rtl-source"
|| name == "padding-right-ltr-source" || name == "padding-right-rtl-source")))
return null;
// Translate these back to the form the user probably expects
if (name == "margin-left-value")
return "margin-left";
else if (name == "margin-right-value")
return "margin-right";
else if (name == "margin-top-value")
return "margin-top";
else if (name == "margin-bottom-value")
return "margin-bottom";
else if (name == "padding-left-value")
return "padding-left";
else if (name == "padding-right-value")
return "padding-right";
else if (name == "padding-top-value")
return "padding-top";
else if (name == "padding-bottom-value")
return "padding-bottom";
// XXXjoe What about border!
else
return name;
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
getDisabledMap: function(context)
{
// Ideally, we'd use a WeakMap here, but WeakMaps don't allow CSS rules
// as keys before Firefox 17. A Map is used instead. (cf. bug 777373.)
if (!context.cssDisabledMap)
context.cssDisabledMap = new Map();
return context.cssDisabledMap;
},
remapRule: function(context, oldRule, newRule)
{
var map = this.getDisabledMap(context);
if (map.has(oldRule))
map.set(newRule, map.get(oldRule));
},
editElementStyle: function()
{
var rulesBox = this.panelNode.getElementsByClassName("cssElementRuleContainer")[0];
var styleRuleBox = rulesBox && Firebug.getElementByRepObject(rulesBox, this.selection);
if (!styleRuleBox)
{
var rule = {rule: this.selection, inherited: false, selector: "element.style", props: []};
if (!rulesBox)
{
// The element did not have any displayed styles. We need to create the
// whole tree and remove the no styles message
styleRuleBox = this.template.cascadedTag.replace({
rules: [rule], inherited: [], inheritLabel: Locale.$STR("InheritedFrom")
}, this.panelNode);
styleRuleBox = styleRuleBox.getElementsByClassName("cssElementRuleContainer")[0];
}
else
styleRuleBox = this.template.ruleTag.insertBefore({rule: rule}, rulesBox);
styleRuleBox = styleRuleBox.getElementsByClassName("insertInto")[0];
}
Firebug.Editor.insertRowForObject(styleRuleBox);
},
addRelatedRule: function()
{
if (!this.panelNode.getElementsByClassName("cssElementRuleContainer")[0])
{
// The element did not have any displayed styles - create the whole
// tree and remove the no styles message.
this.template.cascadedTag.replace({
rules: [], inherited: [],
inheritLabel: Locale.$STR("InheritedFrom")
}, this.panelNode);
}
// Insert the new rule at the top, or after the style rules if there
// are any.
var container = this.panelNode.getElementsByClassName("cssNonInherited")[0];
var ruleBox = container.getElementsByClassName("cssElementRuleContainer")[0];
var styleRuleBox = ruleBox && Firebug.getElementByRepObject(ruleBox, this.selection);
if (styleRuleBox)
ruleBox = this.template.newRuleTag.insertAfter({}, ruleBox);
else if (ruleBox)
ruleBox = this.template.newRuleTag.insertBefore({}, ruleBox);
else
ruleBox = this.template.newRuleTag.append({}, container);
var before = ruleBox.getElementsByClassName("insertBefore")[0];
Firebug.Editor.insertRow(before, "before");
// Auto-fill the selector field with something reasonable, like
// ".some-class" or "#table td".
var el = this.selection, doc = el.ownerDocument;
var base = Xml.getNodeName(el), autofill;
if (el.className)
{
autofill = "." + Arr.cloneArray(el.classList).join(".");
}
else
{
var level = 0;
el = el.parentNode;
while (!autofill && el !== doc)
{
++level;
if (el.id !== "")
autofill = "#" + el.id;
else if (el.className !== "")
autofill = "." + Arr.cloneArray(el.classList).join(".");
el = el.parentNode;
}
if (autofill)
{
if (level === 1)
autofill += " >";
autofill += " " + base;
}
}
if (!autofill ||
doc.querySelectorAll(autofill).length === doc.querySelectorAll(base).length)
{
autofill = base;
}
this.ruleEditor.setValue(autofill);
this.ruleEditor.input.select();
Firebug.Editor.update(true);
},
editMediaQuery: function(target)
{
var row = Dom.getAncestorByClass(target, "cssRule");
var mediaQueryBox = Dom.getChildByClass(row, "cssMediaQuery");
Firebug.Editor.startEditing(mediaQueryBox);
},
insertPropertyRow: function(row)
{
Firebug.Editor.insertRowForObject(row);
},
insertRule: function(row)
{
var location = Dom.getAncestorByClass(row, "cssRule");
if (!location)
{
location = Dom.getChildByClass(this.panelNode, "cssSheet");
// Stylesheet has no rules
if (!location)
this.template.tag.replace({rules: []}, this.panelNode);
location = Dom.getChildByClass(this.panelNode, "cssSheet");
Firebug.Editor.insertRowForObject(location);
}
else
{
Firebug.Editor.insertRow(location, "before");
}
},
editPropertyRow: function(row)
{
var propValueBox = Dom.getChildByClass(row, "cssPropValue");
Firebug.Editor.startEditing(propValueBox);
},
deletePropertyRow: function(row)
{
var rule = Firebug.getRepObject(row);
var propName = Dom.getChildByClass(row, "cssPropName").textContent;
// Try removing the property from the "disabled" map.
var wasDisabled = this.removeDisabledProperty(rule, propName);
// If that fails, remove the actual property instead.
if (!wasDisabled)
CSSModule.deleteProperty(rule, propName, this.context);
if (this.name == "stylesheet")
Events.dispatch(this.fbListeners, "onInlineEditorClose", [this, row.firstChild, true]);
row.parentNode.removeChild(row);
this.markChange(this.name == "stylesheet");
},
removeDisabledProperty: function(rule, propName)
{
var disabledMap = this.getDisabledMap(this.context);
var map = disabledMap.get(rule);
if (!map)
return false;
for (var i = 0; i < map.length; ++i)
{
if (map[i].name === propName)
{
map.splice(i, 1);
return true;
}
}
return false;
},
disablePropertyRow: function(row)
{
Css.toggleClass(row, "disabledStyle");
var rule = Firebug.getRepObject(row);
var propName = Dom.getChildByClass(row, "cssPropName").textContent;
var disabledMap = this.getDisabledMap(this.context);
if (!disabledMap.has(rule))
disabledMap.set(rule, []);
var map = disabledMap.get(rule);
var propValue = Firebug.getRepObject(Dom.getChildByClass(row, "cssPropValue"));
var parsedValue = parsePriority(propValue);
CSSModule.disableProperty(Css.hasClass(row, "disabledStyle"), rule,
propName, parsedValue, map, this.context);
this.markChange(this.name == "stylesheet");
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// When handling disable button clicks, we cannot simply use a 'click'
// event, because refresh() may be (and often is) called in between
// mousedown and mouseup, replacing the DOM structure. Instead, a
// description of the moused-down disable button's property is saved
// and explicitly checked on mouseup (issue 5500).
clickedPropTag: null,
getPropTag: function(event)
{
var row = Dom.getAncestorByClass(event.target, "cssProp");
var rule = Firebug.getRepObject(row);
var propName = Dom.getChildByClass(row, "cssPropName").textContent;
return {
a: rule, b: propName,
equals: function(other)
{
return (other && this.a === other.a && this.b === other.b);
}
};
},
clickedDisableButton: function(event)
{
// XXX hack
if (event.clientX > 20)
return false;
if (Css.hasClass(event.target, "textEditor inlineExpander"))
return false;
var row = Dom.getAncestorByClass(event.target, "cssProp");
return (row && Css.hasClass(row, "editGroup"));
},
onMouseDown: function(event)
{
this.clickedPropTag = null;
if (Events.isLeftClick(event) && this.clickedDisableButton(event))
{
this.clickedPropTag = this.getPropTag(event);
// Don't select text when double-clicking the disable button.
Events.cancelEvent(event);
}
},
onMouseUp: function(event)
{
if (Events.isLeftClick(event) && this.clickedDisableButton(event) &&
this.getPropTag(event).equals(this.clickedPropTag))
{
var row = Dom.getAncestorByClass(event.target, "cssProp");
this.disablePropertyRow(row);
Events.cancelEvent(event);
}
this.clickedPropTag = null;
},
onClick: function(event)
{
if (!Events.isLeftClick(event))
return;
if (Events.isDoubleClick(event) && !this.clickedDisableButton(event))
{
var row = Dom.getAncestorByClass(event.target, "cssRule");
if (row && !Dom.getAncestorByClass(event.target, "cssPropName")
&& !Dom.getAncestorByClass(event.target, "cssPropValue"))
{
this.insertPropertyRow(row);
Events.cancelEvent(event);
}
}
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// extends Panel
name: "stylesheet",
parentPanel: null,
searchable: true,
dependents: ["css", "stylesheet", "dom", "domSide", "layout"],
enableA11y: true,
deriveA11yFrom: "css",
order: 30,
initialize: function()
{
this.onMouseDown = Obj.bind(this.onMouseDown, this);
this.onMouseUp = Obj.bind(this.onMouseUp, this);
this.onClick = Obj.bind(this.onClick, this);
Firebug.Panel.initialize.apply(this, arguments);
},
destroy: function(state)
{
state.scrollTop = this.panelNode.scrollTop ? this.panelNode.scrollTop : this.lastScrollTop;
Persist.persistObjects(this, state);
this.stopEditing();
Firebug.Panel.destroy.apply(this, arguments);
},
initializeNode: function(oldPanelNode)
{
Events.addEventListener(this.panelNode, "mousedown", this.onMouseDown, false);
Events.addEventListener(this.panelNode, "mouseup", this.onMouseUp, false);
Events.addEventListener(this.panelNode, "click", this.onClick, false);
Firebug.Panel.initializeNode.apply(this, arguments);
},
destroyNode: function()
{
Events.removeEventListener(this.panelNode, "mousedown", this.onMouseDown, false);
Events.removeEventListener(this.panelNode, "mouseup", this.onMouseUp, false);
Events.removeEventListener(this.panelNode, "click", this.onClick, false);
Firebug.Panel.destroyNode.apply(this, arguments);
},
show: function(state)
{
Firebug.Inspector.stopInspecting(true);
this.showToolbarButtons("fbCSSButtons", true);
CSSModule.updateEditButton();
// wait for loadedContext to restore the panel
if (this.context.loaded && !this.location)
{
Persist.restoreObjects(this, state);
if (!this.location)
this.location = this.getDefaultLocation();
if (state && state.scrollTop)
this.panelNode.scrollTop = state.scrollTop;
}
},
hide: function()
{
this.lastScrollTop = this.panelNode.scrollTop;
},
supportsObject: function(object, type)
{
if (object instanceof window.CSSStyleSheet)
{
return 1;
}
else if (object instanceof window.CSSRule ||
(object instanceof window.CSSStyleDeclaration && object.parentRule) ||
(object instanceof SourceLink.SourceLink && object.type == "css" &&
Url.reCSS.test(object.href)))
{
return 2;
}
else
{
return 0;
}
},
updateLocation: function(styleSheet)
{
if (FBTrace.DBG_CSS)
FBTrace.sysout("css.updateLocation; " + (styleSheet ? styleSheet.href : "no stylesheet"));
var rules = [];
if (styleSheet)
{
if (!Css.shouldIgnoreSheet(styleSheet))
{
if (styleSheet.editStyleSheet)
styleSheet = styleSheet.editStyleSheet.sheet;
var rules = this.getStyleSheetRules(this.context, styleSheet);
}
}
if (rules.length)
{
this.template.tag.replace({rules: rules}, this.panelNode);
}
else
{