diff --git a/cssutils/util.py b/cssutils/util.py index 66c43e45..c21ec90c 100644 --- a/cssutils/util.py +++ b/cssutils/util.py @@ -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 @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 4a721a3b..ceb5d4f6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"]