HTMLLinkElement.cpp [plain text]
#include "config.h"
#include "HTMLLinkElement.h"
#include "CSSHelper.h"
#include "CachedCSSStyleSheet.h"
#include "DNS.h"
#include "DocLoader.h"
#include "Document.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "FrameTree.h"
#include "HTMLNames.h"
#include "MappedAttribute.h"
#include "MediaList.h"
#include "MediaQueryEvaluator.h"
#include "Page.h"
#include "ScriptEventListener.h"
#include "Settings.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
using namespace HTMLNames;
HTMLLinkElement::HTMLLinkElement(const QualifiedName& qName, Document *doc, bool createdByParser)
: HTMLElement(qName, doc)
, m_cachedSheet(0)
, m_disabledState(0)
, m_loading(false)
, m_alternate(false)
, m_isStyleSheet(false)
, m_isIcon(false)
, m_isDNSPrefetch(false)
, m_createdByParser(createdByParser)
{
ASSERT(hasTagName(linkTag));
}
HTMLLinkElement::~HTMLLinkElement()
{
if (m_cachedSheet) {
m_cachedSheet->removeClient(this);
if (m_loading && !isDisabled() && !isAlternate())
document()->removePendingSheet();
}
}
void HTMLLinkElement::setDisabledState(bool _disabled)
{
int oldDisabledState = m_disabledState;
m_disabledState = _disabled ? 2 : 1;
if (oldDisabledState != m_disabledState) {
if (isLoading()) {
if (m_disabledState == 2 && (!m_alternate || oldDisabledState == 1))
document()->removePendingSheet();
if (m_alternate && m_disabledState == 1)
document()->addPendingSheet();
if (!m_alternate && m_disabledState == 1 && oldDisabledState == 2)
document()->addPendingSheet();
return;
}
if (!m_sheet && m_disabledState == 1)
process();
else
document()->updateStyleSelector(); }
}
StyleSheet* HTMLLinkElement::sheet() const
{
return m_sheet.get();
}
void HTMLLinkElement::parseMappedAttribute(MappedAttribute *attr)
{
if (attr->name() == relAttr) {
tokenizeRelAttribute(attr->value(), m_isStyleSheet, m_alternate, m_isIcon, m_isDNSPrefetch);
process();
} else if (attr->name() == hrefAttr) {
m_url = document()->completeURL(deprecatedParseURL(attr->value()));
process();
} else if (attr->name() == typeAttr) {
m_type = attr->value();
process();
} else if (attr->name() == mediaAttr) {
m_media = attr->value().string().lower();
process();
} else if (attr->name() == disabledAttr) {
setDisabledState(!attr->isNull());
} else if (attr->name() == onbeforeloadAttr)
setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, attr));
else {
if (attr->name() == titleAttr && m_sheet)
m_sheet->setTitle(attr->value());
HTMLElement::parseMappedAttribute(attr);
}
}
void HTMLLinkElement::tokenizeRelAttribute(const AtomicString& rel, bool& styleSheet, bool& alternate, bool& icon, bool& dnsPrefetch)
{
styleSheet = false;
icon = false;
alternate = false;
dnsPrefetch = false;
if (equalIgnoringCase(rel, "stylesheet"))
styleSheet = true;
else if (equalIgnoringCase(rel, "icon") || equalIgnoringCase(rel, "shortcut icon"))
icon = true;
else if (equalIgnoringCase(rel, "dns-prefetch"))
dnsPrefetch = true;
else if (equalIgnoringCase(rel, "alternate stylesheet") || equalIgnoringCase(rel, "stylesheet alternate")) {
styleSheet = true;
alternate = true;
} else {
String relString = rel.string();
relString.replace('\n', ' ');
Vector<String> list;
relString.split(' ', list);
Vector<String>::const_iterator end = list.end();
for (Vector<String>::const_iterator it = list.begin(); it != end; ++it) {
if (equalIgnoringCase(*it, "stylesheet"))
styleSheet = true;
else if (equalIgnoringCase(*it, "alternate"))
alternate = true;
else if (equalIgnoringCase(*it, "icon"))
icon = true;
}
}
}
void HTMLLinkElement::process()
{
if (!inDocument())
return;
String type = m_type.lower();
if (m_isIcon && m_url.isValid() && !m_url.isEmpty())
document()->setIconURL(m_url.string(), type);
if (m_isDNSPrefetch && m_url.isValid() && !m_url.isEmpty())
prefetchDNS(m_url.host());
bool acceptIfTypeContainsTextCSS = document()->page() && document()->page()->settings() && document()->page()->settings()->treatsAnyTextCSSLinkAsStylesheet();
if (m_disabledState != 2 && (m_isStyleSheet || (acceptIfTypeContainsTextCSS && type.contains("text/css"))) && document()->frame() && m_url.isValid()) {
String charset = getAttribute(charsetAttr);
if (charset.isEmpty() && document()->frame())
charset = document()->frame()->loader()->encoding();
if (m_cachedSheet) {
if (m_loading)
document()->removePendingSheet();
m_cachedSheet->removeClient(this);
m_cachedSheet = 0;
}
if (!dispatchBeforeLoadEvent(m_url))
return;
m_loading = true;
if (!isAlternate())
document()->addPendingSheet();
m_cachedSheet = document()->docLoader()->requestCSSStyleSheet(m_url, charset);
if (m_cachedSheet)
m_cachedSheet->addClient(this);
else {
m_loading = false;
if (!isAlternate())
document()->removePendingSheet();
}
} else if (m_sheet) {
m_sheet = 0;
document()->updateStyleSelector();
}
}
void HTMLLinkElement::insertedIntoDocument()
{
HTMLElement::insertedIntoDocument();
document()->addStyleSheetCandidateNode(this, m_createdByParser);
process();
}
void HTMLLinkElement::removedFromDocument()
{
HTMLElement::removedFromDocument();
document()->removeStyleSheetCandidateNode(this);
if (document()->renderer())
document()->updateStyleSelector();
}
void HTMLLinkElement::finishParsingChildren()
{
m_createdByParser = false;
HTMLElement::finishParsingChildren();
}
void HTMLLinkElement::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CachedCSSStyleSheet* sheet)
{
m_sheet = CSSStyleSheet::create(this, href, baseURL, charset);
bool strictParsing = !document()->inCompatMode();
bool enforceMIMEType = strictParsing;
bool crossOriginCSS = false;
bool validMIMEType = false;
bool needsSiteSpecificQuirks = document()->page() && document()->page()->settings()->needsSiteSpecificQuirks();
if (enforceMIMEType && document()->page() && !document()->page()->settings()->enforceCSSMIMETypeInStrictMode())
enforceMIMEType = false;
#if defined(BUILDING_ON_TIGER) || defined(BUILDING_ON_LEOPARD)
if (enforceMIMEType && needsSiteSpecificQuirks) {
if (baseURL.string().contains("mcafee.com/japan/", false))
enforceMIMEType = false;
}
#endif
String sheetText = sheet->sheetText(enforceMIMEType, &validMIMEType);
m_sheet->parseString(sheetText, strictParsing);
if (!document()->securityOrigin()->canRequest(baseURL))
crossOriginCSS = true;
if (crossOriginCSS && !validMIMEType && !m_sheet->hasSyntacticallyValidCSSHeader())
m_sheet = CSSStyleSheet::create(this, href, baseURL, charset);
if (strictParsing && needsSiteSpecificQuirks) {
DEFINE_STATIC_LOCAL(const String, slashKHTMLFixesDotCss, ("/KHTMLFixes.css"));
DEFINE_STATIC_LOCAL(const String, mediaWikiKHTMLFixesStyleSheet, ("/* KHTML fix stylesheet */\n/* work around the horizontal scrollbars */\n#column-content { margin-left: 0; }\n\n"));
if (baseURL.string().endsWith(slashKHTMLFixesDotCss) && !sheetText.isNull() && mediaWikiKHTMLFixesStyleSheet.startsWith(sheetText)
&& sheetText.length() >= mediaWikiKHTMLFixesStyleSheet.length() - 1) {
ASSERT(m_sheet->length() == 1);
ExceptionCode ec;
m_sheet->deleteRule(0, ec);
}
}
m_sheet->setTitle(title());
RefPtr<MediaList> media = MediaList::createAllowingDescriptionSyntax(m_media);
m_sheet->setMedia(media.get());
m_loading = false;
m_sheet->checkLoaded();
}
bool HTMLLinkElement::isLoading() const
{
if (m_loading)
return true;
if (!m_sheet)
return false;
return static_cast<CSSStyleSheet *>(m_sheet.get())->isLoading();
}
bool HTMLLinkElement::sheetLoaded()
{
if (!isLoading() && !isDisabled() && !isAlternate()) {
document()->removePendingSheet();
return true;
}
return false;
}
bool HTMLLinkElement::isURLAttribute(Attribute *attr) const
{
return attr->name() == hrefAttr;
}
bool HTMLLinkElement::disabled() const
{
return !getAttribute(disabledAttr).isNull();
}
void HTMLLinkElement::setDisabled(bool disabled)
{
setAttribute(disabledAttr, disabled ? "" : 0);
}
String HTMLLinkElement::charset() const
{
return getAttribute(charsetAttr);
}
void HTMLLinkElement::setCharset(const String& value)
{
setAttribute(charsetAttr, value);
}
KURL HTMLLinkElement::href() const
{
return document()->completeURL(getAttribute(hrefAttr));
}
void HTMLLinkElement::setHref(const String& value)
{
setAttribute(hrefAttr, value);
}
String HTMLLinkElement::hreflang() const
{
return getAttribute(hreflangAttr);
}
void HTMLLinkElement::setHreflang(const String& value)
{
setAttribute(hreflangAttr, value);
}
String HTMLLinkElement::media() const
{
return getAttribute(mediaAttr);
}
void HTMLLinkElement::setMedia(const String& value)
{
setAttribute(mediaAttr, value);
}
String HTMLLinkElement::rel() const
{
return getAttribute(relAttr);
}
void HTMLLinkElement::setRel(const String& value)
{
setAttribute(relAttr, value);
}
String HTMLLinkElement::rev() const
{
return getAttribute(revAttr);
}
void HTMLLinkElement::setRev(const String& value)
{
setAttribute(revAttr, value);
}
String HTMLLinkElement::target() const
{
return getAttribute(targetAttr);
}
void HTMLLinkElement::setTarget(const String& value)
{
setAttribute(targetAttr, value);
}
String HTMLLinkElement::type() const
{
return getAttribute(typeAttr);
}
void HTMLLinkElement::setType(const String& value)
{
setAttribute(typeAttr, value);
}
void HTMLLinkElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
{
HTMLElement::addSubresourceAttributeURLs(urls);
if (m_isIcon)
return;
if (!m_isStyleSheet)
return;
addSubresourceURL(urls, href());
if (StyleSheet* styleSheet = const_cast<HTMLLinkElement*>(this)->sheet())
styleSheet->addSubresourceStyleURLs(urls);
}
}