Skip to content

Commit

Permalink
Construct namespaces in a single expression instead of for/setitem loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed May 18, 2024
1 parent 8fc50e3 commit ba406ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cssutils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
__all__ = []

import codecs
import operator
import re
import xml.dom
from itertools import chain

from more_itertools import unique_everseen

import cssutils

from . import codec, errorhandler, tokenize2
Expand Down Expand Up @@ -827,14 +830,15 @@ def namespaces(self):
A property holding only effective @namespace rules in
self.parentStyleSheets.
"""
namespaces = {}
for rule in filter(
ns_rules = filter(
lambda r: r.type == r.NAMESPACE_RULE,
reversed(self.parentStyleSheet.cssRules),
):
if rule.namespaceURI not in list(namespaces.values()):
namespaces[rule.prefix] = rule.namespaceURI
return namespaces
)
unique_rules = unique_everseen(
ns_rules,
key=operator.attrgetter('namespaceURI'),
)
return {rule.prefix: rule.namespaceURI for rule in unique_rules}

def get(self, prefix, default):
return self.namespaces.get(prefix, default)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ classifiers = [
keywords = ["CSS", "Cascading Style Sheets", "CSSParser", "DOM Level 2 Stylesheets", "DOM Level 2 CSS"]
requires-python = ">=3.8"
dependencies = [
"more_itertools",
]
dynamic = ["version"]

Expand Down

0 comments on commit ba406ea

Please sign in to comment.