Nested template contents are not cloned by document.importNode
authorrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 16 Dec 2014 19:08:54 +0000 (19:08 +0000)
committerrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 16 Dec 2014 19:08:54 +0000 (19:08 +0000)
commitab2c08c3ff0ef25bbca5b1fa27fbdd8afdb3a9bf
tree0798559e9b274f50c231ce90afb9e3abebf3d049
parent193a0637d05d229ee9323051253a6cbe6afe0825
Nested template contents are not cloned by document.importNode
https://bugs.webkit.org/show_bug.cgi?id=137619

Reviewed by Andreas Kling.

Source/WebCore:

The bug was caused by Document.importNode not cloning HTMLTemplateElement's content.

Fixed the bug by sharing code between Document::importNode and Node::cloneNode by
generalizing Node::cloneNodeInternal, added in r177314, by taking the owner document
as an argument. The most of code changes are the result of adding this argument.

Document::importNode is the only function in which the actual logic changes.
Note that the code to import TEXT_NODE, CDATA_SECTION_NODE, ENTITY_REFERENCE_NODE,
PROCESSING_INSTRUCTION_NODE and COMMENT_NODE nodes are identical to that of cloneNode.

This patch removes the call to hasValidNamespaceForElements in ELEMENT_NODE but this
should not introduce an observable behavior change since all instantiated elements
should have a valid namespace in the first place.

Because DOCUMENT_NODE and DOCUMENT_TYPE_NODE cannot be imported and DOCUMENT_TYPE_NODE
can only appear as a direct child of DOCUMENT_NODE, neither nodes nor unimplemented
XPATH_NAMESPACE_NODE and XPATH_NAMESPACE_NODE can appear inside the recursive calls
for ELEMENT_NODE and DOCUMENT_FRAGMENT_NODE nodes.

While importNode behaves differently from cloneNode for ATTRIBUTE_NODE, namely that
it merges all of its child nodes, this behavior isn't present when recursing inside
ELEMENT_NODE and DOCUMENT_FRAGMENT_NODE since we are using cloneDataFromElement.

Thus there should be no observable behavior changes for DOCUMENT_FRAGMENT_NODE and
ELEMENT_NODE nodes either.

Test: fast/dom/HTMLTemplateElement/importNode-nested-templates.html

* dom/Attr.cpp:
(WebCore::Attr::cloneNodeInternal):
* dom/Attr.h:
* dom/CDATASection.cpp:
(WebCore::CDATASection::cloneNodeInternal):
* dom/CDATASection.h:
* dom/Comment.cpp:
(WebCore::Comment::cloneNodeInternal):
* dom/Comment.h:
* dom/ContainerNode.cpp:
(WebCore::ContainerNode::cloneChildNodes):
* dom/Document.cpp:
(WebCore::Document::importNode):
(WebCore::Document::cloneNodeInternal):
* dom/Document.h:
* dom/DocumentFragment.cpp:
(WebCore::DocumentFragment::cloneNodeInternal):
* dom/DocumentFragment.h:
* dom/DocumentType.cpp:
(WebCore::DocumentType::cloneNodeInternal):
* dom/DocumentType.h:
* dom/Element.cpp:
(WebCore::Element::cloneNodeInternal):
(WebCore::Element::cloneElementWithChildren):
(WebCore::Element::cloneElementWithoutChildren):
(WebCore::Element::cloneElementWithoutAttributesAndChildren):
* dom/Element.h:
* dom/EntityReference.cpp:
(WebCore::EntityReference::cloneNodeInternal):
* dom/EntityReference.h:
* dom/Node.h:
(WebCore::Node::cloneNode):
* dom/ProcessingInstruction.cpp:
(WebCore::ProcessingInstruction::cloneNodeInternal):
* dom/ProcessingInstruction.h:
* dom/ShadowRoot.cpp:
(WebCore::ShadowRoot::cloneNodeInternal):
* dom/ShadowRoot.h:
* dom/Text.cpp:
(WebCore::Text::cloneNodeInternal):
* dom/Text.h:
* editing/ApplyStyleCommand.cpp:
(WebCore::ApplyStyleCommand::pushDownInlineStyleAroundNode):
(WebCore::ApplyStyleCommand::applyInlineStyleChange):
* editing/BreakBlockquoteCommand.cpp:
(WebCore::BreakBlockquoteCommand::doApply):
* editing/InsertParagraphSeparatorCommand.cpp:
(WebCore::InsertParagraphSeparatorCommand::cloneHierarchyUnderNewBlock):
(WebCore::InsertParagraphSeparatorCommand::doApply):
* editing/ModifySelectionListLevel.cpp:
(WebCore::IncreaseSelectionListLevelCommand::doApply):
* editing/SplitElementCommand.cpp:
(WebCore::SplitElementCommand::doApply):
* editing/markup.cpp:
(WebCore::createFragmentFromText):
* html/HTMLKeygenElement.cpp:
* html/HTMLScriptElement.cpp:
(WebCore::HTMLScriptElement::cloneElementWithoutAttributesAndChildren):
* html/HTMLScriptElement.h:
* html/HTMLTemplateElement.cpp:
(WebCore::HTMLTemplateElement::cloneNodeInternal):
* html/HTMLTemplateElement.h:
* html/shadow/SliderThumbElement.cpp:
(WebCore::SliderThumbElement::cloneElementWithoutAttributesAndChildren):
* html/shadow/SliderThumbElement.h:
* html/track/WebVTTElement.cpp:
(WebCore::WebVTTElement::cloneElementWithoutAttributesAndChildren):
* html/track/WebVTTElement.h:
* svg/SVGScriptElement.cpp:
(WebCore::SVGScriptElement::cloneElementWithoutAttributesAndChildren):
* svg/SVGScriptElement.h:
* svg/SVGUseElement.cpp:
(WebCore::SVGUseElement::buildShadowTree):
(WebCore::SVGUseElement::expandUseElementsInShadowTree):

LayoutTests:

Added a regression test.

* fast/dom/HTMLTemplateElement/importNode-nested-templates-expected.txt: Added.
* fast/dom/HTMLTemplateElement/importNode-nested-templates.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@177372 268f45cc-cd09-0410-ab3c-d52691b4dbfc
46 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/dom/HTMLTemplateElement/importNode-nested-templates-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/HTMLTemplateElement/importNode-nested-templates.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/dom/Attr.cpp
Source/WebCore/dom/Attr.h
Source/WebCore/dom/CDATASection.cpp
Source/WebCore/dom/CDATASection.h
Source/WebCore/dom/Comment.cpp
Source/WebCore/dom/Comment.h
Source/WebCore/dom/ContainerNode.cpp
Source/WebCore/dom/Document.cpp
Source/WebCore/dom/Document.h
Source/WebCore/dom/DocumentFragment.cpp
Source/WebCore/dom/DocumentFragment.h
Source/WebCore/dom/DocumentType.cpp
Source/WebCore/dom/DocumentType.h
Source/WebCore/dom/Element.cpp
Source/WebCore/dom/Element.h
Source/WebCore/dom/EntityReference.cpp
Source/WebCore/dom/EntityReference.h
Source/WebCore/dom/Node.h
Source/WebCore/dom/ProcessingInstruction.cpp
Source/WebCore/dom/ProcessingInstruction.h
Source/WebCore/dom/ShadowRoot.cpp
Source/WebCore/dom/ShadowRoot.h
Source/WebCore/dom/Text.cpp
Source/WebCore/dom/Text.h
Source/WebCore/editing/ApplyStyleCommand.cpp
Source/WebCore/editing/BreakBlockquoteCommand.cpp
Source/WebCore/editing/InsertParagraphSeparatorCommand.cpp
Source/WebCore/editing/ModifySelectionListLevel.cpp
Source/WebCore/editing/SplitElementCommand.cpp
Source/WebCore/editing/markup.cpp
Source/WebCore/html/HTMLKeygenElement.cpp
Source/WebCore/html/HTMLScriptElement.cpp
Source/WebCore/html/HTMLScriptElement.h
Source/WebCore/html/HTMLTemplateElement.cpp
Source/WebCore/html/HTMLTemplateElement.h
Source/WebCore/html/shadow/SliderThumbElement.cpp
Source/WebCore/html/shadow/SliderThumbElement.h
Source/WebCore/html/track/WebVTTElement.cpp
Source/WebCore/html/track/WebVTTElement.h
Source/WebCore/svg/SVGScriptElement.cpp
Source/WebCore/svg/SVGScriptElement.h
Source/WebCore/svg/SVGUseElement.cpp