本文整理汇总了C++中Selection::affinity方法的典型用法代码示例。如果您正苦于以下问题:C++ Selection::affinity方法的具体用法?C++ Selection::affinity怎么用?C++ Selection::affinity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Selection
的用法示例。
在下文中一共展示了Selection::affinity方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: writeSelection
static void writeSelection(TextStream& ts, const RenderObject* o)
{
Node* n = o->element();
if (!n || !n->isDocumentNode())
return;
Document* doc = static_cast<Document*>(n);
Frame* frame = doc->frame();
if (!frame)
return;
Selection selection = frame->selection()->selection();
if (selection.isCaret()) {
ts << "caret: position " << selection.start().offset() << " of " << nodePosition(selection.start().node());
if (selection.affinity() == UPSTREAM)
ts << " (upstream affinity)";
ts << "\n";
} else if (selection.isRange())
ts << "selection start: position " << selection.start().offset() << " of " << nodePosition(selection.start().node()) << "\n"
<< "selection end: position " << selection.end().offset() << " of " << nodePosition(selection.end().node()) << "\n";
}
示例2: doApply
void InsertLineBreakCommand::doApply()
{
deleteSelection();
Selection selection = endingSelection();
if (selection.isNone())
return;
Position pos(selection.start().upstream());
pos = positionAvoidingSpecialElementBoundary(pos);
Node* styleNode = pos.node();
bool isTabSpan = isTabSpanTextNode(styleNode);
if (isTabSpan)
styleNode = styleNode->parentNode()->parentNode();
RenderObject* styleRenderer = styleNode->renderer();
bool useBreakElement = !styleRenderer || !styleRenderer->style()->preserveNewline();
RefPtr<Node> nodeToInsert;
if (useBreakElement)
nodeToInsert = createBreakElement(document());
else
nodeToInsert = document()->createTextNode("\n");
// FIXME: Need to merge text nodes when inserting just after or before text.
if (isTabSpan) {
insertNodeAtTabSpanPosition(nodeToInsert.get(), pos);
setEndingSelection(Position(nodeToInsert->traverseNextNode(), 0), DOWNSTREAM);
} else if (isEndOfBlock(VisiblePosition(pos, selection.affinity()))) {
Node* block = pos.node()->enclosingBlockFlowElement();
// Insert an extra break element so that there will be a blank line after the last
// inserted line break. In HTML, a line break at the end of a block ends the last
// line in the block, while in editable text, a line break at the end of block
// creates a last blank line. We need an extra break element to get HTML to act
// the way editable text would.
bool haveBreak = pos.downstream().node()->hasTagName(brTag) && pos.downstream().offset() == 0;
insertNodeAt(nodeToInsert.get(), pos.node(), pos.offset());
if (!haveBreak)
insertNodeAfter(createBreakElement(document()).get(), nodeToInsert.get());
setEndingSelection(Position(block, maxDeepOffset(block)), DOWNSTREAM);
} else if (pos.offset() <= pos.node()->caretMinOffset()) {
LOG(Editing, "input newline case 2");
// Insert node before downstream position, and place caret there as well.
Position endingPosition = pos.downstream();
insertNodeBeforePosition(nodeToInsert.get(), endingPosition);
setEndingSelection(endingPosition, DOWNSTREAM);
} else if (pos.offset() >= pos.node()->caretMaxOffset()) {
LOG(Editing, "input newline case 3");
// Insert BR after this node. Place caret in the position that is downstream
// of the current position, reckoned before inserting the BR in between.
Position endingPosition = pos.downstream();
insertNodeAfterPosition(nodeToInsert.get(), pos);
setEndingSelection(endingPosition, DOWNSTREAM);
} else {
// Split a text node
LOG(Editing, "input newline case 4");
ASSERT(pos.node()->isTextNode());
// Do the split
ExceptionCode ec = 0;
Text *textNode = static_cast<Text *>(pos.node());
RefPtr<Text> textBeforeNode = document()->createTextNode(textNode->substringData(0, selection.start().offset(), ec));
deleteTextFromNode(textNode, 0, pos.offset());
insertNodeBefore(textBeforeNode.get(), textNode);
insertNodeBefore(nodeToInsert.get(), textNode);
Position endingPosition = Position(textNode, 0);
// Handle whitespace that occurs after the split
updateLayout();
if (!endingPosition.isRenderedCharacter()) {
// Clear out all whitespace and insert one non-breaking space
deleteInsignificantTextDownstream(endingPosition);
ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
insertTextIntoNode(textNode, 0, nonBreakingSpaceString());
}
setEndingSelection(endingPosition, DOWNSTREAM);
}
// Handle the case where there is a typing style.
// FIXME: Improve typing style.
// See this bug: <rdar://problem/3769899> Implementation of typing style needs improvement
CSSMutableStyleDeclaration* typingStyle = document()->frame()->typingStyle();
if (typingStyle && typingStyle->length() > 0) {
Selection selectionBeforeStyle = endingSelection();
applyStyle(typingStyle, Position(nodeToInsert.get(), 0),
Position(nodeToInsert.get(), maxDeepOffset(nodeToInsert.get())));
setEndingSelection(selectionBeforeStyle);
}
rebalanceWhitespace();
}