1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "core/dom/Document.h"
8 #include "core/dom/Range.h"
9 #include "core/dom/Text.h"
10 #include "core/html/HTMLElement.h"
11 #include "wtf/text/StringBuilder.h"
13 #include <ostream> // NOLINT
19 // Copied from "dom/Node.cpp".
20 void appendAttributeDesc(const Node
& node
, StringBuilder
& stringBuilder
, const QualifiedName
& name
, const char* attrDesc
)
22 if (!node
.isElementNode())
25 String attr
= toElement(node
).getAttribute(name
);
29 stringBuilder
.append(attrDesc
);
30 stringBuilder
.appendLiteral("=\"");
31 stringBuilder
.append(attr
);
32 stringBuilder
.appendLiteral("\"");
35 template <typename PositionType
>
36 std::ostream
& printPosition(std::ostream
& ostream
, const PositionType
& position
)
38 if (position
.isNull())
39 return ostream
<< "null";
40 ostream
<< position
.anchorNode() << "@";
41 if (position
.isOffsetInAnchor())
42 return ostream
<< position
.offsetInContainerNode();
43 return ostream
<< position
.anchorType();
48 std::ostream
& operator<<(std::ostream
& ostream
, PositionAnchorType anchorType
)
51 case PositionAnchorType::AfterAnchor
:
52 return ostream
<< "afterAnchor";
53 case PositionAnchorType::AfterChildren
:
54 return ostream
<< "afterChildren";
55 case PositionAnchorType::BeforeAnchor
:
56 return ostream
<< "beforeAnchor";
57 case PositionAnchorType::BeforeChildren
:
58 return ostream
<< "beforeChildren";
59 case PositionAnchorType::OffsetInAnchor
:
60 return ostream
<< "offsetInAnchor";
63 return ostream
<< "anchorType=" << static_cast<int>(anchorType
);
66 // |std::ostream| version of |Node::showNode|
67 std::ostream
& operator<<(std::ostream
& ostream
, const Node
& node
)
69 ostream
<< node
.nodeName().utf8().data();
70 if (node
.isTextNode())
71 return ostream
<< " " << node
.nodeValue();
73 appendAttributeDesc(node
, attrs
, HTMLNames::idAttr
, " ID");
74 appendAttributeDesc(node
, attrs
, HTMLNames::classAttr
, " CLASS");
75 appendAttributeDesc(node
, attrs
, HTMLNames::styleAttr
, " STYLE");
76 return ostream
<< attrs
.toString().utf8().data();
79 std::ostream
& operator<<(std::ostream
& ostream
, const Node
* node
)
82 return ostream
<< "null";
83 return ostream
<< *node
;
86 std::ostream
& operator<<(std::ostream
& ostream
, const Position
& position
)
88 return printPosition(ostream
, position
);
91 std::ostream
& operator<<(std::ostream
& ostream
, const PositionInComposedTree
& position
)
93 return printPosition(ostream
, position
);