This source file includes following definitions.
- applyScaleWithoutCollapsingToZero
- nodeInsideFrame
- toString
- dataForRect
- pageScaleFactor
- minNodeContainsNodes
- findBestOverlappingNode
- shouldSkipBackgroundImage
- collectOverlappingChildNodes
- convertRectToWindow
- extractTextFromNode
#include "config.h"
#include "core/frame/SmartClip.h"
#include "core/dom/ContainerNode.h"
#include "core/dom/Document.h"
#include "core/dom/NodeTraversal.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/FrameView.h"
#include "core/html/HTMLFrameOwnerElement.h"
#include "core/page/Page.h"
#include "core/rendering/RenderObject.h"
#include "wtf/text/StringBuilder.h"
namespace WebCore {
static IntRect applyScaleWithoutCollapsingToZero(const IntRect& rect, float scale)
{
    IntRect result = rect;
    result.scale(scale);
    if (rect.width() > 0 && !result.width())
        result.setWidth(1);
    if (rect.height() > 0 && !result.height())
        result.setHeight(1);
    return result;
}
static Node* nodeInsideFrame(Node* node)
{
    if (node->isFrameOwnerElement())
        return toHTMLFrameOwnerElement(node)->contentDocument();
    return 0;
}
String SmartClipData::toString()
{
    if (!m_node)
        return emptyString();
    const UChar fieldSeparator = 0xFFFE;
    const UChar rowSeparator = 0xFFFF;
    StringBuilder result;
    result.append(String::number(m_rect.x()));
    result.append(fieldSeparator);
    result.append(String::number(m_rect.y()));
    result.append(fieldSeparator);
    result.append(String::number(m_rect.width()));
    result.append(fieldSeparator);
    result.append(String::number(m_rect.height()));
    result.append(fieldSeparator);
    result.append(m_string);
    result.append(rowSeparator);
    return result.toString();
}
SmartClip::SmartClip(PassRefPtr<LocalFrame> frame)
    : m_frame(frame)
{
}
SmartClipData SmartClip::dataForRect(const IntRect& cropRect)
{
    IntRect resizedCropRect = applyScaleWithoutCollapsingToZero(cropRect, 1 / pageScaleFactor());
    Node* bestNode = findBestOverlappingNode(m_frame->document(), resizedCropRect);
    if (!bestNode)
        return SmartClipData();
    if (Node* nodeFromFrame = nodeInsideFrame(bestNode)) {
        
        if (Node* bestNodeInFrame = findBestOverlappingNode(nodeFromFrame, resizedCropRect))
            bestNode = bestNodeInFrame;
    }
    Vector<Node*> hitNodes;
    collectOverlappingChildNodes(bestNode, resizedCropRect, hitNodes);
    if (hitNodes.isEmpty() || hitNodes.size() == bestNode->countChildren()) {
        hitNodes.clear();
        hitNodes.append(bestNode);
    }
    
    IntRect unitedRects = hitNodes[0]->pixelSnappedBoundingBox();
    StringBuilder collectedText;
    for (size_t i = 0; i < hitNodes.size(); ++i) {
        collectedText.append(extractTextFromNode(hitNodes[i]));
        unitedRects.unite(hitNodes[i]->pixelSnappedBoundingBox());
    }
    return SmartClipData(bestNode, convertRectToWindow(unitedRects), collectedText.toString());
}
float SmartClip::pageScaleFactor()
{
    return m_frame->page()->pageScaleFactor();
}
Node* SmartClip::minNodeContainsNodes(Node* minNode, Node* newNode)
{
    if (!newNode)
        return minNode;
    if (!minNode)
        return newNode;
    IntRect minNodeRect = minNode->pixelSnappedBoundingBox();
    IntRect newNodeRect = newNode->pixelSnappedBoundingBox();
    Node* parentMinNode = minNode->parentNode();
    Node* parentNewNode = newNode->parentNode();
    if (minNodeRect.contains(newNodeRect)) {
        if (parentMinNode && parentNewNode && parentNewNode->parentNode() == parentMinNode)
            return parentMinNode;
        return minNode;
    }
    if (newNodeRect.contains(minNodeRect)) {
        if (parentMinNode && parentNewNode && parentMinNode->parentNode() == parentNewNode)
            return parentNewNode;
        return newNode;
    }
    
    
    
    
    Node* node = minNode;
    while (node) {
        if (node->renderer()) {
            IntRect nodeRect = node->pixelSnappedBoundingBox();
            if (nodeRect.contains(newNodeRect)) {
                return node;
            }
        }
        node = node->parentNode();
    }
    return 0;
}
Node* SmartClip::findBestOverlappingNode(Node* rootNode, const IntRect& cropRect)
{
    if (!rootNode)
        return 0;
    IntRect resizedCropRect = rootNode->document().view()->windowToContents(cropRect);
    Node* node = rootNode;
    Node* minNode = 0;
    while (node) {
        IntRect nodeRect = node->pixelSnappedBoundingBox();
        if (node->isElementNode() && equalIgnoringCase(toElement(node)->fastGetAttribute(HTMLNames::aria_hiddenAttr), "true")) {
            node = NodeTraversal::nextSkippingChildren(*node, rootNode);
            continue;
        }
        RenderObject* renderer = node->renderer();
        if (renderer && !nodeRect.isEmpty()) {
            if (renderer->isText()
                || renderer->isRenderImage()
                || node->isFrameOwnerElement()
                || (renderer->style()->hasBackgroundImage() && !shouldSkipBackgroundImage(node))) {
                if (resizedCropRect.intersects(nodeRect)) {
                    minNode = minNodeContainsNodes(minNode, node);
                } else {
                    node = NodeTraversal::nextSkippingChildren(*node, rootNode);
                    continue;
                }
            }
        }
        node = NodeTraversal::next(*node, rootNode);
    }
    return minNode;
}
bool SmartClip::shouldSkipBackgroundImage(Node* node)
{
    ASSERT(node);
    
    if (!isHTMLSpanElement(*node) && !isHTMLDivElement(*node))
        return true;
    
    
    
    
    RenderObject* renderer = node->renderer();
    if (renderer && (renderer->style()->logicalHeight().isAuto() || renderer->style()->logicalWidth().isAuto()))
        return true;
    return false;
}
void SmartClip::collectOverlappingChildNodes(Node* parentNode, const IntRect& cropRect, Vector<Node*>& hitNodes)
{
    if (!parentNode)
        return;
    IntRect resizedCropRect = parentNode->document().view()->windowToContents(cropRect);
    for (Node* child = parentNode->firstChild(); child; child = child->nextSibling()) {
        IntRect childRect = child->pixelSnappedBoundingBox();
        if (resizedCropRect.intersects(childRect))
            hitNodes.append(child);
    }
}
IntRect SmartClip::convertRectToWindow(const IntRect& nodeRect)
{
    IntRect result = m_frame->document()->view()->contentsToWindow(nodeRect);
    result.scale(pageScaleFactor());
    return result;
}
String SmartClip::extractTextFromNode(Node* node)
{
    
    int prevYPos = -99999;
    StringBuilder result;
    for (Node* currentNode = node; currentNode; currentNode = NodeTraversal::next(*currentNode, node)) {
        RenderStyle* style = currentNode->computedStyle();
        if (style && style->userSelect() == SELECT_NONE)
            continue;
        if (Node* nodeFromFrame = nodeInsideFrame(currentNode))
            result.append(extractTextFromNode(nodeFromFrame));
        IntRect nodeRect = currentNode->pixelSnappedBoundingBox();
        if (currentNode->renderer() && !nodeRect.isEmpty()) {
            if (currentNode->isTextNode()) {
                String nodeValue = currentNode->nodeValue();
                
                
                if (nodeValue == "\n")
                    nodeValue = "";
                if (nodeRect.y() != prevYPos) {
                    prevYPos = nodeRect.y();
                    result.append('\n');
                }
                result.append(nodeValue);
            }
        }
    }
    return result.toString();
}
}