| 1 | /* |
| 2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 | * (C) 2001 Dirk Mueller (mueller@kde.org) |
| 5 | * Copyright (C) 2003, 2008, 2009, 2010 Apple Inc. All rights reserved. |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Library General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Library General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Library General Public License |
| 18 | * along with this library; see the file COPYING.LIB. If not, write to |
| 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include "HTMLBaseElement.h" |
| 25 | |
| 26 | #include "Document.h" |
| 27 | #include "HTMLNames.h" |
| 28 | #include "HTMLParserIdioms.h" |
| 29 | #include "TextResourceDecoder.h" |
| 30 | #include <wtf/IsoMallocInlines.h> |
| 31 | |
| 32 | namespace WebCore { |
| 33 | |
| 34 | WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLBaseElement); |
| 35 | |
| 36 | using namespace HTMLNames; |
| 37 | |
| 38 | inline HTMLBaseElement::HTMLBaseElement(const QualifiedName& tagName, Document& document) |
| 39 | : HTMLElement(tagName, document) |
| 40 | { |
| 41 | ASSERT(hasTagName(baseTag)); |
| 42 | } |
| 43 | |
| 44 | Ref<HTMLBaseElement> HTMLBaseElement::create(const QualifiedName& tagName, Document& document) |
| 45 | { |
| 46 | return adoptRef(*new HTMLBaseElement(tagName, document)); |
| 47 | } |
| 48 | |
| 49 | void HTMLBaseElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
| 50 | { |
| 51 | if (name == hrefAttr || name == targetAttr) |
| 52 | document().processBaseElement(); |
| 53 | else |
| 54 | HTMLElement::parseAttribute(name, value); |
| 55 | } |
| 56 | |
| 57 | Node::InsertedIntoAncestorResult HTMLBaseElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree) |
| 58 | { |
| 59 | HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree); |
| 60 | if (insertionType.connectedToDocument) |
| 61 | document().processBaseElement(); |
| 62 | return InsertedIntoAncestorResult::Done; |
| 63 | } |
| 64 | |
| 65 | void HTMLBaseElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree) |
| 66 | { |
| 67 | HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree); |
| 68 | if (removalType.disconnectedFromDocument) |
| 69 | document().processBaseElement(); |
| 70 | } |
| 71 | |
| 72 | bool HTMLBaseElement::isURLAttribute(const Attribute& attribute) const |
| 73 | { |
| 74 | return attribute.name().localName() == hrefAttr || HTMLElement::isURLAttribute(attribute); |
| 75 | } |
| 76 | |
| 77 | String HTMLBaseElement::target() const |
| 78 | { |
| 79 | return attributeWithoutSynchronization(targetAttr); |
| 80 | } |
| 81 | |
| 82 | URL HTMLBaseElement::href() const |
| 83 | { |
| 84 | // This does not use the getURLAttribute function because that will resolve relative to the document's base URL; |
| 85 | // base elements like this one can be used to set that base URL. Thus we need to resolve relative to the document's |
| 86 | // URL and ignore the base URL. |
| 87 | |
| 88 | const AtomicString& attributeValue = attributeWithoutSynchronization(hrefAttr); |
| 89 | if (attributeValue.isNull()) |
| 90 | return document().url(); |
| 91 | |
| 92 | auto* encoding = document().decoder() ? document().decoder()->encodingForURLParsing() : nullptr; |
| 93 | URL url(document().url(), stripLeadingAndTrailingHTMLSpaces(attributeValue), encoding); |
| 94 | |
| 95 | if (!url.isValid()) |
| 96 | return URL(); |
| 97 | |
| 98 | return url; |
| 99 | } |
| 100 | |
| 101 | void HTMLBaseElement::setHref(const AtomicString& value) |
| 102 | { |
| 103 | setAttributeWithoutSynchronization(hrefAttr, value); |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |