Ticket #34322: js-commented-regions.patch

File js-commented-regions.patch, 3.1 KB (added by Adam Johnson, 14 months ago)

PoC patch to avoid commented regions

  • django/contrib/staticfiles/storage.py

    diff --git django/contrib/staticfiles/storage.py django/contrib/staticfiles/storage.py
    index b3ee32e665..9f5077edad 100644
    def url(self, name, force=False):  
    194194        """
    195195        return self._url(self.stored_name, name, force)
    196196
    197     def url_converter(self, name, hashed_files, template=None):
     197    def url_converter(self, name, hashed_files, template, commented_regions):
    198198        """
    199199        Return the custom URL converter for the given file name.
    200200        """
    def converter(matchobj):  
    212212            matched = matches["matched"]
    213213            url = matches["url"]
    214214
     215            from bisect import bisect
     216
     217            if len(commented_regions):
     218                m_start, m_end = span = matchobj.span()
     219                comment_index = bisect(commented_regions, span)
     220                if comment_index != 0:
     221                    c_start, c_end = commented_regions[comment_index - 1]
     222                    # If match lies within commented region
     223                    if c_start <= m_start and m_end <= c_end:
     224                        return matched
     225
    215226            # Ignore absolute/protocol-relative and data-uri URLs.
    216227            if re.match(r"^[a-z]+:", url):
    217228                return matched
    def path_level(name):  
    356367                    content = original_file.read().decode("utf-8")
    357368                    for extension, patterns in self._patterns.items():
    358369                        if matches_patterns(path, (extension,)):
     370                            commented_regions = self._parse_commented_regions(
     371                                extension, content
     372                            )
    359373                            for pattern, template in patterns:
    360374                                converter = self.url_converter(
    361                                     name, hashed_files, template
     375                                    name, hashed_files, template, commented_regions
    362376                                )
    363377                                try:
    364378                                    content = pattern.sub(converter, content)
    def path_level(name):  
    396410
    397411                yield name, hashed_name, processed, substitutions
    398412
     413    def _parse_commented_regions(self, extension, content):
     414        if extension == "*.js":
     415            js_region_re = re.compile(
     416                r"""
     417                (
     418                    # single-quoted strings
     419                    '([^'\\]|(\\.))*?'
     420                    # double-quoted strings
     421                    |"([^"\\]|(\\.))*?"
     422                    # template literals
     423                    |`.*?`
     424                    # single line comments
     425                    |//.*?\n
     426                    # multi-line comments
     427                    |/\*.*?\*/
     428                )
     429                """,
     430                re.VERBOSE | re.DOTALL,
     431            )
     432            return [m.span() for m in js_region_re.finditer(content)]
     433        else:
     434            return []
     435
    399436    def clean_name(self, name):
    400437        return name.replace("\\", "/")
    401438
Back to Top