Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: Source/core/dom/Document.cpp

Issue 192293002: Use new is*Element() helper functions in DOM code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Add is*Element(PassRefPtr) helper Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org) 5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 Apple Inc. All r ights reserved. 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 Apple Inc. All r ights reserved.
7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) 7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/)
8 * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved. 8 * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved.
9 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 9 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
10 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved. 10 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved.
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 682
683 DOMImplementation& Document::implementation() 683 DOMImplementation& Document::implementation()
684 { 684 {
685 if (!m_implementation) 685 if (!m_implementation)
686 m_implementation = DOMImplementation::create(*this); 686 m_implementation = DOMImplementation::create(*this);
687 return *m_implementation; 687 return *m_implementation;
688 } 688 }
689 689
690 bool Document::hasManifest() const 690 bool Document::hasManifest() const
691 { 691 {
692 return documentElement() && documentElement()->hasTagName(htmlTag) && docume ntElement()->hasAttribute(manifestAttr); 692 return isHTMLHtmlElement(documentElement()) && documentElement()->hasAttribu te(manifestAttr);
693 } 693 }
694 694
695 Location* Document::location() const 695 Location* Document::location() const
696 { 696 {
697 if (!frame()) 697 if (!frame())
698 return 0; 698 return 0;
699 699
700 return &domWindow()->location(); 700 return &domWindow()->location();
701 } 701 }
702 702
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
928 exceptionState.throwDOMException(NamespaceError, "The imported node has an invalid namespace."); 928 exceptionState.throwDOMException(NamespaceError, "The imported node has an invalid namespace.");
929 return nullptr; 929 return nullptr;
930 } 930 }
931 RefPtr<Element> newElement = createElement(oldElement->tagQName(), false ); 931 RefPtr<Element> newElement = createElement(oldElement->tagQName(), false );
932 932
933 newElement->cloneDataFromElement(*oldElement); 933 newElement->cloneDataFromElement(*oldElement);
934 934
935 if (deep) { 935 if (deep) {
936 if (!importContainerNodeChildren(oldElement, newElement, exceptionSt ate)) 936 if (!importContainerNodeChildren(oldElement, newElement, exceptionSt ate))
937 return nullptr; 937 return nullptr;
938 if (oldElement->hasTagName(templateTag) 938 if (isHTMLTemplateElement(*oldElement)
939 && !importContainerNodeChildren(toHTMLTemplateElement(oldElement )->content(), toHTMLTemplateElement(newElement)->content(), exceptionState)) 939 && !importContainerNodeChildren(toHTMLTemplateElement(oldElement )->content(), toHTMLTemplateElement(newElement)->content(), exceptionState))
940 return nullptr; 940 return nullptr;
941 } 941 }
942 942
943 return newElement.release(); 943 return newElement.release();
944 } 944 }
945 case ATTRIBUTE_NODE: 945 case ATTRIBUTE_NODE:
946 return Attr::create(*this, QualifiedName(nullAtom, AtomicString(toAttr(i mportedNode)->name()), nullAtom), toAttr(importedNode)->value()); 946 return Attr::create(*this, QualifiedName(nullAtom, AtomicString(toAttr(i mportedNode)->name()), nullAtom), toAttr(importedNode)->value());
947 case DOCUMENT_FRAGMENT_NODE: { 947 case DOCUMENT_FRAGMENT_NODE: {
948 if (importedNode->isShadowRoot()) { 948 if (importedNode->isShadowRoot()) {
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
1328 m_titleSetExplicitly = true; 1328 m_titleSetExplicitly = true;
1329 if (!isHTMLDocument() && !isXHTMLDocument()) 1329 if (!isHTMLDocument() && !isXHTMLDocument())
1330 m_titleElement = nullptr; 1330 m_titleElement = nullptr;
1331 else if (!m_titleElement) { 1331 else if (!m_titleElement) {
1332 if (HTMLElement* headElement = head()) { 1332 if (HTMLElement* headElement = head()) {
1333 m_titleElement = HTMLTitleElement::create(*this); 1333 m_titleElement = HTMLTitleElement::create(*this);
1334 headElement->appendChild(m_titleElement); 1334 headElement->appendChild(m_titleElement);
1335 } 1335 }
1336 } 1336 }
1337 1337
1338 if (m_titleElement && m_titleElement->hasTagName(titleTag)) 1338 if (isHTMLTitleElement(m_titleElement))
1339 toHTMLTitleElement(m_titleElement)->setText(title); 1339 toHTMLTitleElement(m_titleElement)->setText(title);
1340 else 1340 else
1341 updateTitle(title); 1341 updateTitle(title);
1342 } 1342 }
1343 1343
1344 void Document::setTitleElement(const String& title, Element* titleElement) 1344 void Document::setTitleElement(const String& title, Element* titleElement)
1345 { 1345 {
1346 if (titleElement != m_titleElement) { 1346 if (titleElement != m_titleElement) {
1347 if (m_titleElement || m_titleSetExplicitly) 1347 if (m_titleElement || m_titleSetExplicitly)
1348 // Only allow the first title element to change the title -- others have no effect. 1348 // Only allow the first title element to change the title -- others have no effect.
(...skipping 880 matching lines...) Expand 10 before | Expand all | Expand 10 after
2229 return HTMLDocumentParser::create(toHTMLDocument(this), reportErrors); 2229 return HTMLDocumentParser::create(toHTMLDocument(this), reportErrors);
2230 } 2230 }
2231 // FIXME: this should probably pass the frame instead 2231 // FIXME: this should probably pass the frame instead
2232 return XMLDocumentParser::create(this, view()); 2232 return XMLDocumentParser::create(this, view());
2233 } 2233 }
2234 2234
2235 bool Document::isFrameSet() const 2235 bool Document::isFrameSet() const
2236 { 2236 {
2237 if (!isHTMLDocument()) 2237 if (!isHTMLDocument())
2238 return false; 2238 return false;
2239 HTMLElement* bodyElement = body(); 2239 return isHTMLFrameSetElement(body());
2240 return bodyElement && bodyElement->hasTagName(framesetTag);
2241 } 2240 }
2242 2241
2243 ScriptableDocumentParser* Document::scriptableDocumentParser() const 2242 ScriptableDocumentParser* Document::scriptableDocumentParser() const
2244 { 2243 {
2245 return parser() ? parser()->asScriptableDocumentParser() : 0; 2244 return parser() ? parser()->asScriptableDocumentParser() : 0;
2246 } 2245 }
2247 2246
2248 void Document::open(Document* ownerDocument) 2247 void Document::open(Document* ownerDocument)
2249 { 2248 {
2250 if (ownerDocument) { 2249 if (ownerDocument) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
2315 setReadyState(Loading); 2314 setReadyState(Loading);
2316 2315
2317 return m_parser; 2316 return m_parser;
2318 } 2317 }
2319 2318
2320 HTMLElement* Document::body() const 2319 HTMLElement* Document::body() const
2321 { 2320 {
2322 if (!documentElement()) 2321 if (!documentElement())
2323 return 0; 2322 return 0;
2324 2323
2325 for (Element* child = ElementTraversal::firstWithin(*documentElement()); chi ld; child = ElementTraversal::nextSibling(*child)) { 2324 for (HTMLElement* child = Traversal<HTMLElement>::firstWithin(*documentEleme nt()); child; child = Traversal<HTMLElement>::nextSibling(*child)) {
2326 if (child->hasTagName(framesetTag) || child->hasTagName(bodyTag)) 2325 if (isHTMLFrameSetElement(*child) || isHTMLBodyElement(*child))
2327 return toHTMLElement(child); 2326 return child;
2328 } 2327 }
2329 2328
2330 return 0; 2329 return 0;
2331 } 2330 }
2332 2331
2333 void Document::setBody(PassRefPtr<HTMLElement> prpNewBody, ExceptionState& excep tionState) 2332 void Document::setBody(PassRefPtr<HTMLElement> prpNewBody, ExceptionState& excep tionState)
2334 { 2333 {
2335 RefPtr<HTMLElement> newBody = prpNewBody; 2334 RefPtr<HTMLElement> newBody = prpNewBody;
2336 2335
2337 if (!newBody) { 2336 if (!newBody) {
2338 exceptionState.throwDOMException(HierarchyRequestError, ExceptionMessage s::argumentNullOrIncorrectType(1, "HTMLElement")); 2337 exceptionState.throwDOMException(HierarchyRequestError, ExceptionMessage s::argumentNullOrIncorrectType(1, "HTMLElement"));
2339 return; 2338 return;
2340 } 2339 }
2341 if (!documentElement()) { 2340 if (!documentElement()) {
2342 exceptionState.throwDOMException(HierarchyRequestError, "No document ele ment exists."); 2341 exceptionState.throwDOMException(HierarchyRequestError, "No document ele ment exists.");
2343 return; 2342 return;
2344 } 2343 }
2345 2344
2346 if (!newBody->hasTagName(bodyTag) && !newBody->hasTagName(framesetTag)) { 2345 if (!isHTMLBodyElement(*newBody) && !isHTMLFrameSetElement(*newBody)) {
2347 exceptionState.throwDOMException(HierarchyRequestError, "The new body el ement is of type '" + newBody->tagName() + "'. It must be either a 'BODY' or 'FR AMESET' element."); 2346 exceptionState.throwDOMException(HierarchyRequestError, "The new body el ement is of type '" + newBody->tagName() + "'. It must be either a 'BODY' or 'FR AMESET' element.");
2348 return; 2347 return;
2349 } 2348 }
2350 2349
2351 HTMLElement* oldBody = body(); 2350 HTMLElement* oldBody = body();
2352 if (oldBody == newBody) 2351 if (oldBody == newBody)
2353 return; 2352 return;
2354 2353
2355 if (oldBody) 2354 if (oldBody)
2356 documentElement()->replaceChild(newBody.release(), oldBody, exceptionSta te); 2355 documentElement()->replaceChild(newBody.release(), oldBody, exceptionSta te);
(...skipping 20 matching lines...) Expand all
2377 // Otherwise it's the root element's properties that are to be propagated. 2376 // Otherwise it's the root element's properties that are to be propagated.
2378 Element* rootElement = documentElement(); 2377 Element* rootElement = documentElement();
2379 Element* bodyElement = body(); 2378 Element* bodyElement = body();
2380 if (!rootElement) 2379 if (!rootElement)
2381 return 0; 2380 return 0;
2382 if (!rootStyle) { 2381 if (!rootStyle) {
2383 rootStyle = rootElement->renderStyle(); 2382 rootStyle = rootElement->renderStyle();
2384 if (!rootStyle) 2383 if (!rootStyle)
2385 return 0; 2384 return 0;
2386 } 2385 }
2387 if (bodyElement && rootStyle->isOverflowVisible() && rootElement->hasTagName (htmlTag)) 2386 if (bodyElement && rootStyle->isOverflowVisible() && isHTMLHtmlElement(*root Element))
2388 return bodyElement; 2387 return bodyElement;
2389 return rootElement; 2388 return rootElement;
2390 } 2389 }
2391 2390
2392 void Document::close() 2391 void Document::close()
2393 { 2392 {
2394 // FIXME: We should follow the specification more closely: 2393 // FIXME: We should follow the specification more closely:
2395 // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-cl ose 2394 // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-cl ose
2396 2395
2397 if (!scriptableDocumentParser() || !scriptableDocumentParser()->wasCreatedBy Script() || !scriptableDocumentParser()->isParsing()) 2396 if (!scriptableDocumentParser() || !scriptableDocumentParser()->wasCreatedBy Script() || !scriptableDocumentParser()->isParsing())
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
2551 } 2550 }
2552 2551
2553 void Document::dispatchUnloadEvents() 2552 void Document::dispatchUnloadEvents()
2554 { 2553 {
2555 RefPtr<Document> protect(this); 2554 RefPtr<Document> protect(this);
2556 if (m_parser) 2555 if (m_parser)
2557 m_parser->stopParsing(); 2556 m_parser->stopParsing();
2558 2557
2559 if (m_loadEventProgress >= LoadEventTried && m_loadEventProgress <= UnloadEv entInProgress) { 2558 if (m_loadEventProgress >= LoadEventTried && m_loadEventProgress <= UnloadEv entInProgress) {
2560 Element* currentFocusedElement = focusedElement(); 2559 Element* currentFocusedElement = focusedElement();
2561 if (currentFocusedElement && currentFocusedElement->hasTagName(inputTag) ) 2560 if (isHTMLInputElement(currentFocusedElement))
2562 toHTMLInputElement(currentFocusedElement)->endEditing(); 2561 toHTMLInputElement(*currentFocusedElement).endEditing();
2563 if (m_loadEventProgress < PageHideInProgress) { 2562 if (m_loadEventProgress < PageHideInProgress) {
2564 m_loadEventProgress = PageHideInProgress; 2563 m_loadEventProgress = PageHideInProgress;
2565 if (DOMWindow* window = domWindow()) 2564 if (DOMWindow* window = domWindow())
2566 window->dispatchEvent(PageTransitionEvent::create(EventTypeNames ::pagehide, false), this); 2565 window->dispatchEvent(PageTransitionEvent::create(EventTypeNames ::pagehide, false), this);
2567 if (!m_frame) 2566 if (!m_frame)
2568 return; 2567 return;
2569 2568
2570 // The DocumentLoader (and thus its DocumentLoadTiming) might get de stroyed 2569 // The DocumentLoader (and thus its DocumentLoadTiming) might get de stroyed
2571 // while dispatching the event, so protect it to prevent writing the end 2570 // while dispatching the event, so protect it to prevent writing the end
2572 // time into freed memory. 2571 // time into freed memory.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
2617 2616
2618 bool Document::shouldScheduleLayout() 2617 bool Document::shouldScheduleLayout()
2619 { 2618 {
2620 // This function will only be called when FrameView thinks a layout is neede d. 2619 // This function will only be called when FrameView thinks a layout is neede d.
2621 // This enforces a couple extra rules. 2620 // This enforces a couple extra rules.
2622 // 2621 //
2623 // (a) Only schedule a layout once the stylesheets are loaded. 2622 // (a) Only schedule a layout once the stylesheets are loaded.
2624 // (b) Only schedule layout once we have a body element. 2623 // (b) Only schedule layout once we have a body element.
2625 2624
2626 return (haveStylesheetsLoaded() && body()) 2625 return (haveStylesheetsLoaded() && body())
2627 || (documentElement() && !documentElement()->hasTagName(htmlTag)); 2626 || (documentElement() && !isHTMLHtmlElement(*documentElement()));
2628 } 2627 }
2629 2628
2630 bool Document::shouldParserYieldAgressivelyBeforeScriptExecution() 2629 bool Document::shouldParserYieldAgressivelyBeforeScriptExecution()
2631 { 2630 {
2632 return view() && view()->layoutPending(); 2631 return view() && view()->layoutPending();
2633 } 2632 }
2634 2633
2635 int Document::elapsedTime() const 2634 int Document::elapsedTime() const
2636 { 2635 {
2637 return static_cast<int>((currentTime() - m_startTime) * 1000); 2636 return static_cast<int>((currentTime() - m_startTime) * 1000);
(...skipping 1581 matching lines...) Expand 10 before | Expand all | Expand 10 after
4219 if (!frame() || frame()->tree().parent()) 4218 if (!frame() || frame()->tree().parent())
4220 return KURL(); 4219 return KURL();
4221 4220
4222 // FIXME: Why do we need to wait for FrameStateComplete? 4221 // FIXME: Why do we need to wait for FrameStateComplete?
4223 if (frame()->loader().state() != FrameStateComplete) 4222 if (frame()->loader().state() != FrameStateComplete)
4224 return KURL(); 4223 return KURL();
4225 4224
4226 if (!head()) 4225 if (!head())
4227 return KURL(); 4226 return KURL();
4228 4227
4229 RefPtr<HTMLCollection> children = head()->children(); 4228 for (HTMLLinkElement* linkElement = Traversal<HTMLLinkElement>::firstChild(* head()); linkElement; linkElement = Traversal<HTMLLinkElement>::nextSibling(*lin kElement)) {
4230 for (unsigned i = 0; Element* child = children->item(i); i++) {
4231 if (!child->hasTagName(linkTag))
4232 continue;
4233 HTMLLinkElement* linkElement = toHTMLLinkElement(child);
4234 if (!equalIgnoringCase(linkElement->type(), openSearchMIMEType) || !equa lIgnoringCase(linkElement->rel(), openSearchRelation)) 4229 if (!equalIgnoringCase(linkElement->type(), openSearchMIMEType) || !equa lIgnoringCase(linkElement->rel(), openSearchRelation))
4235 continue; 4230 continue;
4236 if (linkElement->href().isEmpty()) 4231 if (linkElement->href().isEmpty())
4237 continue; 4232 continue;
4238 return linkElement->href(); 4233 return linkElement->href();
4239 } 4234 }
4240 4235
4241 return KURL(); 4236 return KURL();
4242 } 4237 }
4243 4238
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
4344 4339
4345 SVGDocumentExtensions& Document::accessSVGExtensions() 4340 SVGDocumentExtensions& Document::accessSVGExtensions()
4346 { 4341 {
4347 if (!m_svgExtensions) 4342 if (!m_svgExtensions)
4348 m_svgExtensions = adoptPtr(new SVGDocumentExtensions(this)); 4343 m_svgExtensions = adoptPtr(new SVGDocumentExtensions(this));
4349 return *m_svgExtensions; 4344 return *m_svgExtensions;
4350 } 4345 }
4351 4346
4352 bool Document::hasSVGRootNode() const 4347 bool Document::hasSVGRootNode() const
4353 { 4348 {
4354 return documentElement() && documentElement()->hasTagName(SVGNames::svgTag); 4349 return isSVGSVGElement(documentElement());
4355 } 4350 }
4356 4351
4357 PassRefPtr<HTMLCollection> Document::ensureCachedCollection(CollectionType type) 4352 PassRefPtr<HTMLCollection> Document::ensureCachedCollection(CollectionType type)
4358 { 4353 {
4359 return ensureRareData().ensureNodeLists().addCache<HTMLCollection>(*this, ty pe); 4354 return ensureRareData().ensureNodeLists().addCache<HTMLCollection>(*this, ty pe);
4360 } 4355 }
4361 4356
4362 PassRefPtr<HTMLCollection> Document::images() 4357 PassRefPtr<HTMLCollection> Document::images()
4363 { 4358 {
4364 return ensureCachedCollection(DocImages); 4359 return ensureCachedCollection(DocImages);
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
4465 } 4460 }
4466 4461
4467 Vector<IconURL> Document::iconURLs(int iconTypesMask) 4462 Vector<IconURL> Document::iconURLs(int iconTypesMask)
4468 { 4463 {
4469 IconURL firstFavicon; 4464 IconURL firstFavicon;
4470 IconURL firstTouchIcon; 4465 IconURL firstTouchIcon;
4471 IconURL firstTouchPrecomposedIcon; 4466 IconURL firstTouchPrecomposedIcon;
4472 Vector<IconURL> secondaryIcons; 4467 Vector<IconURL> secondaryIcons;
4473 4468
4474 // Start from the last child node so that icons seen later take precedence a s required by the spec. 4469 // Start from the last child node so that icons seen later take precedence a s required by the spec.
4475 RefPtr<HTMLCollection> children = head() ? head()->children() : nullptr; 4470 for (HTMLLinkElement* linkElement = head() ? Traversal<HTMLLinkElement>::fir stChild(*head()) : 0; linkElement; linkElement = Traversal<HTMLLinkElement>::nex tSibling(*linkElement)) {
4476 unsigned length = children ? children->length() : 0;
4477 for (unsigned i = 0; i < length; i++) {
4478 Element* child = children->item(i);
4479 if (!child->hasTagName(linkTag))
4480 continue;
4481 HTMLLinkElement* linkElement = toHTMLLinkElement(child);
4482 if (!(linkElement->iconType() & iconTypesMask)) 4471 if (!(linkElement->iconType() & iconTypesMask))
4483 continue; 4472 continue;
4484 if (linkElement->href().isEmpty()) 4473 if (linkElement->href().isEmpty())
4485 continue; 4474 continue;
4486 if (!RuntimeEnabledFeatures::touchIconLoadingEnabled() && linkElement->i conType() != Favicon) 4475 if (!RuntimeEnabledFeatures::touchIconLoadingEnabled() && linkElement->i conType() != Favicon)
4487 continue; 4476 continue;
4488 4477
4489 IconURL newURL(linkElement->href(), linkElement->iconSizes(), linkElemen t->type(), linkElement->iconType()); 4478 IconURL newURL(linkElement->href(), linkElement->iconSizes(), linkElemen t->type(), linkElement->iconType());
4490 if (linkElement->iconType() == Favicon) { 4479 if (linkElement->iconType() == Favicon) {
4491 if (firstFavicon.m_iconType != InvalidIcon) 4480 if (firstFavicon.m_iconType != InvalidIcon)
(...skipping 992 matching lines...) Expand 10 before | Expand all | Expand 10 after
5484 } 5473 }
5485 5474
5486 void Document::invalidateNodeListCaches(const QualifiedName* attrName) 5475 void Document::invalidateNodeListCaches(const QualifiedName* attrName)
5487 { 5476 {
5488 HashSet<LiveNodeListBase*>::iterator end = m_listsInvalidatedAtDocument.end( ); 5477 HashSet<LiveNodeListBase*>::iterator end = m_listsInvalidatedAtDocument.end( );
5489 for (HashSet<LiveNodeListBase*>::iterator it = m_listsInvalidatedAtDocument. begin(); it != end; ++it) 5478 for (HashSet<LiveNodeListBase*>::iterator it = m_listsInvalidatedAtDocument. begin(); it != end; ++it)
5490 (*it)->invalidateCache(attrName); 5479 (*it)->invalidateCache(attrName);
5491 } 5480 }
5492 5481
5493 } // namespace WebCore 5482 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698