mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 06:42:43 +00:00 
			
		
		
		
	 35612c6a7f
			
		
	
	
		35612c6a7f
		
	
	
	
	
		
			
			This now defaults to serializing the path with percent decoded segments (which is what all callers expect), but has an option not to. This fixes `file://` URLs with spaces in their paths. The name has been changed to serialize_path() path to make it more clear that this method will generate a new string each call (except for the cannot_be_a_base_url() case). A few callers have then been updated to avoid repeatedly calling this function.
		
			
				
	
	
		
			147 lines
		
	
	
	
		
			5.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			147 lines
		
	
	
	
		
			5.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <LibWeb/HTML/WorkerGlobalScope.h>
 | |
| #include <LibWeb/HTML/WorkerLocation.h>
 | |
| 
 | |
| namespace Web::HTML {
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-href
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::href() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
|     // The href getter steps are to return this's WorkerGlobalScope object's url, serialized.
 | |
|     return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_global_scope->url().serialize()));
 | |
| }
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-origin
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::origin() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
|     // The origin getter steps are to return the serialization of this's WorkerGlobalScope object's url's origin.
 | |
|     return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_global_scope->url().serialize_origin()));
 | |
| }
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-protocol
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::protocol() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
|     // The protocol getter steps are to return this's WorkerGlobalScope object's url's scheme, followed by ":".
 | |
|     return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_global_scope->url().scheme().view()));
 | |
| }
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-host
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::host() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
| 
 | |
|     // The host getter steps are:
 | |
|     // 1. Let url be this's WorkerGlobalScope object's url.
 | |
|     auto const& url = m_global_scope->url();
 | |
| 
 | |
|     // 2. If url's host is null, return the empty string.
 | |
|     if (url.host().is_empty())
 | |
|         return String {};
 | |
| 
 | |
|     // 3. If url's port is null, return url's host, serialized.
 | |
|     if (!url.port().has_value())
 | |
|         return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
 | |
| 
 | |
|     // 4. Return url's host, serialized, followed by ":" and url's port, serialized.
 | |
|     return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.host().view(), url.port().value()));
 | |
| }
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hostname
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::hostname() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
| 
 | |
|     // The hostname getter steps are:
 | |
|     // 1. Let host be this's WorkerGlobalScope object's url's host.
 | |
|     auto const& host = m_global_scope->url().host();
 | |
| 
 | |
|     // 2. If host is null, return the empty string.
 | |
|     if (host.is_empty())
 | |
|         return String {};
 | |
| 
 | |
|     // 3. Return host, serialized.
 | |
|     return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(host));
 | |
| }
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-port
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::port() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
| 
 | |
|     // The port getter steps are:
 | |
|     // 1. Let port be this's WorkerGlobalScope object's url's port.
 | |
|     auto const& port = m_global_scope->url().port();
 | |
| 
 | |
|     // 2. If port is null, return the empty string.
 | |
|     if (!port.has_value())
 | |
|         return String {};
 | |
|     // 3. Return port, serialized.
 | |
|     return TRY_OR_THROW_OOM(vm, String::number(port.value()));
 | |
| }
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-pathname
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::pathname() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
|     // The pathname getter steps are to return the result of URL path serializing this's WorkerGlobalScope object's url.
 | |
|     return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_global_scope->url().serialize_path()));
 | |
| }
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-search
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::search() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
| 
 | |
|     // The search getter steps are:
 | |
|     // 1. Let query be this's WorkerGlobalScope object's url's query.
 | |
|     auto const& query = m_global_scope->url().query();
 | |
| 
 | |
|     // 2. If query is either null or the empty string, return the empty string.
 | |
|     if (query.is_empty())
 | |
|         return String {};
 | |
| 
 | |
|     // 3. Return "?", followed by query.
 | |
|     return TRY_OR_THROW_OOM(vm, String::formatted("?{}", query.view()));
 | |
| }
 | |
| 
 | |
| // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hash
 | |
| WebIDL::ExceptionOr<String> WorkerLocation::hash() const
 | |
| {
 | |
|     auto& vm = realm().vm();
 | |
| 
 | |
|     // The hash getter steps are:
 | |
|     // 1. Let fragment be this's WorkerGlobalScope object's url's fragment.
 | |
|     auto const& fragment = m_global_scope->url().fragment();
 | |
| 
 | |
|     // 2. If fragment is either null or the empty string, return the empty string.
 | |
|     if (fragment.is_empty())
 | |
|         return String {};
 | |
| 
 | |
|     // 3. Return "#", followed by fragment.
 | |
|     return TRY_OR_THROW_OOM(vm, String::formatted("#{}", fragment.view()));
 | |
| }
 | |
| 
 | |
| WorkerLocation::WorkerLocation(WorkerGlobalScope& global_scope)
 | |
|     : PlatformObject(global_scope.realm())
 | |
|     , m_global_scope(global_scope)
 | |
| {
 | |
|     // FIXME: Set prototype once we can get to worker scope prototypes.
 | |
| }
 | |
| 
 | |
| WorkerLocation::~WorkerLocation() = default;
 | |
| 
 | |
| void WorkerLocation::visit_edges(Cell::Visitor& visitor)
 | |
| {
 | |
|     Base::visit_edges(visitor);
 | |
|     visitor.visit(m_global_scope);
 | |
| }
 | |
| 
 | |
| }
 |