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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
|
/*
* Copyright (C) 2010 Google Inc. All rights reserved.
* Copyright (C) 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "InspectorCSSAgent.h"
#include "AuthorStyleSheets.h"
#include "CSSComputedStyleDeclaration.h"
#include "CSSImportRule.h"
#include "CSSPropertyNames.h"
#include "CSSPropertySourceData.h"
#include "CSSRule.h"
#include "CSSRuleList.h"
#include "CSSStyleRule.h"
#include "CSSStyleSheet.h"
#include "ContentSecurityPolicy.h"
#include "DOMWindow.h"
#include "ExceptionCodePlaceholder.h"
#include "FontCache.h"
#include "HTMLHeadElement.h"
#include "HTMLStyleElement.h"
#include "InspectorDOMAgent.h"
#include "InspectorHistory.h"
#include "InspectorPageAgent.h"
#include "InstrumentingAgents.h"
#include "NamedFlowCollection.h"
#include "Node.h"
#include "NodeList.h"
#include "PseudoElement.h"
#include "RenderNamedFlowFragment.h"
#include "SVGStyleElement.h"
#include "SelectorChecker.h"
#include "StyleProperties.h"
#include "StylePropertyShorthand.h"
#include "StyleResolver.h"
#include "StyleRule.h"
#include "StyleSheetList.h"
#include "WebKitNamedFlow.h"
#include <inspector/InspectorProtocolObjects.h>
#include <wtf/HashSet.h>
#include <wtf/Ref.h>
#include <wtf/Vector.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringConcatenate.h>
using namespace Inspector;
namespace WebCore {
enum ForcePseudoClassFlags {
PseudoClassNone = 0,
PseudoClassHover = 1 << 0,
PseudoClassFocus = 1 << 1,
PseudoClassActive = 1 << 2,
PseudoClassVisited = 1 << 3
};
static unsigned computePseudoClassMask(const InspectorArray& pseudoClassArray)
{
static NeverDestroyed<String> active(ASCIILiteral("active"));
static NeverDestroyed<String> hover(ASCIILiteral("hover"));
static NeverDestroyed<String> focus(ASCIILiteral("focus"));
static NeverDestroyed<String> visited(ASCIILiteral("visited"));
if (!pseudoClassArray.length())
return PseudoClassNone;
unsigned result = PseudoClassNone;
for (auto& pseudoClassValue : pseudoClassArray) {
String pseudoClass;
bool success = pseudoClassValue->asString(pseudoClass);
if (!success)
continue;
if (pseudoClass == active)
result |= PseudoClassActive;
else if (pseudoClass == hover)
result |= PseudoClassHover;
else if (pseudoClass == focus)
result |= PseudoClassFocus;
else if (pseudoClass == visited)
result |= PseudoClassVisited;
}
return result;
}
class ChangeRegionOversetTask {
public:
ChangeRegionOversetTask(InspectorCSSAgent*);
void scheduleFor(WebKitNamedFlow*, int documentNodeId);
void unschedule(WebKitNamedFlow*);
void reset();
void timerFired();
private:
InspectorCSSAgent* m_cssAgent;
Timer m_timer;
HashMap<WebKitNamedFlow*, int> m_namedFlows;
};
ChangeRegionOversetTask::ChangeRegionOversetTask(InspectorCSSAgent* cssAgent)
: m_cssAgent(cssAgent)
, m_timer(*this, &ChangeRegionOversetTask::timerFired)
{
}
void ChangeRegionOversetTask::scheduleFor(WebKitNamedFlow* namedFlow, int documentNodeId)
{
m_namedFlows.add(namedFlow, documentNodeId);
if (!m_timer.isActive())
m_timer.startOneShot(0);
}
void ChangeRegionOversetTask::unschedule(WebKitNamedFlow* namedFlow)
{
m_namedFlows.remove(namedFlow);
}
void ChangeRegionOversetTask::reset()
{
m_timer.stop();
m_namedFlows.clear();
}
void ChangeRegionOversetTask::timerFired()
{
// The timer is stopped on m_cssAgent destruction, so this method will never be called after m_cssAgent has been destroyed.
for (auto& namedFlow : m_namedFlows)
m_cssAgent->regionOversetChanged(namedFlow.key, namedFlow.value);
m_namedFlows.clear();
}
class InspectorCSSAgent::StyleSheetAction : public InspectorHistory::Action {
WTF_MAKE_NONCOPYABLE(StyleSheetAction);
public:
StyleSheetAction(const String& name, InspectorStyleSheet* styleSheet)
: InspectorHistory::Action(name)
, m_styleSheet(styleSheet)
{
}
protected:
RefPtr<InspectorStyleSheet> m_styleSheet;
};
class InspectorCSSAgent::SetStyleSheetTextAction final : public InspectorCSSAgent::StyleSheetAction {
WTF_MAKE_NONCOPYABLE(SetStyleSheetTextAction);
public:
SetStyleSheetTextAction(InspectorStyleSheet* styleSheet, const String& text)
: InspectorCSSAgent::StyleSheetAction(ASCIILiteral("SetStyleSheetText"), styleSheet)
, m_text(text)
{
}
virtual bool perform(ExceptionCode& ec) override
{
if (!m_styleSheet->getText(&m_oldText))
return false;
return redo(ec);
}
virtual bool undo(ExceptionCode& ec) override
{
if (m_styleSheet->setText(m_oldText, ec)) {
m_styleSheet->reparseStyleSheet(m_oldText);
return true;
}
return false;
}
virtual bool redo(ExceptionCode& ec) override
{
if (m_styleSheet->setText(m_text, ec)) {
m_styleSheet->reparseStyleSheet(m_text);
return true;
}
return false;
}
virtual String mergeId() override
{
return String::format("SetStyleSheetText %s", m_styleSheet->id().utf8().data());
}
virtual void merge(std::unique_ptr<Action> action) override
{
ASSERT(action->mergeId() == mergeId());
SetStyleSheetTextAction* other = static_cast<SetStyleSheetTextAction*>(action.get());
m_text = other->m_text;
}
private:
String m_text;
String m_oldText;
};
class InspectorCSSAgent::SetStyleTextAction final : public InspectorCSSAgent::StyleSheetAction {
WTF_MAKE_NONCOPYABLE(SetStyleTextAction);
public:
SetStyleTextAction(InspectorStyleSheet* styleSheet, const InspectorCSSId& cssId, const String& text)
: InspectorCSSAgent::StyleSheetAction(ASCIILiteral("SetStyleText"), styleSheet)
, m_cssId(cssId)
, m_text(text)
{
}
virtual bool perform(ExceptionCode& ec) override
{
return redo(ec);
}
virtual bool undo(ExceptionCode& ec) override
{
return m_styleSheet->setStyleText(m_cssId, m_oldText, nullptr, ec);
}
virtual bool redo(ExceptionCode& ec) override
{
return m_styleSheet->setStyleText(m_cssId, m_text, &m_oldText, ec);
}
virtual String mergeId() override
{
ASSERT(m_styleSheet->id() == m_cssId.styleSheetId());
return String::format("SetStyleText %s:%u", m_styleSheet->id().utf8().data(), m_cssId.ordinal());
}
virtual void merge(std::unique_ptr<Action> action) override
{
ASSERT(action->mergeId() == mergeId());
SetStyleTextAction* other = static_cast<SetStyleTextAction*>(action.get());
m_text = other->m_text;
}
private:
InspectorCSSId m_cssId;
String m_text;
String m_oldText;
};
class InspectorCSSAgent::SetRuleSelectorAction final : public InspectorCSSAgent::StyleSheetAction {
WTF_MAKE_NONCOPYABLE(SetRuleSelectorAction);
public:
SetRuleSelectorAction(InspectorStyleSheet* styleSheet, const InspectorCSSId& cssId, const String& selector)
: InspectorCSSAgent::StyleSheetAction(ASCIILiteral("SetRuleSelector"), styleSheet)
, m_cssId(cssId)
, m_selector(selector)
{
}
virtual bool perform(ExceptionCode& ec) override
{
m_oldSelector = m_styleSheet->ruleSelector(m_cssId, ec);
if (ec)
return false;
return redo(ec);
}
virtual bool undo(ExceptionCode& ec) override
{
return m_styleSheet->setRuleSelector(m_cssId, m_oldSelector, ec);
}
virtual bool redo(ExceptionCode& ec) override
{
return m_styleSheet->setRuleSelector(m_cssId, m_selector, ec);
}
private:
InspectorCSSId m_cssId;
String m_selector;
String m_oldSelector;
};
class InspectorCSSAgent::AddRuleAction final : public InspectorCSSAgent::StyleSheetAction {
WTF_MAKE_NONCOPYABLE(AddRuleAction);
public:
AddRuleAction(InspectorStyleSheet* styleSheet, const String& selector)
: InspectorCSSAgent::StyleSheetAction(ASCIILiteral("AddRule"), styleSheet)
, m_selector(selector)
{
}
virtual bool perform(ExceptionCode& ec) override
{
return redo(ec);
}
virtual bool undo(ExceptionCode& ec) override
{
return m_styleSheet->deleteRule(m_newId, ec);
}
virtual bool redo(ExceptionCode& ec) override
{
CSSStyleRule* cssStyleRule = m_styleSheet->addRule(m_selector, ec);
if (ec)
return false;
m_newId = m_styleSheet->ruleId(cssStyleRule);
return true;
}
InspectorCSSId newRuleId() { return m_newId; }
private:
InspectorCSSId m_newId;
String m_selector;
String m_oldSelector;
};
// static
CSSStyleRule* InspectorCSSAgent::asCSSStyleRule(CSSRule& rule)
{
if (!is<CSSStyleRule>(rule))
return nullptr;
return downcast<CSSStyleRule>(&rule);
}
InspectorCSSAgent::InspectorCSSAgent(WebAgentContext& context, InspectorDOMAgent* domAgent)
: InspectorAgentBase(ASCIILiteral("CSS"), context)
, m_frontendDispatcher(std::make_unique<CSSFrontendDispatcher>(context.frontendRouter))
, m_backendDispatcher(CSSBackendDispatcher::create(context.backendDispatcher, this))
, m_domAgent(domAgent)
{
m_domAgent->setDOMListener(this);
}
InspectorCSSAgent::~InspectorCSSAgent()
{
ASSERT(!m_domAgent);
reset();
}
void InspectorCSSAgent::didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*)
{
}
void InspectorCSSAgent::willDestroyFrontendAndBackend(Inspector::DisconnectReason)
{
resetNonPersistentData();
String unused;
disable(unused);
}
void InspectorCSSAgent::discardAgent()
{
m_domAgent->setDOMListener(nullptr);
m_domAgent = nullptr;
}
void InspectorCSSAgent::reset()
{
// FIXME: Should we be resetting on main frame navigations?
m_idToInspectorStyleSheet.clear();
m_cssStyleSheetToInspectorStyleSheet.clear();
m_nodeToInspectorStyleSheet.clear();
m_documentToInspectorStyleSheet.clear();
m_documentToKnownCSSStyleSheets.clear();
resetNonPersistentData();
}
void InspectorCSSAgent::resetNonPersistentData()
{
m_namedFlowCollectionsRequested.clear();
if (m_changeRegionOversetTask)
m_changeRegionOversetTask->reset();
resetPseudoStates();
}
void InspectorCSSAgent::enable(ErrorString&)
{
m_instrumentingAgents.setInspectorCSSAgent(this);
for (auto* document : m_domAgent->documents())
activeStyleSheetsUpdated(*document);
}
void InspectorCSSAgent::disable(ErrorString&)
{
m_instrumentingAgents.setInspectorCSSAgent(nullptr);
}
void InspectorCSSAgent::documentDetached(Document& document)
{
Vector<CSSStyleSheet*> emptyList;
setActiveStyleSheetsForDocument(document, emptyList);
m_documentToKnownCSSStyleSheets.remove(&document);
}
void InspectorCSSAgent::mediaQueryResultChanged()
{
m_frontendDispatcher->mediaQueryResultChanged();
}
void InspectorCSSAgent::activeStyleSheetsUpdated(Document& document)
{
Vector<CSSStyleSheet*> cssStyleSheets;
collectAllDocumentStyleSheets(document, cssStyleSheets);
setActiveStyleSheetsForDocument(document, cssStyleSheets);
}
void InspectorCSSAgent::setActiveStyleSheetsForDocument(Document& document, Vector<CSSStyleSheet*>& activeStyleSheets)
{
HashSet<CSSStyleSheet*>& previouslyKnownActiveStyleSheets = m_documentToKnownCSSStyleSheets.add(&document, HashSet<CSSStyleSheet*>()).iterator->value;
HashSet<CSSStyleSheet*> removedStyleSheets(previouslyKnownActiveStyleSheets);
Vector<CSSStyleSheet*> addedStyleSheets;
for (auto& activeStyleSheet : activeStyleSheets) {
if (removedStyleSheets.contains(activeStyleSheet))
removedStyleSheets.remove(activeStyleSheet);
else
addedStyleSheets.append(activeStyleSheet);
}
for (auto* cssStyleSheet : removedStyleSheets) {
previouslyKnownActiveStyleSheets.remove(cssStyleSheet);
RefPtr<InspectorStyleSheet> inspectorStyleSheet = m_cssStyleSheetToInspectorStyleSheet.get(cssStyleSheet);
if (m_idToInspectorStyleSheet.contains(inspectorStyleSheet->id())) {
String id = unbindStyleSheet(inspectorStyleSheet.get());
m_frontendDispatcher->styleSheetRemoved(id);
}
}
for (auto* cssStyleSheet : addedStyleSheets) {
previouslyKnownActiveStyleSheets.add(cssStyleSheet);
if (!m_cssStyleSheetToInspectorStyleSheet.contains(cssStyleSheet)) {
InspectorStyleSheet* inspectorStyleSheet = bindStyleSheet(cssStyleSheet);
m_frontendDispatcher->styleSheetAdded(inspectorStyleSheet->buildObjectForStyleSheetInfo());
}
}
}
void InspectorCSSAgent::didCreateNamedFlow(Document& document, WebKitNamedFlow& namedFlow)
{
int documentNodeId = documentNodeWithRequestedFlowsId(&document);
if (!documentNodeId)
return;
ErrorString unused;
m_frontendDispatcher->namedFlowCreated(buildObjectForNamedFlow(unused, &namedFlow, documentNodeId));
}
void InspectorCSSAgent::willRemoveNamedFlow(Document& document, WebKitNamedFlow& namedFlow)
{
int documentNodeId = documentNodeWithRequestedFlowsId(&document);
if (!documentNodeId)
return;
if (m_changeRegionOversetTask)
m_changeRegionOversetTask->unschedule(&namedFlow);
m_frontendDispatcher->namedFlowRemoved(documentNodeId, namedFlow.name().string());
}
void InspectorCSSAgent::didChangeRegionOverset(Document& document, WebKitNamedFlow& namedFlow)
{
int documentNodeId = documentNodeWithRequestedFlowsId(&document);
if (!documentNodeId)
return;
if (!m_changeRegionOversetTask)
m_changeRegionOversetTask = std::make_unique<ChangeRegionOversetTask>(this);
m_changeRegionOversetTask->scheduleFor(&namedFlow, documentNodeId);
}
void InspectorCSSAgent::regionOversetChanged(WebKitNamedFlow* namedFlow, int documentNodeId)
{
if (namedFlow->flowState() == WebKitNamedFlow::FlowStateNull)
return;
ErrorString unused;
Ref<WebKitNamedFlow> protect(*namedFlow);
m_frontendDispatcher->regionOversetChanged(buildObjectForNamedFlow(unused, namedFlow, documentNodeId));
}
void InspectorCSSAgent::didRegisterNamedFlowContentElement(Document& document, WebKitNamedFlow& namedFlow, Node& contentElement, Node* nextContentElement)
{
int documentNodeId = documentNodeWithRequestedFlowsId(&document);
if (!documentNodeId)
return;
ErrorString unused;
int contentElementNodeId = m_domAgent->pushNodeToFrontend(unused, documentNodeId, &contentElement);
int nextContentElementNodeId = nextContentElement ? m_domAgent->pushNodeToFrontend(unused, documentNodeId, nextContentElement) : 0;
m_frontendDispatcher->registeredNamedFlowContentElement(documentNodeId, namedFlow.name().string(), contentElementNodeId, nextContentElementNodeId);
}
void InspectorCSSAgent::didUnregisterNamedFlowContentElement(Document& document, WebKitNamedFlow& namedFlow, Node& contentElement)
{
int documentNodeId = documentNodeWithRequestedFlowsId(&document);
if (!documentNodeId)
return;
ErrorString unused;
int contentElementNodeId = m_domAgent->pushNodeToFrontend(unused, documentNodeId, &contentElement);
if (!contentElementNodeId) {
// We've already notified that the DOM node was removed from the DOM, so there's no need to send another event.
return;
}
m_frontendDispatcher->unregisteredNamedFlowContentElement(documentNodeId, namedFlow.name().string(), contentElementNodeId);
}
bool InspectorCSSAgent::forcePseudoState(Element& element, CSSSelector::PseudoClassType pseudoClassType)
{
if (m_nodeIdToForcedPseudoState.isEmpty())
return false;
int nodeId = m_domAgent->boundNodeId(&element);
if (!nodeId)
return false;
NodeIdToForcedPseudoState::iterator it = m_nodeIdToForcedPseudoState.find(nodeId);
if (it == m_nodeIdToForcedPseudoState.end())
return false;
unsigned forcedPseudoState = it->value;
switch (pseudoClassType) {
case CSSSelector::PseudoClassActive:
return forcedPseudoState & PseudoClassActive;
case CSSSelector::PseudoClassFocus:
return forcedPseudoState & PseudoClassFocus;
case CSSSelector::PseudoClassHover:
return forcedPseudoState & PseudoClassHover;
case CSSSelector::PseudoClassVisited:
return forcedPseudoState & PseudoClassVisited;
default:
return false;
}
}
void InspectorCSSAgent::getMatchedStylesForNode(ErrorString& errorString, int nodeId, const bool* includePseudo, const bool* includeInherited, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::RuleMatch>>& matchedCSSRules, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::PseudoIdMatches>>& pseudoIdMatches, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::InheritedStyleEntry>>& inheritedEntries)
{
Element* element = elementForId(errorString, nodeId);
if (!element)
return;
Element* originalElement = element;
PseudoId elementPseudoId = element->pseudoId();
if (elementPseudoId) {
element = downcast<PseudoElement>(*element).hostElement();
if (!element) {
errorString = ASCIILiteral("Pseudo element has no parent");
return;
}
}
// Matched rules.
StyleResolver& styleResolver = element->styleResolver();
auto matchedRules = styleResolver.pseudoStyleRulesForElement(element, elementPseudoId, StyleResolver::AllCSSRules);
matchedCSSRules = buildArrayForMatchedRuleList(matchedRules, styleResolver, element, elementPseudoId);
if (!originalElement->isPseudoElement()) {
// Pseudo elements.
if (!includePseudo || *includePseudo) {
auto pseudoElements = Inspector::Protocol::Array<Inspector::Protocol::CSS::PseudoIdMatches>::create();
for (PseudoId pseudoId = FIRST_PUBLIC_PSEUDOID; pseudoId < AFTER_LAST_INTERNAL_PSEUDOID; pseudoId = static_cast<PseudoId>(pseudoId + 1)) {
auto matchedRules = styleResolver.pseudoStyleRulesForElement(element, pseudoId, StyleResolver::AllCSSRules);
if (!matchedRules.isEmpty()) {
auto matches = Inspector::Protocol::CSS::PseudoIdMatches::create()
.setPseudoId(static_cast<int>(pseudoId))
.setMatches(buildArrayForMatchedRuleList(matchedRules, styleResolver, element, pseudoId))
.release();
pseudoElements->addItem(WTFMove(matches));
}
}
pseudoIdMatches = WTFMove(pseudoElements);
}
// Inherited styles.
if (!includeInherited || *includeInherited) {
auto entries = Inspector::Protocol::Array<Inspector::Protocol::CSS::InheritedStyleEntry>::create();
Element* parentElement = element->parentElement();
while (parentElement) {
StyleResolver& parentStyleResolver = parentElement->styleResolver();
auto parentMatchedRules = parentStyleResolver.styleRulesForElement(parentElement, StyleResolver::AllCSSRules);
auto entry = Inspector::Protocol::CSS::InheritedStyleEntry::create()
.setMatchedCSSRules(buildArrayForMatchedRuleList(parentMatchedRules, styleResolver, parentElement, NOPSEUDO))
.release();
if (parentElement->cssomStyle() && parentElement->cssomStyle()->length()) {
if (InspectorStyleSheetForInlineStyle* styleSheet = asInspectorStyleSheet(parentElement))
entry->setInlineStyle(styleSheet->buildObjectForStyle(styleSheet->styleForId(InspectorCSSId(styleSheet->id(), 0))));
}
entries->addItem(WTFMove(entry));
parentElement = parentElement->parentElement();
}
inheritedEntries = WTFMove(entries);
}
}
}
void InspectorCSSAgent::getInlineStylesForNode(ErrorString& errorString, int nodeId, RefPtr<Inspector::Protocol::CSS::CSSStyle>& inlineStyle, RefPtr<Inspector::Protocol::CSS::CSSStyle>& attributesStyle)
{
Element* element = elementForId(errorString, nodeId);
if (!element)
return;
InspectorStyleSheetForInlineStyle* styleSheet = asInspectorStyleSheet(element);
if (!styleSheet)
return;
inlineStyle = styleSheet->buildObjectForStyle(element->cssomStyle());
RefPtr<Inspector::Protocol::CSS::CSSStyle> attributes = buildObjectForAttributesStyle(element);
attributesStyle = attributes ? attributes.release() : nullptr;
}
void InspectorCSSAgent::getComputedStyleForNode(ErrorString& errorString, int nodeId, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::CSSComputedStyleProperty>>& style)
{
Element* element = elementForId(errorString, nodeId);
if (!element)
return;
RefPtr<CSSComputedStyleDeclaration> computedStyleInfo = CSSComputedStyleDeclaration::create(element, true);
Ref<InspectorStyle> inspectorStyle = InspectorStyle::create(InspectorCSSId(), computedStyleInfo, nullptr);
style = inspectorStyle->buildArrayForComputedStyle();
}
void InspectorCSSAgent::getAllStyleSheets(ErrorString&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::CSSStyleSheetHeader>>& styleInfos)
{
styleInfos = Inspector::Protocol::Array<Inspector::Protocol::CSS::CSSStyleSheetHeader>::create();
Vector<InspectorStyleSheet*> inspectorStyleSheets;
collectAllStyleSheets(inspectorStyleSheets);
for (auto* inspectorStyleSheet : inspectorStyleSheets)
styleInfos->addItem(inspectorStyleSheet->buildObjectForStyleSheetInfo());
}
void InspectorCSSAgent::collectAllStyleSheets(Vector<InspectorStyleSheet*>& result)
{
Vector<CSSStyleSheet*> cssStyleSheets;
for (auto* document : m_domAgent->documents())
collectAllDocumentStyleSheets(*document, cssStyleSheets);
for (auto* cssStyleSheet : cssStyleSheets)
result.append(bindStyleSheet(cssStyleSheet));
}
void InspectorCSSAgent::collectAllDocumentStyleSheets(Document& document, Vector<CSSStyleSheet*>& result)
{
auto cssStyleSheets = document.authorStyleSheets().activeStyleSheetsForInspector();
for (auto& cssStyleSheet : cssStyleSheets)
collectStyleSheets(cssStyleSheet.get(), result);
}
void InspectorCSSAgent::collectStyleSheets(CSSStyleSheet* styleSheet, Vector<CSSStyleSheet*>& result)
{
result.append(styleSheet);
for (unsigned i = 0, size = styleSheet->length(); i < size; ++i) {
CSSRule* rule = styleSheet->item(i);
if (is<CSSImportRule>(*rule)) {
if (CSSStyleSheet* importedStyleSheet = downcast<CSSImportRule>(*rule).styleSheet())
collectStyleSheets(importedStyleSheet, result);
}
}
}
void InspectorCSSAgent::getStyleSheet(ErrorString& errorString, const String& styleSheetId, RefPtr<Inspector::Protocol::CSS::CSSStyleSheetBody>& styleSheetObject)
{
InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
if (!inspectorStyleSheet)
return;
styleSheetObject = inspectorStyleSheet->buildObjectForStyleSheet();
}
void InspectorCSSAgent::getStyleSheetText(ErrorString& errorString, const String& styleSheetId, String* result)
{
InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
if (!inspectorStyleSheet)
return;
inspectorStyleSheet->getText(result);
}
void InspectorCSSAgent::setStyleSheetText(ErrorString& errorString, const String& styleSheetId, const String& text)
{
InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
if (!inspectorStyleSheet)
return;
ExceptionCode ec = 0;
m_domAgent->history()->perform(std::make_unique<SetStyleSheetTextAction>(inspectorStyleSheet, text), ec);
errorString = InspectorDOMAgent::toErrorString(ec);
}
void InspectorCSSAgent::setStyleText(ErrorString& errorString, const InspectorObject& fullStyleId, const String& text, RefPtr<Inspector::Protocol::CSS::CSSStyle>& result)
{
InspectorCSSId compoundId(fullStyleId);
ASSERT(!compoundId.isEmpty());
InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, compoundId.styleSheetId());
if (!inspectorStyleSheet)
return;
ExceptionCode ec = 0;
bool success = m_domAgent->history()->perform(std::make_unique<SetStyleTextAction>(inspectorStyleSheet, compoundId, text), ec);
if (success)
result = inspectorStyleSheet->buildObjectForStyle(inspectorStyleSheet->styleForId(compoundId));
errorString = InspectorDOMAgent::toErrorString(ec);
}
void InspectorCSSAgent::setRuleSelector(ErrorString& errorString, const InspectorObject& fullRuleId, const String& selector, RefPtr<Inspector::Protocol::CSS::CSSRule>& result)
{
InspectorCSSId compoundId(fullRuleId);
ASSERT(!compoundId.isEmpty());
InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, compoundId.styleSheetId());
if (!inspectorStyleSheet)
return;
ExceptionCode ec = 0;
bool success = m_domAgent->history()->perform(std::make_unique<SetRuleSelectorAction>(inspectorStyleSheet, compoundId, selector), ec);
if (success)
result = inspectorStyleSheet->buildObjectForRule(inspectorStyleSheet->ruleForId(compoundId), nullptr);
errorString = InspectorDOMAgent::toErrorString(ec);
}
void InspectorCSSAgent::createStyleSheet(ErrorString& errorString, const String& frameId, String* styleSheetId)
{
Frame* frame = m_domAgent->pageAgent()->frameForId(frameId);
if (!frame) {
errorString = ASCIILiteral("No frame for given id found");
return;
}
Document* document = frame->document();
if (!document) {
errorString = ASCIILiteral("No document for frame");
return;
}
InspectorStyleSheet* inspectorStyleSheet = createInspectorStyleSheetForDocument(*document);
if (!inspectorStyleSheet) {
errorString = ASCIILiteral("Could not create stylesheet for the frame.");
return;
}
*styleSheetId = inspectorStyleSheet->id();
}
InspectorStyleSheet* InspectorCSSAgent::createInspectorStyleSheetForDocument(Document& document)
{
if (!document.isHTMLDocument() && !document.isSVGDocument())
return nullptr;
Ref<Element> styleElement = document.createElement(HTMLNames::styleTag, false);
styleElement->setAttribute(HTMLNames::typeAttr, "text/css");
ContainerNode* targetNode;
// HEAD is absent in ImageDocuments, for example.
if (auto* head = document.head())
targetNode = head;
else if (auto* body = document.bodyOrFrameset())
targetNode = body;
else
return nullptr;
// Inserting this <style> into the document will trigger activeStyleSheetsUpdated
// and we will create an InspectorStyleSheet for this <style>'s CSSStyleSheet.
// Set this flag, so when we create it, we put it into the via inspector map.
m_creatingViaInspectorStyleSheet = true;
InlineStyleOverrideScope overrideScope(document);
ExceptionCode ec = 0;
targetNode->appendChild(WTFMove(styleElement), ec);
m_creatingViaInspectorStyleSheet = false;
if (ec)
return nullptr;
auto iterator = m_documentToInspectorStyleSheet.find(&document);
ASSERT(iterator != m_documentToInspectorStyleSheet.end());
if (iterator == m_documentToInspectorStyleSheet.end())
return nullptr;
auto& inspectorStyleSheetsForDocument = iterator->value;
ASSERT(!inspectorStyleSheetsForDocument.isEmpty());
if (inspectorStyleSheetsForDocument.isEmpty())
return nullptr;
return inspectorStyleSheetsForDocument.last().get();
}
void InspectorCSSAgent::addRule(ErrorString& errorString, const String& styleSheetId, const String& selector, RefPtr<Inspector::Protocol::CSS::CSSRule>& result)
{
InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
if (!inspectorStyleSheet) {
errorString = ASCIILiteral("No target stylesheet found");
return;
}
ExceptionCode ec = 0;
auto action = std::make_unique<AddRuleAction>(inspectorStyleSheet, selector);
AddRuleAction* rawAction = action.get();
bool success = m_domAgent->history()->perform(WTFMove(action), ec);
if (!success) {
errorString = InspectorDOMAgent::toErrorString(ec);
return;
}
InspectorCSSId ruleId = rawAction->newRuleId();
CSSStyleRule* rule = inspectorStyleSheet->ruleForId(ruleId);
result = inspectorStyleSheet->buildObjectForRule(rule, nullptr);
}
void InspectorCSSAgent::getSupportedCSSProperties(ErrorString&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::CSSPropertyInfo>>& cssProperties)
{
auto properties = Inspector::Protocol::Array<Inspector::Protocol::CSS::CSSPropertyInfo>::create();
for (int i = firstCSSProperty; i <= lastCSSProperty; ++i) {
CSSPropertyID id = convertToCSSPropertyID(i);
auto property = Inspector::Protocol::CSS::CSSPropertyInfo::create()
.setName(getPropertyNameString(id))
.release();
const StylePropertyShorthand& shorthand = shorthandForProperty(id);
if (!shorthand.length()) {
properties->addItem(WTFMove(property));
continue;
}
auto longhands = Inspector::Protocol::Array<String>::create();
for (unsigned j = 0; j < shorthand.length(); ++j) {
CSSPropertyID longhandID = shorthand.properties()[j];
longhands->addItem(getPropertyNameString(longhandID));
}
property->setLonghands(WTFMove(longhands));
properties->addItem(WTFMove(property));
}
cssProperties = WTFMove(properties);
}
void InspectorCSSAgent::getSupportedSystemFontFamilyNames(ErrorString&, RefPtr<Inspector::Protocol::Array<String>>& fontFamilyNames)
{
auto families = Inspector::Protocol::Array<String>::create();
Vector<String> systemFontFamilies = FontCache::singleton().systemFontFamilies();
for (const auto& familyName : systemFontFamilies)
families->addItem(familyName);
fontFamilyNames = WTFMove(families);
}
void InspectorCSSAgent::forcePseudoState(ErrorString& errorString, int nodeId, const InspectorArray& forcedPseudoClasses)
{
Element* element = m_domAgent->assertElement(errorString, nodeId);
if (!element)
return;
unsigned forcedPseudoState = computePseudoClassMask(forcedPseudoClasses);
NodeIdToForcedPseudoState::iterator it = m_nodeIdToForcedPseudoState.find(nodeId);
unsigned currentForcedPseudoState = it == m_nodeIdToForcedPseudoState.end() ? 0 : it->value;
bool needStyleRecalc = forcedPseudoState != currentForcedPseudoState;
if (!needStyleRecalc)
return;
if (forcedPseudoState)
m_nodeIdToForcedPseudoState.set(nodeId, forcedPseudoState);
else
m_nodeIdToForcedPseudoState.remove(nodeId);
element->document().styleResolverChanged(RecalcStyleImmediately);
}
void InspectorCSSAgent::getNamedFlowCollection(ErrorString& errorString, int documentNodeId, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::NamedFlow>>& result)
{
Document* document = m_domAgent->assertDocument(errorString, documentNodeId);
if (!document)
return;
m_namedFlowCollectionsRequested.add(documentNodeId);
Vector<RefPtr<WebKitNamedFlow>> namedFlowsVector = document->namedFlows().namedFlows();
auto namedFlows = Inspector::Protocol::Array<Inspector::Protocol::CSS::NamedFlow>::create();
for (auto& namedFlow : namedFlowsVector)
namedFlows->addItem(buildObjectForNamedFlow(errorString, namedFlow.get(), documentNodeId));
result = WTFMove(namedFlows);
}
InspectorStyleSheetForInlineStyle* InspectorCSSAgent::asInspectorStyleSheet(Element* element)
{
NodeToInspectorStyleSheet::iterator it = m_nodeToInspectorStyleSheet.find(element);
if (it == m_nodeToInspectorStyleSheet.end()) {
CSSStyleDeclaration* style = element->cssomStyle();
if (!style)
return nullptr;
String newStyleSheetId = String::number(m_lastStyleSheetId++);
RefPtr<InspectorStyleSheetForInlineStyle> inspectorStyleSheet = InspectorStyleSheetForInlineStyle::create(m_domAgent->pageAgent(), newStyleSheetId, element, Inspector::Protocol::CSS::StyleSheetOrigin::Regular, this);
m_idToInspectorStyleSheet.set(newStyleSheetId, inspectorStyleSheet);
m_nodeToInspectorStyleSheet.set(element, inspectorStyleSheet);
return inspectorStyleSheet.get();
}
return it->value.get();
}
Element* InspectorCSSAgent::elementForId(ErrorString& errorString, int nodeId)
{
Node* node = m_domAgent->nodeForId(nodeId);
if (!node) {
errorString = ASCIILiteral("No node with given id found");
return nullptr;
}
if (!is<Element>(*node)) {
errorString = ASCIILiteral("Not an element node");
return nullptr;
}
return downcast<Element>(node);
}
int InspectorCSSAgent::documentNodeWithRequestedFlowsId(Document* document)
{
int documentNodeId = m_domAgent->boundNodeId(document);
if (!documentNodeId || !m_namedFlowCollectionsRequested.contains(documentNodeId))
return 0;
return documentNodeId;
}
String InspectorCSSAgent::unbindStyleSheet(InspectorStyleSheet* inspectorStyleSheet)
{
String id = inspectorStyleSheet->id();
m_idToInspectorStyleSheet.remove(id);
if (inspectorStyleSheet->pageStyleSheet())
m_cssStyleSheetToInspectorStyleSheet.remove(inspectorStyleSheet->pageStyleSheet());
return id;
}
InspectorStyleSheet* InspectorCSSAgent::bindStyleSheet(CSSStyleSheet* styleSheet)
{
RefPtr<InspectorStyleSheet> inspectorStyleSheet = m_cssStyleSheetToInspectorStyleSheet.get(styleSheet);
if (!inspectorStyleSheet) {
String id = String::number(m_lastStyleSheetId++);
Document* document = styleSheet->ownerDocument();
inspectorStyleSheet = InspectorStyleSheet::create(m_domAgent->pageAgent(), id, styleSheet, detectOrigin(styleSheet, document), InspectorDOMAgent::documentURLString(document), this);
m_idToInspectorStyleSheet.set(id, inspectorStyleSheet);
m_cssStyleSheetToInspectorStyleSheet.set(styleSheet, inspectorStyleSheet);
if (m_creatingViaInspectorStyleSheet) {
auto& inspectorStyleSheetsForDocument = m_documentToInspectorStyleSheet.add(document, Vector<RefPtr<InspectorStyleSheet>>()).iterator->value;
inspectorStyleSheetsForDocument.append(inspectorStyleSheet);
}
}
return inspectorStyleSheet.get();
}
InspectorStyleSheet* InspectorCSSAgent::assertStyleSheetForId(ErrorString& errorString, const String& styleSheetId)
{
IdToInspectorStyleSheet::iterator it = m_idToInspectorStyleSheet.find(styleSheetId);
if (it == m_idToInspectorStyleSheet.end()) {
errorString = ASCIILiteral("No stylesheet with given id found");
return nullptr;
}
return it->value.get();
}
Inspector::Protocol::CSS::StyleSheetOrigin InspectorCSSAgent::detectOrigin(CSSStyleSheet* pageStyleSheet, Document* ownerDocument)
{
if (m_creatingViaInspectorStyleSheet)
return Inspector::Protocol::CSS::StyleSheetOrigin::Inspector;
if (pageStyleSheet && !pageStyleSheet->ownerNode() && pageStyleSheet->href().isEmpty())
return Inspector::Protocol::CSS::StyleSheetOrigin::UserAgent;
if (pageStyleSheet && pageStyleSheet->ownerNode() && pageStyleSheet->ownerNode()->nodeName() == "#document")
return Inspector::Protocol::CSS::StyleSheetOrigin::User;
auto iterator = m_documentToInspectorStyleSheet.find(ownerDocument);
if (iterator != m_documentToInspectorStyleSheet.end()) {
for (auto& inspectorStyleSheet : iterator->value) {
if (pageStyleSheet == inspectorStyleSheet->pageStyleSheet())
return Inspector::Protocol::CSS::StyleSheetOrigin::Inspector;
}
}
return Inspector::Protocol::CSS::StyleSheetOrigin::Regular;
}
RefPtr<Inspector::Protocol::CSS::CSSRule> InspectorCSSAgent::buildObjectForRule(StyleRule* styleRule, StyleResolver& styleResolver, Element* element)
{
if (!styleRule)
return nullptr;
// StyleRules returned by StyleResolver::styleRulesForElement lack parent pointers since that infomation is not cheaply available.
// Since the inspector wants to walk the parent chain, we construct the full wrappers here.
CSSStyleRule* cssomWrapper = styleResolver.inspectorCSSOMWrappers().getWrapperForRuleInSheets(styleRule, styleResolver.document().authorStyleSheets(), styleResolver.document().extensionStyleSheets());
if (!cssomWrapper)
return nullptr;
InspectorStyleSheet* inspectorStyleSheet = bindStyleSheet(cssomWrapper->parentStyleSheet());
return inspectorStyleSheet ? inspectorStyleSheet->buildObjectForRule(cssomWrapper, element) : nullptr;
}
RefPtr<Inspector::Protocol::CSS::CSSRule> InspectorCSSAgent::buildObjectForRule(CSSStyleRule* rule)
{
if (!rule)
return nullptr;
ASSERT(rule->parentStyleSheet());
InspectorStyleSheet* inspectorStyleSheet = bindStyleSheet(rule->parentStyleSheet());
return inspectorStyleSheet ? inspectorStyleSheet->buildObjectForRule(rule, nullptr) : nullptr;
}
RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::RuleMatch>> InspectorCSSAgent::buildArrayForMatchedRuleList(const Vector<RefPtr<StyleRule>>& matchedRules, StyleResolver& styleResolver, Element* element, PseudoId psuedoId)
{
auto result = Inspector::Protocol::Array<Inspector::Protocol::CSS::RuleMatch>::create();
SelectorChecker::CheckingContext context(SelectorChecker::Mode::CollectingRules);
context.pseudoId = psuedoId ? psuedoId : element->pseudoId();
SelectorChecker selectorChecker(element->document());
for (auto& matchedRule : matchedRules) {
RefPtr<Inspector::Protocol::CSS::CSSRule> ruleObject = buildObjectForRule(matchedRule.get(), styleResolver, element);
if (!ruleObject)
continue;
auto matchingSelectors = Inspector::Protocol::Array<int>::create();
const CSSSelectorList& selectorList = matchedRule->selectorList();
int index = 0;
for (const CSSSelector* selector = selectorList.first(); selector; selector = CSSSelectorList::next(selector)) {
unsigned ignoredSpecificity;
bool matched = selectorChecker.match(*selector, *element, context, ignoredSpecificity);
if (matched)
matchingSelectors->addItem(index);
++index;
}
auto match = Inspector::Protocol::CSS::RuleMatch::create()
.setRule(WTFMove(ruleObject))
.setMatchingSelectors(WTFMove(matchingSelectors))
.release();
result->addItem(WTFMove(match));
}
return WTFMove(result);
}
RefPtr<Inspector::Protocol::CSS::CSSStyle> InspectorCSSAgent::buildObjectForAttributesStyle(Element* element)
{
ASSERT(element);
if (!is<StyledElement>(*element))
return nullptr;
// FIXME: Ugliness below.
StyleProperties* attributeStyle = const_cast<StyleProperties*>(downcast<StyledElement>(element)->presentationAttributeStyle());
if (!attributeStyle)
return nullptr;
ASSERT_WITH_SECURITY_IMPLICATION(attributeStyle->isMutable());
MutableStyleProperties* mutableAttributeStyle = static_cast<MutableStyleProperties*>(attributeStyle);
Ref<InspectorStyle> inspectorStyle = InspectorStyle::create(InspectorCSSId(), mutableAttributeStyle->ensureCSSStyleDeclaration(), nullptr);
return inspectorStyle->buildObjectForStyle();
}
RefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::Region>> InspectorCSSAgent::buildArrayForRegions(ErrorString& errorString, RefPtr<NodeList>&& regionList, int documentNodeId)
{
auto regions = Inspector::Protocol::Array<Inspector::Protocol::CSS::Region>::create();
for (unsigned i = 0; i < regionList->length(); ++i) {
Inspector::Protocol::CSS::Region::RegionOverset regionOverset;
switch (downcast<Element>(regionList->item(i))->regionOversetState()) {
case RegionFit:
regionOverset = Inspector::Protocol::CSS::Region::RegionOverset::Fit;
break;
case RegionEmpty:
regionOverset = Inspector::Protocol::CSS::Region::RegionOverset::Empty;
break;
case RegionOverset:
regionOverset = Inspector::Protocol::CSS::Region::RegionOverset::Overset;
break;
case RegionUndefined:
continue;
default:
ASSERT_NOT_REACHED();
continue;
}
auto region = Inspector::Protocol::CSS::Region::create()
.setRegionOverset(regionOverset)
// documentNodeId was previously asserted
.setNodeId(m_domAgent->pushNodeToFrontend(errorString, documentNodeId, regionList->item(i)))
.release();
regions->addItem(WTFMove(region));
}
return WTFMove(regions);
}
RefPtr<Inspector::Protocol::CSS::NamedFlow> InspectorCSSAgent::buildObjectForNamedFlow(ErrorString& errorString, WebKitNamedFlow* webkitNamedFlow, int documentNodeId)
{
RefPtr<NodeList> contentList = webkitNamedFlow->getContent();
auto content = Inspector::Protocol::Array<int>::create();
for (unsigned i = 0; i < contentList->length(); ++i) {
// documentNodeId was previously asserted
content->addItem(m_domAgent->pushNodeToFrontend(errorString, documentNodeId, contentList->item(i)));
}
return Inspector::Protocol::CSS::NamedFlow::create()
.setDocumentNodeId(documentNodeId)
.setName(webkitNamedFlow->name().string())
.setOverset(webkitNamedFlow->overset())
.setContent(WTFMove(content))
.setRegions(buildArrayForRegions(errorString, webkitNamedFlow->getRegions(), documentNodeId))
.release();
}
void InspectorCSSAgent::didRemoveDocument(Document* document)
{
if (document)
m_documentToInspectorStyleSheet.remove(document);
}
void InspectorCSSAgent::didRemoveDOMNode(Node* node)
{
if (!node)
return;
int nodeId = m_domAgent->boundNodeId(node);
if (nodeId)
m_nodeIdToForcedPseudoState.remove(nodeId);
NodeToInspectorStyleSheet::iterator it = m_nodeToInspectorStyleSheet.find(node);
if (it == m_nodeToInspectorStyleSheet.end())
return;
m_idToInspectorStyleSheet.remove(it->value->id());
m_nodeToInspectorStyleSheet.remove(node);
}
void InspectorCSSAgent::didModifyDOMAttr(Element* element)
{
if (!element)
return;
NodeToInspectorStyleSheet::iterator it = m_nodeToInspectorStyleSheet.find(element);
if (it == m_nodeToInspectorStyleSheet.end())
return;
it->value->didModifyElementAttribute();
}
void InspectorCSSAgent::styleSheetChanged(InspectorStyleSheet* styleSheet)
{
m_frontendDispatcher->styleSheetChanged(styleSheet->id());
}
void InspectorCSSAgent::resetPseudoStates()
{
HashSet<Document*> documentsToChange;
for (auto& nodeId : m_nodeIdToForcedPseudoState) {
if (Element* element = downcast<Element>(m_domAgent->nodeForId(nodeId.key)))
documentsToChange.add(&element->document());
}
m_nodeIdToForcedPseudoState.clear();
for (auto& document : documentsToChange)
document->styleResolverChanged(RecalcStyleImmediately);
}
} // namespace WebCore
|