forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSSEditUtils.cpp
1477 lines (1324 loc) · 51.5 KB
/
CSSEditUtils.cpp
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/CSSEditUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/ChangeStyleTransaction.h"
#include "mozilla/HTMLEditor.h"
#include "mozilla/Preferences.h"
#include "mozilla/DeclarationBlockInlines.h"
#include "mozilla/css/StyleRule.h"
#include "mozilla/dom/Element.h"
#include "mozilla/mozalloc.h"
#include "nsAString.h"
#include "nsCOMPtr.h"
#include "nsColor.h"
#include "nsComputedDOMStyle.h"
#include "nsDebug.h"
#include "nsDependentSubstring.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsIAtom.h"
#include "nsIContent.h"
#include "nsIDOMCSSStyleDeclaration.h"
#include "nsIDOMElement.h"
#include "nsIDOMNode.h"
#include "nsIDOMWindow.h"
#include "nsIDocument.h"
#include "nsIEditor.h"
#include "nsINode.h"
#include "nsISupportsImpl.h"
#include "nsISupportsUtils.h"
#include "nsLiteralString.h"
#include "nsPIDOMWindow.h"
#include "nsReadableUtils.h"
#include "nsString.h"
#include "nsStringFwd.h"
#include "nsStringIterator.h"
#include "nsStyledElement.h"
#include "nsSubstringTuple.h"
#include "nsUnicharUtils.h"
namespace mozilla {
using namespace dom;
static
void ProcessBValue(const nsAString* aInputString,
nsAString& aOutputString,
const char* aDefaultValueString,
const char* aPrependString,
const char* aAppendString)
{
if (aInputString && aInputString->EqualsLiteral("-moz-editor-invert-value")) {
aOutputString.AssignLiteral("normal");
}
else {
aOutputString.AssignLiteral("bold");
}
}
static
void ProcessDefaultValue(const nsAString* aInputString,
nsAString& aOutputString,
const char* aDefaultValueString,
const char* aPrependString,
const char* aAppendString)
{
CopyASCIItoUTF16(aDefaultValueString, aOutputString);
}
static
void ProcessSameValue(const nsAString* aInputString,
nsAString & aOutputString,
const char* aDefaultValueString,
const char* aPrependString,
const char* aAppendString)
{
if (aInputString) {
aOutputString.Assign(*aInputString);
}
else
aOutputString.Truncate();
}
static
void ProcessExtendedValue(const nsAString* aInputString,
nsAString& aOutputString,
const char* aDefaultValueString,
const char* aPrependString,
const char* aAppendString)
{
aOutputString.Truncate();
if (aInputString) {
if (aPrependString) {
AppendASCIItoUTF16(aPrependString, aOutputString);
}
aOutputString.Append(*aInputString);
if (aAppendString) {
AppendASCIItoUTF16(aAppendString, aOutputString);
}
}
}
static
void ProcessLengthValue(const nsAString* aInputString,
nsAString& aOutputString,
const char* aDefaultValueString,
const char* aPrependString,
const char* aAppendString)
{
aOutputString.Truncate();
if (aInputString) {
aOutputString.Append(*aInputString);
if (-1 == aOutputString.FindChar(char16_t('%'))) {
aOutputString.AppendLiteral("px");
}
}
}
static
void ProcessListStyleTypeValue(const nsAString* aInputString,
nsAString& aOutputString,
const char* aDefaultValueString,
const char* aPrependString,
const char* aAppendString)
{
aOutputString.Truncate();
if (aInputString) {
if (aInputString->EqualsLiteral("1")) {
aOutputString.AppendLiteral("decimal");
}
else if (aInputString->EqualsLiteral("a")) {
aOutputString.AppendLiteral("lower-alpha");
}
else if (aInputString->EqualsLiteral("A")) {
aOutputString.AppendLiteral("upper-alpha");
}
else if (aInputString->EqualsLiteral("i")) {
aOutputString.AppendLiteral("lower-roman");
}
else if (aInputString->EqualsLiteral("I")) {
aOutputString.AppendLiteral("upper-roman");
}
else if (aInputString->EqualsLiteral("square")
|| aInputString->EqualsLiteral("circle")
|| aInputString->EqualsLiteral("disc")) {
aOutputString.Append(*aInputString);
}
}
}
static
void ProcessMarginLeftValue(const nsAString* aInputString,
nsAString& aOutputString,
const char* aDefaultValueString,
const char* aPrependString,
const char* aAppendString)
{
aOutputString.Truncate();
if (aInputString) {
if (aInputString->EqualsLiteral("center") ||
aInputString->EqualsLiteral("-moz-center")) {
aOutputString.AppendLiteral("auto");
}
else if (aInputString->EqualsLiteral("right") ||
aInputString->EqualsLiteral("-moz-right")) {
aOutputString.AppendLiteral("auto");
}
else {
aOutputString.AppendLiteral("0px");
}
}
}
static
void ProcessMarginRightValue(const nsAString* aInputString,
nsAString& aOutputString,
const char* aDefaultValueString,
const char* aPrependString,
const char* aAppendString)
{
aOutputString.Truncate();
if (aInputString) {
if (aInputString->EqualsLiteral("center") ||
aInputString->EqualsLiteral("-moz-center")) {
aOutputString.AppendLiteral("auto");
}
else if (aInputString->EqualsLiteral("left") ||
aInputString->EqualsLiteral("-moz-left")) {
aOutputString.AppendLiteral("auto");
}
else {
aOutputString.AppendLiteral("0px");
}
}
}
const CSSEditUtils::CSSEquivTable boldEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_font_weight, ProcessBValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable italicEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_font_style, ProcessDefaultValue, "italic", nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable underlineEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_text_decoration, ProcessDefaultValue, "underline", nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable strikeEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_text_decoration, ProcessDefaultValue, "line-through", nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable ttEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_font_family, ProcessDefaultValue, "monospace", nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable fontColorEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_color, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable fontFaceEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_font_family, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable bgcolorEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_background_color, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable backgroundImageEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_background_image, ProcessExtendedValue, nullptr, "url(", ")", true, true },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable textColorEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_color, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable borderEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_border, ProcessExtendedValue, nullptr, nullptr, "px solid", true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable textAlignEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_text_align, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable captionAlignEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_caption_side, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable verticalAlignEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_vertical_align, ProcessSameValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable nowrapEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_whitespace, ProcessDefaultValue, "nowrap", nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable widthEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_width, ProcessLengthValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable heightEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_height, ProcessLengthValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable listStyleTypeEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_list_style_type, ProcessListStyleTypeValue, nullptr, nullptr, nullptr, true, true },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable tableAlignEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_text_align, ProcessDefaultValue, "left", nullptr, nullptr, false, false },
{ CSSEditUtils::eCSSEditableProperty_margin_left, ProcessMarginLeftValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_margin_right, ProcessMarginRightValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
const CSSEditUtils::CSSEquivTable hrAlignEquivTable[] = {
{ CSSEditUtils::eCSSEditableProperty_margin_left, ProcessMarginLeftValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_margin_right, ProcessMarginRightValue, nullptr, nullptr, nullptr, true, false },
{ CSSEditUtils::eCSSEditableProperty_NONE, 0 }
};
CSSEditUtils::CSSEditUtils(HTMLEditor* aHTMLEditor)
: mHTMLEditor(aHTMLEditor)
, mIsCSSPrefChecked(true)
{
// let's retrieve the value of the "CSS editing" pref
mIsCSSPrefChecked = Preferences::GetBool("editor.use_css", mIsCSSPrefChecked);
}
CSSEditUtils::~CSSEditUtils()
{
}
// Answers true if we have some CSS equivalence for the HTML style defined
// by aProperty and/or aAttribute for the node aNode
bool
CSSEditUtils::IsCSSEditableProperty(nsINode* aNode,
nsIAtom* aProperty,
const nsAString* aAttribute)
{
nsCOMPtr<nsIAtom> attribute = aAttribute ? NS_Atomize(*aAttribute) : nullptr;
return IsCSSEditableProperty(aNode, aProperty, attribute);
}
bool
CSSEditUtils::IsCSSEditableProperty(nsINode* aNode,
nsIAtom* aProperty,
nsIAtom* aAttribute)
{
MOZ_ASSERT(aNode);
nsINode* node = aNode;
// we need an element node here
if (node->NodeType() == nsIDOMNode::TEXT_NODE) {
node = node->GetParentNode();
NS_ENSURE_TRUE(node, false);
}
// html inline styles B I TT U STRIKE and COLOR/FACE on FONT
if (nsGkAtoms::b == aProperty ||
nsGkAtoms::i == aProperty ||
nsGkAtoms::tt == aProperty ||
nsGkAtoms::u == aProperty ||
nsGkAtoms::strike == aProperty ||
(nsGkAtoms::font == aProperty && aAttribute &&
(aAttribute == nsGkAtoms::color || aAttribute == nsGkAtoms::face))) {
return true;
}
// ALIGN attribute on elements supporting it
if (aAttribute == nsGkAtoms::align &&
node->IsAnyOfHTMLElements(nsGkAtoms::div,
nsGkAtoms::p,
nsGkAtoms::h1,
nsGkAtoms::h2,
nsGkAtoms::h3,
nsGkAtoms::h4,
nsGkAtoms::h5,
nsGkAtoms::h6,
nsGkAtoms::td,
nsGkAtoms::th,
nsGkAtoms::table,
nsGkAtoms::hr,
// For the above, why not use
// HTMLEditUtils::SupportsAlignAttr?
// It also checks for tbody, tfoot, thead.
// Let's add the following elements here even
// if "align" has a different meaning for them
nsGkAtoms::legend,
nsGkAtoms::caption)) {
return true;
}
if (aAttribute == nsGkAtoms::valign &&
node->IsAnyOfHTMLElements(nsGkAtoms::col,
nsGkAtoms::colgroup,
nsGkAtoms::tbody,
nsGkAtoms::td,
nsGkAtoms::th,
nsGkAtoms::tfoot,
nsGkAtoms::thead,
nsGkAtoms::tr)) {
return true;
}
// attributes TEXT, BACKGROUND and BGCOLOR on BODY
if (node->IsHTMLElement(nsGkAtoms::body) &&
(aAttribute == nsGkAtoms::text || aAttribute == nsGkAtoms::background ||
aAttribute == nsGkAtoms::bgcolor)) {
return true;
}
// attribute BGCOLOR on other elements
if (aAttribute == nsGkAtoms::bgcolor) {
return true;
}
// attributes HEIGHT, WIDTH and NOWRAP on TD and TH
if (node->IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th) &&
(aAttribute == nsGkAtoms::height || aAttribute == nsGkAtoms::width ||
aAttribute == nsGkAtoms::nowrap)) {
return true;
}
// attributes HEIGHT and WIDTH on TABLE
if (node->IsHTMLElement(nsGkAtoms::table) &&
(aAttribute == nsGkAtoms::height || aAttribute == nsGkAtoms::width)) {
return true;
}
// attributes SIZE and WIDTH on HR
if (node->IsHTMLElement(nsGkAtoms::hr) &&
(aAttribute == nsGkAtoms::size || aAttribute == nsGkAtoms::width)) {
return true;
}
// attribute TYPE on OL UL LI
if (node->IsAnyOfHTMLElements(nsGkAtoms::ol, nsGkAtoms::ul,
nsGkAtoms::li) &&
aAttribute == nsGkAtoms::type) {
return true;
}
if (node->IsHTMLElement(nsGkAtoms::img) &&
(aAttribute == nsGkAtoms::border || aAttribute == nsGkAtoms::width ||
aAttribute == nsGkAtoms::height)) {
return true;
}
// other elements that we can align using CSS even if they
// can't carry the html ALIGN attribute
if (aAttribute == nsGkAtoms::align &&
node->IsAnyOfHTMLElements(nsGkAtoms::ul,
nsGkAtoms::ol,
nsGkAtoms::dl,
nsGkAtoms::li,
nsGkAtoms::dd,
nsGkAtoms::dt,
nsGkAtoms::address,
nsGkAtoms::pre)) {
return true;
}
return false;
}
// The lowest level above the transaction; adds the CSS declaration
// "aProperty : aValue" to the inline styles carried by aElement
nsresult
CSSEditUtils::SetCSSProperty(Element& aElement,
nsIAtom& aProperty,
const nsAString& aValue,
bool aSuppressTxn)
{
RefPtr<ChangeStyleTransaction> transaction =
CreateCSSPropertyTxn(aElement, aProperty, aValue,
ChangeStyleTransaction::eSet);
if (aSuppressTxn) {
return transaction->DoTransaction();
}
return mHTMLEditor->DoTransaction(transaction);
}
nsresult
CSSEditUtils::SetCSSPropertyPixels(Element& aElement,
nsIAtom& aProperty,
int32_t aIntValue)
{
nsAutoString s;
s.AppendInt(aIntValue);
return SetCSSProperty(aElement, aProperty, s + NS_LITERAL_STRING("px"),
/* suppress txn */ false);
}
// The lowest level above the transaction; removes the value aValue from the
// list of values specified for the CSS property aProperty, or totally remove
// the declaration if this property accepts only one value
nsresult
CSSEditUtils::RemoveCSSProperty(Element& aElement,
nsIAtom& aProperty,
const nsAString& aValue,
bool aSuppressTxn)
{
RefPtr<ChangeStyleTransaction> transaction =
CreateCSSPropertyTxn(aElement, aProperty, aValue,
ChangeStyleTransaction::eRemove);
if (aSuppressTxn) {
return transaction->DoTransaction();
}
return mHTMLEditor->DoTransaction(transaction);
}
already_AddRefed<ChangeStyleTransaction>
CSSEditUtils::CreateCSSPropertyTxn(
Element& aElement,
nsIAtom& aAttribute,
const nsAString& aValue,
ChangeStyleTransaction::EChangeType aChangeType)
{
RefPtr<ChangeStyleTransaction> transaction =
new ChangeStyleTransaction(aElement, aAttribute, aValue, aChangeType);
return transaction.forget();
}
nsresult
CSSEditUtils::GetSpecifiedProperty(nsINode& aNode,
nsIAtom& aProperty,
nsAString& aValue)
{
return GetCSSInlinePropertyBase(&aNode, &aProperty, aValue, eSpecified);
}
nsresult
CSSEditUtils::GetComputedProperty(nsINode& aNode,
nsIAtom& aProperty,
nsAString& aValue)
{
return GetCSSInlinePropertyBase(&aNode, &aProperty, aValue, eComputed);
}
nsresult
CSSEditUtils::GetCSSInlinePropertyBase(nsINode* aNode,
nsIAtom* aProperty,
nsAString& aValue,
StyleType aStyleType)
{
MOZ_ASSERT(aNode && aProperty);
aValue.Truncate();
nsCOMPtr<Element> element = GetElementContainerOrSelf(aNode);
NS_ENSURE_TRUE(element, NS_ERROR_NULL_POINTER);
if (aStyleType == eComputed) {
// Get the all the computed css styles attached to the element node
RefPtr<nsComputedDOMStyle> cssDecl = GetComputedStyle(element);
NS_ENSURE_STATE(cssDecl);
// from these declarations, get the one we want and that one only
MOZ_ALWAYS_SUCCEEDS(
cssDecl->GetPropertyValue(nsDependentAtomString(aProperty), aValue));
return NS_OK;
}
MOZ_ASSERT(aStyleType == eSpecified);
RefPtr<DeclarationBlock> decl = element->GetInlineStyleDeclaration();
if (!decl) {
return NS_OK;
}
nsCSSPropertyID prop =
nsCSSProps::LookupProperty(nsDependentAtomString(aProperty),
CSSEnabledState::eForAllContent);
MOZ_ASSERT(prop != eCSSProperty_UNKNOWN);
decl->GetPropertyValueByID(prop, aValue);
return NS_OK;
}
already_AddRefed<nsComputedDOMStyle>
CSSEditUtils::GetComputedStyle(Element* aElement)
{
MOZ_ASSERT(aElement);
nsIDocument* doc = aElement->GetUncomposedDoc();
NS_ENSURE_TRUE(doc, nullptr);
nsIPresShell* presShell = doc->GetShell();
NS_ENSURE_TRUE(presShell, nullptr);
RefPtr<nsComputedDOMStyle> style =
NS_NewComputedDOMStyle(aElement, EmptyString(), presShell);
return style.forget();
}
// remove the CSS style "aProperty : aPropertyValue" and possibly remove the whole node
// if it is a span and if its only attribute is _moz_dirty
nsresult
CSSEditUtils::RemoveCSSInlineStyle(nsINode& aNode,
nsIAtom* aProperty,
const nsAString& aPropertyValue)
{
RefPtr<Element> element = aNode.AsElement();
NS_ENSURE_STATE(element);
// remove the property from the style attribute
nsresult rv = RemoveCSSProperty(*element, *aProperty, aPropertyValue);
NS_ENSURE_SUCCESS(rv, rv);
if (!element->IsHTMLElement(nsGkAtoms::span) ||
HTMLEditor::HasAttributes(element)) {
return NS_OK;
}
return mHTMLEditor->RemoveContainer(element);
}
// Answers true if the property can be removed by setting a "none" CSS value
// on a node
bool
CSSEditUtils::IsCSSInvertible(nsIAtom& aProperty,
const nsAString* aAttribute)
{
return nsGkAtoms::b == &aProperty;
}
// Get the default browser background color if we need it for GetCSSBackgroundColorState
void
CSSEditUtils::GetDefaultBackgroundColor(nsAString& aColor)
{
if (Preferences::GetBool("editor.use_custom_colors", false)) {
nsresult rv = Preferences::GetString("editor.background_color", &aColor);
// XXX Why don't you validate the pref value?
if (NS_FAILED(rv)) {
NS_WARNING("failed to get editor.background_color");
aColor.AssignLiteral("#ffffff"); // Default to white
}
return;
}
if (Preferences::GetBool("browser.display.use_system_colors", false)) {
return;
}
nsresult rv =
Preferences::GetString("browser.display.background_color", &aColor);
// XXX Why don't you validate the pref value?
if (NS_FAILED(rv)) {
NS_WARNING("failed to get browser.display.background_color");
aColor.AssignLiteral("#ffffff"); // Default to white
}
}
// Get the default length unit used for CSS Indent/Outdent
void
CSSEditUtils::GetDefaultLengthUnit(nsAString& aLengthUnit)
{
nsresult rv =
Preferences::GetString("editor.css.default_length_unit", &aLengthUnit);
// XXX Why don't you validate the pref value?
if (NS_FAILED(rv)) {
aLengthUnit.AssignLiteral("px");
}
}
// Unfortunately, CSSStyleDeclaration::GetPropertyCSSValue is not yet
// implemented... We need then a way to determine the number part and the unit
// from aString, aString being the result of a GetPropertyValue query...
void
CSSEditUtils::ParseLength(const nsAString& aString,
float* aValue,
nsIAtom** aUnit)
{
if (aString.IsEmpty()) {
*aValue = 0;
*aUnit = NS_Atomize(aString).take();
return;
}
nsAString::const_iterator iter;
aString.BeginReading(iter);
float a = 10.0f , b = 1.0f, value = 0;
int8_t sign = 1;
int32_t i = 0, j = aString.Length();
char16_t c;
bool floatingPointFound = false;
c = *iter;
if (char16_t('-') == c) { sign = -1; iter++; i++; }
else if (char16_t('+') == c) { iter++; i++; }
while (i < j) {
c = *iter;
if ((char16_t('0') == c) ||
(char16_t('1') == c) ||
(char16_t('2') == c) ||
(char16_t('3') == c) ||
(char16_t('4') == c) ||
(char16_t('5') == c) ||
(char16_t('6') == c) ||
(char16_t('7') == c) ||
(char16_t('8') == c) ||
(char16_t('9') == c)) {
value = (value * a) + (b * (c - char16_t('0')));
b = b / 10 * a;
}
else if (!floatingPointFound && (char16_t('.') == c)) {
floatingPointFound = true;
a = 1.0f; b = 0.1f;
}
else break;
iter++;
i++;
}
*aValue = value * sign;
*aUnit = NS_Atomize(StringTail(aString, j-i)).take();
}
void
CSSEditUtils::GetCSSPropertyAtom(nsCSSEditableProperty aProperty,
nsIAtom** aAtom)
{
*aAtom = nullptr;
switch (aProperty) {
case eCSSEditableProperty_background_color:
*aAtom = nsGkAtoms::backgroundColor;
break;
case eCSSEditableProperty_background_image:
*aAtom = nsGkAtoms::background_image;
break;
case eCSSEditableProperty_border:
*aAtom = nsGkAtoms::border;
break;
case eCSSEditableProperty_caption_side:
*aAtom = nsGkAtoms::caption_side;
break;
case eCSSEditableProperty_color:
*aAtom = nsGkAtoms::color;
break;
case eCSSEditableProperty_float:
*aAtom = nsGkAtoms::_float;
break;
case eCSSEditableProperty_font_family:
*aAtom = nsGkAtoms::font_family;
break;
case eCSSEditableProperty_font_size:
*aAtom = nsGkAtoms::font_size;
break;
case eCSSEditableProperty_font_style:
*aAtom = nsGkAtoms::font_style;
break;
case eCSSEditableProperty_font_weight:
*aAtom = nsGkAtoms::fontWeight;
break;
case eCSSEditableProperty_height:
*aAtom = nsGkAtoms::height;
break;
case eCSSEditableProperty_list_style_type:
*aAtom = nsGkAtoms::list_style_type;
break;
case eCSSEditableProperty_margin_left:
*aAtom = nsGkAtoms::marginLeft;
break;
case eCSSEditableProperty_margin_right:
*aAtom = nsGkAtoms::marginRight;
break;
case eCSSEditableProperty_text_align:
*aAtom = nsGkAtoms::textAlign;
break;
case eCSSEditableProperty_text_decoration:
*aAtom = nsGkAtoms::text_decoration;
break;
case eCSSEditableProperty_vertical_align:
*aAtom = nsGkAtoms::vertical_align;
break;
case eCSSEditableProperty_whitespace:
*aAtom = nsGkAtoms::white_space;
break;
case eCSSEditableProperty_width:
*aAtom = nsGkAtoms::width;
break;
case eCSSEditableProperty_NONE:
// intentionally empty
break;
}
}
// Populate aProperty and aValueArray with the CSS declarations equivalent to the
// value aValue according to the equivalence table aEquivTable
void
CSSEditUtils::BuildCSSDeclarations(nsTArray<nsIAtom*>& aPropertyArray,
nsTArray<nsString>& aValueArray,
const CSSEquivTable* aEquivTable,
const nsAString* aValue,
bool aGetOrRemoveRequest)
{
// clear arrays
aPropertyArray.Clear();
aValueArray.Clear();
// if we have an input value, let's use it
nsAutoString value, lowerCasedValue;
if (aValue) {
value.Assign(*aValue);
lowerCasedValue.Assign(*aValue);
ToLowerCase(lowerCasedValue);
}
int8_t index = 0;
nsCSSEditableProperty cssProperty = aEquivTable[index].cssProperty;
while (cssProperty) {
if (!aGetOrRemoveRequest|| aEquivTable[index].gettable) {
nsAutoString cssValue, cssPropertyString;
nsIAtom * cssPropertyAtom;
// find the equivalent css value for the index-th property in
// the equivalence table
(*aEquivTable[index].processValueFunctor) ((!aGetOrRemoveRequest || aEquivTable[index].caseSensitiveValue) ? &value : &lowerCasedValue,
cssValue,
aEquivTable[index].defaultValue,
aEquivTable[index].prependValue,
aEquivTable[index].appendValue);
GetCSSPropertyAtom(cssProperty, &cssPropertyAtom);
aPropertyArray.AppendElement(cssPropertyAtom);
aValueArray.AppendElement(cssValue);
}
index++;
cssProperty = aEquivTable[index].cssProperty;
}
}
// Populate cssPropertyArray and cssValueArray with the declarations equivalent
// to aHTMLProperty/aAttribute/aValue for the node aNode
void
CSSEditUtils::GenerateCSSDeclarationsFromHTMLStyle(
Element* aElement,
nsIAtom* aHTMLProperty,
nsIAtom* aAttribute,
const nsAString* aValue,
nsTArray<nsIAtom*>& cssPropertyArray,
nsTArray<nsString>& cssValueArray,
bool aGetOrRemoveRequest)
{
MOZ_ASSERT(aElement);
const CSSEditUtils::CSSEquivTable* equivTable = nullptr;
if (nsGkAtoms::b == aHTMLProperty) {
equivTable = boldEquivTable;
} else if (nsGkAtoms::i == aHTMLProperty) {
equivTable = italicEquivTable;
} else if (nsGkAtoms::u == aHTMLProperty) {
equivTable = underlineEquivTable;
} else if (nsGkAtoms::strike == aHTMLProperty) {
equivTable = strikeEquivTable;
} else if (nsGkAtoms::tt == aHTMLProperty) {
equivTable = ttEquivTable;
} else if (aAttribute) {
if (nsGkAtoms::font == aHTMLProperty && aAttribute == nsGkAtoms::color) {
equivTable = fontColorEquivTable;
} else if (nsGkAtoms::font == aHTMLProperty &&
aAttribute == nsGkAtoms::face) {
equivTable = fontFaceEquivTable;
} else if (aAttribute == nsGkAtoms::bgcolor) {
equivTable = bgcolorEquivTable;
} else if (aAttribute == nsGkAtoms::background) {
equivTable = backgroundImageEquivTable;
} else if (aAttribute == nsGkAtoms::text) {
equivTable = textColorEquivTable;
} else if (aAttribute == nsGkAtoms::border) {
equivTable = borderEquivTable;
} else if (aAttribute == nsGkAtoms::align) {
if (aElement->IsHTMLElement(nsGkAtoms::table)) {
equivTable = tableAlignEquivTable;
} else if (aElement->IsHTMLElement(nsGkAtoms::hr)) {
equivTable = hrAlignEquivTable;
} else if (aElement->IsAnyOfHTMLElements(nsGkAtoms::legend,
nsGkAtoms::caption)) {
equivTable = captionAlignEquivTable;
} else {
equivTable = textAlignEquivTable;
}
} else if (aAttribute == nsGkAtoms::valign) {
equivTable = verticalAlignEquivTable;
} else if (aAttribute == nsGkAtoms::nowrap) {
equivTable = nowrapEquivTable;
} else if (aAttribute == nsGkAtoms::width) {
equivTable = widthEquivTable;
} else if (aAttribute == nsGkAtoms::height ||
(aElement->IsHTMLElement(nsGkAtoms::hr) &&
aAttribute == nsGkAtoms::size)) {
equivTable = heightEquivTable;
} else if (aAttribute == nsGkAtoms::type &&
aElement->IsAnyOfHTMLElements(nsGkAtoms::ol,
nsGkAtoms::ul,
nsGkAtoms::li)) {
equivTable = listStyleTypeEquivTable;
}
}
if (equivTable) {
BuildCSSDeclarations(cssPropertyArray, cssValueArray, equivTable,
aValue, aGetOrRemoveRequest);
}
}
// Add to aNode the CSS inline style equivalent to HTMLProperty/aAttribute/
// aValue for the node, and return in aCount the number of CSS properties set
// by the call. The Element version returns aCount instead.
int32_t
CSSEditUtils::SetCSSEquivalentToHTMLStyle(nsIDOMNode* aNode,
nsIAtom* aProperty,
const nsAString* aAttribute,
const nsAString* aValue,
bool aSuppressTransaction)
{
MOZ_ASSERT_IF(aAttribute, aValue);
// This can only fail if SetCSSProperty fails, which should only happen if
// something is pretty badly wrong. In this case we assert so that hopefully
// someone will notice, but there's nothing more sensible to do than just
// return the count and carry on.
nsCOMPtr<Element> element = do_QueryInterface(aNode);
return SetCSSEquivalentToHTMLStyle(element,
aProperty, aAttribute,
aValue, aSuppressTransaction);
}
int32_t
CSSEditUtils::SetCSSEquivalentToHTMLStyle(Element* aElement,
nsIAtom* aHTMLProperty,
const nsAString* aAttribute,
const nsAString* aValue,
bool aSuppressTransaction)
{
nsCOMPtr<nsIAtom> attribute = aAttribute ? NS_Atomize(*aAttribute) : nullptr;
return SetCSSEquivalentToHTMLStyle(aElement, aHTMLProperty, attribute,
aValue, aSuppressTransaction);
}
int32_t
CSSEditUtils::SetCSSEquivalentToHTMLStyle(Element* aElement,
nsIAtom* aHTMLProperty,
nsIAtom* aAttribute,
const nsAString* aValue,
bool aSuppressTransaction)
{
MOZ_ASSERT(aElement);
if (!IsCSSEditableProperty(aElement, aHTMLProperty, aAttribute)) {
return 0;
}
// we can apply the styles only if the node is an element and if we have
// an equivalence for the requested HTML style in this implementation
// Find the CSS equivalence to the HTML style
nsTArray<nsIAtom*> cssPropertyArray;
nsTArray<nsString> cssValueArray;
GenerateCSSDeclarationsFromHTMLStyle(aElement, aHTMLProperty, aAttribute,
aValue, cssPropertyArray, cssValueArray,
false);
// set the individual CSS inline styles
size_t count = cssPropertyArray.Length();
for (size_t index = 0; index < count; index++) {
nsresult rv = SetCSSProperty(*aElement, *cssPropertyArray[index],
cssValueArray[index], aSuppressTransaction);
if (NS_WARN_IF(NS_FAILED(rv))) {
return 0;
}
}
return count;
}
// Remove from aNode the CSS inline style equivalent to HTMLProperty/aAttribute/aValue for the node
nsresult
CSSEditUtils::RemoveCSSEquivalentToHTMLStyle(nsIDOMNode* aNode,
nsIAtom* aHTMLProperty,
const nsAString* aAttribute,
const nsAString* aValue,
bool aSuppressTransaction)
{
nsCOMPtr<Element> element = do_QueryInterface(aNode);
nsCOMPtr<nsIAtom> attribute = aAttribute ? NS_Atomize(*aAttribute) : nullptr;
return RemoveCSSEquivalentToHTMLStyle(element, aHTMLProperty, attribute,
aValue, aSuppressTransaction);
}
nsresult
CSSEditUtils::RemoveCSSEquivalentToHTMLStyle(Element* aElement,
nsIAtom* aHTMLProperty,
nsIAtom* aAttribute,
const nsAString* aValue,
bool aSuppressTransaction)
{
if (NS_WARN_IF(!aElement)) {
return NS_OK;
}
if (!IsCSSEditableProperty(aElement, aHTMLProperty, aAttribute)) {
return NS_OK;
}
// we can apply the styles only if the node is an element and if we have
// an equivalence for the requested HTML style in this implementation
// Find the CSS equivalence to the HTML style
nsTArray<nsIAtom*> cssPropertyArray;
nsTArray<nsString> cssValueArray;
GenerateCSSDeclarationsFromHTMLStyle(aElement, aHTMLProperty, aAttribute,
aValue, cssPropertyArray, cssValueArray,
true);
// remove the individual CSS inline styles
int32_t count = cssPropertyArray.Length();
for (int32_t index = 0; index < count; index++) {
nsresult rv = RemoveCSSProperty(*aElement,
*cssPropertyArray[index],
cssValueArray[index],