99import warnings
1010from typing import Match , Any , Iterator , cast
1111from dataclasses import dataclass
12+ from collections import UserDict
13+ import threading
14+
15+ RE_LOCK = threading .Lock ()
16+ SEL_LOCK = threading .RLock ()
1217
1318UNICODE_REPLACEMENT_CHAR = 0xFFFD
1419
@@ -318,8 +323,9 @@ def __init__(self, name: str, pattern: str) -> None:
318323 def re_pattern (self ) -> re .Pattern [str ]:
319324 """Retrieve the compiled regular expression pattern."""
320325
321- if self ._re_pattern is None :
322- self ._re_pattern = re .compile (self .pattern , re .I | re .X | re .U )
326+ with RE_LOCK :
327+ if self ._re_pattern is None :
328+ self ._re_pattern = re .compile (self .pattern , re .I | re .X | re .U )
323329 return self ._re_pattern
324330
325331 def get_name (self ) -> str :
@@ -444,14 +450,35 @@ class CSSPattern:
444450 selector : str
445451 flags : int
446452
453+
454+ class PseudoSelectorMap (UserDict [str , CSSPattern | ct .SelectorList ]):
455+ """Pseudo selector map."""
456+
457+ def __setitem__ (self , key : str , value : CSSPattern | ct .SelectorList ) -> None :
458+ """Set item."""
459+
460+ self .data [key ] = value
461+
462+ def __getitem__ (self , key : str ) -> ct .SelectorList :
463+ """Get item."""
464+
465+ with SEL_LOCK :
466+ value = self .data [key ]
467+ if isinstance (value , CSSPattern ):
468+ value = CSSParser (value .selector ).process_selectors (flags = value .flags )
469+ self .data [key ] = value
470+
471+ return value
472+
473+
447474# CSS pattern for `:link` and `:any-link`
448- CSS_LINK = CSSPattern ('html|*:is(a, area)[href]' , 0 )
475+ CSS_LINK = CSSPattern ('html|*:is(a, area)[href]' , FLG_PSEUDO | FLG_HTML )
449476# CSS pattern for `:checked`
450477CSS_CHECKED = CSSPattern (
451478 '''
452479 html|*:is(input[type=checkbox], input[type=radio])[checked], html|option[selected]
453480 ''' ,
454- 0
481+ FLG_PSEUDO | FLG_HTML
455482)
456483# CSS pattern for `:default` (must compile CSS_CHECKED first)
457484CSS_DEFAULT = CSSPattern (
@@ -464,7 +491,7 @@ class CSSPattern:
464491 */
465492 html|form html|*:is(button, input)[type="submit"]
466493 ''' ,
467- FLG_DEFAULT
494+ FLG_PSEUDO | FLG_HTML | FLG_DEFAULT
468495)
469496# CSS pattern for `:indeterminate`
470497CSS_INDETERMINATE = CSSPattern (
@@ -479,7 +506,7 @@ class CSSPattern:
479506 */
480507 html|input[type="radio"][name]:not([name='']):not([checked])
481508 ''' ,
482- FLG_INDETERMINATE
509+ FLG_PSEUDO | FLG_HTML | FLG_INDETERMINATE
483510)
484511# CSS pattern for `:disabled`
485512CSS_DISABLED = CSSPattern (
@@ -490,19 +517,19 @@ class CSSPattern:
490517 html|fieldset[disabled] >
491518 html|*:not(legend:nth-of-type(1)) html|*:is(input:not([type=hidden]), button, select, textarea, fieldset)
492519 ''' ,
493- 0
520+ FLG_PSEUDO | FLG_HTML
494521)
495522# CSS pattern for `:enabled`
496523CSS_ENABLED = CSSPattern (
497524 '''
498525 html|*:is(input:not([type=hidden]), button, select, textarea, fieldset, optgroup, option, fieldset):not(:disabled)
499526 ''' ,
500- FLG_HTML
527+ FLG_PSEUDO | FLG_HTML
501528)
502529# CSS pattern for `:required`
503- CSS_REQUIRED = CSSPattern ('html|*:is(input, textarea, select)[required]' , 0 )
530+ CSS_REQUIRED = CSSPattern ('html|*:is(input, textarea, select)[required]' , FLG_PSEUDO | FLG_HTML )
504531# CSS pattern for `:optional`
505- CSS_OPTIONAL = CSSPattern ('html|*:is(input, textarea, select):not([required])' , 0 )
532+ CSS_OPTIONAL = CSSPattern ('html|*:is(input, textarea, select):not([required])' , FLG_PSEUDO | FLG_HTML )
506533# CSS pattern for `:placeholder-shown`
507534CSS_PLACEHOLDER_SHOWN = CSSPattern (
508535 '''
@@ -519,7 +546,7 @@ class CSSPattern:
519546 )[placeholder]:not([placeholder='']):is(:not([value]), [value=""]),
520547 html|textarea[placeholder]:not([placeholder=''])
521548 ''' ,
522- FLG_PLACEHOLDER_SHOWN
549+ FLG_PSEUDO | FLG_HTML | FLG_PLACEHOLDER_SHOWN
523550)
524551# CSS pattern for `:read-write` (CSS_DISABLED must be compiled first)
525552CSS_READ_WRITE = CSSPattern (
@@ -545,10 +572,10 @@ class CSSPattern:
545572 ):not([readonly], :disabled),
546573 html|*:is([contenteditable=""], [contenteditable="true" i])
547574 ''' ,
548- 0
575+ FLG_PSEUDO | FLG_HTML
549576)
550577# CSS pattern for `:read-only`
551- CSS_READ_ONLY = CSSPattern ('html|*:not(:read-write)' , 0 )
578+ CSS_READ_ONLY = CSSPattern ('html|*:not(:read-write)' , FLG_PSEUDO | FLG_HTML )
552579# CSS pattern for `:in-range`
553580CSS_IN_RANGE = CSSPattern (
554581 '''
@@ -565,7 +592,7 @@ class CSSPattern:
565592 [max]
566593 )
567594 ''' ,
568- FLG_IN_RANGE
595+ FLG_PSEUDO | FLG_HTML | FLG_IN_RANGE
569596)
570597# CSS pattern for `:out-of-range`
571598CSS_OUT_OF_RANGE = CSSPattern (
@@ -583,18 +610,20 @@ class CSSPattern:
583610 [max]
584611 )
585612 ''' ,
586- FLG_OUT_OF_RANGE
613+ FLG_PSEUDO | FLG_HTML | FLG_OUT_OF_RANGE
587614)
588615# CSS pattern for :open
589- CSS_OPEN = CSSPattern ('html|*:is(details, dialog)[open]' , 0 )
616+ CSS_OPEN = CSSPattern ('html|*:is(details, dialog)[open]' , FLG_PSEUDO | FLG_HTML )
590617# CSS pattern for :muted
591- CSS_MUTED = CSSPattern ('html|*:is(video, audio)[muted]' , 0 )
618+ CSS_MUTED = CSSPattern ('html|*:is(video, audio)[muted]' , FLG_PSEUDO | FLG_HTML )
619+ # CSS pattern default for `:nth-child` "of S" feature
620+ CSS_NTH_OF_S_DEFAULT = CSSPattern ("*|*" , FLG_PSEUDO )
592621
593622
594623class CSSParser :
595624 """Parse CSS selectors."""
596625
597- css_tokens = (
626+ CSS_TOKENS = (
598627 SelectorPattern ("pseudo_close" , PAT_PSEUDO_CLOSE ),
599628 SpecialPseudoPattern (
600629 (
@@ -623,27 +652,27 @@ class CSSParser:
623652 )
624653
625654 # Pseudos that expand to selectors
626- pseudo_selectors : dict [ str , CSSPattern | ct . SelectorList ] = {
627- ':link' : CSS_LINK ,
628- ':any- link' : CSS_LINK ,
629- ':checked ' : CSS_CHECKED ,
630- ':default ' : CSS_DEFAULT ,
631- ':indeterminate ' : CSS_INDETERMINATE ,
632- ':disabled ' : CSS_DISABLED ,
633- ':enabled ' : CSS_ENABLED ,
634- ':required ' : CSS_REQUIRED ,
635- ':muted ' : CSS_MUTED ,
636- ':open ' : CSS_OPEN ,
637- ':optional ' : CSS_OPTIONAL ,
638- ':read-only ' : CSS_READ_ONLY ,
639- ':read-write ' : CSS_READ_WRITE ,
640- ':in-range ' : CSS_IN_RANGE ,
641- ':out-of- range' : CSS_OUT_OF_RANGE ,
642- ':placeholder-shown ' : CSS_PLACEHOLDER_SHOWN
643- }
644-
645- # CSS pattern default for `:nth-child` "of S" feature
646- css_nth_of_s_default = None
655+ PSEUDO_SELECTORS = PseudoSelectorMap (
656+ {
657+ ': link' : CSS_LINK ,
658+ ':any-link ' : CSS_LINK ,
659+ ':checked ' : CSS_CHECKED ,
660+ ':default ' : CSS_DEFAULT ,
661+ ':indeterminate ' : CSS_INDETERMINATE ,
662+ ':disabled ' : CSS_DISABLED ,
663+ ':enabled ' : CSS_ENABLED ,
664+ ':required ' : CSS_REQUIRED ,
665+ ':muted ' : CSS_MUTED ,
666+ ':open ' : CSS_OPEN ,
667+ ':optional ' : CSS_OPTIONAL ,
668+ ':read-only ' : CSS_READ_ONLY ,
669+ ':read-write ' : CSS_READ_WRITE ,
670+ ':in- range' : CSS_IN_RANGE ,
671+ ':out-of-range ' : CSS_OUT_OF_RANGE ,
672+ ':placeholder-shown' : CSS_PLACEHOLDER_SHOWN ,
673+ '<nth-of-s>' : CSS_NTH_OF_S_DEFAULT
674+ }
675+ )
647676
648677 def __init__ (
649678 self ,
@@ -801,15 +830,8 @@ def parse_pseudo_class(
801830 sel .flags |= ct .SEL_SCOPE
802831 elif pseudo == ':empty' :
803832 sel .flags |= ct .SEL_EMPTY
804- elif pseudo in self .pseudo_selectors :
805- pseudo_selector = self .pseudo_selectors [pseudo ]
806- if isinstance (pseudo_selector , CSSPattern ):
807- self .pseudo_selectors [pseudo ] = pseudo_selector = CSSParser (
808- pseudo_selector .selector
809- ).process_selectors (
810- flags = FLG_PSEUDO | FLG_HTML | pseudo_selector .flags
811- )
812-
833+ elif pseudo in self .PSEUDO_SELECTORS :
834+ pseudo_selector = self .PSEUDO_SELECTORS [pseudo ]
813835 self .count += pseudo_selector .count
814836 self .check_count ()
815837 sel .selectors .append (pseudo_selector )
@@ -911,10 +933,7 @@ def parse_pseudo_nth(
911933 nth_sel = self .parse_selectors (iselector , m .end (0 ), FLG_PSEUDO | FLG_OPEN )
912934 else :
913935 # Use default `*|*` for `of S`.
914- if not self .css_nth_of_s_default :
915- self .css_nth_of_s_default = CSSParser ('*|*' ).process_selectors (flags = FLG_PSEUDO )
916-
917- nth_sel = self .css_nth_of_s_default
936+ nth_sel = self .PSEUDO_SELECTORS ['<nth-of-s>' ]
918937 self .count += nth_sel .count
919938 self .check_count ()
920939 if pseudo_sel == ':nth-child' :
@@ -1309,7 +1328,7 @@ def selector_iter(self, pattern: str) -> Iterator[tuple[str, Match[str]]]:
13091328 print (f'## PARSING: { pattern !r} ' )
13101329 while index <= end :
13111330 m = None
1312- for v in self .css_tokens :
1331+ for v in self .CSS_TOKENS :
13131332 m = v .match (pattern , index , self .flags )
13141333 if m :
13151334 name = v .get_name ()
0 commit comments