This source file includes following definitions.
- m_value
- apply
- completeURLs
- appendString
- appendElement
- shouldAnnotate
- shouldApplyWrappingStyle
- m_highestNodeToBeSerialized
- wrapWithNode
- wrapWithStyleNode
- appendStyleNodeOpenTag
- styleNodeCloseTag
- takeResults
- appendText
- renderedText
- stringValueForRange
- appendElement
- serializeNodes
- traverseNodesForSerialization
- isHTMLBlockElement
- ancestorToRetainStructureAndAppearanceForBlock
- ancestorToRetainStructureAndAppearance
- ancestorToRetainStructureAndAppearanceWithNoRenderer
- propertyMissingOrEqualToNone
- needInterchangeNewlineAfter
- styleFromMatchedRulesAndInlineDecl
- isElementPresentational
- highestAncestorToWrapMarkup
- createMarkupInternal
- createMarkup
- createFragmentFromMarkup
- findNodesSurroundingContext
- trimFragment
- createFragmentFromMarkupWithContext
- createMarkup
- fillContainerFromString
- isPlainTextMarkup
- shouldPreserveNewline
- createFragmentFromText
- createFullMarkup
- urlToMarkup
- createFragmentForInnerOuterHTML
- createFragmentForTransformToFragment
- removeElementPreservingChildren
- createContextualFragment
- replaceChildrenWithFragment
- replaceChildrenWithText
- mergeWithNextTextNode
#include "config.h"
#include "core/editing/markup.h"
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include "HTMLNames.h"
#include "bindings/v8/ExceptionState.h"
#include "core/css/CSSPrimitiveValue.h"
#include "core/css/CSSValue.h"
#include "core/css/StylePropertySet.h"
#include "core/dom/CDATASection.h"
#include "core/dom/ChildListMutationScope.h"
#include "core/dom/ContextFeatures.h"
#include "core/dom/DocumentFragment.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/Range.h"
#include "core/editing/Editor.h"
#include "core/editing/MarkupAccumulator.h"
#include "core/editing/TextIterator.h"
#include "core/editing/VisibleSelection.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/htmlediting.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLBodyElement.h"
#include "core/html/HTMLElement.h"
#include "core/html/HTMLTableCellElement.h"
#include "core/html/HTMLTextFormControlElement.h"
#include "core/rendering/RenderObject.h"
#include "platform/weborigin/KURL.h"
#include "wtf/StdLibExtras.h"
#include "wtf/text/StringBuilder.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
static bool propertyMissingOrEqualToNone(StylePropertySet*, CSSPropertyID);
class AttributeChange {
public:
AttributeChange()
: m_name(nullAtom, nullAtom, nullAtom)
{
}
AttributeChange(PassRefPtr<Element> element, const QualifiedName& name, const String& value)
: m_element(element), m_name(name), m_value(value)
{
}
void apply()
{
m_element->setAttribute(m_name, AtomicString(m_value));
}
private:
RefPtr<Element> m_element;
QualifiedName m_name;
String m_value;
};
static void completeURLs(DocumentFragment& fragment, const String& baseURL)
{
Vector<AttributeChange> changes;
KURL parsedBaseURL(ParsedURLString, baseURL);
for (Element* element = ElementTraversal::firstWithin(fragment); element; element = ElementTraversal::next(*element, &fragment)) {
if (!element->hasAttributes())
continue;
unsigned length = element->attributeCount();
for (unsigned i = 0; i < length; i++) {
const Attribute& attribute = element->attributeItem(i);
if (element->isURLAttribute(attribute) && !attribute.value().isEmpty())
changes.append(AttributeChange(element, attribute.name(), KURL(parsedBaseURL, attribute.value()).string()));
}
}
size_t numChanges = changes.size();
for (size_t i = 0; i < numChanges; ++i)
changes[i].apply();
}
class StyledMarkupAccumulator FINAL : public MarkupAccumulator {
public:
enum RangeFullySelectsNode { DoesFullySelectNode, DoesNotFullySelectNode };
StyledMarkupAccumulator(Vector<Node*>* nodes, EAbsoluteURLs, EAnnotateForInterchange, const Range*, Node* highestNodeToBeSerialized = 0);
Node* serializeNodes(Node* startNode, Node* pastEnd);
void appendString(const String& s) { return MarkupAccumulator::appendString(s); }
void wrapWithNode(Node&, bool convertBlocksToInlines = false, RangeFullySelectsNode = DoesFullySelectNode);
void wrapWithStyleNode(StylePropertySet*, const Document&, bool isBlock = false);
String takeResults();
private:
void appendStyleNodeOpenTag(StringBuilder&, StylePropertySet*, const Document&, bool isBlock = false);
const String& styleNodeCloseTag(bool isBlock = false);
virtual void appendText(StringBuilder& out, Text&) OVERRIDE;
String renderedText(Node&, const Range*);
String stringValueForRange(const Node&, const Range*);
void appendElement(StringBuilder& out, Element&, bool addDisplayInline, RangeFullySelectsNode);
virtual void appendElement(StringBuilder& out, Element& element, Namespaces*) OVERRIDE { appendElement(out, element, false, DoesFullySelectNode); }
enum NodeTraversalMode { EmitString, DoNotEmitString };
Node* traverseNodesForSerialization(Node* startNode, Node* pastEnd, NodeTraversalMode);
bool shouldAnnotate() { return m_shouldAnnotate == AnnotateForInterchange; }
bool shouldApplyWrappingStyle(const Node& node) const
{
return m_highestNodeToBeSerialized && m_highestNodeToBeSerialized->parentNode() == node.parentNode()
&& m_wrappingStyle && m_wrappingStyle->style();
}
Vector<String> m_reversedPrecedingMarkup;
const EAnnotateForInterchange m_shouldAnnotate;
Node* m_highestNodeToBeSerialized;
RefPtr<EditingStyle> m_wrappingStyle;
};
inline StyledMarkupAccumulator::StyledMarkupAccumulator(Vector<Node*>* nodes, EAbsoluteURLs shouldResolveURLs, EAnnotateForInterchange shouldAnnotate,
const Range* range, Node* highestNodeToBeSerialized)
: MarkupAccumulator(nodes, shouldResolveURLs, range)
, m_shouldAnnotate(shouldAnnotate)
, m_highestNodeToBeSerialized(highestNodeToBeSerialized)
{
}
void StyledMarkupAccumulator::wrapWithNode(Node& node, bool convertBlocksToInlines, RangeFullySelectsNode rangeFullySelectsNode)
{
StringBuilder markup;
if (node.isElementNode())
appendElement(markup, toElement(node), convertBlocksToInlines && isBlock(&node), rangeFullySelectsNode);
else
appendStartMarkup(markup, node, 0);
m_reversedPrecedingMarkup.append(markup.toString());
appendEndTag(node);
if (m_nodes)
m_nodes->append(&node);
}
void StyledMarkupAccumulator::wrapWithStyleNode(StylePropertySet* style, const Document& document, bool isBlock)
{
StringBuilder openTag;
appendStyleNodeOpenTag(openTag, style, document, isBlock);
m_reversedPrecedingMarkup.append(openTag.toString());
appendString(styleNodeCloseTag(isBlock));
}
void StyledMarkupAccumulator::appendStyleNodeOpenTag(StringBuilder& out, StylePropertySet* style, const Document& document, bool isBlock)
{
ASSERT(propertyMissingOrEqualToNone(style, CSSPropertyWebkitTextDecorationsInEffect));
if (isBlock)
out.appendLiteral("<div style=\"");
else
out.appendLiteral("<span style=\"");
appendAttributeValue(out, style->asText(), document.isHTMLDocument());
out.appendLiteral("\">");
}
const String& StyledMarkupAccumulator::styleNodeCloseTag(bool isBlock)
{
DEFINE_STATIC_LOCAL(const String, divClose, ("</div>"));
DEFINE_STATIC_LOCAL(const String, styleSpanClose, ("</span>"));
return isBlock ? divClose : styleSpanClose;
}
String StyledMarkupAccumulator::takeResults()
{
StringBuilder result;
result.reserveCapacity(totalLength(m_reversedPrecedingMarkup) + length());
for (size_t i = m_reversedPrecedingMarkup.size(); i > 0; --i)
result.append(m_reversedPrecedingMarkup[i - 1]);
concatenateMarkup(result);
return result.toString().replace(0, "");
}
void StyledMarkupAccumulator::appendText(StringBuilder& out, Text& text)
{
const bool parentIsTextarea = text.parentElement() && text.parentElement()->tagQName() == textareaTag;
const bool wrappingSpan = shouldApplyWrappingStyle(text) && !parentIsTextarea;
if (wrappingSpan) {
RefPtr<EditingStyle> wrappingStyle = m_wrappingStyle->copy();
wrappingStyle->forceInline();
wrappingStyle->style()->setProperty(CSSPropertyFloat, CSSValueNone);
appendStyleNodeOpenTag(out, wrappingStyle->style(), text.document());
}
if (!shouldAnnotate() || parentIsTextarea)
MarkupAccumulator::appendText(out, text);
else {
const bool useRenderedText = !enclosingNodeWithTag(firstPositionInNode(&text), selectTag);
String content = useRenderedText ? renderedText(text, m_range) : stringValueForRange(text, m_range);
StringBuilder buffer;
appendCharactersReplacingEntities(buffer, content, 0, content.length(), EntityMaskInPCDATA);
out.append(convertHTMLTextToInterchangeFormat(buffer.toString(), text));
}
if (wrappingSpan)
out.append(styleNodeCloseTag());
}
String StyledMarkupAccumulator::renderedText(Node& node, const Range* range)
{
if (!node.isTextNode())
return String();
const Text& textNode = toText(node);
unsigned startOffset = 0;
unsigned endOffset = textNode.length();
if (range && node == range->startContainer())
startOffset = range->startOffset();
if (range && node == range->endContainer())
endOffset = range->endOffset();
Position start = createLegacyEditingPosition(&node, startOffset);
Position end = createLegacyEditingPosition(&node, endOffset);
return plainText(Range::create(node.document(), start, end).get());
}
String StyledMarkupAccumulator::stringValueForRange(const Node& node, const Range* range)
{
if (!range)
return node.nodeValue();
String str = node.nodeValue();
if (node == range->endContainer())
str.truncate(range->endOffset());
if (node == range->startContainer())
str.remove(0, range->startOffset());
return str;
}
void StyledMarkupAccumulator::appendElement(StringBuilder& out, Element& element, bool addDisplayInline, RangeFullySelectsNode rangeFullySelectsNode)
{
const bool documentIsHTML = element.document().isHTMLDocument();
appendOpenTag(out, element, 0);
const unsigned length = element.hasAttributes() ? element.attributeCount() : 0;
const bool shouldAnnotateOrForceInline = element.isHTMLElement() && (shouldAnnotate() || addDisplayInline);
const bool shouldOverrideStyleAttr = shouldAnnotateOrForceInline || shouldApplyWrappingStyle(element);
for (unsigned i = 0; i < length; ++i) {
const Attribute& attribute = element.attributeItem(i);
if (attribute.name() == styleAttr && shouldOverrideStyleAttr)
continue;
appendAttribute(out, element, attribute, 0);
}
if (shouldOverrideStyleAttr) {
RefPtr<EditingStyle> newInlineStyle;
if (shouldApplyWrappingStyle(element)) {
newInlineStyle = m_wrappingStyle->copy();
newInlineStyle->removePropertiesInElementDefaultStyle(&element);
newInlineStyle->removeStyleConflictingWithStyleOfNode(&element);
} else
newInlineStyle = EditingStyle::create();
if (element.isStyledElement() && element.inlineStyle())
newInlineStyle->overrideWithStyle(element.inlineStyle());
if (shouldAnnotateOrForceInline) {
if (shouldAnnotate())
newInlineStyle->mergeStyleFromRulesForSerialization(&toHTMLElement(element));
if (addDisplayInline)
newInlineStyle->forceInline();
if (rangeFullySelectsNode == DoesNotFullySelectNode && newInlineStyle->style())
newInlineStyle->style()->removeProperty(CSSPropertyFloat);
}
if (!newInlineStyle->isEmpty()) {
out.appendLiteral(" style=\"");
appendAttributeValue(out, newInlineStyle->style()->asText(), documentIsHTML);
out.append('\"');
}
}
appendCloseTag(out, element);
}
Node* StyledMarkupAccumulator::serializeNodes(Node* startNode, Node* pastEnd)
{
if (!m_highestNodeToBeSerialized) {
Node* lastClosed = traverseNodesForSerialization(startNode, pastEnd, DoNotEmitString);
m_highestNodeToBeSerialized = lastClosed;
}
if (m_highestNodeToBeSerialized && m_highestNodeToBeSerialized->parentNode())
m_wrappingStyle = EditingStyle::wrappingStyleForSerialization(m_highestNodeToBeSerialized->parentNode(), shouldAnnotate());
return traverseNodesForSerialization(startNode, pastEnd, EmitString);
}
Node* StyledMarkupAccumulator::traverseNodesForSerialization(Node* startNode, Node* pastEnd, NodeTraversalMode traversalMode)
{
const bool shouldEmit = traversalMode == EmitString;
Vector<Node*> ancestorsToClose;
Node* next;
Node* lastClosed = 0;
for (Node* n = startNode; n != pastEnd; n = next) {
ASSERT(n);
if (!n)
break;
next = NodeTraversal::next(*n);
bool openedTag = false;
if (isBlock(n) && canHaveChildrenForEditing(n) && next == pastEnd)
continue;
if (!n->renderer() && !enclosingNodeWithTag(firstPositionInOrBeforeNode(n), selectTag)) {
next = NodeTraversal::nextSkippingChildren(*n);
if (pastEnd && pastEnd->isDescendantOf(n))
next = pastEnd;
} else {
if (shouldEmit)
appendStartTag(*n);
if (!n->hasChildren()) {
if (shouldEmit)
appendEndTag(*n);
lastClosed = n;
} else {
openedTag = true;
ancestorsToClose.append(n);
}
}
if (!openedTag && (!n->nextSibling() || next == pastEnd)) {
while (!ancestorsToClose.isEmpty()) {
Node* ancestor = ancestorsToClose.last();
ASSERT(ancestor);
if (next != pastEnd && next->isDescendantOf(ancestor))
break;
if (shouldEmit)
appendEndTag(*ancestor);
lastClosed = ancestor;
ancestorsToClose.removeLast();
}
ContainerNode* nextParent = next ? next->parentNode() : 0;
if (next != pastEnd && n != nextParent) {
Node* lastAncestorClosedOrSelf = n->isDescendantOf(lastClosed) ? lastClosed : n;
for (ContainerNode* parent = lastAncestorClosedOrSelf->parentNode(); parent && parent != nextParent; parent = parent->parentNode()) {
if (!parent->renderer())
continue;
ASSERT(startNode->isDescendantOf(parent));
if (shouldEmit)
wrapWithNode(*parent);
lastClosed = parent;
}
}
}
}
return lastClosed;
}
static bool isHTMLBlockElement(const Node* node)
{
ASSERT(node);
return isHTMLTableCellElement(*node)
|| isNonTableCellHTMLBlockElement(node);
}
static Node* ancestorToRetainStructureAndAppearanceForBlock(Node* commonAncestorBlock)
{
if (!commonAncestorBlock)
return 0;
if (commonAncestorBlock->hasTagName(tbodyTag) || isHTMLTableRowElement(*commonAncestorBlock)) {
ContainerNode* table = commonAncestorBlock->parentNode();
while (table && !isHTMLTableElement(*table))
table = table->parentNode();
return table;
}
if (isNonTableCellHTMLBlockElement(commonAncestorBlock))
return commonAncestorBlock;
return 0;
}
static inline Node* ancestorToRetainStructureAndAppearance(Node* commonAncestor)
{
return ancestorToRetainStructureAndAppearanceForBlock(enclosingBlock(commonAncestor));
}
static inline Node* ancestorToRetainStructureAndAppearanceWithNoRenderer(Node* commonAncestor)
{
Node* commonAncestorBlock = enclosingNodeOfType(firstPositionInOrBeforeNode(commonAncestor), isHTMLBlockElement);
return ancestorToRetainStructureAndAppearanceForBlock(commonAncestorBlock);
}
static bool propertyMissingOrEqualToNone(StylePropertySet* style, CSSPropertyID propertyID)
{
if (!style)
return false;
RefPtrWillBeRawPtr<CSSValue> value = style->getPropertyCSSValue(propertyID);
if (!value)
return true;
if (!value->isPrimitiveValue())
return false;
return toCSSPrimitiveValue(value.get())->getValueID() == CSSValueNone;
}
static bool needInterchangeNewlineAfter(const VisiblePosition& v)
{
VisiblePosition next = v.next();
Node* upstreamNode = next.deepEquivalent().upstream().deprecatedNode();
Node* downstreamNode = v.deepEquivalent().downstream().deprecatedNode();
return isEndOfParagraph(v) && isStartOfParagraph(next) && !(isHTMLBRElement(*upstreamNode) && upstreamNode == downstreamNode);
}
static PassRefPtr<EditingStyle> styleFromMatchedRulesAndInlineDecl(const Node* node)
{
if (!node->isHTMLElement())
return nullptr;
HTMLElement* element = const_cast<HTMLElement*>(toHTMLElement(node));
RefPtr<EditingStyle> style = EditingStyle::create(element->inlineStyle());
style->mergeStyleFromRules(element);
return style.release();
}
static bool isElementPresentational(const Node* node)
{
return node->hasTagName(uTag) || node->hasTagName(sTag) || node->hasTagName(strikeTag)
|| node->hasTagName(iTag) || node->hasTagName(emTag) || node->hasTagName(bTag) || node->hasTagName(strongTag);
}
static Node* highestAncestorToWrapMarkup(const Range* range, EAnnotateForInterchange shouldAnnotate, Node* constrainingAncestor)
{
Node* commonAncestor = range->commonAncestorContainer(IGNORE_EXCEPTION);
ASSERT(commonAncestor);
Node* specialCommonAncestor = 0;
if (shouldAnnotate == AnnotateForInterchange) {
specialCommonAncestor = ancestorToRetainStructureAndAppearance(commonAncestor);
if (Node* parentListNode = enclosingNodeOfType(firstPositionInOrBeforeNode(range->firstNode()), isListItem)) {
if (WebCore::areRangesEqual(VisibleSelection::selectionFromContentsOfNode(parentListNode).toNormalizedRange().get(), range)) {
specialCommonAncestor = parentListNode->parentNode();
while (specialCommonAncestor && !isListElement(specialCommonAncestor))
specialCommonAncestor = specialCommonAncestor->parentNode();
}
}
if (Node* highestMailBlockquote = highestEnclosingNodeOfType(firstPositionInOrBeforeNode(range->firstNode()), isMailBlockquote, CanCrossEditingBoundary))
specialCommonAncestor = highestMailBlockquote;
}
Node* checkAncestor = specialCommonAncestor ? specialCommonAncestor : commonAncestor;
if (checkAncestor->renderer()) {
Node* newSpecialCommonAncestor = highestEnclosingNodeOfType(firstPositionInNode(checkAncestor), &isElementPresentational, CanCrossEditingBoundary, constrainingAncestor);
if (newSpecialCommonAncestor)
specialCommonAncestor = newSpecialCommonAncestor;
}
if (!specialCommonAncestor && isTabSpanTextNode(commonAncestor))
specialCommonAncestor = commonAncestor->parentNode();
if (!specialCommonAncestor && isTabSpanNode(commonAncestor))
specialCommonAncestor = commonAncestor;
if (Node *enclosingAnchor = enclosingNodeWithTag(firstPositionInNode(specialCommonAncestor ? specialCommonAncestor : commonAncestor), aTag))
specialCommonAncestor = enclosingAnchor;
return specialCommonAncestor;
}
static String createMarkupInternal(Document& document, const Range* range, const Range* updatedRange, Vector<Node*>* nodes,
EAnnotateForInterchange shouldAnnotate, bool convertBlocksToInlines, EAbsoluteURLs shouldResolveURLs, Node* constrainingAncestor)
{
ASSERT(range);
ASSERT(updatedRange);
DEFINE_STATIC_LOCAL(const String, interchangeNewlineString, ("<br class=\"" AppleInterchangeNewline "\">"));
bool collapsed = updatedRange->collapsed(ASSERT_NO_EXCEPTION);
if (collapsed)
return emptyString();
Node* commonAncestor = updatedRange->commonAncestorContainer(ASSERT_NO_EXCEPTION);
if (!commonAncestor)
return emptyString();
document.updateLayoutIgnorePendingStylesheets();
Node* body = enclosingNodeWithTag(firstPositionInNode(commonAncestor), bodyTag);
Node* fullySelectedRoot = 0;
if (body && areRangesEqual(VisibleSelection::selectionFromContentsOfNode(body).toNormalizedRange().get(), range))
fullySelectedRoot = body;
Node* specialCommonAncestor = highestAncestorToWrapMarkup(updatedRange, shouldAnnotate, constrainingAncestor);
StyledMarkupAccumulator accumulator(nodes, shouldResolveURLs, shouldAnnotate, updatedRange, specialCommonAncestor);
Node* pastEnd = updatedRange->pastLastNode();
Node* startNode = updatedRange->firstNode();
VisiblePosition visibleStart(updatedRange->startPosition(), VP_DEFAULT_AFFINITY);
VisiblePosition visibleEnd(updatedRange->endPosition(), VP_DEFAULT_AFFINITY);
if (shouldAnnotate == AnnotateForInterchange && needInterchangeNewlineAfter(visibleStart)) {
if (visibleStart == visibleEnd.previous())
return interchangeNewlineString;
accumulator.appendString(interchangeNewlineString);
startNode = visibleStart.next().deepEquivalent().deprecatedNode();
if (pastEnd && Range::compareBoundaryPoints(startNode, 0, pastEnd, 0, ASSERT_NO_EXCEPTION) >= 0)
return interchangeNewlineString;
}
Node* lastClosed = accumulator.serializeNodes(startNode, pastEnd);
if (specialCommonAncestor && lastClosed) {
for (ContainerNode* ancestor = lastClosed->parentNode(); ancestor; ancestor = ancestor->parentNode()) {
if (ancestor == fullySelectedRoot && !convertBlocksToInlines) {
RefPtr<EditingStyle> fullySelectedRootStyle = styleFromMatchedRulesAndInlineDecl(fullySelectedRoot);
if ((!fullySelectedRootStyle || !fullySelectedRootStyle->style() || !fullySelectedRootStyle->style()->getPropertyCSSValue(CSSPropertyBackgroundImage))
&& toElement(fullySelectedRoot)->hasAttribute(backgroundAttr))
fullySelectedRootStyle->style()->setProperty(CSSPropertyBackgroundImage, "url('" + toElement(fullySelectedRoot)->getAttribute(backgroundAttr) + "')");
if (fullySelectedRootStyle->style()) {
if (!propertyMissingOrEqualToNone(fullySelectedRootStyle->style(), CSSPropertyTextDecoration))
fullySelectedRootStyle->style()->setProperty(CSSPropertyTextDecoration, CSSValueNone);
if (!propertyMissingOrEqualToNone(fullySelectedRootStyle->style(), CSSPropertyWebkitTextDecorationsInEffect))
fullySelectedRootStyle->style()->setProperty(CSSPropertyWebkitTextDecorationsInEffect, CSSValueNone);
accumulator.wrapWithStyleNode(fullySelectedRootStyle->style(), document, true);
}
} else {
accumulator.wrapWithNode(*ancestor, convertBlocksToInlines, StyledMarkupAccumulator::DoesNotFullySelectNode);
}
if (nodes)
nodes->append(ancestor);
if (ancestor == specialCommonAncestor)
break;
}
}
if (shouldAnnotate == AnnotateForInterchange && needInterchangeNewlineAfter(visibleEnd.previous()))
accumulator.appendString(interchangeNewlineString);
return accumulator.takeResults();
}
String createMarkup(const Range* range, Vector<Node*>* nodes, EAnnotateForInterchange shouldAnnotate, bool convertBlocksToInlines, EAbsoluteURLs shouldResolveURLs, Node* constrainingAncestor)
{
if (!range)
return emptyString();
Document& document = range->ownerDocument();
const Range* updatedRange = range;
return createMarkupInternal(document, range, updatedRange, nodes, shouldAnnotate, convertBlocksToInlines, shouldResolveURLs, constrainingAncestor);
}
PassRefPtr<DocumentFragment> createFragmentFromMarkup(Document& document, const String& markup, const String& baseURL, ParserContentPolicy parserContentPolicy)
{
RefPtr<HTMLBodyElement> fakeBody = HTMLBodyElement::create(document);
RefPtr<DocumentFragment> fragment = DocumentFragment::create(document);
fragment->parseHTML(markup, fakeBody.get(), parserContentPolicy);
if (!baseURL.isEmpty() && baseURL != blankURL() && baseURL != document.baseURL())
completeURLs(*fragment, baseURL);
return fragment.release();
}
static const char fragmentMarkerTag[] = "webkit-fragment-marker";
static bool findNodesSurroundingContext(Document* document, RefPtr<Node>& nodeBeforeContext, RefPtr<Node>& nodeAfterContext)
{
for (Node* node = document->firstChild(); node; node = NodeTraversal::next(*node)) {
if (node->nodeType() == Node::COMMENT_NODE && toCharacterData(node)->data() == fragmentMarkerTag) {
if (!nodeBeforeContext)
nodeBeforeContext = node;
else {
nodeAfterContext = node;
return true;
}
}
}
return false;
}
static void trimFragment(DocumentFragment* fragment, Node* nodeBeforeContext, Node* nodeAfterContext)
{
RefPtr<Node> next;
for (RefPtr<Node> node = fragment->firstChild(); node; node = next) {
if (nodeBeforeContext->isDescendantOf(node.get())) {
next = NodeTraversal::next(*node);
continue;
}
next = NodeTraversal::nextSkippingChildren(*node);
ASSERT(!node->contains(nodeAfterContext));
node->parentNode()->removeChild(node.get(), ASSERT_NO_EXCEPTION);
if (nodeBeforeContext == node)
break;
}
ASSERT(nodeAfterContext->parentNode());
for (RefPtr<Node> node = nodeAfterContext; node; node = next) {
next = NodeTraversal::nextSkippingChildren(*node);
node->parentNode()->removeChild(node.get(), ASSERT_NO_EXCEPTION);
}
}
PassRefPtr<DocumentFragment> createFragmentFromMarkupWithContext(Document& document, const String& markup, unsigned fragmentStart, unsigned fragmentEnd,
const String& baseURL, ParserContentPolicy parserContentPolicy)
{
StringBuilder taggedMarkup;
taggedMarkup.append(markup.left(fragmentStart));
MarkupAccumulator::appendComment(taggedMarkup, fragmentMarkerTag);
taggedMarkup.append(markup.substring(fragmentStart, fragmentEnd - fragmentStart));
MarkupAccumulator::appendComment(taggedMarkup, fragmentMarkerTag);
taggedMarkup.append(markup.substring(fragmentEnd));
RefPtr<DocumentFragment> taggedFragment = createFragmentFromMarkup(document, taggedMarkup.toString(), baseURL, parserContentPolicy);
RefPtr<Document> taggedDocument = Document::create();
taggedDocument->setContextFeatures(document.contextFeatures());
taggedDocument->parserTakeAllChildrenFrom(*taggedFragment);
RefPtr<Node> nodeBeforeContext;
RefPtr<Node> nodeAfterContext;
if (!findNodesSurroundingContext(taggedDocument.get(), nodeBeforeContext, nodeAfterContext))
return nullptr;
RefPtrWillBeRawPtr<Range> range = Range::create(*taggedDocument.get(),
positionAfterNode(nodeBeforeContext.get()).parentAnchoredEquivalent(),
positionBeforeNode(nodeAfterContext.get()).parentAnchoredEquivalent());
Node* commonAncestor = range->commonAncestorContainer(ASSERT_NO_EXCEPTION);
Node* specialCommonAncestor = ancestorToRetainStructureAndAppearanceWithNoRenderer(commonAncestor);
RefPtr<DocumentFragment> fragment = DocumentFragment::create(document);
if (specialCommonAncestor)
fragment->appendChild(specialCommonAncestor);
else
fragment->parserTakeAllChildrenFrom(toContainerNode(*commonAncestor));
trimFragment(fragment.get(), nodeBeforeContext.get(), nodeAfterContext.get());
return fragment;
}
String createMarkup(const Node* node, EChildrenOnly childrenOnly, Vector<Node*>* nodes, EAbsoluteURLs shouldResolveURLs, Vector<QualifiedName>* tagNamesToSkip)
{
if (!node)
return "";
MarkupAccumulator accumulator(nodes, shouldResolveURLs);
return accumulator.serializeNodes(const_cast<Node&>(*node), childrenOnly, tagNamesToSkip);
}
static void fillContainerFromString(ContainerNode* paragraph, const String& string)
{
Document& document = paragraph->document();
if (string.isEmpty()) {
paragraph->appendChild(createBlockPlaceholderElement(document));
return;
}
ASSERT(string.find('\n') == kNotFound);
Vector<String> tabList;
string.split('\t', true, tabList);
StringBuilder tabText;
bool first = true;
size_t numEntries = tabList.size();
for (size_t i = 0; i < numEntries; ++i) {
const String& s = tabList[i];
if (!s.isEmpty()) {
if (!tabText.isEmpty()) {
paragraph->appendChild(createTabSpanElement(document, tabText.toString()));
tabText.clear();
}
RefPtr<Node> textNode = document.createTextNode(stringWithRebalancedWhitespace(s, first, i + 1 == numEntries));
paragraph->appendChild(textNode.release());
}
if (i + 1 != numEntries)
tabText.append('\t');
else if (!tabText.isEmpty())
paragraph->appendChild(createTabSpanElement(document, tabText.toString()));
first = false;
}
}
bool isPlainTextMarkup(Node* node)
{
ASSERT(node);
if (!node->isElementNode())
return false;
Element* element = toElement(node);
if (!isHTMLDivElement(*element) || !element->hasAttributes())
return false;
if (element->hasOneChild() && (element->firstChild()->isTextNode() || (element->firstChild()->firstChild())))
return true;
return element->hasChildCount(2) && isTabSpanTextNode(element->firstChild()->firstChild()) && element->lastChild()->isTextNode();
}
static bool shouldPreserveNewline(const Range& range)
{
if (Node* node = range.firstNode()) {
if (RenderObject* renderer = node->renderer())
return renderer->style()->preserveNewline();
}
if (Node* node = range.startPosition().anchorNode()) {
if (RenderObject* renderer = node->renderer())
return renderer->style()->preserveNewline();
}
return false;
}
PassRefPtr<DocumentFragment> createFragmentFromText(Range* context, const String& text)
{
if (!context)
return nullptr;
Document& document = context->ownerDocument();
RefPtr<DocumentFragment> fragment = document.createDocumentFragment();
if (text.isEmpty())
return fragment.release();
String string = text;
string.replace("\r\n", "\n");
string.replace('\r', '\n');
if (shouldPreserveNewline(*context)) {
fragment->appendChild(document.createTextNode(string));
if (string.endsWith('\n')) {
RefPtr<Element> element = createBreakElement(document);
element->setAttribute(classAttr, AppleInterchangeNewline);
fragment->appendChild(element.release());
}
return fragment.release();
}
if (string.find('\n') == kNotFound) {
fillContainerFromString(fragment.get(), string);
return fragment.release();
}
Node* blockNode = enclosingBlock(context->firstNode());
Element* block = toElement(blockNode);
bool useClonesOfEnclosingBlock = blockNode
&& blockNode->isElementNode()
&& !isHTMLBodyElement(*block)
&& !isHTMLHtmlElement(*block)
&& block != editableRootForPosition(context->startPosition());
bool useLineBreak = enclosingTextFormControl(context->startPosition());
Vector<String> list;
string.split('\n', true, list);
size_t numLines = list.size();
for (size_t i = 0; i < numLines; ++i) {
const String& s = list[i];
RefPtr<Element> element;
if (s.isEmpty() && i + 1 == numLines) {
element = createBreakElement(document);
element->setAttribute(classAttr, AppleInterchangeNewline);
} else if (useLineBreak) {
element = createBreakElement(document);
fillContainerFromString(fragment.get(), s);
} else {
if (useClonesOfEnclosingBlock)
element = block->cloneElementWithoutChildren();
else
element = createDefaultParagraphElement(document);
fillContainerFromString(element.get(), s);
}
fragment->appendChild(element.release());
}
return fragment.release();
}
String createFullMarkup(const Node* node)
{
if (!node)
return String();
LocalFrame* frame = node->document().frame();
if (!frame)
return String();
String markupString = createMarkup(node, IncludeNode, 0);
Node::NodeType nodeType = node->nodeType();
if (nodeType != Node::DOCUMENT_NODE && !node->isDocumentTypeNode())
markupString = frame->documentTypeString() + markupString;
return markupString;
}
String urlToMarkup(const KURL& url, const String& title)
{
StringBuilder markup;
markup.appendLiteral("<a href=\"");
markup.append(url.string());
markup.appendLiteral("\">");
MarkupAccumulator::appendCharactersReplacingEntities(markup, title, 0, title.length(), EntityMaskInPCDATA);
markup.appendLiteral("</a>");
return markup.toString();
}
PassRefPtr<DocumentFragment> createFragmentForInnerOuterHTML(const String& markup, Element* contextElement, ParserContentPolicy parserContentPolicy, const char* method, ExceptionState& exceptionState)
{
ASSERT(contextElement);
Document& document = isHTMLTemplateElement(*contextElement) ? contextElement->document().ensureTemplateDocument() : contextElement->document();
RefPtr<DocumentFragment> fragment = DocumentFragment::create(document);
if (document.isHTMLDocument()) {
fragment->parseHTML(markup, contextElement, parserContentPolicy);
return fragment;
}
bool wasValid = fragment->parseXML(markup, contextElement, parserContentPolicy);
if (!wasValid) {
exceptionState.throwDOMException(SyntaxError, "The provided markup is invalid XML, and therefore cannot be inserted into an XML document.");
return nullptr;
}
return fragment.release();
}
PassRefPtr<DocumentFragment> createFragmentForTransformToFragment(const String& sourceString, const String& sourceMIMEType, Document& outputDoc)
{
RefPtr<DocumentFragment> fragment = outputDoc.createDocumentFragment();
if (sourceMIMEType == "text/html") {
RefPtr<HTMLBodyElement> fakeBody = HTMLBodyElement::create(outputDoc);
fragment->parseHTML(sourceString, fakeBody.get());
} else if (sourceMIMEType == "text/plain") {
fragment->parserAppendChild(Text::create(outputDoc, sourceString));
} else {
bool successfulParse = fragment->parseXML(sourceString, 0);
if (!successfulParse)
return nullptr;
}
return fragment.release();
}
static inline void removeElementPreservingChildren(PassRefPtr<DocumentFragment> fragment, HTMLElement* element)
{
RefPtr<Node> nextChild;
for (RefPtr<Node> child = element->firstChild(); child; child = nextChild) {
nextChild = child->nextSibling();
element->removeChild(child.get());
fragment->insertBefore(child, element);
}
fragment->removeChild(element);
}
PassRefPtr<DocumentFragment> createContextualFragment(const String& markup, HTMLElement* element, ParserContentPolicy parserContentPolicy, ExceptionState& exceptionState)
{
ASSERT(element);
if (element->ieForbidsInsertHTML() || element->hasLocalName(colTag) || element->hasLocalName(colgroupTag) || element->hasLocalName(framesetTag)
|| element->hasLocalName(headTag) || element->hasLocalName(styleTag) || element->hasLocalName(titleTag)) {
exceptionState.throwDOMException(NotSupportedError, "The range's container is '" + element->localName() + "', which is not supported.");
return nullptr;
}
RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(markup, element, parserContentPolicy, "createContextualFragment", exceptionState);
if (!fragment)
return nullptr;
RefPtr<Node> nextNode;
for (RefPtr<Node> node = fragment->firstChild(); node; node = nextNode) {
nextNode = node->nextSibling();
if (isHTMLHtmlElement(*node) || isHTMLHeadElement(*node) || isHTMLBodyElement(*node)) {
HTMLElement* element = toHTMLElement(node);
if (Node* firstChild = element->firstChild())
nextNode = firstChild;
removeElementPreservingChildren(fragment, element);
}
}
return fragment.release();
}
void replaceChildrenWithFragment(ContainerNode* container, PassRefPtr<DocumentFragment> fragment, ExceptionState& exceptionState)
{
ASSERT(container);
RefPtr<ContainerNode> containerNode(container);
ChildListMutationScope mutation(*containerNode);
if (!fragment->firstChild()) {
containerNode->removeChildren();
return;
}
if (containerNode->hasOneTextChild() && fragment->hasOneTextChild()) {
toText(containerNode->firstChild())->setData(toText(fragment->firstChild())->data());
return;
}
if (containerNode->hasOneChild()) {
containerNode->replaceChild(fragment, containerNode->firstChild(), exceptionState);
return;
}
containerNode->removeChildren();
containerNode->appendChild(fragment, exceptionState);
}
void replaceChildrenWithText(ContainerNode* container, const String& text, ExceptionState& exceptionState)
{
ASSERT(container);
RefPtr<ContainerNode> containerNode(container);
ChildListMutationScope mutation(*containerNode);
if (containerNode->hasOneTextChild()) {
toText(containerNode->firstChild())->setData(text);
return;
}
RefPtr<Text> textNode = Text::create(containerNode->document(), text);
if (containerNode->hasOneChild()) {
containerNode->replaceChild(textNode.release(), containerNode->firstChild(), exceptionState);
return;
}
containerNode->removeChildren();
containerNode->appendChild(textNode.release(), exceptionState);
}
void mergeWithNextTextNode(PassRefPtr<Node> node, ExceptionState& exceptionState)
{
ASSERT(node && node->isTextNode());
Node* next = node->nextSibling();
if (!next || !next->isTextNode())
return;
RefPtr<Text> textNode = toText(node.get());
RefPtr<Text> textNext = toText(next);
textNode->appendData(textNext->data());
if (textNext->parentNode())
textNext->remove(exceptionState);
}
}