1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
9 def __init__(self, resource):
10 self.resource = resource
14 def relative_path(self):
15 return self.resource.relative_path
18 def absolute_path(self):
19 return self.resource.absolute_path
23 return self.resource.contents
25 class StyleSheet(object):
26 """Represents a stylesheet resource referenced by a module via the
27 base.requireStylesheet(xxx) directive."""
28 def __init__(self, loader, name, resource):
31 self.resource = resource
36 return self.resource.absolute_path
40 return self.resource.contents
43 return "StyleSheet(%s)" % self.name
47 if self._images != None:
53 def contents_with_inlined_images(self):
61 image = images_by_url[url]
63 ext = os.path.splitext(image.absolute_path)[1]
64 data = base64.standard_b64encode(image.contents)
66 return "url(data:image/%s;base64,%s)" % (ext[1:], data)
68 # I'm assuming we only have url()'s associated with images
69 return re.sub('url\((?P<quote>"|\'|)(?P<url>[^"\'()]*)(?P=quote)\)',
70 lambda m: InlineUrl(m),
76 'url\((?:["|\']?)([^"\'()]*)(?:["|\']?)\)',
79 module_dirname = os.path.dirname(self.resource.absolute_path)
81 if os.path.isabs(url):
82 raise module.DepsException('URL references must be relative')
83 # URLS are relative to this module's directory
84 abs_path = os.path.abspath(os.path.join(module_dirname, url))
85 image = self.loader.load_image(abs_path)
86 image.aliases.append(url)
89 self._images = [resolve_url(x) for x in matches]