1
- import { strip } from ' ./strip.js' ;
1
+ import { strip } from " ./strip.js" ;
2
2
3
3
/**
4
4
* Removes every HTML-comment from the string that is provided
@@ -26,15 +26,15 @@ export function removeCssComments(str: string): string {
26
26
* @returns {String }
27
27
*/
28
28
29
- export function removeJSComments ( codeStr : string ) : string {
29
+ export function removeJSComments ( codeStr : string ) : string {
30
30
// TODO: publish type declarations and re-enable eslint
31
31
try {
32
32
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
33
33
return strip ( codeStr ) ;
34
34
} catch ( err ) {
35
35
return codeStr ;
36
36
}
37
- } ;
37
+ }
38
38
39
39
/**
40
40
* Removes every white-space from the string that is provided
@@ -43,7 +43,7 @@ export function removeJSComments(codeStr: string): string {
43
43
*/
44
44
45
45
export function removeWhiteSpace ( str : string ) : string {
46
- return str . replace ( / \s / g, '' ) ;
46
+ return str . replace ( / \s / g, "" ) ;
47
47
}
48
48
49
49
/**
@@ -83,9 +83,9 @@ interface ExtendedStyleDeclaration extends CSSStyleDeclaration {
83
83
const getIsDeclaredAfter = ( styleRule : CSSStyleRule ) => ( selector : string ) => {
84
84
const cssStyleRules = Array . from (
85
85
styleRule . parentStyleSheet ?. cssRules || [ ]
86
- ) ?. filter ( ele => ele . type === CSSRule . STYLE_RULE ) as CSSStyleRule [ ] ;
86
+ ) ?. filter ( ( ele ) => ele . type === CSSRule . STYLE_RULE ) as CSSStyleRule [ ] ;
87
87
const previousStyleRule = cssStyleRules . find (
88
- ele => ele ?. selectorText === selector
88
+ ( ele ) => ele ?. selectorText === selector
89
89
) ;
90
90
if ( ! previousStyleRule ) return false ;
91
91
const currPosition = Array . from (
@@ -97,7 +97,32 @@ const getIsDeclaredAfter = (styleRule: CSSStyleRule) => (selector: string) => {
97
97
return currPosition > prevPosition ;
98
98
} ;
99
99
100
+ export module python {
101
+ export function getDef ( code : string , functionName : string ) {
102
+ const regex = new RegExp (
103
+ `\\n(?<function_indentation>\\s*?)def\\s+${ functionName } \\s*\\((?<function_parameters>[^\\)]*)\\)\\s*:\\n(?<function_body>.*?)(?=\\n\\k<function_indentation>[\\w#])` ,
104
+ "s"
105
+ ) ;
100
106
107
+ const matchedCode = regex . exec ( code ) ;
108
+ if ( matchedCode ) {
109
+ const { function_parameters, function_body, function_indentation } =
110
+ matchedCode . groups ;
111
+
112
+ const functionIndentationSansNewLine = function_indentation . replace (
113
+ / \n + / ,
114
+ ""
115
+ ) ;
116
+ return {
117
+ def : matchedCode [ 0 ] ,
118
+ function_parameters,
119
+ function_body,
120
+ function_indentation : functionIndentationSansNewLine . length ,
121
+ } ;
122
+ }
123
+ return null ;
124
+ }
125
+ }
101
126
102
127
export class CSSHelp {
103
128
doc : Document ;
@@ -107,23 +132,23 @@ export class CSSHelp {
107
132
private _getStyleRules ( ) {
108
133
const styleSheet = this . getStyleSheet ( ) ;
109
134
return this . styleSheetToCssRulesArray ( styleSheet ) . filter (
110
- ele => ele . type === CSSRule . STYLE_RULE
135
+ ( ele ) => ele . type === CSSRule . STYLE_RULE
111
136
) as CSSStyleRule [ ] ;
112
137
}
113
138
114
139
getStyleDeclarations ( selector : string ) : CSSStyleDeclaration [ ] {
115
140
return this . _getStyleRules ( )
116
- ?. filter ( ele => ele ?. selectorText === selector )
117
- . map ( x => x . style ) ;
141
+ ?. filter ( ( ele ) => ele ?. selectorText === selector )
142
+ . map ( ( x ) => x . style ) ;
118
143
}
119
144
getStyle ( selector : string ) : ExtendedStyleDeclaration | null {
120
145
const style = this . _getStyleRules ( ) . find (
121
- ele => ele ?. selectorText === selector
146
+ ( ele ) => ele ?. selectorText === selector
122
147
) ?. style as ExtendedStyleDeclaration | undefined ;
123
148
if ( ! style ) return null ;
124
149
style . getPropVal = ( prop : string , strip = false ) => {
125
150
return strip
126
- ? style . getPropertyValue ( prop ) . replace ( / \s + / g, '' )
151
+ ? style . getPropertyValue ( prop ) . replace ( / \s + / g, "" )
127
152
: style . getPropertyValue ( prop ) ;
128
153
} ;
129
154
return style ;
@@ -132,7 +157,7 @@ export class CSSHelp {
132
157
getStyleAny ( selectors : string [ ] ) : ExtendedStyleDeclaration | null {
133
158
for ( const selector of selectors ) {
134
159
const style = this . getStyle ( selector ) ;
135
-
160
+
136
161
if ( style ) {
137
162
return style ;
138
163
}
@@ -142,13 +167,13 @@ export class CSSHelp {
142
167
}
143
168
getStyleRule ( selector : string ) : ExtendedStyleRule | null {
144
169
const styleRule = this . _getStyleRules ( ) ?. find (
145
- ele => ele ?. selectorText === selector
170
+ ( ele ) => ele ?. selectorText === selector
146
171
) ;
147
172
if ( styleRule ) {
148
173
return {
149
174
...styleRule ,
150
175
isDeclaredAfter : ( selector : string ) =>
151
- getIsDeclaredAfter ( styleRule ) ( selector )
176
+ getIsDeclaredAfter ( styleRule ) ( selector ) ,
152
177
} ;
153
178
} else {
154
179
return null ;
@@ -158,26 +183,26 @@ export class CSSHelp {
158
183
const styleSheet = this . getStyleSheet ( ) ;
159
184
const cssRules = this . styleSheetToCssRulesArray ( styleSheet ) ;
160
185
switch ( element ) {
161
- case ' media' :
162
- return cssRules . filter ( ele => ele . type === CSSRule . MEDIA_RULE ) ;
163
- case ' fontface' :
164
- return cssRules . filter ( ele => ele . type === CSSRule . FONT_FACE_RULE ) ;
165
- case ' import' :
166
- return cssRules . filter ( ele => ele . type === CSSRule . IMPORT_RULE ) ;
167
- case ' keyframes' :
168
- return cssRules . filter ( ele => ele . type === CSSRule . KEYFRAMES_RULE ) ;
186
+ case " media" :
187
+ return cssRules . filter ( ( ele ) => ele . type === CSSRule . MEDIA_RULE ) ;
188
+ case " fontface" :
189
+ return cssRules . filter ( ( ele ) => ele . type === CSSRule . FONT_FACE_RULE ) ;
190
+ case " import" :
191
+ return cssRules . filter ( ( ele ) => ele . type === CSSRule . IMPORT_RULE ) ;
192
+ case " keyframes" :
193
+ return cssRules . filter ( ( ele ) => ele . type === CSSRule . KEYFRAMES_RULE ) ;
169
194
default :
170
195
return cssRules ;
171
196
}
172
197
}
173
198
isPropertyUsed ( property : string ) : boolean {
174
- return this . _getStyleRules ( ) . some ( ele =>
199
+ return this . _getStyleRules ( ) . some ( ( ele ) =>
175
200
ele . style ?. getPropertyValue ( property )
176
201
) ;
177
202
}
178
203
getRuleListsWithinMedia ( mediaText : string ) : CSSStyleRule [ ] {
179
- const medias = this . getCSSRules ( ' media' ) as CSSMediaRule [ ] ;
180
- const cond = medias ?. find ( x => x ?. media ?. mediaText === mediaText ) ;
204
+ const medias = this . getCSSRules ( " media" ) as CSSMediaRule [ ] ;
205
+ const cond = medias ?. find ( ( x ) => x ?. media ?. mediaText === mediaText ) ;
181
206
const cssRules = cond ?. cssRules ;
182
207
return Array . from ( cssRules || [ ] ) as CSSStyleRule [ ] ;
183
208
}
@@ -189,12 +214,12 @@ export class CSSHelp {
189
214
190
215
// When using the styles.css tab, we add a 'fcc-injected-styles' class so we can target that. This allows users to add external scripts without them interfering
191
216
const stylesDotCss : HTMLStyleElement | null = this . doc ?. querySelector (
192
- ' style.fcc-injected-styles'
217
+ " style.fcc-injected-styles"
193
218
) ;
194
219
195
220
// For steps that use <style> tags, where they don't add the above class - most* browser extensions inject styles with class/media attributes, so it filters those
196
221
const styleTag : HTMLStyleElement | null = this . doc ?. querySelector (
197
- ' style:not([class]):not([media])'
222
+ " style:not([class]):not([media])"
198
223
) ;
199
224
200
225
if ( link ?. sheet ?. cssRules ?. length ) {
@@ -208,41 +233,45 @@ export class CSSHelp {
208
233
}
209
234
}
210
235
styleSheetToCssRulesArray (
211
- styleSheet : ReturnType < CSSHelp [ ' getStyleSheet' ] >
236
+ styleSheet : ReturnType < CSSHelp [ " getStyleSheet" ] >
212
237
) : CSSRule [ ] {
213
238
return Array . from ( styleSheet ?. cssRules || [ ] ) ;
214
239
}
215
240
// takes a CSS selector, returns all equivelant selectors from the current document
216
241
// or an empty array if there are no matches
217
242
selectorsFromSelector ( selector : string ) : string [ ] {
218
243
const elements = this . doc . querySelectorAll ( selector ) ;
219
- const allSelectors = Array . from ( elements ) . map ( ( element : Element ) => {
220
- let directPath = [ ] ;
221
- let indirectPath = [ ] ;
222
- let allPaths = [ ] ;
223
-
224
- while ( element . parentNode ) {
225
- let tag = element . tagName . toLowerCase ( ) ;
226
- let siblings = Array . from ( element . parentNode . children ) ;
227
-
228
- if ( siblings . filter ( e => e . tagName === element . tagName ) . length > 1 ) {
229
- let allSiblings = Array . from ( element . parentNode . childNodes ) ;
230
- let index = allSiblings . indexOf ( element ) ;
231
- tag += `:nth-child(${ index + 1 } )` ;
232
- }
244
+ const allSelectors = Array . from ( elements )
245
+ . map ( ( element : Element ) => {
246
+ let directPath = [ ] ;
247
+ let indirectPath = [ ] ;
248
+ let allPaths = [ ] ;
233
249
234
- directPath . unshift ( tag ) ;
235
- indirectPath . unshift ( tag ) ;
236
- allPaths . push ( [ directPath . join ( ' > ' ) , indirectPath . join ( ' ' ) ] ) ;
237
-
238
- // traverse up the DOM tree
239
- element = element . parentNode as Element ;
240
- }
250
+ while ( element . parentNode ) {
251
+ let tag = element . tagName . toLowerCase ( ) ;
252
+ let siblings = Array . from ( element . parentNode . children ) ;
253
+
254
+ if (
255
+ siblings . filter ( ( e ) => e . tagName === element . tagName ) . length > 1
256
+ ) {
257
+ let allSiblings = Array . from ( element . parentNode . childNodes ) ;
258
+ let index = allSiblings . indexOf ( element ) ;
259
+ tag += `:nth-child(${ index + 1 } )` ;
260
+ }
241
261
242
- return allPaths . flat ( ) ;
243
- } ) . flat ( ) ;
262
+ directPath . unshift ( tag ) ;
263
+ indirectPath . unshift ( tag ) ;
264
+ allPaths . push ( [ directPath . join ( " > " ) , indirectPath . join ( " " ) ] ) ;
265
+
266
+ // traverse up the DOM tree
267
+ element = element . parentNode as Element ;
268
+ }
269
+
270
+ return allPaths . flat ( ) ;
271
+ } )
272
+ . flat ( ) ;
244
273
245
274
// remove duplicates
246
275
return [ ...new Set ( allSelectors ) ] ;
247
276
}
248
- }
277
+ }
0 commit comments