11 Node Node::createCombinator(const Complex_Selector::Combinator& combinator) {
13 return Node(COMBINATOR, combinator, NULL /*pSelector*/, null /*pCollection*/);
17 Node Node::createSelector(Complex_Selector_Ptr pSelector, Context& ctx) {
20 Complex_Selector_Ptr pStripped = SASS_MEMORY_COPY(pSelector);
21 pStripped->tail(NULL);
22 pStripped->combinator(Complex_Selector::ANCESTOR_OF);
24 Node n(SELECTOR, Complex_Selector::ANCESTOR_OF, pStripped, null /*pCollection*/);
25 if (pSelector) n.got_line_feed = pSelector->has_line_feed();
30 Node Node::createCollection() {
31 NodeDequePtr pEmptyCollection = std::make_shared<NodeDeque>();
32 return Node(COLLECTION, Complex_Selector::ANCESTOR_OF, NULL /*pSelector*/, pEmptyCollection);
36 Node Node::createCollection(const NodeDeque& values) {
37 NodeDequePtr pShallowCopiedCollection = std::make_shared<NodeDeque>(values);
38 return Node(COLLECTION, Complex_Selector::ANCESTOR_OF, NULL /*pSelector*/, pShallowCopiedCollection);
42 Node Node::createNil() {
44 return Node(NIL, Complex_Selector::ANCESTOR_OF, NULL /*pSelector*/, null /*pCollection*/);
48 Node::Node(const TYPE& type, Complex_Selector::Combinator combinator, Complex_Selector_Ptr pSelector, NodeDequePtr& pCollection)
49 : got_line_feed(false), mType(type), mCombinator(combinator), mpSelector(pSelector), mpCollection(pCollection)
50 { if (pSelector) got_line_feed = pSelector->has_line_feed(); }
53 Node Node::klone(Context& ctx) const {
54 NodeDequePtr pNewCollection = std::make_shared<NodeDeque>();
56 for (NodeDeque::iterator iter = mpCollection->begin(), iterEnd = mpCollection->end(); iter != iterEnd; iter++) {
57 Node& toClone = *iter;
58 pNewCollection->push_back(toClone.klone(ctx));
62 Node n(mType, mCombinator, mpSelector ? SASS_MEMORY_COPY(mpSelector) : NULL, pNewCollection);
63 n.got_line_feed = got_line_feed;
68 bool Node::contains(const Node& potentialChild, bool simpleSelectorOrderDependent) const {
71 for (NodeDeque::iterator iter = mpCollection->begin(), iterEnd = mpCollection->end(); iter != iterEnd; iter++) {
74 if (toTest == potentialChild) {
84 bool Node::operator==(const Node& rhs) const {
85 if (this->type() != rhs.type()) {
89 if (this->isCombinator()) {
91 return this->combinator() == rhs.combinator();
93 } else if (this->isNil()) {
95 return true; // no state to check
97 } else if (this->isSelector()){
99 return *this->selector() == *rhs.selector();
101 } else if (this->isCollection()) {
103 if (this->collection()->size() != rhs.collection()->size()) {
107 for (NodeDeque::iterator lhsIter = this->collection()->begin(), lhsIterEnd = this->collection()->end(),
108 rhsIter = rhs.collection()->begin(); lhsIter != lhsIterEnd; lhsIter++, rhsIter++) {
110 if (*lhsIter != *rhsIter) {
120 // We shouldn't get here.
121 throw "Comparing unknown node types. A new type was probably added and this method wasn't implemented for it.";
125 void Node::plus(Node& rhs) {
126 if (!this->isCollection() || !rhs.isCollection()) {
127 throw "Both the current node and rhs must be collections.";
129 this->collection()->insert(this->collection()->end(), rhs.collection()->begin(), rhs.collection()->end());
133 std::ostream& operator<<(std::ostream& os, const Node& node) {
135 if (node.isCombinator()) {
137 switch (node.combinator()) {
138 case Complex_Selector::ANCESTOR_OF: os << "\" \""; break;
139 case Complex_Selector::PARENT_OF: os << "\">\""; break;
140 case Complex_Selector::PRECEDES: os << "\"~\""; break;
141 case Complex_Selector::ADJACENT_TO: os << "\"+\""; break;
142 case Complex_Selector::REFERENCE: os << "\"/\""; break;
145 } else if (node.isNil()) {
149 } else if (node.isSelector()){
151 os << node.selector()->head()->to_string();
153 } else if (node.isCollection()) {
157 for (NodeDeque::iterator iter = node.collection()->begin(), iterBegin = node.collection()->begin(), iterEnd = node.collection()->end(); iter != iterEnd; iter++) {
158 if (iter != iterBegin) {
175 Node complexSelectorToNode(Complex_Selector_Ptr pToConvert, Context& ctx) {
176 if (pToConvert == NULL) {
177 return Node::createNil();
179 Node node = Node::createCollection();
180 node.got_line_feed = pToConvert->has_line_feed();
181 bool has_lf = pToConvert->has_line_feed();
183 // unwrap the selector from parent ref
184 if (pToConvert->head() && pToConvert->head()->has_parent_ref()) {
185 Complex_Selector_Obj tail = pToConvert->tail();
186 if (tail) tail->has_line_feed(pToConvert->has_line_feed());
192 bool empty_parent_ref = pToConvert->head() && pToConvert->head()->is_empty_reference();
194 if (pToConvert->head() || empty_parent_ref) {
197 // the first Complex_Selector may contain a dummy head pointer, skip it.
198 if (pToConvert->head() && !empty_parent_ref) {
199 node.collection()->push_back(Node::createSelector(pToConvert, ctx));
200 if (has_lf) node.collection()->back().got_line_feed = has_lf;
204 if (pToConvert->combinator() != Complex_Selector::ANCESTOR_OF) {
205 node.collection()->push_back(Node::createCombinator(pToConvert->combinator()));
206 if (has_lf) node.collection()->back().got_line_feed = has_lf;
210 if (pToConvert && empty_parent_ref && pToConvert->tail()) {
211 // pToConvert->tail()->has_line_feed(pToConvert->has_line_feed());
214 pToConvert = pToConvert->tail();
221 Complex_Selector_Ptr nodeToComplexSelector(const Node& toConvert, Context& ctx) {
222 if (toConvert.isNil()) {
227 if (!toConvert.isCollection()) {
228 throw "The node to convert to a Complex_Selector_Ptr must be a collection type or nil.";
232 NodeDeque& childNodes = *toConvert.collection();
234 std::string noPath("");
235 Position noPosition(-1, -1, -1);
236 Complex_Selector_Obj pFirst = SASS_MEMORY_NEW(Complex_Selector, ParserState("[NODE]"), Complex_Selector::ANCESTOR_OF, NULL, NULL);
238 Complex_Selector_Obj pCurrent = pFirst;
240 if (toConvert.isSelector()) pFirst->has_line_feed(toConvert.got_line_feed);
241 if (toConvert.isCombinator()) pFirst->has_line_feed(toConvert.got_line_feed);
243 for (NodeDeque::iterator childIter = childNodes.begin(), childIterEnd = childNodes.end(); childIter != childIterEnd; childIter++) {
245 Node& child = *childIter;
247 if (child.isSelector()) {
248 // JMA - need to clone the selector, because they can end up getting shared across Node
249 // collections, and can result in an infinite loop during the call to parentSuperselector()
250 pCurrent->tail(SASS_MEMORY_COPY(child.selector()));
251 // if (child.got_line_feed) pCurrent->has_line_feed(child.got_line_feed);
252 pCurrent = pCurrent->tail();
253 } else if (child.isCombinator()) {
254 pCurrent->combinator(child.combinator());
255 if (child.got_line_feed) pCurrent->has_line_feed(child.got_line_feed);
257 // if the next node is also a combinator, create another Complex_Selector to hold it so it doesn't replace the current combinator
258 if (childIter+1 != childIterEnd) {
259 Node& nextNode = *(childIter+1);
260 if (nextNode.isCombinator()) {
261 pCurrent->tail(SASS_MEMORY_NEW(Complex_Selector, ParserState("[NODE]"), Complex_Selector::ANCESTOR_OF, NULL, NULL));
262 if (nextNode.got_line_feed) pCurrent->tail()->has_line_feed(nextNode.got_line_feed);
263 pCurrent = pCurrent->tail();
267 throw "The node to convert's children must be only combinators or selectors.";
271 // Put the dummy Compound_Selector in the first position, for consistency with the rest of libsass
272 Compound_Selector_Ptr fakeHead = SASS_MEMORY_NEW(Compound_Selector, ParserState("[NODE]"), 1);
273 Parent_Selector_Ptr selectorRef = SASS_MEMORY_NEW(Parent_Selector, ParserState("[NODE]"));
274 fakeHead->elements().push_back(selectorRef);
275 if (toConvert.got_line_feed) pFirst->has_line_feed(toConvert.got_line_feed);
276 // pFirst->has_line_feed(pFirst->has_line_feed() || pFirst->tail()->has_line_feed() || toConvert.got_line_feed);
277 pFirst->head(fakeHead);
278 return SASS_MEMORY_COPY(pFirst);
281 // A very naive trim function, which removes duplicates in a node
282 // This is only used in Complex_Selector::unify_with for now, may need modifications to fit other needs
283 Node Node::naiveTrim(Node& seqses, Context& ctx) {
285 std::vector<Node*> res;
286 std::vector<Complex_Selector_Obj> known;
288 NodeDeque::reverse_iterator seqsesIter = seqses.collection()->rbegin(),
289 seqsesIterEnd = seqses.collection()->rend();
291 for (; seqsesIter != seqsesIterEnd; ++seqsesIter)
293 Node& seqs1 = *seqsesIter;
294 if( seqs1.isSelector() ) {
295 Complex_Selector_Obj sel = seqs1.selector();
296 std::vector<Complex_Selector_Obj>::iterator it;
298 for (it = known.begin(); it != known.end(); ++it) {
299 if (**it == *sel) { found = true; break; }
302 known.push_back(seqs1.selector());
303 res.push_back(&seqs1);
306 res.push_back(&seqs1);
310 Node result = Node::createCollection();
312 for (size_t i = res.size() - 1; i != std::string::npos; --i) {
313 result.collection()->push_back(*res[i]);