forked from llimllib/chrome-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSS.py
454 lines (349 loc) · 18.1 KB
/
CSS.py
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
from enum import Enum
from typing import Any, List
from base import ChromeCommand
import Page
import DOM
StyleSheetId = str
StyleSheetOrigin = Enum("StyleSheetOrigin", "injected user-agent inspector regular")
StyleSheetOrigin.__doc__ = "Stylesheet type: "injected" for stylesheets injected via extension, "user-agent" for user-agent stylesheets, "inspector" for stylesheets created by the inspector (i.e. those holding the "via inspector" rules), "regular" for regular stylesheets."
class PseudoElementMatches:
"""CSS rule collection for a single pseudo style."""
def __init__(self, pseudoType: "DOM.PseudoType", matches: List):
# Pseudo element type.
self.pseudoType = pseudoType
# Matches of CSS rules applicable to the pseudo style.
self.matches = matches
class InheritedStyleEntry:
"""Inherited CSS rule collection from ancestor node."""
def __init__(self, matchedCSSRules: List, inlineStyle: "CSSStyle"=None):
# Matches of CSS rules matching the ancestor node in the style inheritance chain.
self.matchedCSSRules = matchedCSSRules
# The ancestor node's inline style, if any, in the style inheritance chain.
self.inlineStyle = inlineStyle
class RuleMatch:
"""Match data for a CSS rule."""
def __init__(self, rule: "CSSRule", matchingSelectors: List):
# CSS rule in the match.
self.rule = rule
# Matching selector indices in the rule's selectorList selectors (0-based).
self.matchingSelectors = matchingSelectors
class Value:
"""Data for a simple selector (these are delimited by commas in a selector list)."""
def __init__(self, text: str, range: "SourceRange"=None):
# Value text.
self.text = text
# Value range in the underlying resource (if available).
self.range = range
class SelectorList:
"""Selector list data."""
def __init__(self, selectors: List, text: str):
# Selectors in the list.
self.selectors = selectors
# Rule selector text.
self.text = text
class CSSStyleSheetHeader:
"""CSS stylesheet metainformation."""
def __init__(self, styleSheetId: "StyleSheetId", frameId: "Page.FrameId", sourceURL: str, origin: "StyleSheetOrigin", title: str, disabled: bool, isInline: bool, startLine: float, startColumn: float, sourceMapURL: str=None, ownerNode: "DOM.BackendNodeId"=None, hasSourceURL: bool=None):
# The stylesheet identifier.
self.styleSheetId = styleSheetId
# Owner frame identifier.
self.frameId = frameId
# Stylesheet resource URL.
self.sourceURL = sourceURL
# Stylesheet origin.
self.origin = origin
# Stylesheet title.
self.title = title
# Denotes whether the stylesheet is disabled.
self.disabled = disabled
# Whether this stylesheet is created for STYLE tag by parser. This flag is not set for document.written STYLE tags.
self.isInline = isInline
# Line offset of the stylesheet within the resource (zero based).
self.startLine = startLine
# Column offset of the stylesheet within the resource (zero based).
self.startColumn = startColumn
# URL of source map associated with the stylesheet (if any).
self.sourceMapURL = sourceMapURL
# The backend id for the owner node of the stylesheet.
self.ownerNode = ownerNode
# Whether the sourceURL field value comes from the sourceURL comment.
self.hasSourceURL = hasSourceURL
class CSSRule:
"""CSS rule representation."""
def __init__(self, selectorList: "SelectorList", origin: "StyleSheetOrigin", style: "CSSStyle", styleSheetId: "StyleSheetId"=None, media: List=None):
# Rule selector data.
self.selectorList = selectorList
# Parent stylesheet's origin.
self.origin = origin
# Associated style declaration.
self.style = style
# The css style sheet identifier (absent for user agent stylesheet and user-specified stylesheet rules) this rule came from.
self.styleSheetId = styleSheetId
# Media list array (for rules involving media queries). The array enumerates media queries starting with the innermost one, going outwards.
self.media = media
class RuleUsage:
"""CSS rule usage information."""
def __init__(self, styleSheetId: "StyleSheetId", range: "SourceRange", used: bool):
# The css style sheet identifier (absent for user agent stylesheet and user-specified stylesheet rules) this rule came from.
self.styleSheetId = styleSheetId
# Style declaration range in the enclosing stylesheet (if available).
self.range = range
# Indicates whether the rule was actually used by some element in the page.
self.used = used
class SourceRange:
"""Text range within a resource. All numbers are zero-based."""
def __init__(self, startLine: int, startColumn: int, endLine: int, endColumn: int):
# Start line of range.
self.startLine = startLine
# Start column of range (inclusive).
self.startColumn = startColumn
# End line of range
self.endLine = endLine
# End column of range (exclusive).
self.endColumn = endColumn
class ShorthandEntry:
def __init__(self, name: str, value: str, important: bool=None):
# Shorthand name.
self.name = name
# Shorthand value.
self.value = value
# Whether the property has "!important" annotation (implies <code>false</code> if absent).
self.important = important
class CSSComputedStyleProperty:
def __init__(self, name: str, value: str):
# Computed style property name.
self.name = name
# Computed style property value.
self.value = value
class CSSStyle:
"""CSS style representation."""
def __init__(self, cssProperties: List, shorthandEntries: List, styleSheetId: "StyleSheetId"=None, cssText: str=None, range: "SourceRange"=None):
# CSS properties in the style.
self.cssProperties = cssProperties
# Computed values for all shorthands found in the style.
self.shorthandEntries = shorthandEntries
# The css style sheet identifier (absent for user agent stylesheet and user-specified stylesheet rules) this rule came from.
self.styleSheetId = styleSheetId
# Style declaration text (if available).
self.cssText = cssText
# Style declaration range in the enclosing stylesheet (if available).
self.range = range
class CSSProperty:
"""CSS property declaration data."""
def __init__(self, name: str, value: str, important: bool=None, implicit: bool=None, text: str=None, parsedOk: bool=None, disabled: bool=None, range: "SourceRange"=None):
# The property name.
self.name = name
# The property value.
self.value = value
# Whether the property has "!important" annotation (implies <code>false</code> if absent).
self.important = important
# Whether the property is implicit (implies <code>false</code> if absent).
self.implicit = implicit
# The full property text as specified in the style.
self.text = text
# Whether the property is understood by the browser (implies <code>true</code> if absent).
self.parsedOk = parsedOk
# Whether the property is disabled by the user (present for source-based properties only).
self.disabled = disabled
# The entire property range in the enclosing style declaration (if available).
self.range = range
class CSSMedia:
"""CSS media rule descriptor."""
def __init__(self, text: str, source: str, sourceURL: str=None, range: "SourceRange"=None, styleSheetId: "StyleSheetId"=None, mediaList: List=None):
# Media query text.
self.text = text
# Source of the media query: "mediaRule" if specified by a @media rule, "importRule" if specified by an @import rule, "linkedSheet" if specified by a "media" attribute in a linked stylesheet's LINK tag, "inlineSheet" if specified by a "media" attribute in an inline stylesheet's STYLE tag.
self.source = source
# URL of the document containing the media query description.
self.sourceURL = sourceURL
# The associated rule (@media or @import) header range in the enclosing stylesheet (if available).
self.range = range
# Identifier of the stylesheet containing this object (if exists).
self.styleSheetId = styleSheetId
# Array of media queries.
self.mediaList = mediaList
class MediaQuery:
"""Media query descriptor."""
def __init__(self, expressions: List, active: bool):
# Array of media query expressions.
self.expressions = expressions
# Whether the media query condition is satisfied.
self.active = active
class MediaQueryExpression:
"""Media query expression descriptor."""
def __init__(self, value: float, unit: str, feature: str, valueRange: "SourceRange"=None, computedLength: float=None):
# Media query expression value.
self.value = value
# Media query expression units.
self.unit = unit
# Media query expression feature.
self.feature = feature
# The associated range of the value text in the enclosing stylesheet (if available).
self.valueRange = valueRange
# Computed length of media query expression (if applicable).
self.computedLength = computedLength
class PlatformFontUsage:
"""Information about amount of glyphs that were rendered with given font."""
def __init__(self, familyName: str, isCustomFont: bool, glyphCount: float):
# Font's family name reported by platform.
self.familyName = familyName
# Indicates if the font was downloaded or resolved locally.
self.isCustomFont = isCustomFont
# Amount of glyphs that were rendered with this font.
self.glyphCount = glyphCount
class CSSKeyframesRule:
"""CSS keyframes rule representation."""
def __init__(self, animationName: "Value", keyframes: List):
# Animation name.
self.animationName = animationName
# List of keyframes.
self.keyframes = keyframes
class CSSKeyframeRule:
"""CSS keyframe rule representation."""
def __init__(self, origin: "StyleSheetOrigin", keyText: "Value", style: "CSSStyle", styleSheetId: "StyleSheetId"=None):
# Parent stylesheet's origin.
self.origin = origin
# Associated key text.
self.keyText = keyText
# Associated style declaration.
self.style = style
# The css style sheet identifier (absent for user agent stylesheet and user-specified stylesheet rules) this rule came from.
self.styleSheetId = styleSheetId
class StyleDeclarationEdit:
"""A descriptor of operation to mutate style declaration text."""
def __init__(self, styleSheetId: "StyleSheetId", range: "SourceRange", text: str):
# The css style sheet identifier.
self.styleSheetId = styleSheetId
# The range of the style text in the enclosing stylesheet.
self.range = range
# New style text.
self.text = text
class InlineTextBox:
"""Details of post layout rendered text positions. The exact layout should not be regarded as stable and may change between versions."""
def __init__(self, boundingBox: "DOM.Rect", startCharacterIndex: int, numCharacters: int):
# The absolute position bounding box.
self.boundingBox = boundingBox
# The starting index in characters, for this post layout textbox substring.
self.startCharacterIndex = startCharacterIndex
# The number of characters in this post layout textbox substring.
self.numCharacters = numCharacters
class LayoutTreeNode:
"""Details of an element in the DOM tree with a LayoutObject."""
def __init__(self, nodeId: "DOM.NodeId", boundingBox: "DOM.Rect", layoutText: str=None, inlineTextNodes: List=None, styleIndex: int=None):
# The id of the related DOM node matching one from DOM.GetDocument.
self.nodeId = nodeId
# The absolute position bounding box.
self.boundingBox = boundingBox
# Contents of the LayoutText if any
self.layoutText = layoutText
# The post layout inline text nodes, if any.
self.inlineTextNodes = inlineTextNodes
# Index into the computedStyles array returned by getLayoutTreeAndStyles.
self.styleIndex = styleIndex
class ComputedStyle:
"""A subset of the full ComputedStyle as defined by the request whitelist."""
def __init__(self, properties: List):
self.properties = properties
class enable(ChromeCommand):
"""Enables the CSS agent for the given page. Clients should not assume that the CSS agent has been enabled until the result of this command is received."""
def __init__(self): pass
class disable(ChromeCommand):
"""Disables the CSS agent for the given page."""
def __init__(self): pass
class getMatchedStylesForNode(ChromeCommand):
"""Returns requested styles for a DOM node identified by <code>nodeId</code>."""
def __init__(self, nodeId: "DOM.NodeId"):
self.nodeId = nodeId
class getInlineStylesForNode(ChromeCommand):
"""Returns the styles defined inline (explicitly in the "style" attribute and implicitly, using DOM attributes) for a DOM node identified by <code>nodeId</code>."""
def __init__(self, nodeId: "DOM.NodeId"):
self.nodeId = nodeId
class getComputedStyleForNode(ChromeCommand):
"""Returns the computed style for a DOM node identified by <code>nodeId</code>."""
def __init__(self, nodeId: "DOM.NodeId"):
self.nodeId = nodeId
class getPlatformFontsForNode(ChromeCommand):
"""Requests information about platform fonts which we used to render child TextNodes in the given node."""
def __init__(self, nodeId: "DOM.NodeId"):
self.nodeId = nodeId
class getStyleSheetText(ChromeCommand):
"""Returns the current textual content and the URL for a stylesheet."""
def __init__(self, styleSheetId: "StyleSheetId"):
self.styleSheetId = styleSheetId
class collectClassNames(ChromeCommand):
"""Returns all class names from specified stylesheet."""
def __init__(self, styleSheetId: "StyleSheetId"):
self.styleSheetId = styleSheetId
class setStyleSheetText(ChromeCommand):
"""Sets the new stylesheet text."""
def __init__(self, styleSheetId: "StyleSheetId", text: str):
self.styleSheetId = styleSheetId
self.text = text
class setRuleSelector(ChromeCommand):
"""Modifies the rule selector."""
def __init__(self, styleSheetId: "StyleSheetId", range: "SourceRange", selector: str):
self.styleSheetId = styleSheetId
self.range = range
self.selector = selector
class setKeyframeKey(ChromeCommand):
"""Modifies the keyframe rule key text."""
def __init__(self, styleSheetId: "StyleSheetId", range: "SourceRange", keyText: str):
self.styleSheetId = styleSheetId
self.range = range
self.keyText = keyText
class setStyleTexts(ChromeCommand):
"""Applies specified style edits one after another in the given order."""
def __init__(self, edits: List):
self.edits = edits
class setMediaText(ChromeCommand):
"""Modifies the rule selector."""
def __init__(self, styleSheetId: "StyleSheetId", range: "SourceRange", text: str):
self.styleSheetId = styleSheetId
self.range = range
self.text = text
class createStyleSheet(ChromeCommand):
"""Creates a new special "via-inspector" stylesheet in the frame with given <code>frameId</code>."""
def __init__(self, frameId: "Page.FrameId"):
# Identifier of the frame where "via-inspector" stylesheet should be created.
self.frameId = frameId
class addRule(ChromeCommand):
"""Inserts a new rule with the given <code>ruleText</code> in a stylesheet with given <code>styleSheetId</code>, at the position specified by <code>location</code>."""
def __init__(self, styleSheetId: "StyleSheetId", ruleText: str, location: "SourceRange"):
# The css style sheet identifier where a new rule should be inserted.
self.styleSheetId = styleSheetId
# The text of a new rule.
self.ruleText = ruleText
# Text position of a new rule in the target style sheet.
self.location = location
class forcePseudoState(ChromeCommand):
"""Ensures that the given node will have specified pseudo-classes whenever its style is computed by the browser."""
def __init__(self, nodeId: "DOM.NodeId", forcedPseudoClasses: List):
# The element id for which to force the pseudo state.
self.nodeId = nodeId
# Element pseudo classes to force when computing the element's style.
self.forcedPseudoClasses = forcedPseudoClasses
class getMediaQueries(ChromeCommand):
"""Returns all media queries parsed by the rendering engine."""
def __init__(self): pass
class setEffectivePropertyValueForNode(ChromeCommand):
"""Find a rule with the given active property for the given node and set the new value for this property"""
def __init__(self, nodeId: "DOM.NodeId", propertyName: str, value: str):
# The element id for which to set property.
self.nodeId = nodeId
self.propertyName = propertyName
self.value = value
class getBackgroundColors(ChromeCommand):
def __init__(self, nodeId: "DOM.NodeId"):
# Id of the node to get background colors for.
self.nodeId = nodeId
class getLayoutTreeAndStyles(ChromeCommand):
"""For the main document and any content documents, return the LayoutTreeNodes and a whitelisted subset of the computed style. It only returns pushed nodes, on way to pull all nodes is to call DOM.getDocument with a depth of -1."""
def __init__(self, computedStyleWhitelist: List):
# Whitelist of computed styles to return.
self.computedStyleWhitelist = computedStyleWhitelist
class startRuleUsageTracking(ChromeCommand):
"""Enables the selector recording."""
def __init__(self): pass
class stopRuleUsageTracking(ChromeCommand):
"""The list of rules with an indication of whether these were used"""
def __init__(self): pass