mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-09 09:39:39 +00:00
Meta: Remove one global variable in the wpt import script
This commit is contained in:
parent
8a0c63f168
commit
ccb4c205cd
Notes:
github-actions[bot]
2024-12-19 09:55:47 +00:00
Author: https://github.com/LucasChollet
Commit: ccb4c205cd
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2929
Reviewed-by: https://github.com/tcl3 ✅
1 changed files with 16 additions and 12 deletions
|
@ -33,7 +33,6 @@ PathMapping = namedtuple('PathMapping', ['source', 'destination'])
|
||||||
test_type = TestType.TEXT
|
test_type = TestType.TEXT
|
||||||
raw_reference_path = None # As specified in the test HTML
|
raw_reference_path = None # As specified in the test HTML
|
||||||
reference_path = None # With parent directories
|
reference_path = None # With parent directories
|
||||||
src_values = []
|
|
||||||
|
|
||||||
|
|
||||||
class LinkedResourceFinder(HTMLParser):
|
class LinkedResourceFinder(HTMLParser):
|
||||||
|
@ -42,17 +41,22 @@ class LinkedResourceFinder(HTMLParser):
|
||||||
self._tag_stack_ = []
|
self._tag_stack_ = []
|
||||||
self._match_css_url_ = re.compile(r"url\(\"?(?P<url>[^\")]+)\"?\)")
|
self._match_css_url_ = re.compile(r"url\(\"?(?P<url>[^\")]+)\"?\)")
|
||||||
self._match_css_import_string_ = re.compile(r"@import\s+\"(?P<url>[^\")]+)\"")
|
self._match_css_import_string_ = re.compile(r"@import\s+\"(?P<url>[^\")]+)\"")
|
||||||
|
self._resources = []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def resources(self):
|
||||||
|
return self._resources
|
||||||
|
|
||||||
def handle_starttag(self, tag, attrs):
|
def handle_starttag(self, tag, attrs):
|
||||||
self._tag_stack_.append(tag)
|
self._tag_stack_.append(tag)
|
||||||
if tag in ["script", "img"]:
|
if tag in ["script", "img"]:
|
||||||
attr_dict = dict(attrs)
|
attr_dict = dict(attrs)
|
||||||
if "src" in attr_dict:
|
if "src" in attr_dict:
|
||||||
src_values.append(attr_dict["src"])
|
self._resources.append(attr_dict["src"])
|
||||||
if tag == "link":
|
if tag == "link":
|
||||||
attr_dict = dict(attrs)
|
attr_dict = dict(attrs)
|
||||||
if attr_dict["rel"] == "stylesheet":
|
if attr_dict["rel"] == "stylesheet":
|
||||||
src_values.append(attr_dict["href"])
|
self._resources.append(attr_dict["href"])
|
||||||
|
|
||||||
def handle_endtag(self, tag):
|
def handle_endtag(self, tag):
|
||||||
self._tag_stack_.pop()
|
self._tag_stack_.pop()
|
||||||
|
@ -62,11 +66,11 @@ class LinkedResourceFinder(HTMLParser):
|
||||||
# Look for uses of url()
|
# Look for uses of url()
|
||||||
url_iterator = self._match_css_url_.finditer(data)
|
url_iterator = self._match_css_url_.finditer(data)
|
||||||
for match in url_iterator:
|
for match in url_iterator:
|
||||||
src_values.append(match.group("url"))
|
self._resources.append(match.group("url"))
|
||||||
# Look for @imports that use plain strings - we already found the url() ones
|
# Look for @imports that use plain strings - we already found the url() ones
|
||||||
import_iterator = self._match_css_import_string_.finditer(data)
|
import_iterator = self._match_css_import_string_.finditer(data)
|
||||||
for match in import_iterator:
|
for match in import_iterator:
|
||||||
src_values.append(match.group("url"))
|
self._resources.append(match.group("url"))
|
||||||
|
|
||||||
|
|
||||||
class TestTypeIdentifier(HTMLParser):
|
class TestTypeIdentifier(HTMLParser):
|
||||||
|
@ -124,7 +128,7 @@ def map_to_path(sources, is_resource=True, resource_path=None):
|
||||||
return filepaths
|
return filepaths
|
||||||
|
|
||||||
|
|
||||||
def modify_sources(files):
|
def modify_sources(files, resources):
|
||||||
for file in files:
|
for file in files:
|
||||||
# Get the distance to the wpt-imports folder
|
# Get the distance to the wpt-imports folder
|
||||||
folder_index = str(file).find(test_type.input_path)
|
folder_index = str(file).find(test_type.input_path)
|
||||||
|
@ -141,10 +145,10 @@ def modify_sources(files):
|
||||||
page_source = f.read()
|
page_source = f.read()
|
||||||
|
|
||||||
# Iterate all scripts and overwrite the src attribute
|
# Iterate all scripts and overwrite the src attribute
|
||||||
for i, src_value in enumerate(src_values):
|
for i, resource in enumerate(resources):
|
||||||
if src_value.startswith('/'):
|
if resource.startswith('/'):
|
||||||
new_src_value = parent_folder_path + src_value[1::]
|
new_src_value = parent_folder_path + resource[1::]
|
||||||
page_source = page_source.replace(src_value, new_src_value)
|
page_source = page_source.replace(resource, new_src_value)
|
||||||
|
|
||||||
# Look for mentions of the reference page, and update their href
|
# Look for mentions of the reference page, and update their href
|
||||||
if raw_reference_path is not None:
|
if raw_reference_path is not None:
|
||||||
|
@ -245,8 +249,8 @@ def main():
|
||||||
parser = LinkedResourceFinder()
|
parser = LinkedResourceFinder()
|
||||||
parser.feed(page)
|
parser.feed(page)
|
||||||
|
|
||||||
modify_sources(files_to_modify)
|
modify_sources(files_to_modify, parser.resources)
|
||||||
script_paths = map_to_path(src_values, True, resource_path)
|
script_paths = map_to_path(parser.resources, True, resource_path)
|
||||||
download_files(script_paths)
|
download_files(script_paths)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue