void CreateLinkCommand::doApply() { if (endingSelection().isNone()) return; RefPtr<HTMLAnchorElement> anchorElement = HTMLAnchorElement::create(document()); anchorElement->setHref(m_url); if (endingSelection().isRange()) applyStyledElement(anchorElement.get()); else { insertNodeAt(anchorElement.get(), endingSelection().start()); RefPtr<Text> textNode = Text::create(document(), m_url); appendNode(textNode.get(), anchorElement.get()); setEndingSelection(VisibleSelection(positionInParentBeforeNode(anchorElement.get()), positionInParentAfterNode(anchorElement.get()), DOWNSTREAM)); } }
void CreateLinkCommand::doApply() { if (endingSelection().isNone()) return; RefPtrWillBeRawPtr<HTMLAnchorElement> anchorElement = HTMLAnchorElement::create(document()); anchorElement->setHref(AtomicString(m_url)); if (endingSelection().isRange()) { applyStyledElement(anchorElement.get()); } else { insertNodeAt(anchorElement.get(), endingSelection().start()); RefPtrWillBeRawPtr<Text> textNode = Text::create(document(), m_url); appendNode(textNode.get(), anchorElement.get()); setEndingSelection(VisibleSelection(positionInParentBeforeNode(*anchorElement), positionInParentAfterNode(*anchorElement), TextAffinity::Downstream, endingSelection().isDirectional())); } }
// Neighbor-anchored positions are invalid DOM positions, so they need to be // fixed up before handing them off to the Range object. Position Position::parentAnchoredEquivalent() const { if (!m_anchorNode) return Position(); // FIXME: This should only be necessary for legacy positions, but is also needed for positions before and after Tables if (m_offset <= 0 && m_anchorType != PositionIsAfterAnchor) { if (m_anchorNode->parentNode() && (editingIgnoresContent(m_anchorNode.get()) || isTableElement(m_anchorNode.get()))) return positionInParentBeforeNode(m_anchorNode.get()); return firstPositionInOrBeforeNode(m_anchorNode.get()); } if (!m_anchorNode->offsetInCharacters() && (m_anchorType == PositionIsAfterAnchor || static_cast<unsigned>(m_offset) == m_anchorNode->childNodeCount()) && (editingIgnoresContent(m_anchorNode.get()) || isTableElement(m_anchorNode.get())) && containerNode()) { return positionInParentAfterNode(m_anchorNode.get()); } return Position(containerNode(), computeOffsetInContainerNode(), PositionIsOffsetInAnchor); }
void VisibleSelection::adjustSelectionToAvoidCrossingEditingBoundaries() { if (m_base.isNull() || m_start.isNull() || m_end.isNull()) return; // Early return in the caret case (the state hasn't actually been set yet, so we can't use isCaret()) to avoid the // expense of computing highestEditableRoot. if (m_base == m_start && m_base == m_end) return; Node* baseRoot = highestEditableRoot(m_base); Node* startRoot = highestEditableRoot(m_start); Node* endRoot = highestEditableRoot(m_end); Node* baseEditableAncestor = lowestEditableAncestor(m_base.containerNode()); // The base, start and end are all in the same region. No adjustment necessary. if (baseRoot == startRoot && baseRoot == endRoot) return; // The selection is based in editable content. if (baseRoot) { // If the start is outside the base's editable root, cap it at the start of that root. // If the start is in non-editable content that is inside the base's editable root, put it // at the first editable position after start inside the base's editable root. if (startRoot != baseRoot) { VisiblePosition first = firstEditablePositionAfterPositionInRoot(m_start, baseRoot); m_start = first.deepEquivalent(); if (m_start.isNull()) { ASSERT_NOT_REACHED(); m_start = m_end; } } // If the end is outside the base's editable root, cap it at the end of that root. // If the end is in non-editable content that is inside the base's root, put it // at the last editable position before the end inside the base's root. if (endRoot != baseRoot) { VisiblePosition last = lastEditablePositionBeforePositionInRoot(m_end, baseRoot); m_end = last.deepEquivalent(); if (m_end.isNull()) m_end = m_start; } // The selection is based in non-editable content. } else { // FIXME: Non-editable pieces inside editable content should be atomic, in the same way that editable // pieces in non-editable content are atomic. // The selection ends in editable content or non-editable content inside a different editable ancestor, // move backward until non-editable content inside the same lowest editable ancestor is reached. Node* endEditableAncestor = lowestEditableAncestor(m_end.containerNode()); if (endRoot || endEditableAncestor != baseEditableAncestor) { Position p = previousVisuallyDistinctCandidate(m_end); Node* shadowAncestor = endRoot ? endRoot->shadowHost() : 0; if (p.isNull() && shadowAncestor) p = positionAfterNode(shadowAncestor); while (p.isNotNull() && !(lowestEditableAncestor(p.containerNode()) == baseEditableAncestor && !isEditablePosition(p))) { Node* root = editableRootForPosition(p); shadowAncestor = root ? root->shadowHost() : 0; p = isAtomicNode(p.containerNode()) ? positionInParentBeforeNode(p.containerNode()) : previousVisuallyDistinctCandidate(p); if (p.isNull() && shadowAncestor) p = positionAfterNode(shadowAncestor); } VisiblePosition previous(p); if (previous.isNull()) { // The selection crosses an Editing boundary. This is a // programmer error in the editing code. Happy debugging! ASSERT_NOT_REACHED(); m_base = Position(); m_extent = Position(); validate(); return; } m_end = previous.deepEquivalent(); } // The selection starts in editable content or non-editable content inside a different editable ancestor, // move forward until non-editable content inside the same lowest editable ancestor is reached. Node* startEditableAncestor = lowestEditableAncestor(m_start.containerNode()); if (startRoot || startEditableAncestor != baseEditableAncestor) { Position p = nextVisuallyDistinctCandidate(m_start); Node* shadowAncestor = startRoot ? startRoot->shadowHost() : 0; if (p.isNull() && shadowAncestor) p = positionBeforeNode(shadowAncestor); while (p.isNotNull() && !(lowestEditableAncestor(p.containerNode()) == baseEditableAncestor && !isEditablePosition(p))) { Node* root = editableRootForPosition(p); shadowAncestor = root ? root->shadowHost() : 0; p = isAtomicNode(p.containerNode()) ? positionInParentAfterNode(p.containerNode()) : nextVisuallyDistinctCandidate(p); if (p.isNull() && shadowAncestor) p = positionBeforeNode(shadowAncestor); } VisiblePosition next(p); if (next.isNull()) { // The selection crosses an Editing boundary. This is a // programmer error in the editing code. Happy debugging! ASSERT_NOT_REACHED(); m_base = Position(); m_extent = Position(); validate(); return; } m_start = next.deepEquivalent(); } } // Correct the extent if necessary. if (baseEditableAncestor != lowestEditableAncestor(m_extent.containerNode())) m_extent = m_baseIsFirst ? m_end : m_start; }
void InsertTextCommand::input(const String& text, bool selectInsertedText, RebalanceType whitespaceRebalance) { ASSERT(text.find('\n') == notFound); if (!endingSelection().isNonOrphanedCaretOrRange()) return; // Delete the current selection. // FIXME: This delete operation blows away the typing style. if (endingSelection().isRange()) { if (performTrivialReplace(text, selectInsertedText)) return; deleteSelection(false, true, true, false); } Position startPosition(endingSelection().start()); Position placeholder; // We want to remove preserved newlines and brs that will collapse (and thus become unnecessary) when content // is inserted just before them. // FIXME: We shouldn't really have to do this, but removing placeholders is a workaround for 9661. // If the caret is just before a placeholder, downstream will normalize the caret to it. Position downstream(startPosition.downstream()); if (lineBreakExistsAtPosition(downstream)) { // FIXME: This doesn't handle placeholders at the end of anonymous blocks. VisiblePosition caret(startPosition); if (isEndOfBlock(caret) && isStartOfParagraph(caret)) placeholder = downstream; // Don't remove the placeholder yet, otherwise the block we're inserting into would collapse before // we get a chance to insert into it. We check for a placeholder now, though, because doing so requires // the creation of a VisiblePosition, and if we did that post-insertion it would force a layout. } // Insert the character at the leftmost candidate. startPosition = startPosition.upstream(); // It is possible for the node that contains startPosition to contain only unrendered whitespace, // and so deleteInsignificantText could remove it. Save the position before the node in case that happens. Position positionBeforeStartNode(positionInParentBeforeNode(startPosition.containerNode())); deleteInsignificantText(startPosition.upstream(), startPosition.downstream()); if (!startPosition.anchorNode()->inDocument()) startPosition = positionBeforeStartNode; if (!startPosition.isCandidate()) startPosition = startPosition.downstream(); startPosition = positionAvoidingSpecialElementBoundary(startPosition); Position endPosition; if (text == "\t") { endPosition = insertTab(startPosition); startPosition = endPosition.previous(); if (placeholder.isNotNull()) removePlaceholderAt(placeholder); } else { // Make sure the document is set up to receive text startPosition = positionInsideTextNode(startPosition); ASSERT(startPosition.anchorType() == Position::PositionIsOffsetInAnchor); ASSERT(startPosition.containerNode()); ASSERT(startPosition.containerNode()->isTextNode()); if (placeholder.isNotNull()) removePlaceholderAt(placeholder); RefPtr<Text> textNode = static_cast<Text*>(startPosition.containerNode()); const unsigned offset = startPosition.offsetInContainerNode(); insertTextIntoNode(textNode, offset, text); endPosition = Position(textNode, offset + text.length()); if (whitespaceRebalance == RebalanceLeadingAndTrailingWhitespaces) { // The insertion may require adjusting adjacent whitespace, if it is present. rebalanceWhitespaceAt(endPosition); // Rebalancing on both sides isn't necessary if we've inserted only spaces. if (!shouldRebalanceLeadingWhitespaceFor(text)) rebalanceWhitespaceAt(startPosition); } else { ASSERT(whitespaceRebalance == RebalanceAllWhitespaces); if (canRebalance(startPosition) && canRebalance(endPosition)) rebalanceWhitespaceOnTextSubstring(textNode, startPosition.offsetInContainerNode(), endPosition.offsetInContainerNode()); } } // We could have inserted a part of composed character sequence, // so we are basically treating ending selection as a range to avoid validation. // <http://bugs.webkit.org/show_bug.cgi?id=15781> VisibleSelection forcedEndingSelection; forcedEndingSelection.setWithoutValidation(startPosition, endPosition); setEndingSelection(forcedEndingSelection); // Handle the case where there is a typing style. if (RefPtr<EditingStyle> typingStyle = document()->frame()->selection()->typingStyle()) { typingStyle->prepareToApplyAt(endPosition, EditingStyle::PreserveWritingDirection); if (!typingStyle->isEmpty()) applyStyle(typingStyle.get()); } if (!selectInsertedText) setEndingSelection(VisibleSelection(endingSelection().end(), endingSelection().affinity())); }
void InsertTextCommand::input(const String& text, bool selectInsertedText) { ASSERT(text.find('\n') == notFound); if (!endingSelection().isNonOrphanedCaretOrRange()) return; // Delete the current selection. // FIXME: This delete operation blows away the typing style. if (endingSelection().isRange()) { if (performTrivialReplace(text, selectInsertedText)) return; deleteSelection(false, true, true, false); } Position startPosition(endingSelection().start()); Position placeholder; // We want to remove preserved newlines and brs that will collapse (and thus become unnecessary) when content // is inserted just before them. // FIXME: We shouldn't really have to do this, but removing placeholders is a workaround for 9661. // If the caret is just before a placeholder, downstream will normalize the caret to it. Position downstream(startPosition.downstream()); if (lineBreakExistsAtPosition(downstream)) { // FIXME: This doesn't handle placeholders at the end of anonymous blocks. VisiblePosition caret(startPosition); if (isEndOfBlock(caret) && isStartOfParagraph(caret)) placeholder = downstream; // Don't remove the placeholder yet, otherwise the block we're inserting into would collapse before // we get a chance to insert into it. We check for a placeholder now, though, because doing so requires // the creation of a VisiblePosition, and if we did that post-insertion it would force a layout. } // Insert the character at the leftmost candidate. startPosition = startPosition.upstream(); // It is possible for the node that contains startPosition to contain only unrendered whitespace, // and so deleteInsignificantText could remove it. Save the position before the node in case that happens. Position positionBeforeStartNode(positionInParentBeforeNode(startPosition.node())); deleteInsignificantText(startPosition.upstream(), startPosition.downstream()); if (!startPosition.node()->inDocument()) startPosition = positionBeforeStartNode; if (!startPosition.isCandidate()) startPosition = startPosition.downstream(); startPosition = positionAvoidingSpecialElementBoundary(startPosition); Position endPosition; if (text == "\t") { endPosition = insertTab(startPosition); startPosition = endPosition.previous(); if (placeholder.isNotNull()) removePlaceholderAt(placeholder); m_charactersAdded += 1; } else { // Make sure the document is set up to receive text startPosition = prepareForTextInsertion(startPosition); if (placeholder.isNotNull()) removePlaceholderAt(placeholder); Text *textNode = static_cast<Text *>(startPosition.node()); int offset = startPosition.deprecatedEditingOffset(); insertTextIntoNode(textNode, offset, text); endPosition = Position(textNode, offset + text.length()); // The insertion may require adjusting adjacent whitespace, if it is present. rebalanceWhitespaceAt(endPosition); // Rebalancing on both sides isn't necessary if we've inserted a space. if (text != " ") rebalanceWhitespaceAt(startPosition); m_charactersAdded += text.length(); } // We could have inserted a part of composed character sequence, // so we are basically treating ending selection as a range to avoid validation. // <http://bugs.webkit.org/show_bug.cgi?id=15781> VisibleSelection forcedEndingSelection; forcedEndingSelection.setWithoutValidation(startPosition, endPosition); setEndingSelection(forcedEndingSelection); // Handle the case where there is a typing style. CSSMutableStyleDeclaration* typingStyle = document()->frame()->selection()->typingStyle(); RefPtr<CSSComputedStyleDeclaration> endingStyle = endPosition.computedStyle(); RefPtr<CSSValue> unicodeBidi; RefPtr<CSSValue> direction; if (typingStyle) { unicodeBidi = typingStyle->getPropertyCSSValue(CSSPropertyUnicodeBidi); direction = typingStyle->getPropertyCSSValue(CSSPropertyDirection); } endingStyle->diff(typingStyle); if (typingStyle && unicodeBidi) { ASSERT(unicodeBidi->isPrimitiveValue()); typingStyle->setProperty(CSSPropertyUnicodeBidi, static_cast<CSSPrimitiveValue*>(unicodeBidi.get())->getIdent()); if (direction) { ASSERT(direction->isPrimitiveValue()); typingStyle->setProperty(CSSPropertyDirection, static_cast<CSSPrimitiveValue*>(direction.get())->getIdent()); } } if (typingStyle && typingStyle->length()) applyStyle(typingStyle); if (!selectInsertedText) setEndingSelection(VisibleSelection(endingSelection().end(), endingSelection().affinity())); }
void InsertTextCommand::doApply() { ASSERT(m_text.find('\n') == kNotFound); if (!endingSelection().isNonOrphanedCaretOrRange()) return; // Delete the current selection. // FIXME: This delete operation blows away the typing style. if (endingSelection().isRange()) { if (performTrivialReplace(m_text, m_selectInsertedText)) return; bool endOfSelectionWasAtStartOfBlock = isStartOfBlock(endingSelection().visibleEnd()); deleteSelection(false, true, false, false); // deleteSelection eventually makes a new endingSelection out of a Position. If that Position doesn't have // a renderer (e.g. it is on a <frameset> in the DOM), the VisibleSelection cannot be canonicalized to // anything other than NoSelection. The rest of this function requires a real endingSelection, so bail out. if (endingSelection().isNone()) return; if (endOfSelectionWasAtStartOfBlock) { if (EditingStyle* typingStyle = document().frame()->selection().typingStyle()) typingStyle->removeBlockProperties(); } } else if (document().frame()->editor().isOverwriteModeEnabled()) { if (performOverwrite(m_text, m_selectInsertedText)) return; } Position startPosition(endingSelection().start()); Position placeholder; // We want to remove preserved newlines and brs that will collapse (and thus become unnecessary) when content // is inserted just before them. // FIXME: We shouldn't really have to do this, but removing placeholders is a workaround for 9661. // If the caret is just before a placeholder, downstream will normalize the caret to it. Position downstream(startPosition.downstream()); if (lineBreakExistsAtPosition(downstream)) { // FIXME: This doesn't handle placeholders at the end of anonymous blocks. VisiblePosition caret(startPosition); if (isEndOfBlock(caret) && isStartOfParagraph(caret)) placeholder = downstream; // Don't remove the placeholder yet, otherwise the block we're inserting into would collapse before // we get a chance to insert into it. We check for a placeholder now, though, because doing so requires // the creation of a VisiblePosition, and if we did that post-insertion it would force a layout. } // Insert the character at the leftmost candidate. startPosition = startPosition.upstream(); // It is possible for the node that contains startPosition to contain only unrendered whitespace, // and so deleteInsignificantText could remove it. Save the position before the node in case that happens. ASSERT(startPosition.containerNode()); Position positionBeforeStartNode(positionInParentBeforeNode(*startPosition.containerNode())); deleteInsignificantText(startPosition, startPosition.downstream()); if (!startPosition.inDocument()) startPosition = positionBeforeStartNode; if (!startPosition.isCandidate()) startPosition = startPosition.downstream(); startPosition = positionAvoidingSpecialElementBoundary(startPosition); Position endPosition; if (m_text == "\t") { endPosition = insertTab(startPosition); startPosition = endPosition.previous(); if (placeholder.isNotNull()) removePlaceholderAt(placeholder); } else { // Make sure the document is set up to receive m_text startPosition = positionInsideTextNode(startPosition); ASSERT(startPosition.anchorType() == Position::PositionIsOffsetInAnchor); ASSERT(startPosition.containerNode()); ASSERT(startPosition.containerNode()->isTextNode()); if (placeholder.isNotNull()) removePlaceholderAt(placeholder); RefPtrWillBeRawPtr<Text> textNode = startPosition.containerText(); const unsigned offset = startPosition.offsetInContainerNode(); insertTextIntoNode(textNode, offset, m_text); endPosition = Position(textNode, offset + m_text.length()); if (m_rebalanceType == RebalanceLeadingAndTrailingWhitespaces) { // The insertion may require adjusting adjacent whitespace, if it is present. rebalanceWhitespaceAt(endPosition); // Rebalancing on both sides isn't necessary if we've inserted only spaces. if (!shouldRebalanceLeadingWhitespaceFor(m_text)) rebalanceWhitespaceAt(startPosition); } else { ASSERT(m_rebalanceType == RebalanceAllWhitespaces); if (canRebalance(startPosition) && canRebalance(endPosition)) rebalanceWhitespaceOnTextSubstring(textNode, startPosition.offsetInContainerNode(), endPosition.offsetInContainerNode()); } } setEndingSelectionWithoutValidation(startPosition, endPosition); // Handle the case where there is a typing style. if (RefPtrWillBeRawPtr<EditingStyle> typingStyle = document().frame()->selection().typingStyle()) { typingStyle->prepareToApplyAt(endPosition, EditingStyle::PreserveWritingDirection); if (!typingStyle->isEmpty()) applyStyle(typingStyle.get()); } if (!m_selectInsertedText) setEndingSelection(VisibleSelection(endingSelection().end(), endingSelection().affinity(), endingSelection().isDirectional())); }