def test_relative_urls(self): """URLs relative to the CSS file are correctly identified and encoded""" expected_output = self.read_fixture_file("relative_urls.expected.css") new_css = make_css_images_inline( self.get_fixture_file_path("relative_urls.css"), ) eq_(expected_output, new_css)
def test_whitespace(self): """ All styles of whitespace around the URL being defined are allowed as per http://www.w3.org/TR/CSS2/syndata.html#uri """ expected_output = self.read_fixture_file("whitespace.expected.css") new_css = make_css_images_inline( self.get_fixture_file_path("whitespace.css"), "/whitespace.css", )
def test_skipped_urls(self): """ URLs which point to http:// locations or cannot be found on disk are left unchanged. """ expected_output = self.read_fixture_file("skip.css") new_css = make_css_images_inline( self.get_fixture_file_path("skip.css"), "/skip.css", ) eq_(expected_output, new_css)
def test_size_limit_met(self): """ When the size of a file exactly matches the limit or is under the limit, it is still encoded. """ expected_output = self.read_fixture_file("relative_urls.expected.css") # Exact hit on size of blue.jpg new_css = make_css_images_inline( self.get_fixture_file_path("relative_urls.css"), "/relative_urls.css", image_size_limit=16965 ) eq_(expected_output, new_css) # Easily clear of blue.jpg size new_css = make_css_images_inline( self.get_fixture_file_path("relative_urls.css"), "/relative_urls.css", image_size_limit=20000 ) eq_(expected_output, new_css)
def test_quotes(self): """ All styles of quoting are permitted as defined by http://www.w3.org/TR/CSS2/syndata.html#uri """ expected_output = self.read_fixture_file("quotes.expected.css") new_css = make_css_images_inline( self.get_fixture_file_path("quotes.css"), "/quotes.css", ) eq_(expected_output, new_css)
def test_size_limit_not_met(self): """ When the size of a file exceeds the maximum file size that file is left alone in the output. """ expected_output = self.read_fixture_file( "image_size_not_met.expected.css") new_css = make_css_images_inline( self.get_fixture_file_path("relative_urls.css"), "/relative_urls.css", image_size_limit=3000 ) eq_(expected_output, new_css)
def main(): parser = OptionParser(usage="%prog [options] <input CSS file> <absolute URL of CSS file> " "<ouptut file>") parser.add_option( "-m", "--max-image-size", type="int", dest="max_image_size", help="The size (in bytes) which images must be under to be encoded", ) options, arguments = parser.parse_args() if len(arguments) != 3: print "%s takes exactly three arguments!\n" % parser.get_prog_name() print parser.get_usage() sys.exit(1) css_file_path = path.abspath(path.expanduser(arguments[0])) css_file_url = arguments[1] output_file_name = path.abspath(path.expanduser(arguments[2])) if not path.exists(css_file_path): print "'%s' cannot be found - aborting conversion" % css_file_path sys.exit(2) output_dir = path.dirname(output_file_name) if not path.exists(output_dir): print "The directory to contain the output file '%s' does not exist " "- aborting conversion" % output_dir sys.exit(2) print "Converting '%s'" % css_file_path try: converted_css = make_css_images_inline(css_file_path, css_file_url, options.max_image_size) except CssFileError, exc: print str(exc) sys.exit(3)