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

Side by Side Diff: Source/core/html/parser/HTMLConstructionSite.cpp

Issue 198453003: Use new is*Element() helper functions more in HTML code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase 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
« no previous file with comments | « Source/core/html/forms/RadioInputType.cpp ('k') | Source/core/html/parser/HTMLElementStack.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google, Inc. All Rights Reserved. 2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved. 3 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 28 matching lines...) Expand all
39 #include "core/html/HTMLFormElement.h" 39 #include "core/html/HTMLFormElement.h"
40 #include "core/html/HTMLHtmlElement.h" 40 #include "core/html/HTMLHtmlElement.h"
41 #include "core/html/HTMLScriptElement.h" 41 #include "core/html/HTMLScriptElement.h"
42 #include "core/html/HTMLTemplateElement.h" 42 #include "core/html/HTMLTemplateElement.h"
43 #include "core/html/parser/AtomicHTMLToken.h" 43 #include "core/html/parser/AtomicHTMLToken.h"
44 #include "core/html/parser/HTMLParserIdioms.h" 44 #include "core/html/parser/HTMLParserIdioms.h"
45 #include "core/html/parser/HTMLStackItem.h" 45 #include "core/html/parser/HTMLStackItem.h"
46 #include "core/html/parser/HTMLToken.h" 46 #include "core/html/parser/HTMLToken.h"
47 #include "core/loader/FrameLoader.h" 47 #include "core/loader/FrameLoader.h"
48 #include "core/loader/FrameLoaderClient.h" 48 #include "core/loader/FrameLoaderClient.h"
49 #include "core/svg/SVGScriptElement.h"
49 #include "platform/NotImplemented.h" 50 #include "platform/NotImplemented.h"
50 #include "platform/text/TextBreakIterator.h" 51 #include "platform/text/TextBreakIterator.h"
51 #include <limits> 52 #include <limits>
52 53
53 namespace WebCore { 54 namespace WebCore {
54 55
55 using namespace HTMLNames; 56 using namespace HTMLNames;
56 57
57 static const unsigned maximumHTMLParserDOMTreeDepth = 512; 58 static const unsigned maximumHTMLParserDOMTreeDepth = 512;
58 59
59 static inline void setAttributes(Element* element, AtomicHTMLToken* token, Parse rContentPolicy parserContentPolicy) 60 static inline void setAttributes(Element* element, AtomicHTMLToken* token, Parse rContentPolicy parserContentPolicy)
60 { 61 {
61 if (!scriptingContentIsAllowed(parserContentPolicy)) 62 if (!scriptingContentIsAllowed(parserContentPolicy))
62 element->stripScriptingAttributes(token->attributes()); 63 element->stripScriptingAttributes(token->attributes());
63 element->parserSetAttributes(token->attributes()); 64 element->parserSetAttributes(token->attributes());
64 } 65 }
65 66
66 static bool hasImpliedEndTag(const HTMLStackItem* item) 67 static bool hasImpliedEndTag(const HTMLStackItem* item)
67 { 68 {
68 return item->hasTagName(ddTag) 69 return item->hasTagName(ddTag)
69 || item->hasTagName(dtTag) 70 || item->hasTagName(dtTag)
70 || item->hasTagName(liTag) 71 || item->hasTagName(liTag)
71 || item->hasTagName(optionTag) 72 || item->hasTagName(optionTag)
72 || item->hasTagName(optgroupTag) 73 || item->hasTagName(optgroupTag)
73 || item->hasTagName(pTag) 74 || item->hasTagName(pTag)
74 || item->hasTagName(rpTag) 75 || item->hasTagName(rpTag)
75 || item->hasTagName(rtTag); 76 || item->hasTagName(rtTag);
76 } 77 }
77 78
78 static bool shouldUseLengthLimit(const ContainerNode* node) 79 static bool shouldUseLengthLimit(const ContainerNode& node)
79 { 80 {
80 return !node->hasTagName(scriptTag) 81 return !isHTMLScriptElement(node)
81 && !node->hasTagName(styleTag) 82 && !isHTMLStyleElement(node)
82 && !node->hasTagName(SVGNames::scriptTag); 83 && !isSVGScriptElement(node);
83 } 84 }
84 85
85 static unsigned textLengthLimitForContainer(const ContainerNode* node) 86 static unsigned textLengthLimitForContainer(const ContainerNode& node)
86 { 87 {
87 return shouldUseLengthLimit(node) ? Text::defaultLengthLimit : std::numeric_ limits<unsigned>::max(); 88 return shouldUseLengthLimit(node) ? Text::defaultLengthLimit : std::numeric_ limits<unsigned>::max();
88 } 89 }
89 90
90 static inline bool isAllWhitespace(const String& string) 91 static inline bool isAllWhitespace(const String& string)
91 { 92 {
92 return string.isAllSpecialCharacters<isHTMLSpace<UChar> >(); 93 return string.isAllSpecialCharacters<isHTMLSpace<UChar> >();
93 } 94 }
94 95
95 static inline void insert(HTMLConstructionSiteTask& task) 96 static inline void insert(HTMLConstructionSiteTask& task)
96 { 97 {
97 if (task.parent->hasTagName(templateTag)) 98 if (isHTMLTemplateElement(*task.parent))
Inactive 2014/03/14 02:50:44 for example here, I feel it makes sense to use isH
98 task.parent = toHTMLTemplateElement(task.parent.get())->content(); 99 task.parent = toHTMLTemplateElement(task.parent.get())->content();
99 100
100 if (ContainerNode* parent = task.child->parentNode()) 101 if (ContainerNode* parent = task.child->parentNode())
101 parent->parserRemoveChild(*task.child); 102 parent->parserRemoveChild(*task.child);
102 103
103 if (task.nextChild) 104 if (task.nextChild)
104 task.parent->parserInsertBefore(task.child.get(), *task.nextChild); 105 task.parent->parserInsertBefore(task.child.get(), *task.nextChild);
105 else 106 else
106 task.parent->parserAppendChild(task.child.get()); 107 task.parent->parserAppendChild(task.child.get());
107 } 108 }
(...skipping 16 matching lines...) Expand all
124 { 125 {
125 ASSERT(task.operation == HTMLConstructionSiteTask::InsertText); 126 ASSERT(task.operation == HTMLConstructionSiteTask::InsertText);
126 ASSERT(task.child->isTextNode()); 127 ASSERT(task.child->isTextNode());
127 128
128 // Merge text nodes into previous ones if possible: 129 // Merge text nodes into previous ones if possible:
129 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construc tion.html#insert-a-character 130 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construc tion.html#insert-a-character
130 Text* newText = toText(task.child.get()); 131 Text* newText = toText(task.child.get());
131 Node* previousChild = task.nextChild ? task.nextChild->previousSibling() : t ask.parent->lastChild(); 132 Node* previousChild = task.nextChild ? task.nextChild->previousSibling() : t ask.parent->lastChild();
132 if (previousChild && previousChild->isTextNode()) { 133 if (previousChild && previousChild->isTextNode()) {
133 Text* previousText = toText(previousChild); 134 Text* previousText = toText(previousChild);
134 unsigned lengthLimit = textLengthLimitForContainer(task.parent.get()); 135 unsigned lengthLimit = textLengthLimitForContainer(*task.parent);
135 if (previousText->length() + newText->length() < lengthLimit) { 136 if (previousText->length() + newText->length() < lengthLimit) {
136 previousText->parserAppendData(newText->data()); 137 previousText->parserAppendData(newText->data());
137 return; 138 return;
138 } 139 }
139 } 140 }
140 141
141 insert(task); 142 insert(task);
142 } 143 }
143 144
144 static inline void executeReparentTask(HTMLConstructionSiteTask& task) 145 static inline void executeReparentTask(HTMLConstructionSiteTask& task)
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 if (m_pendingText.isEmpty()) 233 if (m_pendingText.isEmpty())
233 return; 234 return;
234 235
235 PendingText pendingText; 236 PendingText pendingText;
236 // Hold onto the current pending text on the stack so that queueTask doesn't recurse infinitely. 237 // Hold onto the current pending text on the stack so that queueTask doesn't recurse infinitely.
237 m_pendingText.swap(pendingText); 238 m_pendingText.swap(pendingText);
238 ASSERT(m_pendingText.isEmpty()); 239 ASSERT(m_pendingText.isEmpty());
239 240
240 // Splitting text nodes into smaller chunks contradicts HTML5 spec, but is n ecessary 241 // Splitting text nodes into smaller chunks contradicts HTML5 spec, but is n ecessary
241 // for performance, see: https://bugs.webkit.org/show_bug.cgi?id=55898 242 // for performance, see: https://bugs.webkit.org/show_bug.cgi?id=55898
242 unsigned lengthLimit = textLengthLimitForContainer(pendingText.parent.get()) ; 243 unsigned lengthLimit = textLengthLimitForContainer(*pendingText.parent);
243 244
244 unsigned currentPosition = 0; 245 unsigned currentPosition = 0;
245 const StringBuilder& string = pendingText.stringBuilder; 246 const StringBuilder& string = pendingText.stringBuilder;
246 while (currentPosition < string.length()) { 247 while (currentPosition < string.length()) {
247 unsigned proposedBreakIndex = std::min(currentPosition + lengthLimit, st ring.length()); 248 unsigned proposedBreakIndex = std::min(currentPosition + lengthLimit, st ring.length());
248 unsigned breakIndex = findBreakIndexBetween(string, currentPosition, pro posedBreakIndex); 249 unsigned breakIndex = findBreakIndexBetween(string, currentPosition, pro posedBreakIndex);
249 ASSERT(breakIndex <= string.length()); 250 ASSERT(breakIndex <= string.length());
250 String substring = string.substring(currentPosition, breakIndex - curren tPosition); 251 String substring = string.substring(currentPosition, breakIndex - curren tPosition);
251 substring = atomizeIfAllWhitespace(substring, pendingText.whitespaceMode ); 252 substring = atomizeIfAllWhitespace(substring, pendingText.whitespaceMode );
252 253
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 RefPtr<Element> body = createHTMLElement(token); 591 RefPtr<Element> body = createHTMLElement(token);
591 attachLater(currentNode(), body); 592 attachLater(currentNode(), body);
592 m_openElements.pushHTMLBodyElement(HTMLStackItem::create(body.release(), tok en)); 593 m_openElements.pushHTMLBodyElement(HTMLStackItem::create(body.release(), tok en));
593 if (LocalFrame* frame = m_document->frame()) 594 if (LocalFrame* frame = m_document->frame())
594 frame->loader().client()->dispatchWillInsertBody(); 595 frame->loader().client()->dispatchWillInsertBody();
595 } 596 }
596 597
597 void HTMLConstructionSite::insertHTMLFormElement(AtomicHTMLToken* token, bool is Demoted) 598 void HTMLConstructionSite::insertHTMLFormElement(AtomicHTMLToken* token, bool is Demoted)
598 { 599 {
599 RefPtr<Element> element = createHTMLElement(token); 600 RefPtr<Element> element = createHTMLElement(token);
600 ASSERT(element->hasTagName(formTag)); 601 ASSERT(isHTMLFormElement(element));
601 m_form = static_pointer_cast<HTMLFormElement>(element.release()); 602 m_form = static_pointer_cast<HTMLFormElement>(element.release());
602 m_form->setDemoted(isDemoted); 603 m_form->setDemoted(isDemoted);
603 attachLater(currentNode(), m_form); 604 attachLater(currentNode(), m_form);
604 m_openElements.push(HTMLStackItem::create(m_form, token)); 605 m_openElements.push(HTMLStackItem::create(m_form, token));
605 } 606 }
606 607
607 void HTMLConstructionSite::insertHTMLElement(AtomicHTMLToken* token) 608 void HTMLConstructionSite::insertHTMLElement(AtomicHTMLToken* token)
608 { 609 {
609 RefPtr<Element> element = createHTMLElement(token); 610 RefPtr<Element> element = createHTMLElement(token);
610 attachLater(currentNode(), element); 611 attachLater(currentNode(), element);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 662
662 void HTMLConstructionSite::insertTextNode(const String& string, WhitespaceMode w hitespaceMode) 663 void HTMLConstructionSite::insertTextNode(const String& string, WhitespaceMode w hitespaceMode)
663 { 664 {
664 HTMLConstructionSiteTask dummyTask(HTMLConstructionSiteTask::Insert); 665 HTMLConstructionSiteTask dummyTask(HTMLConstructionSiteTask::Insert);
665 dummyTask.parent = currentNode(); 666 dummyTask.parent = currentNode();
666 667
667 if (shouldFosterParent()) 668 if (shouldFosterParent())
668 findFosterSite(dummyTask); 669 findFosterSite(dummyTask);
669 670
670 // FIXME: This probably doesn't need to be done both here and in insert(Task ). 671 // FIXME: This probably doesn't need to be done both here and in insert(Task ).
671 if (dummyTask.parent->hasTagName(templateTag)) 672 if (isHTMLTemplateElement(*dummyTask.parent))
672 dummyTask.parent = toHTMLTemplateElement(dummyTask.parent.get())->conten t(); 673 dummyTask.parent = toHTMLTemplateElement(dummyTask.parent.get())->conten t();
673 674
674 // Unclear when parent != case occurs. Somehow we insert text into two separ ate nodes while processing the same Token. 675 // Unclear when parent != case occurs. Somehow we insert text into two separ ate nodes while processing the same Token.
675 // The nextChild != dummy.nextChild case occurs whenever foster parenting ha ppened and we hit a new text node "<table>a</table>b" 676 // The nextChild != dummy.nextChild case occurs whenever foster parenting ha ppened and we hit a new text node "<table>a</table>b"
676 // In either case we have to flush the pending text into the task queue befo re making more. 677 // In either case we have to flush the pending text into the task queue befo re making more.
677 if (!m_pendingText.isEmpty() && (m_pendingText.parent != dummyTask.parent || m_pendingText.nextChild != dummyTask.nextChild)) 678 if (!m_pendingText.isEmpty() && (m_pendingText.parent != dummyTask.parent || m_pendingText.nextChild != dummyTask.nextChild))
678 flushPendingText(); 679 flushPendingText();
679 m_pendingText.append(dummyTask.parent, dummyTask.nextChild, string, whitespa ceMode); 680 m_pendingText.append(dummyTask.parent, dummyTask.nextChild, string, whitespa ceMode);
680 } 681 }
681 682
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 PassRefPtr<Element> HTMLConstructionSite::createElement(AtomicHTMLToken* token, const AtomicString& namespaceURI) 720 PassRefPtr<Element> HTMLConstructionSite::createElement(AtomicHTMLToken* token, const AtomicString& namespaceURI)
720 { 721 {
721 QualifiedName tagName(nullAtom, token->name(), namespaceURI); 722 QualifiedName tagName(nullAtom, token->name(), namespaceURI);
722 RefPtr<Element> element = ownerDocumentForCurrentNode().createElement(tagNam e, true); 723 RefPtr<Element> element = ownerDocumentForCurrentNode().createElement(tagNam e, true);
723 setAttributes(element.get(), token, m_parserContentPolicy); 724 setAttributes(element.get(), token, m_parserContentPolicy);
724 return element.release(); 725 return element.release();
725 } 726 }
726 727
727 inline Document& HTMLConstructionSite::ownerDocumentForCurrentNode() 728 inline Document& HTMLConstructionSite::ownerDocumentForCurrentNode()
728 { 729 {
729 if (currentNode()->hasTagName(templateTag)) 730 if (isHTMLTemplateElement(*currentNode()))
730 return toHTMLTemplateElement(currentElement())->content()->document(); 731 return toHTMLTemplateElement(currentElement())->content()->document();
731 return currentNode()->document(); 732 return currentNode()->document();
732 } 733 }
733 734
734 PassRefPtr<Element> HTMLConstructionSite::createHTMLElement(AtomicHTMLToken* tok en) 735 PassRefPtr<Element> HTMLConstructionSite::createHTMLElement(AtomicHTMLToken* tok en)
735 { 736 {
736 Document& document = ownerDocumentForCurrentNode(); 737 Document& document = ownerDocumentForCurrentNode();
737 // Only associate the element with the current form if we're creating the ne w element 738 // Only associate the element with the current form if we're creating the ne w element
738 // in a document with a browsing context (rather than in <template> contents ). 739 // in a document with a browsing context (rather than in <template> contents ).
739 HTMLFormElement* form = document.frame() ? m_form.get() : 0; 740 HTMLFormElement* form = document.frame() ? m_form.get() : 0;
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 void HTMLConstructionSite::fosterParent(PassRefPtr<Node> node) 852 void HTMLConstructionSite::fosterParent(PassRefPtr<Node> node)
852 { 853 {
853 HTMLConstructionSiteTask task(HTMLConstructionSiteTask::Insert); 854 HTMLConstructionSiteTask task(HTMLConstructionSiteTask::Insert);
854 findFosterSite(task); 855 findFosterSite(task);
855 task.child = node; 856 task.child = node;
856 ASSERT(task.parent); 857 ASSERT(task.parent);
857 queueTask(task); 858 queueTask(task);
858 } 859 }
859 860
860 } 861 }
OLDNEW
« no previous file with comments | « Source/core/html/forms/RadioInputType.cpp ('k') | Source/core/html/parser/HTMLElementStack.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698