Serenity Operating System
at master 114 lines 4.4 kB view raw
1/* 2 * Copyright (c) 2020, the SerenityOS developers. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibWeb/DOM/Document.h> 8#include <LibWeb/HTML/HTMLBaseElement.h> 9 10namespace Web::HTML { 11 12HTMLBaseElement::HTMLBaseElement(DOM::Document& document, DOM::QualifiedName qualified_name) 13 : HTMLElement(document, move(qualified_name)) 14{ 15} 16 17HTMLBaseElement::~HTMLBaseElement() = default; 18 19JS::ThrowCompletionOr<void> HTMLBaseElement::initialize(JS::Realm& realm) 20{ 21 MUST_OR_THROW_OOM(Base::initialize(realm)); 22 set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLBaseElementPrototype>(realm, "HTMLBaseElement")); 23 24 return {}; 25} 26 27void HTMLBaseElement::inserted() 28{ 29 HTMLElement::inserted(); 30 31 document().update_base_element({}); 32 33 // The frozen base URL must be immediately set for an element whenever any of the following situations occur: 34 // - The base element becomes the first base element in tree order with an href content attribute in its Document. 35 36 // NOTE: inserted() is called after this element has been inserted into the document. 37 auto first_base_element_with_href_in_document = document().first_base_element_with_href_in_tree_order(); 38 if (first_base_element_with_href_in_document.ptr() == this) 39 set_the_frozen_base_url(); 40} 41 42void HTMLBaseElement::removed_from(Node* parent) 43{ 44 HTMLElement::removed_from(parent); 45 document().update_base_element({}); 46} 47 48void HTMLBaseElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) 49{ 50 HTMLElement::parse_attribute(name, value); 51 52 // The frozen base URL must be immediately set for an element whenever any of the following situations occur: 53 // - The base element is the first base element in tree order with an href content attribute in its Document, and its href content attribute is changed. 54 if (name != AttributeNames::href) 55 return; 56 57 document().update_base_element({}); 58 59 auto first_base_element_with_href_in_document = document().first_base_element_with_href_in_tree_order(); 60 if (first_base_element_with_href_in_document.ptr() == this) 61 set_the_frozen_base_url(); 62} 63 64// https://html.spec.whatwg.org/multipage/semantics.html#set-the-frozen-base-url 65void HTMLBaseElement::set_the_frozen_base_url() 66{ 67 // 1. Let document be element's node document. 68 auto& document = this->document(); 69 70 // 2. Let urlRecord be the result of parsing the value of element's href content attribute with document's fallback base URL, and document's character encoding. (Thus, the base element isn't affected by itself.) 71 auto href = attribute(AttributeNames::href); 72 auto url_record = document.fallback_base_url().complete_url(href); 73 74 // 3. Set element's frozen base URL to document's fallback base URL, if urlRecord is failure or running Is base allowed for Document? on the resulting URL record and document returns "Blocked", and to urlRecord otherwise. 75 // FIXME: Apply "Is base allowed for Document?" CSP 76 if (!url_record.is_valid()) { 77 m_frozen_base_url = document.fallback_base_url(); 78 return; 79 } 80 81 m_frozen_base_url = move(url_record); 82} 83 84// https://html.spec.whatwg.org/multipage/semantics.html#dom-base-href 85DeprecatedString HTMLBaseElement::href() const 86{ 87 // 1. Let document be element's node document. 88 auto& document = this->document(); 89 90 // 2. Let url be the value of the href attribute of this element, if it has one, and the empty string otherwise. 91 auto url = DeprecatedString::empty(); 92 if (has_attribute(AttributeNames::href)) 93 url = attribute(AttributeNames::href); 94 95 // 3. Let urlRecord be the result of parsing url with document's fallback base URL, and document's character encoding. (Thus, the base element isn't affected by other base elements or itself.) 96 // FIXME: Pass in document's character encoding. 97 auto url_record = document.fallback_base_url().complete_url(url); 98 99 // 4. If urlRecord is failure, return url. 100 if (!url_record.is_valid()) 101 return url; 102 103 // 5. Return the serialization of urlRecord. 104 return url_record.to_deprecated_string(); 105} 106 107// https://html.spec.whatwg.org/multipage/semantics.html#dom-base-href 108void HTMLBaseElement::set_href(DeprecatedString const& href) 109{ 110 // The href IDL attribute, on setting, must set the href content attribute to the given new value. 111 MUST(set_attribute(AttributeNames::href, href)); 112} 113 114}