Skip to content

Commit

Permalink
Simplify URL generation with generator expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Mar 12, 2021
1 parent 8bd63d4 commit a69b78e
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions cssutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import urllib.request
import urllib.parse
import xml.dom
import itertools

from . import errorhandler
from . import css
Expand Down Expand Up @@ -194,7 +195,7 @@ def setSerializer(serializer):
globals().update(ser=serializer)


def getUrls(sheet): # noqa: C901
def getUrls(sheet):
"""Retrieve all ``url(urlstring)`` values (in e.g.
:class:`cssutils.css.CSSImportRule` or :class:`cssutils.css.CSSValue`
objects of given `sheet`.
Expand All @@ -205,8 +206,7 @@ def getUrls(sheet): # noqa: C901
This function is a generator. The generated URL values exclude ``url(`` and
``)`` and surrounding single or double quotes.
"""
for importrule in (r for r in sheet if r.type == r.IMPORT_RULE):
yield importrule.href
imports = (rule.href for rule in sheet if rule.type == rule.IMPORT_RULE)

def styleDeclarations(base):
"recursive generator to find all CSSStyleDeclarations"
Expand All @@ -217,11 +217,15 @@ def styleDeclarations(base):
elif hasattr(base, 'style'):
yield base.style

for style in styleDeclarations(sheet):
for p in style.getProperties(all=True):
for v in p.propertyValue:
if v.type == 'URI':
yield v.uri
other = (
v.uri
for style in styleDeclarations(sheet)
for p in style.getProperties(all=True)
for v in p.propertyValue
if v.type == 'URI'
)

return itertools.chain(imports, other)


def replaceUrls(sheetOrStyle, replacer, ignoreImportRules=False): # noqa: C901
Expand Down

0 comments on commit a69b78e

Please sign in to comment.