#include "config.h"
#include "HTMLDocument.h"
#include "CSSPropertyNames.h"
#include "CommonVM.h"
#include "CookieJar.h"
#include "DOMWindow.h"
#include "DocumentLoader.h"
#include "DocumentType.h"
#include "ElementChildIterator.h"
#include "FocusController.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "HTMLBodyElement.h"
#include "HTMLCollection.h"
#include "HTMLDocumentParser.h"
#include "HTMLElementFactory.h"
#include "HTMLFrameOwnerElement.h"
#include "HTMLFrameSetElement.h"
#include "HTMLHtmlElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLNames.h"
#include "ScriptController.h"
#include "StyleResolver.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/text/CString.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLDocument);
using namespace HTMLNames;
Ref<HTMLDocument> HTMLDocument::createSynthesizedDocument(Frame& frame, const URL& url)
{
return adoptRef(*new HTMLDocument(&frame, url, HTMLDocumentClass, Synthesized));
}
HTMLDocument::HTMLDocument(Frame* frame, const URL& url, DocumentClassFlags documentClasses, unsigned constructionFlags)
: Document(frame, url, documentClasses | HTMLDocumentClass, constructionFlags)
{
clearXMLVersion();
}
HTMLDocument::~HTMLDocument() = default;
int HTMLDocument::width()
{
updateLayoutIgnorePendingStylesheets();
RefPtr<FrameView> frameView = view();
return frameView ? frameView->contentsWidth() : 0;
}
int HTMLDocument::height()
{
updateLayoutIgnorePendingStylesheets();
RefPtr<FrameView> frameView = view();
return frameView ? frameView->contentsHeight() : 0;
}
Ref<DocumentParser> HTMLDocument::createParser()
{
return HTMLDocumentParser::create(*this);
}
Optional<Variant<RefPtr<WindowProxy>, RefPtr<Element>, RefPtr<HTMLCollection>>> HTMLDocument::namedItem(const AtomString& name)
{
if (name.isNull() || !hasDocumentNamedItem(*name.impl()))
return WTF::nullopt;
if (UNLIKELY(documentNamedItemContainsMultipleElements(*name.impl()))) {
auto collection = documentNamedItems(name);
ASSERT(collection->length() > 1);
return Variant<RefPtr<WindowProxy>, RefPtr<Element>, RefPtr<HTMLCollection>> { RefPtr<HTMLCollection> { WTFMove(collection) } };
}
auto& element = *documentNamedItem(*name.impl());
if (UNLIKELY(is<HTMLIFrameElement>(element))) {
if (auto domWindow = makeRefPtr(downcast<HTMLIFrameElement>(element).contentWindow()))
return Variant<RefPtr<WindowProxy>, RefPtr<Element>, RefPtr<HTMLCollection>> { WTFMove(domWindow) };
}
return Variant<RefPtr<WindowProxy>, RefPtr<Element>, RefPtr<HTMLCollection>> { RefPtr<Element> { &element } };
}
Vector<AtomString> HTMLDocument::supportedPropertyNames() const
{
return { };
}
void HTMLDocument::addDocumentNamedItem(const AtomStringImpl& name, Element& item)
{
m_documentNamedItem.add(name, item, *this);
addImpureProperty(AtomString(const_cast<AtomStringImpl*>(&name)));
}
void HTMLDocument::removeDocumentNamedItem(const AtomStringImpl& name, Element& item)
{
m_documentNamedItem.remove(name, item);
}
void HTMLDocument::addWindowNamedItem(const AtomStringImpl& name, Element& item)
{
m_windowNamedItem.add(name, item, *this);
}
void HTMLDocument::removeWindowNamedItem(const AtomStringImpl& name, Element& item)
{
m_windowNamedItem.remove(name, item);
}
bool HTMLDocument::isCaseSensitiveAttribute(const QualifiedName& attributeName)
{
static const auto caseInsensitiveAttributeSet = makeNeverDestroyed([] {
static const QualifiedName* const names[] = {
&accept_charsetAttr.get(),
&acceptAttr.get(),
&alignAttr.get(),
&alinkAttr.get(),
&axisAttr.get(),
&bgcolorAttr.get(),
&charsetAttr.get(),
&checkedAttr.get(),
&clearAttr.get(),
&codetypeAttr.get(),
&colorAttr.get(),
&compactAttr.get(),
&declareAttr.get(),
&deferAttr.get(),
&dirAttr.get(),
&disabledAttr.get(),
&enctypeAttr.get(),
&faceAttr.get(),
&frameAttr.get(),
&hreflangAttr.get(),
&http_equivAttr.get(),
&langAttr.get(),
&languageAttr.get(),
&linkAttr.get(),
&mediaAttr.get(),
&methodAttr.get(),
&multipleAttr.get(),
&nohrefAttr.get(),
&noresizeAttr.get(),
&noshadeAttr.get(),
&nowrapAttr.get(),
&readonlyAttr.get(),
&relAttr.get(),
&revAttr.get(),
&rulesAttr.get(),
&scopeAttr.get(),
&scrollingAttr.get(),
&selectedAttr.get(),
&shapeAttr.get(),
&targetAttr.get(),
&textAttr.get(),
&typeAttr.get(),
&valignAttr.get(),
&valuetypeAttr.get(),
&vlinkAttr.get(),
};
HashSet<AtomString> set;
for (auto* name : names)
set.add(name->localName());
return set;
}());
bool isPossibleHTMLAttr = !attributeName.hasPrefix() && attributeName.namespaceURI().isNull();
return !isPossibleHTMLAttr || !caseInsensitiveAttributeSet.get().contains(attributeName.localName());
}
bool HTMLDocument::isFrameSet() const
{
if (!documentElement())
return false;
return !!childrenOfType<HTMLFrameSetElement>(*documentElement()).first();
}
Ref<Document> HTMLDocument::cloneDocumentWithoutChildren() const
{
return create(nullptr, url());
}
}