def _setHref(self, href): # set new href self._href = href # update seq for i, item in enumerate(self.seq): type_ = item.type if 'href' == type_: self._seq[i] = (href, type_, item.line, item.col) break importedSheet = css_parser.css.CSSStyleSheet(media=self.media, ownerRule=self, title=self.name) self.hrefFound = False # set styleSheet if href and self.parentStyleSheet: # loading errors are all catched! # relative href parentHref = self.parentStyleSheet.href if parentHref is None: # use cwd instead parentHref = css_parser.helper.path2url(os.getcwd()) + '/' fullhref = urllib_urljoin(parentHref, self.href) # all possible exceptions are ignored try: usedEncoding, enctype, cssText = \ self.parentStyleSheet._resolveImport(fullhref) if cssText is None: # catched in next except below! raise IOError('Cannot read Stylesheet.') # contentEncoding with parentStyleSheet.overrideEncoding, # HTTP or parent encodingOverride, encoding = None, None if enctype == 0: encodingOverride = usedEncoding elif 0 < enctype < 5: encoding = usedEncoding # inherit fetcher for @imports in styleSheet importedSheet._href = fullhref importedSheet._setFetcher(self.parentStyleSheet._fetcher) importedSheet._setCssTextWithEncodingOverride( cssText, encodingOverride=encodingOverride, encoding=encoding) except (OSError, IOError, ValueError) as e: self._log.warn('CSSImportRule: While processing imported ' 'style sheet href=%s: %r' % (self.href, e), neverraise=True) else: # used by resolveImports if to keep unprocessed href self.hrefFound = True self._styleSheet = importedSheet
def absoluteUri(self): """Actual URL, made absolute if possible, else same as `uri`.""" # Ancestry: PropertyValue, Property, CSSStyleDeclaration, CSSStyleRule, # CSSStyleSheet try: # TODO: better way? styleSheet = self.parent.parent.parent.parentRule.parentStyleSheet except AttributeError: return self.uri else: return urllib_urljoin(styleSheet.href, self.uri)
def _doImports(self, parentStyleSheet, base=None): """ handle all @import CSS stylesheet recursively found CSS stylesheets are appended to stylesheetlist """ # TODO: only if not parsed these have to be read extra! for rule in parentStyleSheet.cssRules: if rule.type == rule.IMPORT_RULE: self._log.info('+ PROCESSING @import:') self._log.debug(' IN: %s\n' % parentStyleSheet.href) sheet = rule.styleSheet href = urllib_urljoin(base, rule.href) if sheet: self._log.info(' %s\n' % sheet) self.stylesheetlist.append(sheet) self._doImports(sheet, base=href)
def _findStyleSheets(self, docurl, doctext): """ parse text for stylesheets fills stylesheetlist with all found StyleSheets docurl to build a full url of found StyleSheets @href doctext to parse """ # TODO: ownerNode should be set to the <link> node self._htmlparser.feed(doctext) for typ, data in self._htmlparser.sheets: sheet = None if LINK == typ: self._log.info('+ PROCESSING <link> %r' % data) atts = data href = urllib_urljoin(docurl, atts.get('href', None)) sheet = self._createStyleSheet(href=href, media=atts.get('media', None), title=atts.get('title', None)) elif STYLE == typ: self._log.info('+ PROCESSING <style> %r' % data) atts, cssText = data sheet = self._createStyleSheet(cssText=cssText, href=docurl, media=atts.get('media', None), title=atts.get('title', None), encoding=self.docencoding) if sheet: sheet._href = None # inline have no href! print(sheet.cssText) if sheet: self.stylesheetlist.append(sheet) self._doImports(sheet, base=docurl)
def urljoin(base_url, relative_url): return urllib_urljoin(base_url, relative_url)
def test_assert_same_urljoin_result(base, url, expected): urllib_result = urllib_urljoin(base, url) whatwg_result = whatwg_urljoin(base, url) assert urllib_result == expected assert whatwg_result == expected