Skip to content
Browse files

Warn (not crash) on missing base_url for @import

  • Loading branch information...
1 parent a85c137 commit 7984fbd3bc606672edd8df634afda2cbd861f67c @SimonSapin SimonSapin committed Sep 25, 2012
Showing with 28 additions and 17 deletions.
  1. +9 −6 weasyprint/css/__init__.py
  2. +19 −11 weasyprint/urls.py
View
15 weasyprint/css/__init__.py
@@ -51,9 +51,9 @@
from . import properties
from . import computed_values
from .validation import preprocess_declarations
-from ..urls import get_url_attribute
+from ..urls import get_url_attribute, url_join
from ..logger import LOGGER
-from ..compat import iteritems, urljoin
+from ..compat import iteritems
from .. import CSS
@@ -379,10 +379,13 @@ def preprocess_stylesheet(device_media_type, base_url, rules, url_fetcher):
elif rule.at_keyword == '@import':
if not evaluate_media_query(rule.media, device_media_type):
continue
- for result in CSS(url=urljoin(base_url, rule.uri),
- url_fetcher=url_fetcher,
- media_type=device_media_type).rules:
- yield result
+ url = url_join(base_url, rule.uri, '@import at %s:%s',
+ rule.line, rule.column)
+ if url is not None:
+ for result in CSS(url=url,
+ url_fetcher=url_fetcher,
+ media_type=device_media_type).rules:
+ yield result
elif rule.at_keyword == '@media':
if not evaluate_media_query(rule.media, device_media_type):
View
30 weasyprint/urls.py
@@ -100,17 +100,25 @@ def get_url_attribute(element, attr_name):
Otherwise, return an absolute URI.
"""
- attr_value = element.get(attr_name, '').strip()
- if attr_value:
- if url_is_absolute(attr_value):
- return attr_value
- elif element.base_url:
- return urljoin(element.base_url, attr_value)
- else:
- LOGGER.warn(
- 'Relative URI reference without a base URI: '
- '<%s %s="%s"> at line %d',
- element.tag, attr_name, attr_value, element.sourceline)
+ value = element.get(attr_name, '').strip()
+ if value:
+ return url_join(element.base_url, value, '<%s %s="%s"> at line %d',
+ element.tag, attr_name, value, element.sourceline)
+
+
+def url_join(base_url, url, context, *args):
+ """Like urllib.urljoin, but issue a warning and return None if base_url
+ is required but missing.
+
+ """
+ if url_is_absolute(url):
+ return url
+ elif base_url:
+ return urljoin(base_url, url)
+ else:
+ LOGGER.warn('Relative URI reference without a base URI: ' + context,
+ *args)
+ return None
def get_link_attribute(element, attr_name):

0 comments on commit 7984fbd

Please sign in to comment.
Something went wrong with that request. Please try again.