mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-18 06:52:07 +00:00
LibWeb: Replace usages of Document::parse_url()
The spec has been updated to use `encoding_parse_url()` and `encoding_parse_and_serialize_url()` instead.
This commit is contained in:
parent
8d6f2390f6
commit
ff3d3840ac
Notes:
github-actions[bot]
2025-06-24 17:56:55 +00:00
Author: https://github.com/tcl3
Commit: ff3d3840ac
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5201
Reviewed-by: https://github.com/gmta ✅
12 changed files with 106 additions and 98 deletions
|
@ -554,22 +554,21 @@ void HTMLImageElement::update_the_image_data_impl(bool restart_animations, bool
|
|||
|
||||
// 7. If selected source is not null, then:
|
||||
if (selected_source.has_value()) {
|
||||
// 1. Parse selected source, relative to the element's node document.
|
||||
// If that is not successful, then abort this inner set of steps.
|
||||
// Otherwise, let urlString be the resulting URL string.
|
||||
auto maybe_url = document().parse_url(selected_source.value());
|
||||
if (!maybe_url.has_value())
|
||||
goto after_step_7;
|
||||
auto url_string = maybe_url->serialize();
|
||||
// 1. Let urlString be the result of encoding-parsing-and-serializing a URL given selected source, relative to the element's node document.
|
||||
auto url_string = document().encoding_parse_and_serialize_url(selected_source.value());
|
||||
|
||||
// 2. Let key be a tuple consisting of urlString, the img element's crossorigin attribute's mode,
|
||||
// 2. If urlString is failure, then abort this inner set of steps.
|
||||
if (!url_string.has_value())
|
||||
goto after_step_7;
|
||||
|
||||
// 3. Let key be a tuple consisting of urlString, the img element's crossorigin attribute's mode,
|
||||
// and, if that mode is not No CORS, the node document's origin.
|
||||
ListOfAvailableImages::Key key;
|
||||
key.url = maybe_url.value();
|
||||
key.url = *url_string;
|
||||
key.mode = m_cors_setting;
|
||||
key.origin = document().origin();
|
||||
|
||||
// 3. If the list of available images contains an entry for key, then:
|
||||
// 4. If the list of available images contains an entry for key, then:
|
||||
if (auto* entry = document().list_of_available_images().get(key)) {
|
||||
// 1. Set the ignore higher-layer caching flag for that entry.
|
||||
entry->ignore_higher_layer_caching = true;
|
||||
|
@ -601,7 +600,7 @@ void HTMLImageElement::update_the_image_data_impl(bool restart_animations, bool
|
|||
restart_the_animation();
|
||||
|
||||
// 2. Set the current request's current URL to urlString.
|
||||
m_current_request->set_current_url(realm(), url_string);
|
||||
m_current_request->set_current_url(realm(), *url_string);
|
||||
|
||||
// 3. If maybe omit events is not set or previousURL is not equal to urlString, then fire an event named load at the img element.
|
||||
if (!maybe_omit_events || previous_url != url_string)
|
||||
|
@ -656,10 +655,11 @@ after_step_7:
|
|||
return;
|
||||
}
|
||||
|
||||
// 12. Parse selected source, relative to the element's node document, and let urlString be the resulting URL string.
|
||||
auto maybe_url = document().parse_url(selected_source.value().url.to_byte_string());
|
||||
// If that is not successful, then:
|
||||
if (!maybe_url.has_value()) {
|
||||
// 12. Let urlString be the result of encoding-parsing-and-serializing a URL given selected source, relative to the element's node document.
|
||||
auto url_string = document().encoding_parse_and_serialize_url(selected_source.value().url);
|
||||
|
||||
// 13. If urlString is failure, then:
|
||||
if (!url_string.has_value()) {
|
||||
// 1. Abort the image request for the current request and the pending request.
|
||||
abort_the_image_request(realm(), m_current_request);
|
||||
abort_the_image_request(realm(), m_pending_request);
|
||||
|
@ -683,13 +683,12 @@ after_step_7:
|
|||
// 5. Return.
|
||||
return;
|
||||
}
|
||||
auto url_string = maybe_url->serialize();
|
||||
|
||||
// 13. If the pending request is not null and urlString is the same as the pending request's current URL, then return.
|
||||
// 14. If the pending request is not null and urlString is the same as the pending request's current URL, then return.
|
||||
if (m_pending_request && url_string == m_pending_request->current_url())
|
||||
return;
|
||||
|
||||
// 14. If urlString is the same as the current request's current URL and the current request's state is partially available,
|
||||
// 15. If urlString is the same as the current request's current URL and the current request's state is partially available,
|
||||
// then abort the image request for the pending request,
|
||||
// queue an element task on the DOM manipulation task source given the img element
|
||||
// to restart the animation if restart animation is set, and return.
|
||||
|
@ -703,24 +702,24 @@ after_step_7:
|
|||
return;
|
||||
}
|
||||
|
||||
// 15. If the pending request is not null, then abort the image request for the pending request.
|
||||
// 16. If the pending request is not null, then abort the image request for the pending request.
|
||||
abort_the_image_request(realm(), m_pending_request);
|
||||
|
||||
// AD-HOC: At this point we start deviating from the spec in order to allow sharing ImageRequest between
|
||||
// multiple image elements (as well as CSS background-images, etc.)
|
||||
|
||||
// 16. Set image request to a new image request whose current URL is urlString.
|
||||
// 17. Set image request to a new image request whose current URL is urlString.
|
||||
auto image_request = ImageRequest::create(realm(), document().page());
|
||||
image_request->set_current_url(realm(), url_string);
|
||||
image_request->set_current_url(realm(), *url_string);
|
||||
|
||||
// 17. If the current request's state is unavailable or broken, then set the current request to image request.
|
||||
// 18. If the current request's state is unavailable or broken, then set the current request to image request.
|
||||
// Otherwise, set the pending request to image request.
|
||||
if (m_current_request->state() == ImageRequest::State::Unavailable || m_current_request->state() == ImageRequest::State::Broken)
|
||||
m_current_request = image_request;
|
||||
else
|
||||
m_pending_request = image_request;
|
||||
|
||||
// 23. Let delay load event be true if the img's lazy loading attribute is in the Eager state, or if scripting is disabled for the img, and false otherwise.
|
||||
// 24. Let delay load event be true if the img's lazy loading attribute is in the Eager state, or if scripting is disabled for the img, and false otherwise.
|
||||
auto delay_load_event = lazy_loading_attribute() == LazyLoading::Eager;
|
||||
|
||||
// When delay load event is true, fetching the image must delay the load event of the element's node document
|
||||
|
@ -728,30 +727,34 @@ after_step_7:
|
|||
if (delay_load_event)
|
||||
m_load_event_delayer.emplace(document());
|
||||
|
||||
add_callbacks_to_image_request(*image_request, maybe_omit_events, *maybe_url, previous_url);
|
||||
add_callbacks_to_image_request(*image_request, maybe_omit_events, *url_string, previous_url);
|
||||
|
||||
// AD-HOC: If the image request is already available or fetching, no need to start another fetch.
|
||||
if (image_request->is_available() || image_request->is_fetching())
|
||||
return;
|
||||
|
||||
// 18. Let request be the result of creating a potential-CORS request given urlString, "image",
|
||||
// and the current state of the element's crossorigin content attribute.
|
||||
auto request = create_potential_CORS_request(vm(), *maybe_url, Fetch::Infrastructure::Request::Destination::Image, m_cors_setting);
|
||||
// AD-HOC: create_potential_CORS_request expects a url, but the following step passes a URL string.
|
||||
auto url_record = document().encoding_parse_url(selected_source.value().url);
|
||||
VERIFY(url_record.has_value());
|
||||
|
||||
// 19. Set request's client to the element's node document's relevant settings object.
|
||||
// 19. Let request be the result of creating a potential-CORS request given urlString, "image",
|
||||
// and the current state of the element's crossorigin content attribute.
|
||||
auto request = create_potential_CORS_request(vm(), *url_record, Fetch::Infrastructure::Request::Destination::Image, m_cors_setting);
|
||||
|
||||
// 20. Set request's client to the element's node document's relevant settings object.
|
||||
request->set_client(&document().relevant_settings_object());
|
||||
|
||||
// 20. If the element uses srcset or picture, set request's initiator to "imageset".
|
||||
// 21. If the element uses srcset or picture, set request's initiator to "imageset".
|
||||
if (uses_srcset_or_picture())
|
||||
request->set_initiator(Fetch::Infrastructure::Request::Initiator::ImageSet);
|
||||
|
||||
// 21. Set request's referrer policy to the current state of the element's referrerpolicy attribute.
|
||||
// 22. Set request's referrer policy to the current state of the element's referrerpolicy attribute.
|
||||
request->set_referrer_policy(ReferrerPolicy::from_string(get_attribute_value(HTML::AttributeNames::referrerpolicy)).value_or(ReferrerPolicy::ReferrerPolicy::EmptyString));
|
||||
|
||||
// 22. Set request's priority to the current state of the element's fetchpriority attribute.
|
||||
// 23. Set request's priority to the current state of the element's fetchpriority attribute.
|
||||
request->set_priority(Fetch::Infrastructure::request_priority_from_string(get_attribute_value(HTML::AttributeNames::fetchpriority)).value_or(Fetch::Infrastructure::Request::Priority::Auto));
|
||||
|
||||
// 24. If the will lazy load element steps given the img return true, then:
|
||||
// 25. If the will lazy load element steps given the img return true, then:
|
||||
if (will_lazy_load_element()) {
|
||||
// 1. Set the img's lazy load resumption steps to the rest of this algorithm starting with the step labeled fetch the image.
|
||||
set_lazy_load_resumption_steps([this, request, image_request]() {
|
||||
|
@ -769,7 +772,7 @@ after_step_7:
|
|||
}));
|
||||
}
|
||||
|
||||
void HTMLImageElement::add_callbacks_to_image_request(GC::Ref<ImageRequest> image_request, bool maybe_omit_events, URL::URL const& url_string, String const& previous_url)
|
||||
void HTMLImageElement::add_callbacks_to_image_request(GC::Ref<ImageRequest> image_request, bool maybe_omit_events, String const& url_string, String const& previous_url)
|
||||
{
|
||||
image_request->add_callbacks(
|
||||
[this, image_request, maybe_omit_events, url_string, previous_url]() {
|
||||
|
@ -803,7 +806,7 @@ void HTMLImageElement::add_callbacks_to_image_request(GC::Ref<ImageRequest> imag
|
|||
layout_node->set_needs_layout_update(DOM::SetNeedsLayoutReason::HTMLImageElementUpdateTheImageData);
|
||||
|
||||
// 4. If maybe omit events is not set or previousURL is not equal to urlString, then fire an event named load at the img element.
|
||||
if (!maybe_omit_events || previous_url != url_string.serialize())
|
||||
if (!maybe_omit_events || previous_url != url_string)
|
||||
dispatch_event(DOM::Event::create(realm(), HTML::EventNames::load));
|
||||
|
||||
if (image_data->is_animated() && image_data->frame_count() > 1) {
|
||||
|
@ -832,7 +835,7 @@ void HTMLImageElement::add_callbacks_to_image_request(GC::Ref<ImageRequest> imag
|
|||
// and then, if maybe omit events is not set or previousURL is not equal to urlString,
|
||||
// queue an element task on the DOM manipulation task source given the img element
|
||||
// to fire an event named error at the img element.
|
||||
if (!maybe_omit_events || previous_url != url_string.serialize())
|
||||
if (!maybe_omit_events || previous_url != url_string)
|
||||
dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error));
|
||||
|
||||
m_load_event_delayer.clear();
|
||||
|
@ -883,40 +886,40 @@ void HTMLImageElement::react_to_changes_in_the_environment()
|
|||
if (selected_source == m_last_selected_source && pixel_density == m_current_request->current_pixel_density())
|
||||
return;
|
||||
|
||||
// 6. ⌛ Parse selected source, relative to the element's node document,
|
||||
// and let urlString be the resulting URL string. If that is not successful, then return.
|
||||
auto maybe_url = document().parse_url(selected_source.value());
|
||||
if (!maybe_url.has_value())
|
||||
return;
|
||||
auto url_string = maybe_url->serialize();
|
||||
// 6. ⌛ Let urlString be the result of encoding-parsing-and-serializing a URL given selected source, relative to the element's node document.
|
||||
auto url_string = document().encoding_parse_and_serialize_url(selected_source.value());
|
||||
|
||||
// 7. ⌛ Let corsAttributeState be the state of the element's crossorigin content attribute.
|
||||
// 7. ⌛ If urlString is failure, then return.
|
||||
if (!url_string.has_value())
|
||||
return;
|
||||
|
||||
// 8. ⌛ Let corsAttributeState be the state of the element's crossorigin content attribute.
|
||||
auto cors_attribute_state = m_cors_setting;
|
||||
|
||||
// 8. ⌛ Let origin be the img element's node document's origin.
|
||||
// 9. ⌛ Let origin be the img element's node document's origin.
|
||||
auto origin = document().origin();
|
||||
|
||||
// 9. ⌛ Let client be the img element's node document's relevant settings object.
|
||||
// 10. ⌛ Let client be the img element's node document's relevant settings object.
|
||||
auto& client = document().relevant_settings_object();
|
||||
|
||||
// 10. ⌛ Let key be a tuple consisting of urlString, corsAttributeState, and, if corsAttributeState is not No CORS, origin.
|
||||
// 11. ⌛ Let key be a tuple consisting of urlString, corsAttributeState, and, if corsAttributeState is not No CORS, origin.
|
||||
ListOfAvailableImages::Key key;
|
||||
key.url = *maybe_url;
|
||||
key.url = *url_string;
|
||||
key.mode = m_cors_setting;
|
||||
if (cors_attribute_state != CORSSettingAttribute::NoCORS)
|
||||
key.origin = document().origin();
|
||||
|
||||
// 11. ⌛ Let image request be a new image request whose current URL is urlString
|
||||
// 12. ⌛ Let image request be a new image request whose current URL is urlString
|
||||
auto image_request = ImageRequest::create(realm(), document().page());
|
||||
image_request->set_current_url(realm(), url_string);
|
||||
image_request->set_current_url(realm(), *url_string);
|
||||
|
||||
// 12. ⌛ Set the element's pending request to image request.
|
||||
// 13. ⌛ Set the element's pending request to image request.
|
||||
m_pending_request = image_request;
|
||||
|
||||
// FIXME: 13. End the synchronous section, continuing the remaining steps in parallel.
|
||||
// FIXME: 14. End the synchronous section, continuing the remaining steps in parallel.
|
||||
|
||||
auto step_15 = [this](String const& selected_source, GC::Ref<ImageRequest> image_request, ListOfAvailableImages::Key const& key, GC::Ref<DecodedImageData> image_data) {
|
||||
// 15. Queue an element task on the DOM manipulation task source given the img element and the following steps:
|
||||
auto step_16 = [this](String const& selected_source, GC::Ref<ImageRequest> image_request, ListOfAvailableImages::Key const& key, GC::Ref<DecodedImageData> image_data) {
|
||||
// 16. Queue an element task on the DOM manipulation task source given the img element and the following steps:
|
||||
queue_an_element_task(HTML::Task::Source::DOMManipulation, [this, selected_source, image_request, key, image_data] {
|
||||
// 1. FIXME: If the img element has experienced relevant mutations since this algorithm started, then set the pending request to null and abort these steps.
|
||||
// AD-HOC: Check if we have a pending request still, otherwise we will crash when upgrading the request. This will happen if the image has experienced mutations,
|
||||
|
@ -949,16 +952,20 @@ void HTMLImageElement::react_to_changes_in_the_environment()
|
|||
});
|
||||
};
|
||||
|
||||
// 14. If the list of available images contains an entry for key, then set image request's image data to that of the entry.
|
||||
// 15. If the list of available images contains an entry for key, then set image request's image data to that of the entry.
|
||||
// Continue to the next step.
|
||||
if (auto* entry = document().list_of_available_images().get(key)) {
|
||||
image_request->set_image_data(entry->image_data);
|
||||
step_15(selected_source.value(), *image_request, key, entry->image_data);
|
||||
step_16(selected_source.value(), *image_request, key, entry->image_data);
|
||||
}
|
||||
// Otherwise:
|
||||
else {
|
||||
// AD-HOC: create_potential_CORS_request expects a url, but the following step passes a URL string.
|
||||
auto url_record = document().encoding_parse_url(selected_source.value());
|
||||
VERIFY(url_record.has_value());
|
||||
|
||||
// 1. Let request be the result of creating a potential-CORS request given urlString, "image", and corsAttributeState.
|
||||
auto request = create_potential_CORS_request(vm(), *maybe_url, Fetch::Infrastructure::Request::Destination::Image, m_cors_setting);
|
||||
auto request = create_potential_CORS_request(vm(), *url_record, Fetch::Infrastructure::Request::Destination::Image, m_cors_setting);
|
||||
|
||||
// 2. Set request's client to client, set request's initiator to "imageset", and set request's synchronous flag.
|
||||
request->set_client(&client);
|
||||
|
@ -971,7 +978,7 @@ void HTMLImageElement::react_to_changes_in_the_environment()
|
|||
|
||||
// Set the callbacks to handle steps 6 and 7 before starting the fetch request.
|
||||
image_request->add_callbacks(
|
||||
[this, step_15, selected_source = selected_source.value(), image_request, key]() mutable {
|
||||
[this, step_16, selected_source = selected_source.value(), image_request, key]() mutable {
|
||||
// 6. If response's unsafe response is a network error
|
||||
// NOTE: This is handled in the second callback below.
|
||||
|
||||
|
@ -985,13 +992,13 @@ void HTMLImageElement::react_to_changes_in_the_environment()
|
|||
|
||||
// then set the pending request to null and abort these steps.
|
||||
|
||||
batching_dispatcher().enqueue(GC::create_function(realm().heap(), [step_15, selected_source = move(selected_source), image_request, key] {
|
||||
batching_dispatcher().enqueue(GC::create_function(realm().heap(), [step_16, selected_source = move(selected_source), image_request, key] {
|
||||
// 7. Otherwise, response's unsafe response is image request's image data. It can be either CORS-same-origin
|
||||
// or CORS-cross-origin; this affects the image's interaction with other APIs (e.g., when used on a canvas).
|
||||
VERIFY(image_request->shared_resource_request());
|
||||
auto image_data = image_request->shared_resource_request()->image_data();
|
||||
image_request->set_image_data(image_data);
|
||||
step_15(selected_source, image_request, key, *image_data);
|
||||
step_16(selected_source, image_request, key, *image_data);
|
||||
}));
|
||||
},
|
||||
[this]() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue