2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "CDATASection.h"
30 #include "CharacterNames.h"
32 #include "CSSComputedStyleDeclaration.h"
33 #include "CSSPrimitiveValue.h"
34 #include "CSSProperty.h"
35 #include "CSSPropertyNames.h"
37 #include "CSSRuleList.h"
38 #include "CSSStyleRule.h"
39 #include "CSSStyleSelector.h"
41 #include "CSSValueKeywords.h"
42 #include "DeleteButtonController.h"
44 #include "DocumentFragment.h"
45 #include "DocumentType.h"
48 #include "HTMLElement.h"
49 #include "HTMLNames.h"
50 #include "InlineTextBox.h"
52 #include "ProcessingInstruction.h"
53 #include "QualifiedName.h"
55 #include "VisibleSelection.h"
56 #include "TextIterator.h"
57 #include "htmlediting.h"
58 #include "visible_units.h"
59 #include <wtf/StdLibExtras.h>
65 using namespace HTMLNames;
67 static inline bool shouldSelfClose(const Node *node);
69 class AttributeChange {
72 : m_name(nullAtom, nullAtom, nullAtom)
76 AttributeChange(PassRefPtr<Element> element, const QualifiedName& name, const String& value)
77 : m_element(element), m_name(name), m_value(value)
83 m_element->setAttribute(m_name, m_value);
87 RefPtr<Element> m_element;
92 static void appendAttributeValue(Vector<UChar>& result, const String& attr, bool escapeNBSP)
94 const UChar* uchars = attr.characters();
95 unsigned len = attr.length();
96 unsigned lastCopiedFrom = 0;
98 DEFINE_STATIC_LOCAL(const String, ampEntity, ("&"));
99 DEFINE_STATIC_LOCAL(const String, gtEntity, (">"));
100 DEFINE_STATIC_LOCAL(const String, ltEntity, ("<"));
101 DEFINE_STATIC_LOCAL(const String, quotEntity, ("""));
102 DEFINE_STATIC_LOCAL(const String, nbspEntity, (" "));
104 for (unsigned i = 0; i < len; ++i) {
108 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
109 append(result, ampEntity);
110 lastCopiedFrom = i + 1;
113 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
114 append(result, ltEntity);
115 lastCopiedFrom = i + 1;
118 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
119 append(result, gtEntity);
120 lastCopiedFrom = i + 1;
123 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
124 append(result, quotEntity);
125 lastCopiedFrom = i + 1;
129 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
130 append(result, nbspEntity);
131 lastCopiedFrom = i + 1;
137 result.append(uchars + lastCopiedFrom, len - lastCopiedFrom);
140 static void appendEscapedContent(Vector<UChar>& result, pair<const UChar*, size_t> range, bool escapeNBSP)
142 const UChar* uchars = range.first;
143 unsigned len = range.second;
144 unsigned lastCopiedFrom = 0;
146 DEFINE_STATIC_LOCAL(const String, ampEntity, ("&"));
147 DEFINE_STATIC_LOCAL(const String, gtEntity, (">"));
148 DEFINE_STATIC_LOCAL(const String, ltEntity, ("<"));
149 DEFINE_STATIC_LOCAL(const String, nbspEntity, (" "));
151 for (unsigned i = 0; i < len; ++i) {
155 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
156 append(result, ampEntity);
157 lastCopiedFrom = i + 1;
160 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
161 append(result, ltEntity);
162 lastCopiedFrom = i + 1;
165 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
166 append(result, gtEntity);
167 lastCopiedFrom = i + 1;
171 result.append(uchars + lastCopiedFrom, i - lastCopiedFrom);
172 append(result, nbspEntity);
173 lastCopiedFrom = i + 1;
179 result.append(uchars + lastCopiedFrom, len - lastCopiedFrom);
182 static String escapeContentText(const String& in, bool escapeNBSP)
184 Vector<UChar> buffer;
185 appendEscapedContent(buffer, make_pair(in.characters(), in.length()), escapeNBSP);
186 return String::adopt(buffer);
189 static void appendQuotedURLAttributeValue(Vector<UChar>& result, const String& urlString)
191 UChar quoteChar = '\"';
192 String strippedURLString = urlString.stripWhiteSpace();
193 if (protocolIs(strippedURLString, "javascript")) {
194 // minimal escaping for javascript urls
195 if (strippedURLString.contains('"')) {
196 if (strippedURLString.contains('\''))
197 strippedURLString.replace('\"', """);
201 result.append(quoteChar);
202 append(result, strippedURLString);
203 result.append(quoteChar);
207 // FIXME: This does not fully match other browsers. Firefox percent-escapes non-ASCII characters for innerHTML.
208 result.append(quoteChar);
209 appendAttributeValue(result, urlString, false);
210 result.append(quoteChar);
213 static String stringValueForRange(const Node* node, const Range* range)
216 return node->nodeValue();
218 String str = node->nodeValue();
220 if (node == range->endContainer(ec))
221 str.truncate(range->endOffset(ec));
222 if (node == range->startContainer(ec))
223 str.remove(0, range->startOffset(ec));
227 static inline pair<const UChar*, size_t> ucharRange(const Node *node, const Range *range)
229 String str = node->nodeValue();
230 const UChar* characters = str.characters();
231 size_t length = str.length();
235 if (node == range->endContainer(ec))
236 length = range->endOffset(ec);
237 if (node == range->startContainer(ec)) {
238 size_t start = range->startOffset(ec);
244 return make_pair(characters, length);
247 static inline void appendUCharRange(Vector<UChar>& result, const pair<const UChar*, size_t> range)
249 result.append(range.first, range.second);
252 static String renderedText(const Node* node, const Range* range)
254 if (!node->isTextNode())
258 const Text* textNode = static_cast<const Text*>(node);
259 unsigned startOffset = 0;
260 unsigned endOffset = textNode->length();
262 if (range && node == range->startContainer(ec))
263 startOffset = range->startOffset(ec);
264 if (range && node == range->endContainer(ec))
265 endOffset = range->endOffset(ec);
267 Position start(const_cast<Node*>(node), startOffset);
268 Position end(const_cast<Node*>(node), endOffset);
269 return plainText(Range::create(node->document(), start, end).get());
272 static PassRefPtr<CSSMutableStyleDeclaration> styleFromMatchedRulesForElement(Element* element, bool authorOnly = true)
274 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
275 RefPtr<CSSRuleList> matchedRules = element->document()->styleSelector()->styleRulesForElement(element, authorOnly);
277 for (unsigned i = 0; i < matchedRules->length(); i++) {
278 if (matchedRules->item(i)->type() == CSSRule::STYLE_RULE) {
279 RefPtr<CSSMutableStyleDeclaration> s = static_cast<CSSStyleRule*>(matchedRules->item(i))->style();
280 style->merge(s.get(), true);
285 return style.release();
288 static void removeEnclosingMailBlockquoteStyle(CSSMutableStyleDeclaration* style, Node* node)
290 Node* blockquote = nearestMailBlockquote(node);
291 if (!blockquote || !blockquote->parentNode())
294 RefPtr<CSSMutableStyleDeclaration> parentStyle = Position(blockquote->parentNode(), 0).computedStyle()->copyInheritableProperties();
295 RefPtr<CSSMutableStyleDeclaration> blockquoteStyle = Position(blockquote, 0).computedStyle()->copyInheritableProperties();
296 parentStyle->diff(blockquoteStyle.get());
297 blockquoteStyle->diff(style);
300 static void removeDefaultStyles(CSSMutableStyleDeclaration* style, Document* document)
302 if (!document || !document->documentElement())
305 RefPtr<CSSMutableStyleDeclaration> documentStyle = computedStyle(document->documentElement())->copyInheritableProperties();
306 documentStyle->diff(style);
309 static bool shouldAddNamespaceElem(const Element* elem)
311 // Don't add namespace attribute if it is already defined for this elem.
312 const AtomicString& prefix = elem->prefix();
313 AtomicString attr = !prefix.isEmpty() ? "xmlns:" + prefix : "xmlns";
314 return !elem->hasAttribute(attr);
317 static bool shouldAddNamespaceAttr(const Attribute* attr, HashMap<AtomicStringImpl*, AtomicStringImpl*>& namespaces)
319 // Don't add namespace attributes twice
320 DEFINE_STATIC_LOCAL(const AtomicString, xmlnsURI, ("http://www.w3.org/2000/xmlns/"));
321 DEFINE_STATIC_LOCAL(const QualifiedName, xmlnsAttr, (nullAtom, "xmlns", xmlnsURI));
322 if (attr->name() == xmlnsAttr) {
323 namespaces.set(emptyAtom.impl(), attr->value().impl());
327 QualifiedName xmlnsPrefixAttr("xmlns", attr->localName(), xmlnsURI);
328 if (attr->name() == xmlnsPrefixAttr) {
329 namespaces.set(attr->localName().impl(), attr->value().impl());
336 static void appendNamespace(Vector<UChar>& result, const AtomicString& prefix, const AtomicString& ns, HashMap<AtomicStringImpl*, AtomicStringImpl*>& namespaces)
341 // Use emptyAtoms's impl() for both null and empty strings since the HashMap can't handle 0 as a key
342 AtomicStringImpl* pre = prefix.isEmpty() ? emptyAtom.impl() : prefix.impl();
343 AtomicStringImpl* foundNS = namespaces.get(pre);
344 if (foundNS != ns.impl()) {
345 namespaces.set(pre, ns.impl());
346 DEFINE_STATIC_LOCAL(const String, xmlns, ("xmlns"));
348 append(result, xmlns);
349 if (!prefix.isEmpty()) {
351 append(result, prefix);
356 appendAttributeValue(result, ns, false);
361 static void appendDocumentType(Vector<UChar>& result, const DocumentType* n)
363 if (n->name().isEmpty())
366 append(result, "<!DOCTYPE ");
367 append(result, n->name());
368 if (!n->publicId().isEmpty()) {
369 append(result, " PUBLIC \"");
370 append(result, n->publicId());
371 append(result, "\"");
372 if (!n->systemId().isEmpty()) {
373 append(result, " \"");
374 append(result, n->systemId());
375 append(result, "\"");
377 } else if (!n->systemId().isEmpty()) {
378 append(result, " SYSTEM \"");
379 append(result, n->systemId());
380 append(result, "\"");
382 if (!n->internalSubset().isEmpty()) {
383 append(result, " [");
384 append(result, n->internalSubset());
390 static void appendStartMarkup(Vector<UChar>& result, const Node *node, const Range *range, EAnnotateForInterchange annotate, bool convertBlocksToInlines = false, HashMap<AtomicStringImpl*, AtomicStringImpl*>* namespaces = 0)
392 bool documentIsHTML = node->document()->isHTMLDocument();
393 switch (node->nodeType()) {
394 case Node::TEXT_NODE: {
395 if (Node* parent = node->parentNode()) {
396 if (parent->hasTagName(scriptTag)
397 || parent->hasTagName(styleTag)
398 || parent->hasTagName(textareaTag)
399 || parent->hasTagName(xmpTag)) {
400 appendUCharRange(result, ucharRange(node, range));
405 appendEscapedContent(result, ucharRange(node, range), documentIsHTML);
409 bool useRenderedText = !enclosingNodeWithTag(Position(const_cast<Node*>(node), 0), selectTag);
410 String markup = escapeContentText(useRenderedText ? renderedText(node, range) : stringValueForRange(node, range), false);
412 markup = convertHTMLTextToInterchangeFormat(markup, static_cast<const Text*>(node));
413 append(result, markup);
416 case Node::COMMENT_NODE:
417 // FIXME: Comment content is not escaped, but XMLSerializer (and possibly other callers) should raise an exception if it includes "-->".
418 append(result, "<!--");
419 append(result, static_cast<const Comment*>(node)->nodeValue());
420 append(result, "-->");
422 case Node::DOCUMENT_NODE:
423 case Node::DOCUMENT_FRAGMENT_NODE:
425 case Node::DOCUMENT_TYPE_NODE:
426 appendDocumentType(result, static_cast<const DocumentType*>(node));
428 case Node::PROCESSING_INSTRUCTION_NODE: {
429 // FIXME: PI data is not escaped, but XMLSerializer (and possibly other callers) this should raise an exception if it includes "?>".
430 const ProcessingInstruction* n = static_cast<const ProcessingInstruction*>(node);
431 append(result, "<?");
432 append(result, n->target());
434 append(result, n->data());
435 append(result, "?>");
438 case Node::ELEMENT_NODE: {
440 const Element* el = static_cast<const Element*>(node);
441 bool convert = convertBlocksToInlines & isBlock(const_cast<Node*>(node));
442 append(result, el->nodeNamePreservingCase());
443 NamedAttrMap *attrs = el->attributes();
444 unsigned length = attrs->length();
445 if (!documentIsHTML && namespaces && shouldAddNamespaceElem(el))
446 appendNamespace(result, el->prefix(), el->namespaceURI(), *namespaces);
448 for (unsigned int i = 0; i < length; i++) {
449 Attribute *attr = attrs->attributeItem(i);
450 // We'll handle the style attribute separately, below.
451 if (attr->name() == styleAttr && el->isHTMLElement() && (annotate || convert))
456 append(result, attr->name().localName());
458 append(result, attr->name().toString());
462 if (el->isURLAttribute(attr))
463 appendQuotedURLAttributeValue(result, attr->value());
466 appendAttributeValue(result, attr->value(), documentIsHTML);
470 if (!documentIsHTML && namespaces && shouldAddNamespaceAttr(attr, *namespaces))
471 appendNamespace(result, attr->prefix(), attr->namespaceURI(), *namespaces);
474 if (el->isHTMLElement() && (annotate || convert)) {
475 Element* element = const_cast<Element*>(el);
476 RefPtr<CSSMutableStyleDeclaration> style = static_cast<HTMLElement*>(element)->getInlineStyleDecl()->copy();
478 RefPtr<CSSMutableStyleDeclaration> styleFromMatchedRules = styleFromMatchedRulesForElement(const_cast<Element*>(el));
479 // Styles from the inline style declaration, held in the variable "style", take precedence
480 // over those from matched rules.
481 styleFromMatchedRules->merge(style.get());
482 style = styleFromMatchedRules;
484 RefPtr<CSSComputedStyleDeclaration> computedStyleForElement = computedStyle(element);
485 RefPtr<CSSMutableStyleDeclaration> fromComputedStyle = CSSMutableStyleDeclaration::create();
488 CSSMutableStyleDeclaration::const_iterator end = style->end();
489 for (CSSMutableStyleDeclaration::const_iterator it = style->begin(); it != end; ++it) {
490 const CSSProperty& property = *it;
491 CSSValue* value = property.value();
492 // The property value, if it's a percentage, may not reflect the actual computed value.
493 // For example: style="height: 1%; overflow: visible;" in quirksmode
494 // FIXME: There are others like this, see <rdar://problem/5195123> Slashdot copy/paste fidelity problem
495 if (value->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE)
496 if (static_cast<CSSPrimitiveValue*>(value)->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
497 if (RefPtr<CSSValue> computedPropertyValue = computedStyleForElement->getPropertyCSSValue(property.id()))
498 fromComputedStyle->addParsedProperty(CSSProperty(property.id(), computedPropertyValue));
502 style->merge(fromComputedStyle.get());
505 style->setProperty(CSSPropertyDisplay, CSSValueInline, true);
506 if (style->length() > 0) {
507 DEFINE_STATIC_LOCAL(const String, stylePrefix, (" style=\""));
508 append(result, stylePrefix);
509 appendAttributeValue(result, style->cssText(), documentIsHTML);
514 if (shouldSelfClose(el)) {
515 if (el->isHTMLElement())
516 result.append(' '); // XHTML 1.0 <-> HTML compatibility.
522 case Node::CDATA_SECTION_NODE: {
523 // FIXME: CDATA content is not escaped, but XMLSerializer (and possibly other callers) should raise an exception if it includes "]]>".
524 const CDATASection* n = static_cast<const CDATASection*>(node);
525 append(result, "<![CDATA[");
526 append(result, n->data());
527 append(result, "]]>");
530 case Node::ATTRIBUTE_NODE:
531 case Node::ENTITY_NODE:
532 case Node::ENTITY_REFERENCE_NODE:
533 case Node::NOTATION_NODE:
534 case Node::XPATH_NAMESPACE_NODE:
535 ASSERT_NOT_REACHED();
540 static String getStartMarkup(const Node *node, const Range *range, EAnnotateForInterchange annotate, bool convertBlocksToInlines = false, HashMap<AtomicStringImpl*, AtomicStringImpl*>* namespaces = 0)
542 Vector<UChar> result;
543 appendStartMarkup(result, node, range, annotate, convertBlocksToInlines, namespaces);
544 return String::adopt(result);
547 static inline bool doesHTMLForbidEndTag(const Node *node)
549 if (node->isHTMLElement()) {
550 const HTMLElement* htmlElt = static_cast<const HTMLElement*>(node);
551 return (htmlElt->endTagRequirement() == TagStatusForbidden);
556 // Rules of self-closure
557 // 1. No elements in HTML documents use the self-closing syntax.
558 // 2. Elements w/ children never self-close because they use a separate end tag.
559 // 3. HTML elements which do not have a "forbidden" end tag will close with a separate end tag.
560 // 4. Other elements self-close.
561 static inline bool shouldSelfClose(const Node *node)
563 if (node->document()->isHTMLDocument())
565 if (node->hasChildNodes())
567 if (node->isHTMLElement() && !doesHTMLForbidEndTag(node))
572 static void appendEndMarkup(Vector<UChar>& result, const Node* node)
574 if (!node->isElementNode() || shouldSelfClose(node) || (!node->hasChildNodes() && doesHTMLForbidEndTag(node)))
579 append(result, static_cast<const Element*>(node)->nodeNamePreservingCase());
583 static String getEndMarkup(const Node *node)
585 Vector<UChar> result;
586 appendEndMarkup(result, node);
587 return String::adopt(result);
590 static void appendMarkup(Vector<UChar>& result, Node* startNode, bool onlyIncludeChildren, Vector<Node*>* nodes, const HashMap<AtomicStringImpl*, AtomicStringImpl*>* namespaces = 0)
592 HashMap<AtomicStringImpl*, AtomicStringImpl*> namespaceHash;
594 namespaceHash = *namespaces;
596 if (!onlyIncludeChildren) {
598 nodes->append(startNode);
600 appendStartMarkup(result,startNode, 0, DoNotAnnotateForInterchange, false, &namespaceHash);
603 if (!(startNode->document()->isHTMLDocument() && doesHTMLForbidEndTag(startNode)))
604 for (Node* current = startNode->firstChild(); current; current = current->nextSibling())
605 appendMarkup(result, current, false, nodes, &namespaceHash);
607 // Print my ending tag
608 if (!onlyIncludeChildren)
609 appendEndMarkup(result, startNode);
612 static void completeURLs(Node* node, const String& baseURL)
614 Vector<AttributeChange> changes;
616 KURL parsedBaseURL(baseURL);
618 Node* end = node->traverseNextSibling();
619 for (Node* n = node; n != end; n = n->traverseNextNode()) {
620 if (n->isElementNode()) {
621 Element* e = static_cast<Element*>(n);
622 NamedAttrMap* attrs = e->attributes();
623 unsigned length = attrs->length();
624 for (unsigned i = 0; i < length; i++) {
625 Attribute* attr = attrs->attributeItem(i);
626 if (e->isURLAttribute(attr))
627 changes.append(AttributeChange(e, attr->name(), KURL(parsedBaseURL, attr->value()).string()));
632 size_t numChanges = changes.size();
633 for (size_t i = 0; i < numChanges; ++i)
637 static bool needInterchangeNewlineAfter(const VisiblePosition& v)
639 VisiblePosition next = v.next();
640 Node* upstreamNode = next.deepEquivalent().upstream().node();
641 Node* downstreamNode = v.deepEquivalent().downstream().node();
642 // Add an interchange newline if a paragraph break is selected and a br won't already be added to the markup to represent it.
643 return isEndOfParagraph(v) && isStartOfParagraph(next) && !(upstreamNode->hasTagName(brTag) && upstreamNode == downstreamNode);
646 static PassRefPtr<CSSMutableStyleDeclaration> styleFromMatchedRulesAndInlineDecl(const Node* node)
648 if (!node->isHTMLElement())
651 // FIXME: Having to const_cast here is ugly, but it is quite a bit of work to untangle
652 // the non-const-ness of styleFromMatchedRulesForElement.
653 HTMLElement* element = const_cast<HTMLElement*>(static_cast<const HTMLElement*>(node));
654 RefPtr<CSSMutableStyleDeclaration> style = styleFromMatchedRulesForElement(element);
655 RefPtr<CSSMutableStyleDeclaration> inlineStyleDecl = element->getInlineStyleDecl();
656 style->merge(inlineStyleDecl.get());
657 return style.release();
660 static bool propertyMissingOrEqualToNone(CSSMutableStyleDeclaration* style, int propertyID)
664 RefPtr<CSSValue> value = style->getPropertyCSSValue(propertyID);
667 if (!value->isPrimitiveValue())
669 return static_cast<CSSPrimitiveValue*>(value.get())->getIdent() == CSSValueNone;
672 static bool elementHasTextDecorationProperty(const Node* node)
674 RefPtr<CSSMutableStyleDeclaration> style = styleFromMatchedRulesAndInlineDecl(node);
677 return !propertyMissingOrEqualToNone(style.get(), CSSPropertyTextDecoration);
680 static String joinMarkups(const Vector<String>& preMarkups, const Vector<String>& postMarkups)
684 size_t preCount = preMarkups.size();
685 for (size_t i = 0; i < preCount; ++i)
686 length += preMarkups[i].length();
688 size_t postCount = postMarkups.size();
689 for (size_t i = 0; i < postCount; ++i)
690 length += postMarkups[i].length();
692 Vector<UChar> result;
693 result.reserveInitialCapacity(length);
695 for (size_t i = preCount; i > 0; --i)
696 append(result, preMarkups[i - 1]);
698 for (size_t i = 0; i < postCount; ++i)
699 append(result, postMarkups[i]);
701 return String::adopt(result);
704 static bool isSpecialAncestorBlock(Node* node)
706 if (!node || !isBlock(node))
709 return node->hasTagName(listingTag) ||
710 node->hasTagName(olTag) ||
711 node->hasTagName(preTag) ||
712 node->hasTagName(tableTag) ||
713 node->hasTagName(ulTag) ||
714 node->hasTagName(xmpTag) ||
715 node->hasTagName(h1Tag) ||
716 node->hasTagName(h2Tag) ||
717 node->hasTagName(h3Tag) ||
718 node->hasTagName(h4Tag) ||
719 node->hasTagName(h5Tag);
722 // FIXME: Shouldn't we omit style info when annotate == DoNotAnnotateForInterchange?
723 // FIXME: At least, annotation and style info should probably not be included in range.markupString()
724 String createMarkup(const Range* range, Vector<Node*>* nodes, EAnnotateForInterchange annotate, bool convertBlocksToInlines)
726 DEFINE_STATIC_LOCAL(const String, interchangeNewlineString, ("<br class=\"" AppleInterchangeNewline "\">"));
731 Document* document = range->ownerDocument();
735 bool documentIsHTML = document->isHTMLDocument();
737 // Disable the delete button so it's elements are not serialized into the markup,
738 // but make sure neither endpoint is inside the delete user interface.
739 Frame* frame = document->frame();
740 DeleteButtonController* deleteButton = frame ? frame->editor()->deleteButtonController() : 0;
741 RefPtr<Range> updatedRange = avoidIntersectionWithNode(range, deleteButton ? deleteButton->containerElement() : 0);
746 deleteButton->disable();
748 ExceptionCode ec = 0;
749 bool collapsed = updatedRange->collapsed(ec);
753 Node* commonAncestor = updatedRange->commonAncestorContainer(ec);
758 document->updateLayoutIgnorePendingStylesheets();
760 Vector<String> markups;
761 Vector<String> preMarkups;
762 Node* pastEnd = updatedRange->pastLastNode();
763 Node* lastClosed = 0;
764 Vector<Node*> ancestorsToClose;
766 Node* startNode = updatedRange->firstNode();
767 VisiblePosition visibleStart(updatedRange->startPosition(), VP_DEFAULT_AFFINITY);
768 VisiblePosition visibleEnd(updatedRange->endPosition(), VP_DEFAULT_AFFINITY);
769 if (annotate && needInterchangeNewlineAfter(visibleStart)) {
770 if (visibleStart == visibleEnd.previous()) {
772 deleteButton->enable();
773 return interchangeNewlineString;
776 markups.append(interchangeNewlineString);
777 startNode = visibleStart.next().deepEquivalent().node();
781 for (Node* n = startNode; n != pastEnd; n = next) {
783 // According to <rdar://problem/5730668>, it is possible for n to blow past pastEnd and become null here. This
784 // shouldn't be possible. This null check will prevent crashes (but create too much markup) and the ASSERT will
785 // hopefully lead us to understanding the problem.
790 next = n->traverseNextNode();
791 bool skipDescendants = false;
792 bool addMarkupForNode = true;
794 if (!n->renderer() && !enclosingNodeWithTag(Position(n, 0), selectTag)) {
795 skipDescendants = true;
796 addMarkupForNode = false;
797 next = n->traverseNextSibling();
798 // Don't skip over pastEnd.
799 if (pastEnd && pastEnd->isDescendantOf(n))
803 if (isBlock(n) && canHaveChildrenForEditing(n) && next == pastEnd)
804 // Don't write out empty block containers that aren't fully selected.
807 // Add the node to the markup.
808 if (addMarkupForNode) {
809 markups.append(getStartMarkup(n, updatedRange.get(), annotate));
814 if (n->firstChild() == 0 || skipDescendants) {
815 // Node has no children, or we are skipping it's descendants, add its close tag now.
816 if (addMarkupForNode) {
817 markups.append(getEndMarkup(n));
821 // Check if the node is the last leaf of a tree.
822 if (!n->nextSibling() || next == pastEnd) {
823 if (!ancestorsToClose.isEmpty()) {
824 // Close up the ancestors.
826 Node *ancestor = ancestorsToClose.last();
827 if (next != pastEnd && next->isDescendantOf(ancestor))
829 // Not at the end of the range, close ancestors up to sibling of next node.
830 markups.append(getEndMarkup(ancestor));
831 lastClosed = ancestor;
832 ancestorsToClose.removeLast();
833 } while (!ancestorsToClose.isEmpty());
836 // Surround the currently accumulated markup with markup for ancestors we never opened as we leave the subtree(s) rooted at those ancestors.
837 Node* nextParent = next ? next->parentNode() : 0;
838 if (next != pastEnd && n != nextParent) {
839 Node* lastAncestorClosedOrSelf = n->isDescendantOf(lastClosed) ? lastClosed : n;
840 for (Node *parent = lastAncestorClosedOrSelf->parent(); parent != 0 && parent != nextParent; parent = parent->parentNode()) {
841 // All ancestors that aren't in the ancestorsToClose list should either be a) unrendered:
842 if (!parent->renderer())
844 // or b) ancestors that we never encountered during a pre-order traversal starting at startNode:
845 ASSERT(startNode->isDescendantOf(parent));
846 preMarkups.append(getStartMarkup(parent, updatedRange.get(), annotate));
847 markups.append(getEndMarkup(parent));
849 nodes->append(parent);
854 } else if (addMarkupForNode && !skipDescendants)
855 // We added markup for this node, and we're descending into it. Set it to close eventually.
856 ancestorsToClose.append(n);
859 // Include ancestors that aren't completely inside the range but are required to retain
860 // the structure and appearance of the copied markup.
861 Node* specialCommonAncestor = 0;
862 Node* commonAncestorBlock = commonAncestor ? enclosingBlock(commonAncestor) : 0;
863 if (annotate && commonAncestorBlock) {
864 if (commonAncestorBlock->hasTagName(tbodyTag) || commonAncestorBlock->hasTagName(trTag)) {
865 Node* table = commonAncestorBlock->parentNode();
866 while (table && !table->hasTagName(tableTag))
867 table = table->parentNode();
869 specialCommonAncestor = table;
870 } else if (isSpecialAncestorBlock(commonAncestorBlock))
871 specialCommonAncestor = commonAncestorBlock;
874 // Retain the Mail quote level by including all ancestor mail block quotes.
875 if (lastClosed && annotate) {
876 for (Node *ancestor = lastClosed->parentNode(); ancestor; ancestor = ancestor->parentNode())
877 if (isMailBlockquote(ancestor))
878 specialCommonAncestor = ancestor;
881 Node* checkAncestor = specialCommonAncestor ? specialCommonAncestor : commonAncestor;
882 if (checkAncestor->renderer()) {
883 RefPtr<CSSMutableStyleDeclaration> checkAncestorStyle = computedStyle(checkAncestor)->copyInheritableProperties();
884 if (!propertyMissingOrEqualToNone(checkAncestorStyle.get(), CSSPropertyWebkitTextDecorationsInEffect))
885 specialCommonAncestor = enclosingNodeOfType(Position(checkAncestor, 0), &elementHasTextDecorationProperty);
888 // If a single tab is selected, commonAncestor will be a text node inside a tab span.
889 // If two or more tabs are selected, commonAncestor will be the tab span.
890 // In either case, if there is a specialCommonAncestor already, it will necessarily be above
891 // any tab span that needs to be included.
892 if (!specialCommonAncestor && isTabSpanTextNode(commonAncestor))
893 specialCommonAncestor = commonAncestor->parentNode();
894 if (!specialCommonAncestor && isTabSpanNode(commonAncestor))
895 specialCommonAncestor = commonAncestor;
897 if (Node *enclosingAnchor = enclosingNodeWithTag(Position(specialCommonAncestor ? specialCommonAncestor : commonAncestor, 0), aTag))
898 specialCommonAncestor = enclosingAnchor;
900 Node* body = enclosingNodeWithTag(Position(commonAncestor, 0), bodyTag);
901 // FIXME: Only include markup for a fully selected root (and ancestors of lastClosed up to that root) if
902 // there are styles/attributes on those nodes that need to be included to preserve the appearance of the copied markup.
903 // FIXME: Do this for all fully selected blocks, not just the body.
904 Node* fullySelectedRoot = body && *VisibleSelection::selectionFromContentsOfNode(body).toNormalizedRange() == *updatedRange ? body : 0;
905 if (annotate && fullySelectedRoot)
906 specialCommonAncestor = fullySelectedRoot;
908 if (specialCommonAncestor && lastClosed) {
909 // Also include all of the ancestors of lastClosed up to this special ancestor.
910 for (Node* ancestor = lastClosed->parentNode(); ancestor; ancestor = ancestor->parentNode()) {
911 if (ancestor == fullySelectedRoot && !convertBlocksToInlines) {
912 RefPtr<CSSMutableStyleDeclaration> style = styleFromMatchedRulesAndInlineDecl(fullySelectedRoot);
914 // Bring the background attribute over, but not as an attribute because a background attribute on a div
915 // appears to have no effect.
916 if (!style->getPropertyCSSValue(CSSPropertyBackgroundImage) && static_cast<Element*>(fullySelectedRoot)->hasAttribute(backgroundAttr))
917 style->setProperty(CSSPropertyBackgroundImage, "url('" + static_cast<Element*>(fullySelectedRoot)->getAttribute(backgroundAttr) + "')");
919 if (style->length()) {
920 Vector<UChar> openTag;
921 DEFINE_STATIC_LOCAL(const String, divStyle, ("<div style=\""));
922 append(openTag, divStyle);
923 appendAttributeValue(openTag, style->cssText(), documentIsHTML);
924 openTag.append('\"');
926 preMarkups.append(String::adopt(openTag));
928 DEFINE_STATIC_LOCAL(const String, divCloseTag, ("</div>"));
929 markups.append(divCloseTag);
932 preMarkups.append(getStartMarkup(ancestor, updatedRange.get(), annotate, convertBlocksToInlines));
933 markups.append(getEndMarkup(ancestor));
936 nodes->append(ancestor);
938 lastClosed = ancestor;
940 if (ancestor == specialCommonAncestor)
945 DEFINE_STATIC_LOCAL(const String, styleSpanOpen, ("<span class=\"" AppleStyleSpanClass "\" style=\""));
946 DEFINE_STATIC_LOCAL(const String, styleSpanClose, ("</span>"));
948 // Add a wrapper span with the styles that all of the nodes in the markup inherit.
949 Node* parentOfLastClosed = lastClosed ? lastClosed->parentNode() : 0;
950 if (parentOfLastClosed && parentOfLastClosed->renderer()) {
951 RefPtr<CSSMutableStyleDeclaration> style = computedStyle(parentOfLastClosed)->copyInheritableProperties();
953 // Styles that Mail blockquotes contribute should only be placed on the Mail blockquote, to help
954 // us differentiate those styles from ones that the user has applied. This helps us
955 // get the color of content pasted into blockquotes right.
956 removeEnclosingMailBlockquoteStyle(style.get(), parentOfLastClosed);
958 // Document default styles will be added on another wrapper span.
959 removeDefaultStyles(style.get(), document);
961 // Since we are converting blocks to inlines, remove any inherited block properties that are in the style.
962 // This cuts out meaningless properties and prevents properties from magically affecting blocks later
963 // if the style is cloned for a new block element during a future editing operation.
964 if (convertBlocksToInlines)
965 style->removeBlockProperties();
967 if (style->length() > 0) {
968 Vector<UChar> openTag;
969 append(openTag, styleSpanOpen);
970 appendAttributeValue(openTag, style->cssText(), documentIsHTML);
971 openTag.append('\"');
973 preMarkups.append(String::adopt(openTag));
975 markups.append(styleSpanClose);
979 if (lastClosed && lastClosed != document->documentElement()) {
980 // Add a style span with the document's default styles. We add these in a separate
981 // span so that at paste time we can differentiate between document defaults and user
983 RefPtr<CSSMutableStyleDeclaration> defaultStyle = computedStyle(document->documentElement())->copyInheritableProperties();
985 if (defaultStyle->length() > 0) {
986 Vector<UChar> openTag;
987 append(openTag, styleSpanOpen);
988 appendAttributeValue(openTag, defaultStyle->cssText(), documentIsHTML);
989 openTag.append('\"');
991 preMarkups.append(String::adopt(openTag));
992 markups.append(styleSpanClose);
996 // FIXME: The interchange newline should be placed in the block that it's in, not after all of the content, unconditionally.
997 if (annotate && needInterchangeNewlineAfter(visibleEnd.previous()))
998 markups.append(interchangeNewlineString);
1001 deleteButton->enable();
1003 return joinMarkups(preMarkups, markups);
1006 PassRefPtr<DocumentFragment> createFragmentFromMarkup(Document* document, const String& markup, const String& baseURL)
1008 ASSERT(document->documentElement()->isHTMLElement());
1009 // FIXME: What if the document element is not an HTML element?
1010 HTMLElement *element = static_cast<HTMLElement*>(document->documentElement());
1012 RefPtr<DocumentFragment> fragment = element->createContextualFragment(markup);
1014 if (fragment && !baseURL.isEmpty() && baseURL != blankURL() && baseURL != document->baseURL())
1015 completeURLs(fragment.get(), baseURL);
1017 return fragment.release();
1020 String createMarkup(const Node* node, EChildrenOnly includeChildren, Vector<Node*>* nodes)
1022 Vector<UChar> result;
1027 Document* document = node->document();
1028 Frame* frame = document->frame();
1029 DeleteButtonController* deleteButton = frame ? frame->editor()->deleteButtonController() : 0;
1031 // disable the delete button so it's elements are not serialized into the markup
1033 if (node->isDescendantOf(deleteButton->containerElement()))
1035 deleteButton->disable();
1038 document->updateLayoutIgnorePendingStylesheets();
1040 appendMarkup(result, const_cast<Node*>(node), includeChildren, nodes);
1043 deleteButton->enable();
1045 return String::adopt(result);
1048 static void fillContainerFromString(ContainerNode* paragraph, const String& string)
1050 Document* document = paragraph->document();
1052 ExceptionCode ec = 0;
1053 if (string.isEmpty()) {
1054 paragraph->appendChild(createBlockPlaceholderElement(document), ec);
1059 ASSERT(string.find('\n') == -1);
1061 Vector<String> tabList;
1062 string.split('\t', true, tabList);
1063 String tabText = "";
1065 size_t numEntries = tabList.size();
1066 for (size_t i = 0; i < numEntries; ++i) {
1067 const String& s = tabList[i];
1069 // append the non-tab textual part
1071 if (!tabText.isEmpty()) {
1072 paragraph->appendChild(createTabSpanElement(document, tabText), ec);
1076 RefPtr<Node> textNode = document->createTextNode(stringWithRebalancedWhitespace(s, first, i + 1 == numEntries));
1077 paragraph->appendChild(textNode.release(), ec);
1081 // there is a tab after every entry, except the last entry
1082 // (if the last character is a tab, the list gets an extra empty entry)
1083 if (i + 1 != numEntries)
1084 tabText.append('\t');
1085 else if (!tabText.isEmpty()) {
1086 paragraph->appendChild(createTabSpanElement(document, tabText), ec);
1094 PassRefPtr<DocumentFragment> createFragmentFromText(Range* context, const String& text)
1099 Node* styleNode = context->firstNode();
1101 styleNode = context->startPosition().node();
1106 Document* document = styleNode->document();
1107 RefPtr<DocumentFragment> fragment = document->createDocumentFragment();
1110 return fragment.release();
1112 String string = text;
1113 string.replace("\r\n", "\n");
1114 string.replace('\r', '\n');
1116 ExceptionCode ec = 0;
1117 RenderObject* renderer = styleNode->renderer();
1118 if (renderer && renderer->style()->preserveNewline()) {
1119 fragment->appendChild(document->createTextNode(string), ec);
1121 if (string.endsWith("\n")) {
1122 RefPtr<Element> element = createBreakElement(document);
1123 element->setAttribute(classAttr, AppleInterchangeNewline);
1124 fragment->appendChild(element.release(), ec);
1127 return fragment.release();
1130 // A string with no newlines gets added inline, rather than being put into a paragraph.
1131 if (string.find('\n') == -1) {
1132 fillContainerFromString(fragment.get(), string);
1133 return fragment.release();
1136 // Break string into paragraphs. Extra line breaks turn into empty paragraphs.
1137 Node* blockNode = enclosingBlock(context->firstNode());
1138 Element* block = static_cast<Element*>(blockNode);
1139 bool useClonesOfEnclosingBlock = blockNode
1140 && blockNode->isElementNode()
1141 && !block->hasTagName(bodyTag)
1142 && !block->hasTagName(htmlTag)
1143 && block != editableRootForPosition(context->startPosition());
1145 Vector<String> list;
1146 string.split('\n', true, list); // true gets us empty strings in the list
1147 size_t numLines = list.size();
1148 for (size_t i = 0; i < numLines; ++i) {
1149 const String& s = list[i];
1151 RefPtr<Element> element;
1152 if (s.isEmpty() && i + 1 == numLines) {
1153 // For last line, use the "magic BR" rather than a P.
1154 element = createBreakElement(document);
1155 element->setAttribute(classAttr, AppleInterchangeNewline);
1157 if (useClonesOfEnclosingBlock)
1158 element = block->cloneElementWithoutChildren();
1160 element = createDefaultParagraphElement(document);
1161 fillContainerFromString(element.get(), s);
1163 fragment->appendChild(element.release(), ec);
1166 return fragment.release();
1169 PassRefPtr<DocumentFragment> createFragmentFromNodes(Document *document, const Vector<Node*>& nodes)
1174 // disable the delete button so it's elements are not serialized into the markup
1175 if (document->frame())
1176 document->frame()->editor()->deleteButtonController()->disable();
1178 RefPtr<DocumentFragment> fragment = document->createDocumentFragment();
1180 ExceptionCode ec = 0;
1181 size_t size = nodes.size();
1182 for (size_t i = 0; i < size; ++i) {
1183 RefPtr<Element> element = createDefaultParagraphElement(document);
1184 element->appendChild(nodes[i], ec);
1186 fragment->appendChild(element.release(), ec);
1190 if (document->frame())
1191 document->frame()->editor()->deleteButtonController()->enable();
1193 return fragment.release();
1196 String createFullMarkup(const Node* node)
1201 Document* document = node->document();
1205 Frame* frame = document->frame();
1209 // FIXME: This is never "for interchange". Is that right?
1210 String markupString = createMarkup(node, IncludeNode, 0);
1211 Node::NodeType nodeType = node->nodeType();
1212 if (nodeType != Node::DOCUMENT_NODE && nodeType != Node::DOCUMENT_TYPE_NODE)
1213 markupString = frame->documentTypeString() + markupString;
1215 return markupString;
1218 String createFullMarkup(const Range* range)
1223 Node* node = range->startContainer();
1227 Document* document = node->document();
1231 Frame* frame = document->frame();
1235 // FIXME: This is always "for interchange". Is that right? See the previous method.
1236 return frame->documentTypeString() + createMarkup(range, 0, AnnotateForInterchange);