Blame src/ast.cpp

Packit Service 7770af
#include "sass.hpp"
Packit Service 7770af
#include "ast.hpp"
Packit Service 7770af
#include "context.hpp"
Packit Service 7770af
#include "node.hpp"
Packit Service 7770af
#include "eval.hpp"
Packit Service 7770af
#include "extend.hpp"
Packit Service 7770af
#include "emitter.hpp"
Packit Service 7770af
#include "color_maps.hpp"
Packit Service 7770af
#include "ast_fwd_decl.hpp"
Packit Service 7770af
#include <set>
Packit Service 7770af
#include <iomanip>
Packit Service 7770af
#include <iostream>
Packit Service 7770af
#include <algorithm>
Packit Service 7770af
#include <functional>
Packit Service 7770af
#include <cctype>
Packit Service 7770af
#include <locale>
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
Packit Service 7770af
  static Null sass_null(ParserState("null"));
Packit Service 7770af
Packit Service 7770af
  bool Wrapped_Selector::find ( bool (*f)(AST_Node_Obj) )
Packit Service 7770af
  {
Packit Service 7770af
    // check children first
Packit Service 7770af
    if (selector_) {
Packit Service 7770af
      if (selector_->find(f)) return true;
Packit Service 7770af
    }
Packit Service 7770af
    // execute last
Packit Service 7770af
    return f(this);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_List::find ( bool (*f)(AST_Node_Obj) )
Packit Service 7770af
  {
Packit Service 7770af
    // check children first
Packit Service 7770af
    for (Complex_Selector_Obj sel : elements()) {
Packit Service 7770af
      if (sel->find(f)) return true;
Packit Service 7770af
    }
Packit Service 7770af
    // execute last
Packit Service 7770af
    return f(this);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::find ( bool (*f)(AST_Node_Obj) )
Packit Service 7770af
  {
Packit Service 7770af
    // check children first
Packit Service 7770af
    for (Simple_Selector_Obj sel : elements()) {
Packit Service 7770af
      if (sel->find(f)) return true;
Packit Service 7770af
    }
Packit Service 7770af
    // execute last
Packit Service 7770af
    return f(this);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::find ( bool (*f)(AST_Node_Obj) )
Packit Service 7770af
  {
Packit Service 7770af
    // check children first
Packit Service 7770af
    if (head_ && head_->find(f)) return true;
Packit Service 7770af
    if (tail_ && tail_->find(f)) return true;
Packit Service 7770af
    // execute last
Packit Service 7770af
    return f(this);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Supports_Operator::needs_parens(Supports_Condition_Obj cond) const {
Packit Service 7770af
    if (Supports_Operator_Obj op = Cast<Supports_Operator>(cond)) {
Packit Service 7770af
      return op->operand() != operand();
Packit Service 7770af
    }
Packit Service 7770af
    return Cast<Supports_Negation>(cond) != NULL;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Supports_Negation::needs_parens(Supports_Condition_Obj cond) const {
Packit Service 7770af
    return Cast<Supports_Negation>(cond) ||
Packit Service 7770af
           Cast<Supports_Operator>(cond);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void str_rtrim(std::string& str, const std::string& delimiters = " \f\n\r\t\v")
Packit Service 7770af
  {
Packit Service 7770af
    str.erase( str.find_last_not_of( delimiters ) + 1 );
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void String_Constant::rtrim()
Packit Service 7770af
  {
Packit Service 7770af
    str_rtrim(value_);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void String_Schema::rtrim()
Packit Service 7770af
  {
Packit Service 7770af
    if (!empty()) {
Packit Service 7770af
      if (String_Ptr str = Cast<String>(last())) str->rtrim();
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Argument::set_delayed(bool delayed)
Packit Service 7770af
  {
Packit Service 7770af
    if (value_) value_->set_delayed(delayed);
Packit Service 7770af
    is_delayed(delayed);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Arguments::set_delayed(bool delayed)
Packit Service 7770af
  {
Packit Service 7770af
    for (Argument_Obj arg : elements()) {
Packit Service 7770af
      if (arg) arg->set_delayed(delayed);
Packit Service 7770af
    }
Packit Service 7770af
    is_delayed(delayed);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  bool At_Root_Query::exclude(std::string str)
Packit Service 7770af
  {
Packit Service 7770af
    bool with = feature() && unquote(feature()->to_string()).compare("with") == 0;
Packit Service 7770af
    List_Ptr l = static_cast<List_Ptr>(value().ptr());
Packit Service 7770af
    std::string v;
Packit Service 7770af
Packit Service 7770af
    if (with)
Packit Service 7770af
    {
Packit Service 7770af
      if (!l || l->length() == 0) return str.compare("rule") != 0;
Packit Service 7770af
      for (size_t i = 0, L = l->length(); i < L; ++i)
Packit Service 7770af
      {
Packit Service 7770af
        v = unquote((*l)[i]->to_string());
Packit Service 7770af
        if (v.compare("all") == 0 || v == str) return false;
Packit Service 7770af
      }
Packit Service 7770af
      return true;
Packit Service 7770af
    }
Packit Service 7770af
    else
Packit Service 7770af
    {
Packit Service 7770af
      if (!l || !l->length()) return str.compare("rule") == 0;
Packit Service 7770af
      for (size_t i = 0, L = l->length(); i < L; ++i)
Packit Service 7770af
      {
Packit Service 7770af
        v = unquote((*l)[i]->to_string());
Packit Service 7770af
        if (v.compare("all") == 0 || v == str) return true;
Packit Service 7770af
      }
Packit Service 7770af
      return false;
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void AST_Node::update_pstate(const ParserState& pstate)
Packit Service 7770af
  {
Packit Service 7770af
    pstate_.offset += pstate - pstate_ + pstate.offset;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Simple_Selector::is_ns_eq(const Simple_Selector& r) const
Packit Service 7770af
  {
Packit Service 7770af
    // https://github.com/sass/sass/issues/2229
Packit Service 7770af
    if ((has_ns_ == r.has_ns_) ||
Packit Service 7770af
        (has_ns_ && ns_.empty()) ||
Packit Service 7770af
        (r.has_ns_ && r.ns_.empty())
Packit Service 7770af
    ) {
Packit Service 7770af
      if (ns_.empty() && r.ns() == "*") return false;
Packit Service 7770af
      else if (r.ns().empty() && ns() == "*") return false;
Packit Service 7770af
      else return ns() == r.ns();
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::operator< (const Compound_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    size_t L = std::min(length(), rhs.length());
Packit Service 7770af
    for (size_t i = 0; i < L; ++i)
Packit Service 7770af
    {
Packit Service 7770af
      Simple_Selector_Obj l = (*this)[i];
Packit Service 7770af
      Simple_Selector_Obj r = rhs[i];
Packit Service 7770af
      if (!l && !r) return false;
Packit Service 7770af
      else if (!r) return false;
Packit Service 7770af
      else if (!l) return true;
Packit Service 7770af
      else if (*l != *r)
Packit Service 7770af
      { return *l < *r; }
Packit Service 7770af
    }
Packit Service 7770af
    // just compare the length now
Packit Service 7770af
    return length() < rhs.length();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::has_parent_ref() const
Packit Service 7770af
  {
Packit Service 7770af
    for (Simple_Selector_Obj s : *this) {
Packit Service 7770af
      if (s && s->has_parent_ref()) return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::has_real_parent_ref() const
Packit Service 7770af
  {
Packit Service 7770af
    for (Simple_Selector_Obj s : *this) {
Packit Service 7770af
      if (s && s->has_real_parent_ref()) return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::has_parent_ref() const
Packit Service 7770af
  {
Packit Service 7770af
    return (head() && head()->has_parent_ref()) ||
Packit Service 7770af
           (tail() && tail()->has_parent_ref());
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::has_real_parent_ref() const
Packit Service 7770af
  {
Packit Service 7770af
    return (head() && head()->has_real_parent_ref()) ||
Packit Service 7770af
           (tail() && tail()->has_real_parent_ref());
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::operator< (const Complex_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // const iterators for tails
Packit Service 7770af
    Complex_Selector_Ptr_Const l = this;
Packit Service 7770af
    Complex_Selector_Ptr_Const r = &rhs;
Packit Service 7770af
    Compound_Selector_Ptr l_h = NULL;
Packit Service 7770af
    Compound_Selector_Ptr r_h = NULL;
Packit Service 7770af
    if (l) l_h = l->head();
Packit Service 7770af
    if (r) r_h = r->head();
Packit Service 7770af
    // process all tails
Packit Service 7770af
    while (true)
Packit Service 7770af
    {
Packit Service 7770af
      #ifdef DEBUG
Packit Service 7770af
      // skip empty ancestor first
Packit Service 7770af
      if (l && l->is_empty_ancestor())
Packit Service 7770af
      {
Packit Service 7770af
        l_h = NULL;
Packit Service 7770af
        l = l->tail();
Packit Service 7770af
        if(l) l_h = l->head();
Packit Service 7770af
        continue;
Packit Service 7770af
      }
Packit Service 7770af
      // skip empty ancestor first
Packit Service 7770af
      if (r && r->is_empty_ancestor())
Packit Service 7770af
      {
Packit Service 7770af
        r_h = NULL;
Packit Service 7770af
        r = r->tail();
Packit Service 7770af
        if (r) r_h = r->head();
Packit Service 7770af
        continue;
Packit Service 7770af
      }
Packit Service 7770af
      #endif
Packit Service 7770af
      // check for valid selectors
Packit Service 7770af
      if (!l) return !!r;
Packit Service 7770af
      if (!r) return false;
Packit Service 7770af
      // both are null
Packit Service 7770af
      else if (!l_h && !r_h)
Packit Service 7770af
      {
Packit Service 7770af
        // check combinator after heads
Packit Service 7770af
        if (l->combinator() != r->combinator())
Packit Service 7770af
        { return l->combinator() < r->combinator(); }
Packit Service 7770af
        // advance to next tails
Packit Service 7770af
        l = l->tail();
Packit Service 7770af
        r = r->tail();
Packit Service 7770af
        // fetch the next headers
Packit Service 7770af
        l_h = NULL; r_h = NULL;
Packit Service 7770af
        if (l) l_h = l->head();
Packit Service 7770af
        if (r) r_h = r->head();
Packit Service 7770af
      }
Packit Service 7770af
      // one side is null
Packit Service 7770af
      else if (!r_h) return true;
Packit Service 7770af
      else if (!l_h) return false;
Packit Service 7770af
      // heads ok and equal
Packit Service 7770af
      else if (*l_h == *r_h)
Packit Service 7770af
      {
Packit Service 7770af
        // check combinator after heads
Packit Service 7770af
        if (l->combinator() != r->combinator())
Packit Service 7770af
        { return l->combinator() < r->combinator(); }
Packit Service 7770af
        // advance to next tails
Packit Service 7770af
        l = l->tail();
Packit Service 7770af
        r = r->tail();
Packit Service 7770af
        // fetch the next headers
Packit Service 7770af
        l_h = NULL; r_h = NULL;
Packit Service 7770af
        if (l) l_h = l->head();
Packit Service 7770af
        if (r) r_h = r->head();
Packit Service 7770af
      }
Packit Service 7770af
      // heads are not equal
Packit Service 7770af
      else return *l_h < *r_h;
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::operator== (const Complex_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // const iterators for tails
Packit Service 7770af
    Complex_Selector_Ptr_Const l = this;
Packit Service 7770af
    Complex_Selector_Ptr_Const r = &rhs;
Packit Service 7770af
    Compound_Selector_Ptr l_h = NULL;
Packit Service 7770af
    Compound_Selector_Ptr r_h = NULL;
Packit Service 7770af
    if (l) l_h = l->head();
Packit Service 7770af
    if (r) r_h = r->head();
Packit Service 7770af
    // process all tails
Packit Service 7770af
    while (true)
Packit Service 7770af
    {
Packit Service 7770af
      #ifdef DEBUG
Packit Service 7770af
      // skip empty ancestor first
Packit Service 7770af
      if (l && l->is_empty_ancestor())
Packit Service 7770af
      {
Packit Service 7770af
        l_h = NULL;
Packit Service 7770af
        l = l->tail();
Packit Service 7770af
        if (l) l_h = l->head();
Packit Service 7770af
        continue;
Packit Service 7770af
      }
Packit Service 7770af
      // skip empty ancestor first
Packit Service 7770af
      if (r && r->is_empty_ancestor())
Packit Service 7770af
      {
Packit Service 7770af
        r_h = NULL;
Packit Service 7770af
        r = r->tail();
Packit Service 7770af
        if (r) r_h = r->head();
Packit Service 7770af
        continue;
Packit Service 7770af
      }
Packit Service 7770af
      #endif
Packit Service 7770af
      // check the pointers
Packit Service 7770af
      if (!r) return !l;
Packit Service 7770af
      if (!l) return !r;
Packit Service 7770af
      // both are null
Packit Service 7770af
      if (!l_h && !r_h)
Packit Service 7770af
      {
Packit Service 7770af
        // check combinator after heads
Packit Service 7770af
        if (l->combinator() != r->combinator())
Packit Service 7770af
        { return l->combinator() < r->combinator(); }
Packit Service 7770af
        // advance to next tails
Packit Service 7770af
        l = l->tail();
Packit Service 7770af
        r = r->tail();
Packit Service 7770af
        // fetch the next heads
Packit Service 7770af
        l_h = NULL; r_h = NULL;
Packit Service 7770af
        if (l) l_h = l->head();
Packit Service 7770af
        if (r) r_h = r->head();
Packit Service 7770af
      }
Packit Service 7770af
      // equals if other head is empty
Packit Service 7770af
      else if ((!l_h && !r_h) ||
Packit Service 7770af
               (!l_h && r_h->empty()) ||
Packit Service 7770af
               (!r_h && l_h->empty()) ||
Packit Service 7770af
               (l_h && r_h && *l_h == *r_h))
Packit Service 7770af
      {
Packit Service 7770af
        // check combinator after heads
Packit Service 7770af
        if (l->combinator() != r->combinator())
Packit Service 7770af
        { return l->combinator() == r->combinator(); }
Packit Service 7770af
        // advance to next tails
Packit Service 7770af
        l = l->tail();
Packit Service 7770af
        r = r->tail();
Packit Service 7770af
        // fetch the next heads
Packit Service 7770af
        l_h = NULL; r_h = NULL;
Packit Service 7770af
        if (l) l_h = l->head();
Packit Service 7770af
        if (r) r_h = r->head();
Packit Service 7770af
      }
Packit Service 7770af
      // abort
Packit Service 7770af
      else break;
Packit Service 7770af
    }
Packit Service 7770af
    // unreachable
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Compound_Selector_Ptr Compound_Selector::unify_with(Compound_Selector_Ptr rhs)
Packit Service 7770af
  {
Packit Service 7770af
    if (empty()) return rhs;
Packit Service 7770af
    Compound_Selector_Obj unified = SASS_MEMORY_COPY(rhs);
Packit Service 7770af
    for (size_t i = 0, L = length(); i < L; ++i)
Packit Service 7770af
    {
Packit Service 7770af
      if (unified.isNull()) break;
Packit Service 7770af
      unified = at(i)->unify_with(unified);
Packit Service 7770af
    }
Packit Service 7770af
    return unified.detach();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::operator== (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (const Selector_List* sl = Cast<Selector_List>(&rhs)) return *this == *sl;
Packit Service 7770af
    if (const Simple_Selector* sp = Cast<Simple_Selector>(&rhs)) return *this == *sp;
Packit Service 7770af
    if (const Complex_Selector* cs = Cast<Complex_Selector>(&rhs)) return *this == *cs;
Packit Service 7770af
    if (const Compound_Selector* ch = Cast<Compound_Selector>(&rhs)) return *this == *ch;
Packit Service 7770af
    throw std::runtime_error("invalid selector base classes to compare");
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::operator< (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (const Selector_List* sl = Cast<Selector_List>(&rhs)) return *this < *sl;
Packit Service 7770af
    if (const Simple_Selector* sp = Cast<Simple_Selector>(&rhs)) return *this < *sp;
Packit Service 7770af
    if (const Complex_Selector* cs = Cast<Complex_Selector>(&rhs)) return *this < *cs;
Packit Service 7770af
    if (const Compound_Selector* ch = Cast<Compound_Selector>(&rhs)) return *this < *ch;
Packit Service 7770af
    throw std::runtime_error("invalid selector base classes to compare");
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::operator== (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (const Selector_List* sl = Cast<Selector_List>(&rhs)) return *this == *sl;
Packit Service 7770af
    if (const Simple_Selector* sp = Cast<Simple_Selector>(&rhs)) return *this == *sp;
Packit Service 7770af
    if (const Complex_Selector* cs = Cast<Complex_Selector>(&rhs)) return *this == *cs;
Packit Service 7770af
    if (const Compound_Selector* ch = Cast<Compound_Selector>(&rhs)) return *this == *ch;
Packit Service 7770af
    throw std::runtime_error("invalid selector base classes to compare");
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::operator< (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (const Selector_List* sl = Cast<Selector_List>(&rhs)) return *this < *sl;
Packit Service 7770af
    if (const Simple_Selector* sp = Cast<Simple_Selector>(&rhs)) return *this < *sp;
Packit Service 7770af
    if (const Complex_Selector* cs = Cast<Complex_Selector>(&rhs)) return *this < *cs;
Packit Service 7770af
    if (const Compound_Selector* ch = Cast<Compound_Selector>(&rhs)) return *this < *ch;
Packit Service 7770af
    throw std::runtime_error("invalid selector base classes to compare");
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_Schema::operator== (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (const Selector_List* sl = Cast<Selector_List>(&rhs)) return *this == *sl;
Packit Service 7770af
    if (const Simple_Selector* sp = Cast<Simple_Selector>(&rhs)) return *this == *sp;
Packit Service 7770af
    if (const Complex_Selector* cs = Cast<Complex_Selector>(&rhs)) return *this == *cs;
Packit Service 7770af
    if (const Compound_Selector* ch = Cast<Compound_Selector>(&rhs)) return *this == *ch;
Packit Service 7770af
    throw std::runtime_error("invalid selector base classes to compare");
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_Schema::operator< (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (const Selector_List* sl = Cast<Selector_List>(&rhs)) return *this < *sl;
Packit Service 7770af
    if (const Simple_Selector* sp = Cast<Simple_Selector>(&rhs)) return *this < *sp;
Packit Service 7770af
    if (const Complex_Selector* cs = Cast<Complex_Selector>(&rhs)) return *this < *cs;
Packit Service 7770af
    if (const Compound_Selector* ch = Cast<Compound_Selector>(&rhs)) return *this < *ch;
Packit Service 7770af
    throw std::runtime_error("invalid selector base classes to compare");
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Simple_Selector::operator== (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Simple_Selector_Ptr_Const sp = Cast<Simple_Selector>(&rhs)) return *this == *sp;
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Simple_Selector::operator< (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Simple_Selector_Ptr_Const sp = Cast<Simple_Selector>(&rhs)) return *this < *sp;
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Simple_Selector::operator== (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // solve the double dispatch problem by using RTTI information via dynamic cast
Packit Service 7770af
    if (const Pseudo_Selector* lhs = Cast<Pseudo_Selector>(this)) {return *lhs == rhs; }
Packit Service 7770af
    else if (const Wrapped_Selector* lhs = Cast<Wrapped_Selector>(this)) {return *lhs == rhs; }
Packit Service 7770af
    else if (const Element_Selector* lhs = Cast<Element_Selector>(this)) {return *lhs == rhs; }
Packit Service 7770af
    else if (const Attribute_Selector* lhs = Cast<Attribute_Selector>(this)) {return *lhs == rhs; }
Packit Service 7770af
    else if (name_ == rhs.name_)
Packit Service 7770af
    { return is_ns_eq(rhs); }
Packit Service 7770af
    else return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Simple_Selector::operator< (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // solve the double dispatch problem by using RTTI information via dynamic cast
Packit Service 7770af
    if (const Pseudo_Selector* lhs = Cast<Pseudo_Selector>(this)) {return *lhs < rhs; }
Packit Service 7770af
    else if (const Wrapped_Selector* lhs = Cast<Wrapped_Selector>(this)) {return *lhs < rhs; }
Packit Service 7770af
    else if (const Element_Selector* lhs = Cast<Element_Selector>(this)) {return *lhs < rhs; }
Packit Service 7770af
    else if (const Attribute_Selector* lhs = Cast<Attribute_Selector>(this)) {return *lhs < rhs; }
Packit Service 7770af
    if (is_ns_eq(rhs))
Packit Service 7770af
    { return name_ < rhs.name_; }
Packit Service 7770af
    return ns_ < rhs.ns_;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_List::operator== (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // solve the double dispatch problem by using RTTI information via dynamic cast
Packit Service 7770af
    if (Selector_List_Ptr_Const sl = Cast<Selector_List>(&rhs)) { return *this == *sl; }
Packit Service 7770af
    else if (Complex_Selector_Ptr_Const cpx = Cast<Complex_Selector>(&rhs)) { return *this == *cpx; }
Packit Service 7770af
    else if (Compound_Selector_Ptr_Const cpd = Cast<Compound_Selector>(&rhs)) { return *this == *cpd; }
Packit Service 7770af
    // no compare method
Packit Service 7770af
    return this == &rhs;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // Selector lists can be compared to comma lists
Packit Service 7770af
  bool Selector_List::operator==(const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // solve the double dispatch problem by using RTTI information via dynamic cast
Packit Service 7770af
    if (List_Ptr_Const ls = Cast<List>(&rhs)) { return *this == *ls; }
Packit Service 7770af
    if (Selector_Ptr_Const ls = Cast<Selector>(&rhs)) { return *this == *ls; }
Packit Service 7770af
    // compare invalid (maybe we should error?)
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_List::operator== (const Selector_List& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // for array access
Packit Service 7770af
    size_t i = 0, n = 0;
Packit Service 7770af
    size_t iL = length();
Packit Service 7770af
    size_t nL = rhs.length();
Packit Service 7770af
    // create temporary vectors and sort them
Packit Service 7770af
    std::vector<Complex_Selector_Obj> l_lst = this->elements();
Packit Service 7770af
    std::vector<Complex_Selector_Obj> r_lst = rhs.elements();
Packit Service 7770af
    std::sort(l_lst.begin(), l_lst.end(), OrderNodes());
Packit Service 7770af
    std::sort(r_lst.begin(), r_lst.end(), OrderNodes());
Packit Service 7770af
    // process loop
Packit Service 7770af
    while (true)
Packit Service 7770af
    {
Packit Service 7770af
      // first check for valid index
Packit Service 7770af
      if (i == iL) return iL == nL;
Packit Service 7770af
      else if (n == nL) return iL == nL;
Packit Service 7770af
      // the access the vector items
Packit Service 7770af
      Complex_Selector_Obj l = l_lst[i];
Packit Service 7770af
      Complex_Selector_Obj r = r_lst[n];
Packit Service 7770af
      // skip nulls
Packit Service 7770af
      if (!l) ++i;
Packit Service 7770af
      else if (!r) ++n;
Packit Service 7770af
      // do the check
Packit Service 7770af
      else if (*l != *r)
Packit Service 7770af
      { return false; }
Packit Service 7770af
      // advance
Packit Service 7770af
      ++i; ++n;
Packit Service 7770af
    }
Packit Service 7770af
    // there is no break?!
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_List::operator< (const Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Selector_List_Ptr_Const sp = Cast<Selector_List>(&rhs)) return *this < *sp;
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_List::operator< (const Selector_List& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    size_t l = rhs.length();
Packit Service 7770af
    if (length() < l) l = length();
Packit Service 7770af
    for (size_t i = 0; i < l; i ++) {
Packit Service 7770af
      if (*at(i) < *rhs.at(i)) return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Compound_Selector_Ptr Simple_Selector::unify_with(Compound_Selector_Ptr rhs)
Packit Service 7770af
  {
Packit Service 7770af
    for (size_t i = 0, L = rhs->length(); i < L; ++i)
Packit Service 7770af
    { if (to_string() == rhs->at(i)->to_string()) return rhs; }
Packit Service 7770af
Packit Service 7770af
    // check for pseudo elements because they are always last
Packit Service 7770af
    size_t i, L;
Packit Service 7770af
    bool found = false;
Packit Service 7770af
    if (typeid(*this) == typeid(Pseudo_Selector) || typeid(*this) == typeid(Wrapped_Selector))
Packit Service 7770af
    {
Packit Service 7770af
      for (i = 0, L = rhs->length(); i < L; ++i)
Packit Service 7770af
      {
Packit Service 7770af
        if ((Cast<Pseudo_Selector>((*rhs)[i]) || Cast<Wrapped_Selector>((*rhs)[i])) && (*rhs)[L-1]->is_pseudo_element())
Packit Service 7770af
        { found = true; break; }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    else
Packit Service 7770af
    {
Packit Service 7770af
      for (i = 0, L = rhs->length(); i < L; ++i)
Packit Service 7770af
      {
Packit Service 7770af
        if (Cast<Pseudo_Selector>((*rhs)[i]) || Cast<Wrapped_Selector>((*rhs)[i]))
Packit Service 7770af
        { found = true; break; }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    if (!found)
Packit Service 7770af
    {
Packit Service 7770af
      rhs->append(this);
Packit Service 7770af
      return rhs;
Packit Service 7770af
    }
Packit Service 7770af
    rhs->elements().insert(rhs->elements().begin() + i, this);
Packit Service 7770af
    return rhs;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Simple_Selector_Ptr Element_Selector::unify_with(Simple_Selector_Ptr rhs)
Packit Service 7770af
  {
Packit Service 7770af
    // check if ns can be extended
Packit Service 7770af
    // true for no ns or universal
Packit Service 7770af
    if (has_universal_ns())
Packit Service 7770af
    {
Packit Service 7770af
      // but dont extend with universal
Packit Service 7770af
      // true for valid ns and universal
Packit Service 7770af
      if (!rhs->is_universal_ns())
Packit Service 7770af
      {
Packit Service 7770af
        // overwrite the name if star is given as name
Packit Service 7770af
        if (this->name() == "*") { this->name(rhs->name()); }
Packit Service 7770af
        // now overwrite the namespace name and flag
Packit Service 7770af
        this->ns(rhs->ns()); this->has_ns(rhs->has_ns());
Packit Service 7770af
        // return copy
Packit Service 7770af
        return this;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    // namespace may changed, check the name now
Packit Service 7770af
    // overwrite star (but not with another star)
Packit Service 7770af
    if (name() == "*" && rhs->name() != "*")
Packit Service 7770af
    {
Packit Service 7770af
      // simply set the new name
Packit Service 7770af
      this->name(rhs->name());
Packit Service 7770af
      // return copy
Packit Service 7770af
      return this;
Packit Service 7770af
    }
Packit Service 7770af
    // return original
Packit Service 7770af
    return this;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Compound_Selector_Ptr Element_Selector::unify_with(Compound_Selector_Ptr rhs)
Packit Service 7770af
  {
Packit Service 7770af
    // TODO: handle namespaces
Packit Service 7770af
Packit Service 7770af
    // if the rhs is empty, just return a copy of this
Packit Service 7770af
    if (rhs->length() == 0) {
Packit Service 7770af
      rhs->append(this);
Packit Service 7770af
      return rhs;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Simple_Selector_Ptr rhs_0 = rhs->at(0);
Packit Service 7770af
    // otherwise, this is a tag name
Packit Service 7770af
    if (name() == "*")
Packit Service 7770af
    {
Packit Service 7770af
      if (typeid(*rhs_0) == typeid(Element_Selector))
Packit Service 7770af
      {
Packit Service 7770af
        // if rhs is universal, just return this tagname + rhs's qualifiers
Packit Service 7770af
        Element_Selector_Ptr ts = Cast<Element_Selector>(rhs_0);
Packit Service 7770af
        rhs->at(0) = this->unify_with(ts);
Packit Service 7770af
        return rhs;
Packit Service 7770af
      }
Packit Service 7770af
      else if (Cast<Class_Selector>(rhs_0) || Cast<Id_Selector>(rhs_0)) {
Packit Service 7770af
        // qualifier is `.class`, so we can prefix with `ns|*.class`
Packit Service 7770af
        if (has_ns() && !rhs_0->has_ns()) {
Packit Service 7770af
          if (ns() != "*") rhs->elements().insert(rhs->begin(), this);
Packit Service 7770af
        }
Packit Service 7770af
        return rhs;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
      return rhs;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (typeid(*rhs_0) == typeid(Element_Selector))
Packit Service 7770af
    {
Packit Service 7770af
      // if rhs is universal, just return this tagname + rhs's qualifiers
Packit Service 7770af
      if (rhs_0->name() != "*" && rhs_0->ns() != "*" && rhs_0->name() != name()) return 0;
Packit Service 7770af
      // otherwise create new compound and unify first simple selector
Packit Service 7770af
      rhs->at(0) = this->unify_with(rhs_0);
Packit Service 7770af
      return rhs;
Packit Service 7770af
Packit Service 7770af
    }
Packit Service 7770af
    // else it's a tag name and a bunch of qualifiers -- just append them
Packit Service 7770af
    if (name() != "*") rhs->elements().insert(rhs->begin(), this);
Packit Service 7770af
    return rhs;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Compound_Selector_Ptr Class_Selector::unify_with(Compound_Selector_Ptr rhs)
Packit Service 7770af
  {
Packit Service 7770af
    rhs->has_line_break(has_line_break());
Packit Service 7770af
    return Simple_Selector::unify_with(rhs);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Compound_Selector_Ptr Id_Selector::unify_with(Compound_Selector_Ptr rhs)
Packit Service 7770af
  {
Packit Service 7770af
    for (size_t i = 0, L = rhs->length(); i < L; ++i)
Packit Service 7770af
    {
Packit Service 7770af
      if (Id_Selector_Ptr sel = Cast<Id_Selector>(rhs->at(i))) {
Packit Service 7770af
        if (sel->name() != name()) return 0;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    rhs->has_line_break(has_line_break());
Packit Service 7770af
    return Simple_Selector::unify_with(rhs);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Compound_Selector_Ptr Pseudo_Selector::unify_with(Compound_Selector_Ptr rhs)
Packit Service 7770af
  {
Packit Service 7770af
    if (is_pseudo_element())
Packit Service 7770af
    {
Packit Service 7770af
      for (size_t i = 0, L = rhs->length(); i < L; ++i)
Packit Service 7770af
      {
Packit Service 7770af
        if (Pseudo_Selector_Ptr sel = Cast<Pseudo_Selector>(rhs->at(i))) {
Packit Service 7770af
          if (sel->is_pseudo_element() && sel->name() != name()) return 0;
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return Simple_Selector::unify_with(rhs);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Attribute_Selector::operator< (const Attribute_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (is_ns_eq(rhs)) {
Packit Service 7770af
      if (name() == rhs.name()) {
Packit Service 7770af
        if (matcher() == rhs.matcher()) {
Packit Service 7770af
          bool no_lhs_val = value().isNull();
Packit Service 7770af
          bool no_rhs_val = rhs.value().isNull();
Packit Service 7770af
          if (no_lhs_val && no_rhs_val) return false; // equal
Packit Service 7770af
          else if (no_lhs_val) return true; // lhs is null
Packit Service 7770af
          else if (no_rhs_val) return false; // rhs is null
Packit Service 7770af
          return *value() < *rhs.value(); // both are given
Packit Service 7770af
        } else { return matcher() < rhs.matcher(); }
Packit Service 7770af
      } else { return name() < rhs.name(); }
Packit Service 7770af
    } else { return ns() < rhs.ns(); }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Attribute_Selector::operator< (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Attribute_Selector_Ptr_Const w = Cast<Attribute_Selector>(&rhs))
Packit Service 7770af
    {
Packit Service 7770af
      return *this < *w;
Packit Service 7770af
    }
Packit Service 7770af
    if (is_ns_eq(rhs))
Packit Service 7770af
    { return name() < rhs.name(); }
Packit Service 7770af
    return ns() < rhs.ns();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Attribute_Selector::operator== (const Attribute_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // get optional value state
Packit Service 7770af
    bool no_lhs_val = value().isNull();
Packit Service 7770af
    bool no_rhs_val = rhs.value().isNull();
Packit Service 7770af
    // both are null, therefore equal
Packit Service 7770af
    if (no_lhs_val && no_rhs_val) {
Packit Service 7770af
      return (name() == rhs.name())
Packit Service 7770af
        && (matcher() == rhs.matcher())
Packit Service 7770af
        && (is_ns_eq(rhs));
Packit Service 7770af
    }
Packit Service 7770af
    // both are defined, evaluate
Packit Service 7770af
    if (no_lhs_val == no_rhs_val) {
Packit Service 7770af
      return (name() == rhs.name())
Packit Service 7770af
        && (matcher() == rhs.matcher())
Packit Service 7770af
        && (is_ns_eq(rhs))
Packit Service 7770af
        && (*value() == *rhs.value());
Packit Service 7770af
    }
Packit Service 7770af
    // not equal
Packit Service 7770af
    return false;
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Attribute_Selector::operator== (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Attribute_Selector_Ptr_Const w = Cast<Attribute_Selector>(&rhs))
Packit Service 7770af
    {
Packit Service 7770af
      return is_ns_eq(rhs) &&
Packit Service 7770af
             name() == rhs.name() &&
Packit Service 7770af
             *this == *w;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Element_Selector::operator< (const Element_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (is_ns_eq(rhs))
Packit Service 7770af
    { return name() < rhs.name(); }
Packit Service 7770af
    return ns() < rhs.ns();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Element_Selector::operator< (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Element_Selector_Ptr_Const w = Cast<Element_Selector>(&rhs))
Packit Service 7770af
    {
Packit Service 7770af
      return *this < *w;
Packit Service 7770af
    }
Packit Service 7770af
    if (is_ns_eq(rhs))
Packit Service 7770af
    { return name() < rhs.name(); }
Packit Service 7770af
    return ns() < rhs.ns();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Element_Selector::operator== (const Element_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    return is_ns_eq(rhs) &&
Packit Service 7770af
           name() == rhs.name();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Element_Selector::operator== (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Element_Selector_Ptr_Const w = Cast<Element_Selector>(&rhs))
Packit Service 7770af
    {
Packit Service 7770af
      return is_ns_eq(rhs) &&
Packit Service 7770af
             name() == rhs.name() &&
Packit Service 7770af
             *this == *w;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Pseudo_Selector::operator== (const Pseudo_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (is_ns_eq(rhs) && name() == rhs.name())
Packit Service 7770af
    {
Packit Service 7770af
      String_Obj lhs_ex = expression();
Packit Service 7770af
      String_Obj rhs_ex = rhs.expression();
Packit Service 7770af
      if (rhs_ex && lhs_ex) return *lhs_ex == *rhs_ex;
Packit Service 7770af
      else return lhs_ex.ptr() == rhs_ex.ptr();
Packit Service 7770af
    }
Packit Service 7770af
    else return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Pseudo_Selector::operator== (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Pseudo_Selector_Ptr_Const w = Cast<Pseudo_Selector>(&rhs))
Packit Service 7770af
    {
Packit Service 7770af
      return *this == *w;
Packit Service 7770af
    }
Packit Service 7770af
    return is_ns_eq(rhs) &&
Packit Service 7770af
           name() == rhs.name();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Pseudo_Selector::operator< (const Pseudo_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (is_ns_eq(rhs) && name() == rhs.name())
Packit Service 7770af
    {
Packit Service 7770af
      String_Obj lhs_ex = expression();
Packit Service 7770af
      String_Obj rhs_ex = rhs.expression();
Packit Service 7770af
      if (rhs_ex && lhs_ex) return *lhs_ex < *rhs_ex;
Packit Service 7770af
      else return lhs_ex.ptr() < rhs_ex.ptr();
Packit Service 7770af
    }
Packit Service 7770af
    if (is_ns_eq(rhs))
Packit Service 7770af
    { return name() < rhs.name(); }
Packit Service 7770af
    return ns() < rhs.ns();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Pseudo_Selector::operator< (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Pseudo_Selector_Ptr_Const w = Cast<Pseudo_Selector>(&rhs))
Packit Service 7770af
    {
Packit Service 7770af
      return *this < *w;
Packit Service 7770af
    }
Packit Service 7770af
    if (is_ns_eq(rhs))
Packit Service 7770af
    { return name() < rhs.name(); }
Packit Service 7770af
    return ns() < rhs.ns();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Wrapped_Selector::operator== (const Wrapped_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (is_ns_eq(rhs) && name() == rhs.name())
Packit Service 7770af
    { return *(selector()) == *(rhs.selector()); }
Packit Service 7770af
    else return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Wrapped_Selector::operator== (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Wrapped_Selector_Ptr_Const w = Cast<Wrapped_Selector>(&rhs))
Packit Service 7770af
    {
Packit Service 7770af
      return *this == *w;
Packit Service 7770af
    }
Packit Service 7770af
    return is_ns_eq(rhs) &&
Packit Service 7770af
           name() == rhs.name();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Wrapped_Selector::operator< (const Wrapped_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (is_ns_eq(rhs) && name() == rhs.name())
Packit Service 7770af
    { return *(selector()) < *(rhs.selector()); }
Packit Service 7770af
    if (is_ns_eq(rhs))
Packit Service 7770af
    { return name() < rhs.name(); }
Packit Service 7770af
    return ns() < rhs.ns();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Wrapped_Selector::operator< (const Simple_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Wrapped_Selector_Ptr_Const w = Cast<Wrapped_Selector>(&rhs))
Packit Service 7770af
    {
Packit Service 7770af
      return *this < *w;
Packit Service 7770af
    }
Packit Service 7770af
    if (is_ns_eq(rhs))
Packit Service 7770af
    { return name() < rhs.name(); }
Packit Service 7770af
    return ns() < rhs.ns();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Wrapped_Selector::is_superselector_of(Wrapped_Selector_Obj sub)
Packit Service 7770af
  {
Packit Service 7770af
    if (this->name() != sub->name()) return false;
Packit Service 7770af
    if (this->name() == ":current") return false;
Packit Service 7770af
    if (Selector_List_Obj rhs_list = Cast<Selector_List>(sub->selector())) {
Packit Service 7770af
      if (Selector_List_Obj lhs_list = Cast<Selector_List>(selector())) {
Packit Service 7770af
        return lhs_list->is_superselector_of(rhs_list);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    error("is_superselector expected a Selector_List", sub->pstate());
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::is_superselector_of(Selector_List_Obj rhs, std::string wrapped)
Packit Service 7770af
  {
Packit Service 7770af
    for (Complex_Selector_Obj item : rhs->elements()) {
Packit Service 7770af
      if (is_superselector_of(item, wrapped)) return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::is_superselector_of(Complex_Selector_Obj rhs, std::string wrapped)
Packit Service 7770af
  {
Packit Service 7770af
    if (rhs->head()) return is_superselector_of(rhs->head(), wrapped);
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::is_superselector_of(Compound_Selector_Obj rhs, std::string wrapping)
Packit Service 7770af
  {
Packit Service 7770af
    Compound_Selector_Ptr lhs = this;
Packit Service 7770af
    Simple_Selector_Ptr lbase = lhs->base();
Packit Service 7770af
    Simple_Selector_Ptr rbase = rhs->base();
Packit Service 7770af
Packit Service 7770af
    // Check if pseudo-elements are the same between the selectors
Packit Service 7770af
Packit Service 7770af
    std::set<std::string> lpsuedoset, rpsuedoset;
Packit Service 7770af
    for (size_t i = 0, L = length(); i < L; ++i)
Packit Service 7770af
    {
Packit Service 7770af
      if ((*this)[i]->is_pseudo_element()) {
Packit Service 7770af
        std::string pseudo((*this)[i]->to_string());
Packit Service 7770af
        pseudo = pseudo.substr(pseudo.find_first_not_of(":")); // strip off colons to ensure :after matches ::after since ruby sass is forgiving
Packit Service 7770af
        lpsuedoset.insert(pseudo);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    for (size_t i = 0, L = rhs->length(); i < L; ++i)
Packit Service 7770af
    {
Packit Service 7770af
      if ((*rhs)[i]->is_pseudo_element()) {
Packit Service 7770af
        std::string pseudo((*rhs)[i]->to_string());
Packit Service 7770af
        pseudo = pseudo.substr(pseudo.find_first_not_of(":")); // strip off colons to ensure :after matches ::after since ruby sass is forgiving
Packit Service 7770af
        rpsuedoset.insert(pseudo);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    if (lpsuedoset != rpsuedoset) {
Packit Service 7770af
      return false;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // would like to replace this without stringification
Packit Service 7770af
    // https://github.com/sass/sass/issues/2229
Packit Service 7770af
    // SimpleSelectorSet lset, rset;
Packit Service 7770af
    std::set<std::string> lset, rset;
Packit Service 7770af
Packit Service 7770af
    if (lbase && rbase)
Packit Service 7770af
    {
Packit Service 7770af
      if (lbase->to_string() == rbase->to_string()) {
Packit Service 7770af
        for (size_t i = 1, L = length(); i < L; ++i)
Packit Service 7770af
        { lset.insert((*this)[i]->to_string()); }
Packit Service 7770af
        for (size_t i = 1, L = rhs->length(); i < L; ++i)
Packit Service 7770af
        { rset.insert((*rhs)[i]->to_string()); }
Packit Service 7770af
        return includes(rset.begin(), rset.end(), lset.begin(), lset.end());
Packit Service 7770af
      }
Packit Service 7770af
      return false;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    for (size_t i = 0, iL = length(); i < iL; ++i)
Packit Service 7770af
    {
Packit Service 7770af
      Selector_Obj wlhs = (*this)[i];
Packit Service 7770af
      // very special case for wrapped matches selector
Packit Service 7770af
      if (Wrapped_Selector_Obj wrapped = Cast<Wrapped_Selector>(wlhs)) {
Packit Service 7770af
        if (wrapped->name() == ":not") {
Packit Service 7770af
          if (Selector_List_Obj not_list = Cast<Selector_List>(wrapped->selector())) {
Packit Service 7770af
            if (not_list->is_superselector_of(rhs, wrapped->name())) return false;
Packit Service 7770af
          } else {
Packit Service 7770af
            throw std::runtime_error("wrapped not selector is not a list");
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        if (wrapped->name() == ":matches" || wrapped->name() == ":-moz-any") {
Packit Service 7770af
          wlhs = wrapped->selector();
Packit Service 7770af
          if (Selector_List_Obj list = Cast<Selector_List>(wrapped->selector())) {
Packit Service 7770af
            if (Compound_Selector_Obj comp = Cast<Compound_Selector>(rhs)) {
Packit Service 7770af
              if (!wrapping.empty() && wrapping != wrapped->name()) return false;
Packit Service 7770af
              if (wrapping.empty() || wrapping != wrapped->name()) {;
Packit Service 7770af
                if (list->is_superselector_of(comp, wrapped->name())) return true;
Packit Service 7770af
              }
Packit Service 7770af
            }
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        Simple_Selector_Ptr rhs_sel = NULL;
Packit Service 7770af
        if (rhs->elements().size() > i) rhs_sel = (*rhs)[i];
Packit Service 7770af
        if (Wrapped_Selector_Ptr wrapped_r = Cast<Wrapped_Selector>(rhs_sel)) {
Packit Service 7770af
          if (wrapped->name() == wrapped_r->name()) {
Packit Service 7770af
          if (wrapped->is_superselector_of(wrapped_r)) {
Packit Service 7770af
             continue;
Packit Service 7770af
          }}
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      // match from here on as strings
Packit Service 7770af
      lset.insert(wlhs->to_string());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    for (size_t n = 0, nL = rhs->length(); n < nL; ++n)
Packit Service 7770af
    {
Packit Service 7770af
      Selector_Obj r = (*rhs)[n];
Packit Service 7770af
      if (Wrapped_Selector_Obj wrapped = Cast<Wrapped_Selector>(r)) {
Packit Service 7770af
        if (wrapped->name() == ":not") {
Packit Service 7770af
          if (Selector_List_Obj ls = Cast<Selector_List>(wrapped->selector())) {
Packit Service 7770af
            ls->remove_parent_selectors();
Packit Service 7770af
            if (is_superselector_of(ls, wrapped->name())) return false;
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        if (wrapped->name() == ":matches" || wrapped->name() == ":-moz-any") {
Packit Service 7770af
          if (!wrapping.empty()) {
Packit Service 7770af
            if (wrapping != wrapped->name()) return false;
Packit Service 7770af
          }
Packit Service 7770af
          if (Selector_List_Obj ls = Cast<Selector_List>(wrapped->selector())) {
Packit Service 7770af
            ls->remove_parent_selectors();
Packit Service 7770af
            return (is_superselector_of(ls, wrapped->name()));
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      rset.insert(r->to_string());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    //for (auto l : lset) { cerr << "l: " << l << endl; }
Packit Service 7770af
    //for (auto r : rset) { cerr << "r: " << r << endl; }
Packit Service 7770af
Packit Service 7770af
    if (lset.empty()) return true;
Packit Service 7770af
    // return true if rset contains all the elements of lset
Packit Service 7770af
    return includes(rset.begin(), rset.end(), lset.begin(), lset.end());
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // create complex selector (ancestor of) from compound selector
Packit Service 7770af
  Complex_Selector_Obj Compound_Selector::to_complex()
Packit Service 7770af
  {
Packit Service 7770af
    // create an intermediate complex selector
Packit Service 7770af
    return SASS_MEMORY_NEW(Complex_Selector,
Packit Service 7770af
                           pstate(),
Packit Service 7770af
                           Complex_Selector::ANCESTOR_OF,
Packit Service 7770af
                           this,
Packit Service 7770af
                           0);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Selector_List_Ptr Complex_Selector::unify_with(Complex_Selector_Ptr other)
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    // get last tails (on the right side)
Packit Service 7770af
    Complex_Selector_Obj l_last = this->last();
Packit Service 7770af
    Complex_Selector_Obj r_last = other->last();
Packit Service 7770af
Packit Service 7770af
    // check valid pointers (assertion)
Packit Service 7770af
    SASS_ASSERT(l_last, "lhs is null");
Packit Service 7770af
    SASS_ASSERT(r_last, "rhs is null");
Packit Service 7770af
Packit Service 7770af
    // Not sure about this check, but closest way I could check
Packit Service 7770af
    // was to see if this is a ruby 'SimpleSequence' equivalent.
Packit Service 7770af
    // It seems to do the job correctly as some specs react to this
Packit Service 7770af
    if (l_last->combinator() != Combinator::ANCESTOR_OF) return 0;
Packit Service 7770af
    if (r_last->combinator() != Combinator::ANCESTOR_OF ) return 0;
Packit Service 7770af
Packit Service 7770af
    // get the headers for the last tails
Packit Service 7770af
    Compound_Selector_Obj l_last_head = l_last->head();
Packit Service 7770af
    Compound_Selector_Obj r_last_head = r_last->head();
Packit Service 7770af
Packit Service 7770af
    // check valid head pointers (assertion)
Packit Service 7770af
    SASS_ASSERT(l_last_head, "lhs head is null");
Packit Service 7770af
    SASS_ASSERT(r_last_head, "rhs head is null");
Packit Service 7770af
Packit Service 7770af
    // get the unification of the last compound selectors
Packit Service 7770af
    Compound_Selector_Obj unified = r_last_head->unify_with(l_last_head);
Packit Service 7770af
Packit Service 7770af
    // abort if we could not unify heads
Packit Service 7770af
    if (unified == 0) return 0;
Packit Service 7770af
Packit Service 7770af
    // check for universal (star: `*`) selector
Packit Service 7770af
    bool is_universal = l_last_head->is_universal() ||
Packit Service 7770af
                        r_last_head->is_universal();
Packit Service 7770af
Packit Service 7770af
    if (is_universal)
Packit Service 7770af
    {
Packit Service 7770af
      // move the head
Packit Service 7770af
      l_last->head(0);
Packit Service 7770af
      r_last->head(unified);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // create nodes from both selectors
Packit Service 7770af
    Node lhsNode = complexSelectorToNode(this);
Packit Service 7770af
    Node rhsNode = complexSelectorToNode(other);
Packit Service 7770af
Packit Service 7770af
    // overwrite universal base
Packit Service 7770af
    if (!is_universal)
Packit Service 7770af
    {
Packit Service 7770af
      // create some temporaries to convert to node
Packit Service 7770af
      Complex_Selector_Obj fake = unified->to_complex();
Packit Service 7770af
      Node unified_node = complexSelectorToNode(fake);
Packit Service 7770af
      // add to permutate the list?
Packit Service 7770af
      rhsNode.plus(unified_node);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // do some magic we inherit from node and extend
Packit Service 7770af
    Node node = subweave(lhsNode, rhsNode);
Packit Service 7770af
    Selector_List_Obj result = SASS_MEMORY_NEW(Selector_List, pstate());
Packit Service 7770af
    NodeDequePtr col = node.collection(); // move from collection to list
Packit Service 7770af
    for (NodeDeque::iterator it = col->begin(), end = col->end(); it != end; it++)
Packit Service 7770af
    { result->append(nodeToComplexSelector(Node::naiveTrim(*it))); }
Packit Service 7770af
Packit Service 7770af
    // only return if list has some entries
Packit Service 7770af
    return result->length() ? result.detach() : 0;
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Compound_Selector::operator== (const Compound_Selector& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    // for array access
Packit Service 7770af
    size_t i = 0, n = 0;
Packit Service 7770af
    size_t iL = length();
Packit Service 7770af
    size_t nL = rhs.length();
Packit Service 7770af
    // create temporary vectors and sort them
Packit Service 7770af
    std::vector<Simple_Selector_Obj> l_lst = this->elements();
Packit Service 7770af
    std::vector<Simple_Selector_Obj> r_lst = rhs.elements();
Packit Service 7770af
    std::sort(l_lst.begin(), l_lst.end(), OrderNodes());
Packit Service 7770af
    std::sort(r_lst.begin(), r_lst.end(), OrderNodes());
Packit Service 7770af
    // process loop
Packit Service 7770af
    while (true)
Packit Service 7770af
    {
Packit Service 7770af
      // first check for valid index
Packit Service 7770af
      if (i == iL) return iL == nL;
Packit Service 7770af
      else if (n == nL) return iL == nL;
Packit Service 7770af
      // the access the vector items
Packit Service 7770af
      Simple_Selector_Obj l = l_lst[i];
Packit Service 7770af
      Simple_Selector_Obj r = r_lst[n];
Packit Service 7770af
      // skip nulls
Packit Service 7770af
      if (!l) ++i;
Packit Service 7770af
      if (!r) ++n;
Packit Service 7770af
      // do the check now
Packit Service 7770af
      else if (*l != *r)
Packit Service 7770af
      { return false; }
Packit Service 7770af
      // advance now
Packit Service 7770af
      ++i; ++n;
Packit Service 7770af
    }
Packit Service 7770af
    // there is no break?!
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::is_superselector_of(Compound_Selector_Obj rhs, std::string wrapping)
Packit Service 7770af
  {
Packit Service 7770af
    return last()->head() && last()->head()->is_superselector_of(rhs, wrapping);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Complex_Selector::is_superselector_of(Complex_Selector_Obj rhs, std::string wrapping)
Packit Service 7770af
  {
Packit Service 7770af
    Complex_Selector_Ptr lhs = this;
Packit Service 7770af
    // check for selectors with leading or trailing combinators
Packit Service 7770af
    if (!lhs->head() || !rhs->head())
Packit Service 7770af
    { return false; }
Packit Service 7770af
    Complex_Selector_Obj l_innermost = lhs->innermost();
Packit Service 7770af
    if (l_innermost->combinator() != Complex_Selector::ANCESTOR_OF)
Packit Service 7770af
    { return false; }
Packit Service 7770af
    Complex_Selector_Obj r_innermost = rhs->innermost();
Packit Service 7770af
    if (r_innermost->combinator() != Complex_Selector::ANCESTOR_OF)
Packit Service 7770af
    { return false; }
Packit Service 7770af
    // more complex (i.e., longer) selectors are always more specific
Packit Service 7770af
    size_t l_len = lhs->length(), r_len = rhs->length();
Packit Service 7770af
    if (l_len > r_len)
Packit Service 7770af
    { return false; }
Packit Service 7770af
Packit Service 7770af
    if (l_len == 1)
Packit Service 7770af
    { return lhs->head()->is_superselector_of(rhs->last()->head(), wrapping); }
Packit Service 7770af
Packit Service 7770af
    // we have to look one tail deeper, since we cary the
Packit Service 7770af
    // combinator around for it (which is important here)
Packit Service 7770af
    if (rhs->tail() && lhs->tail() && combinator() != Complex_Selector::ANCESTOR_OF) {
Packit Service 7770af
      Complex_Selector_Obj lhs_tail = lhs->tail();
Packit Service 7770af
      Complex_Selector_Obj rhs_tail = rhs->tail();
Packit Service 7770af
      if (lhs_tail->combinator() != rhs_tail->combinator()) return false;
Packit Service 7770af
      if (lhs_tail->head() && !rhs_tail->head()) return false;
Packit Service 7770af
      if (!lhs_tail->head() && rhs_tail->head()) return false;
Packit Service 7770af
      if (lhs_tail->head() && rhs_tail->head()) {
Packit Service 7770af
        if (!lhs_tail->head()->is_superselector_of(rhs_tail->head())) return false;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    bool found = false;
Packit Service 7770af
    Complex_Selector_Obj marker = rhs;
Packit Service 7770af
    for (size_t i = 0, L = rhs->length(); i < L; ++i) {
Packit Service 7770af
      if (i == L-1)
Packit Service 7770af
      { return false; }
Packit Service 7770af
      if (lhs->head() && marker->head() && lhs->head()->is_superselector_of(marker->head(), wrapping))
Packit Service 7770af
      { found = true; break; }
Packit Service 7770af
      marker = marker->tail();
Packit Service 7770af
    }
Packit Service 7770af
    if (!found)
Packit Service 7770af
    { return false; }
Packit Service 7770af
Packit Service 7770af
    /*
Packit Service 7770af
      Hmm, I hope I have the logic right:
Packit Service 7770af
Packit Service 7770af
      if lhs has a combinator:
Packit Service 7770af
        if !(marker has a combinator) return false
Packit Service 7770af
        if !(lhs.combinator == '~' ? marker.combinator != '>' : lhs.combinator == marker.combinator) return false
Packit Service 7770af
        return lhs.tail-without-innermost.is_superselector_of(marker.tail-without-innermost)
Packit Service 7770af
      else if marker has a combinator:
Packit Service 7770af
        if !(marker.combinator == ">") return false
Packit Service 7770af
        return lhs.tail.is_superselector_of(marker.tail)
Packit Service 7770af
      else
Packit Service 7770af
        return lhs.tail.is_superselector_of(marker.tail)
Packit Service 7770af
    */
Packit Service 7770af
    if (lhs->combinator() != Complex_Selector::ANCESTOR_OF)
Packit Service 7770af
    {
Packit Service 7770af
      if (marker->combinator() == Complex_Selector::ANCESTOR_OF)
Packit Service 7770af
      { return false; }
Packit Service 7770af
      if (!(lhs->combinator() == Complex_Selector::PRECEDES ? marker->combinator() != Complex_Selector::PARENT_OF : lhs->combinator() == marker->combinator()))
Packit Service 7770af
      { return false; }
Packit Service 7770af
      return lhs->tail()->is_superselector_of(marker->tail());
Packit Service 7770af
    }
Packit Service 7770af
    else if (marker->combinator() != Complex_Selector::ANCESTOR_OF)
Packit Service 7770af
    {
Packit Service 7770af
      if (marker->combinator() != Complex_Selector::PARENT_OF)
Packit Service 7770af
      { return false; }
Packit Service 7770af
      return lhs->tail()->is_superselector_of(marker->tail());
Packit Service 7770af
    }
Packit Service 7770af
    return lhs->tail()->is_superselector_of(marker->tail());
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  size_t Complex_Selector::length() const
Packit Service 7770af
  {
Packit Service 7770af
    // TODO: make this iterative
Packit Service 7770af
    if (!tail()) return 1;
Packit Service 7770af
    return 1 + tail()->length();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // append another complex selector at the end
Packit Service 7770af
  // check if we need to append some headers
Packit Service 7770af
  // then we need to check for the combinator
Packit Service 7770af
  // only then we can safely set the new tail
Packit Service 7770af
  void Complex_Selector::append(Complex_Selector_Obj ss)
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    Complex_Selector_Obj t = ss->tail();
Packit Service 7770af
    Combinator c = ss->combinator();
Packit Service 7770af
    String_Obj r = ss->reference();
Packit Service 7770af
    Compound_Selector_Obj h = ss->head();
Packit Service 7770af
Packit Service 7770af
    if (ss->has_line_feed()) has_line_feed(true);
Packit Service 7770af
    if (ss->has_line_break()) has_line_break(true);
Packit Service 7770af
Packit Service 7770af
    // append old headers
Packit Service 7770af
    if (h && h->length()) {
Packit Service 7770af
      if (last()->combinator() != ANCESTOR_OF && c != ANCESTOR_OF) {
Packit Service 7770af
        error("Invalid parent selector", pstate_);
Packit Service 7770af
      } else if (last()->head_ && last()->head_->length()) {
Packit Service 7770af
        Compound_Selector_Obj rh = last()->head();
Packit Service 7770af
        size_t i;
Packit Service 7770af
        size_t L = h->length();
Packit Service 7770af
        if (Cast<Element_Selector>(h->first())) {
Packit Service 7770af
          if (Class_Selector_Ptr cs = Cast<Class_Selector>(rh->last())) {
Packit Service 7770af
            Class_Selector_Ptr sqs = SASS_MEMORY_COPY(cs);
Packit Service 7770af
            sqs->name(sqs->name() + (*h)[0]->name());
Packit Service 7770af
            sqs->pstate((*h)[0]->pstate());
Packit Service 7770af
            (*rh)[rh->length()-1] = sqs;
Packit Service 7770af
            rh->pstate(h->pstate());
Packit Service 7770af
            for (i = 1; i < L; ++i) rh->append((*h)[i]);
Packit Service 7770af
          } else if (Id_Selector_Ptr is = Cast<Id_Selector>(rh->last())) {
Packit Service 7770af
            Id_Selector_Ptr sqs = SASS_MEMORY_COPY(is);
Packit Service 7770af
            sqs->name(sqs->name() + (*h)[0]->name());
Packit Service 7770af
            sqs->pstate((*h)[0]->pstate());
Packit Service 7770af
            (*rh)[rh->length()-1] = sqs;
Packit Service 7770af
            rh->pstate(h->pstate());
Packit Service 7770af
            for (i = 1; i < L; ++i) rh->append((*h)[i]);
Packit Service 7770af
          } else if (Element_Selector_Ptr ts = Cast<Element_Selector>(rh->last())) {
Packit Service 7770af
            Element_Selector_Ptr tss = SASS_MEMORY_COPY(ts);
Packit Service 7770af
            tss->name(tss->name() + (*h)[0]->name());
Packit Service 7770af
            tss->pstate((*h)[0]->pstate());
Packit Service 7770af
            (*rh)[rh->length()-1] = tss;
Packit Service 7770af
            rh->pstate(h->pstate());
Packit Service 7770af
            for (i = 1; i < L; ++i) rh->append((*h)[i]);
Packit Service 7770af
          } else if (Placeholder_Selector_Ptr ps = Cast<Placeholder_Selector>(rh->last())) {
Packit Service 7770af
            Placeholder_Selector_Ptr pss = SASS_MEMORY_COPY(ps);
Packit Service 7770af
            pss->name(pss->name() + (*h)[0]->name());
Packit Service 7770af
            pss->pstate((*h)[0]->pstate());
Packit Service 7770af
            (*rh)[rh->length()-1] = pss;
Packit Service 7770af
            rh->pstate(h->pstate());
Packit Service 7770af
            for (i = 1; i < L; ++i) rh->append((*h)[i]);
Packit Service 7770af
          } else {
Packit Service 7770af
            last()->head_->concat(h);
Packit Service 7770af
          }
Packit Service 7770af
        } else {
Packit Service 7770af
          last()->head_->concat(h);
Packit Service 7770af
        }
Packit Service 7770af
      } else {
Packit Service 7770af
        last()->head_->concat(h);
Packit Service 7770af
      }
Packit Service 7770af
    } else {
Packit Service 7770af
      // std::cerr << "has no or empty head\n";
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    if (last()) {
Packit Service 7770af
      if (last()->combinator() != ANCESTOR_OF && c != ANCESTOR_OF) {
Packit Service 7770af
        Complex_Selector_Ptr inter = SASS_MEMORY_NEW(Complex_Selector, pstate());
Packit Service 7770af
        inter->reference(r);
Packit Service 7770af
        inter->combinator(c);
Packit Service 7770af
        inter->tail(t);
Packit Service 7770af
        last()->tail(inter);
Packit Service 7770af
      } else {
Packit Service 7770af
        if (last()->combinator() == ANCESTOR_OF) {
Packit Service 7770af
          last()->combinator(c);
Packit Service 7770af
          last()->reference(r);
Packit Service 7770af
        }
Packit Service 7770af
        last()->tail(t);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Selector_List_Obj Selector_List::eval(Eval& eval)
Packit Service 7770af
  {
Packit Service 7770af
    Selector_List_Obj list = schema() ?
Packit Service 7770af
      eval(schema()) : eval(this);
Packit Service 7770af
    list->schema(schema());
Packit Service 7770af
    return list;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Selector_List_Ptr Selector_List::resolve_parent_refs(std::vector<Selector_List_Obj>& pstack, bool implicit_parent)
Packit Service 7770af
  {
Packit Service 7770af
    if (!this->has_parent_ref()) return this;
Packit Service 7770af
    Selector_List_Ptr ss = SASS_MEMORY_NEW(Selector_List, pstate());
Packit Service 7770af
    Selector_List_Ptr ps = pstack.back();
Packit Service 7770af
    for (size_t pi = 0, pL = ps->length(); pi < pL; ++pi) {
Packit Service 7770af
      for (size_t si = 0, sL = this->length(); si < sL; ++si) {
Packit Service 7770af
        Selector_List_Obj rv = at(si)->resolve_parent_refs(pstack, implicit_parent);
Packit Service 7770af
        ss->concat(rv);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return ss;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Selector_List_Ptr Complex_Selector::resolve_parent_refs(std::vector<Selector_List_Obj>& pstack, bool implicit_parent)
Packit Service 7770af
  {
Packit Service 7770af
    Complex_Selector_Obj tail = this->tail();
Packit Service 7770af
    Compound_Selector_Obj head = this->head();
Packit Service 7770af
    Selector_List_Ptr parents = pstack.back();
Packit Service 7770af
Packit Service 7770af
    if (!this->has_real_parent_ref() && !implicit_parent) {
Packit Service 7770af
      Selector_List_Ptr retval = SASS_MEMORY_NEW(Selector_List, pstate());
Packit Service 7770af
      retval->append(this);
Packit Service 7770af
      return retval;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // first resolve_parent_refs the tail (which may return an expanded list)
Packit Service 7770af
    Selector_List_Obj tails = tail ? tail->resolve_parent_refs(pstack, implicit_parent) : 0;
Packit Service 7770af
Packit Service 7770af
    if (head && head->length() > 0) {
Packit Service 7770af
Packit Service 7770af
      Selector_List_Obj retval;
Packit Service 7770af
      // we have a parent selector in a simple compound list
Packit Service 7770af
      // mix parent complex selector into the compound list
Packit Service 7770af
      if (Cast<Parent_Selector>((*head)[0])) {
Packit Service 7770af
        retval = SASS_MEMORY_NEW(Selector_List, pstate());
Packit Service 7770af
Packit Service 7770af
        // it turns out that real parent references reach
Packit Service 7770af
        // across @at-root rules, which comes unexpected
Packit Service 7770af
        if (parents == NULL && head->has_real_parent_ref()) {
Packit Service 7770af
          int i = pstack.size() - 1;
Packit Service 7770af
          while (!parents && i > -1) {
Packit Service 7770af
            parents = pstack.at(i--);
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        if (parents && parents->length()) {
Packit Service 7770af
          if (tails && tails->length() > 0) {
Packit Service 7770af
            for (size_t n = 0, nL = tails->length(); n < nL; ++n) {
Packit Service 7770af
              for (size_t i = 0, iL = parents->length(); i < iL; ++i) {
Packit Service 7770af
                Complex_Selector_Obj t = (*tails)[n];
Packit Service 7770af
                Complex_Selector_Obj parent = (*parents)[i];
Packit Service 7770af
                Complex_Selector_Obj s = SASS_MEMORY_CLONE(parent);
Packit Service 7770af
                Complex_Selector_Obj ss = SASS_MEMORY_CLONE(this);
Packit Service 7770af
                ss->tail(t ? SASS_MEMORY_CLONE(t) : NULL);
Packit Service 7770af
                Compound_Selector_Obj h = SASS_MEMORY_COPY(head_);
Packit Service 7770af
                // remove parent selector from sequence
Packit Service 7770af
                if (h->length()) {
Packit Service 7770af
                  h->erase(h->begin());
Packit Service 7770af
                  ss->head(h);
Packit Service 7770af
                } else {
Packit Service 7770af
                  ss->head(NULL);
Packit Service 7770af
                }
Packit Service 7770af
                // adjust for parent selector (1 char)
Packit Service 7770af
                if (h->length()) {
Packit Service 7770af
                  ParserState state(h->at(0)->pstate());
Packit Service 7770af
                  state.offset.column += 1;
Packit Service 7770af
                  state.column -= 1;
Packit Service 7770af
                  (*h)[0]->pstate(state);
Packit Service 7770af
                }
Packit Service 7770af
                // keep old parser state
Packit Service 7770af
                s->pstate(pstate());
Packit Service 7770af
                // append new tail
Packit Service 7770af
                s->append(ss);
Packit Service 7770af
                retval->append(s);
Packit Service 7770af
              }
Packit Service 7770af
            }
Packit Service 7770af
          }
Packit Service 7770af
          // have no tails but parents
Packit Service 7770af
          // loop above is inside out
Packit Service 7770af
          else {
Packit Service 7770af
            for (size_t i = 0, iL = parents->length(); i < iL; ++i) {
Packit Service 7770af
              Complex_Selector_Obj parent = (*parents)[i];
Packit Service 7770af
              Complex_Selector_Obj s = SASS_MEMORY_CLONE(parent);
Packit Service 7770af
              Complex_Selector_Obj ss = SASS_MEMORY_CLONE(this);
Packit Service 7770af
              // this is only if valid if the parent has no trailing op
Packit Service 7770af
              // otherwise we cannot append more simple selectors to head
Packit Service 7770af
              if (parent->last()->combinator() != ANCESTOR_OF) {
Packit Service 7770af
                throw Exception::InvalidParent(parent, ss);
Packit Service 7770af
              }
Packit Service 7770af
              ss->tail(tail ? SASS_MEMORY_CLONE(tail) : NULL);
Packit Service 7770af
              Compound_Selector_Obj h = SASS_MEMORY_COPY(head_);
Packit Service 7770af
              // remove parent selector from sequence
Packit Service 7770af
              if (h->length()) {
Packit Service 7770af
                h->erase(h->begin());
Packit Service 7770af
                ss->head(h);
Packit Service 7770af
              } else {
Packit Service 7770af
                ss->head(NULL);
Packit Service 7770af
              }
Packit Service 7770af
              // \/ IMO ruby sass bug \/
Packit Service 7770af
              ss->has_line_feed(false);
Packit Service 7770af
              // adjust for parent selector (1 char)
Packit Service 7770af
              if (h->length()) {
Packit Service 7770af
                ParserState state(h->at(0)->pstate());
Packit Service 7770af
                state.offset.column += 1;
Packit Service 7770af
                state.column -= 1;
Packit Service 7770af
                (*h)[0]->pstate(state);
Packit Service 7770af
              }
Packit Service 7770af
              // keep old parser state
Packit Service 7770af
              s->pstate(pstate());
Packit Service 7770af
              // append new tail
Packit Service 7770af
              s->append(ss);
Packit Service 7770af
              retval->append(s);
Packit Service 7770af
            }
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        // have no parent but some tails
Packit Service 7770af
        else {
Packit Service 7770af
          if (tails && tails->length() > 0) {
Packit Service 7770af
            for (size_t n = 0, nL = tails->length(); n < nL; ++n) {
Packit Service 7770af
              Complex_Selector_Obj cpy = SASS_MEMORY_CLONE(this);
Packit Service 7770af
              cpy->tail(SASS_MEMORY_CLONE(tails->at(n)));
Packit Service 7770af
              cpy->head(SASS_MEMORY_NEW(Compound_Selector, head->pstate()));
Packit Service 7770af
              for (size_t i = 1, L = this->head()->length(); i < L; ++i)
Packit Service 7770af
                cpy->head()->append((*this->head())[i]);
Packit Service 7770af
              if (!cpy->head()->length()) cpy->head(0);
Packit Service 7770af
              retval->append(cpy->skip_empty_reference());
Packit Service 7770af
            }
Packit Service 7770af
          }
Packit Service 7770af
          // have no parent nor tails
Packit Service 7770af
          else {
Packit Service 7770af
            Complex_Selector_Obj cpy = SASS_MEMORY_CLONE(this);
Packit Service 7770af
            cpy->head(SASS_MEMORY_NEW(Compound_Selector, head->pstate()));
Packit Service 7770af
            for (size_t i = 1, L = this->head()->length(); i < L; ++i)
Packit Service 7770af
              cpy->head()->append((*this->head())[i]);
Packit Service 7770af
            if (!cpy->head()->length()) cpy->head(0);
Packit Service 7770af
            retval->append(cpy->skip_empty_reference());
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      // no parent selector in head
Packit Service 7770af
      else {
Packit Service 7770af
        retval = this->tails(tails);
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      for (Simple_Selector_Obj ss : head->elements()) {
Packit Service 7770af
        if (Wrapped_Selector_Ptr ws = Cast<Wrapped_Selector>(ss)) {
Packit Service 7770af
          if (Selector_List_Ptr sl = Cast<Selector_List>(ws->selector())) {
Packit Service 7770af
            if (parents) ws->selector(sl->resolve_parent_refs(pstack, implicit_parent));
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      return retval.detach();
Packit Service 7770af
Packit Service 7770af
    }
Packit Service 7770af
    // has no head
Packit Service 7770af
    return this->tails(tails);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Selector_List_Ptr Complex_Selector::tails(Selector_List_Ptr tails)
Packit Service 7770af
  {
Packit Service 7770af
    Selector_List_Ptr rv = SASS_MEMORY_NEW(Selector_List, pstate_);
Packit Service 7770af
    if (tails && tails->length()) {
Packit Service 7770af
      for (size_t i = 0, iL = tails->length(); i < iL; ++i) {
Packit Service 7770af
        Complex_Selector_Obj pr = SASS_MEMORY_CLONE(this);
Packit Service 7770af
        pr->tail(tails->at(i));
Packit Service 7770af
        rv->append(pr);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    else {
Packit Service 7770af
      rv->append(this);
Packit Service 7770af
    }
Packit Service 7770af
    return rv;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // return the last tail that is defined
Packit Service 7770af
  Complex_Selector_Obj Complex_Selector::first()
Packit Service 7770af
  {
Packit Service 7770af
    // declare variables used in loop
Packit Service 7770af
    Complex_Selector_Obj cur = this;
Packit Service 7770af
    Compound_Selector_Obj head;
Packit Service 7770af
    // processing loop
Packit Service 7770af
    while (cur)
Packit Service 7770af
    {
Packit Service 7770af
      // get the head
Packit Service 7770af
      head = cur->head_;
Packit Service 7770af
      // abort (and return) if it is not a parent selector
Packit Service 7770af
      if (!head || head->length() != 1 || !Cast<Parent_Selector>((*head)[0])) {
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
      // advance to next
Packit Service 7770af
      cur = cur->tail_;
Packit Service 7770af
    }
Packit Service 7770af
    // result
Packit Service 7770af
    return cur;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // return the last tail that is defined
Packit Service 7770af
  Complex_Selector_Obj Complex_Selector::last()
Packit Service 7770af
  {
Packit Service 7770af
    Complex_Selector_Ptr cur = this;
Packit Service 7770af
    Complex_Selector_Ptr nxt = cur;
Packit Service 7770af
    // loop until last
Packit Service 7770af
    while (nxt) {
Packit Service 7770af
      cur = nxt;
Packit Service 7770af
      nxt = cur->tail();
Packit Service 7770af
    }
Packit Service 7770af
    return cur;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Complex_Selector::Combinator Complex_Selector::clear_innermost()
Packit Service 7770af
  {
Packit Service 7770af
    Combinator c;
Packit Service 7770af
    if (!tail() || tail()->tail() == 0)
Packit Service 7770af
    { c = combinator(); combinator(ANCESTOR_OF); tail(0); }
Packit Service 7770af
    else
Packit Service 7770af
    { c = tail()->clear_innermost(); }
Packit Service 7770af
    return c;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Complex_Selector::set_innermost(Complex_Selector_Obj val, Combinator c)
Packit Service 7770af
  {
Packit Service 7770af
    if (!tail())
Packit Service 7770af
    { tail(val); combinator(c); }
Packit Service 7770af
    else
Packit Service 7770af
    { tail()->set_innermost(val, c); }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Complex_Selector::cloneChildren()
Packit Service 7770af
  {
Packit Service 7770af
    if (head()) head(SASS_MEMORY_CLONE(head()));
Packit Service 7770af
    if (tail()) tail(SASS_MEMORY_CLONE(tail()));
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Compound_Selector::cloneChildren()
Packit Service 7770af
  {
Packit Service 7770af
    for (size_t i = 0, l = length(); i < l; i++) {
Packit Service 7770af
      at(i) = SASS_MEMORY_CLONE(at(i));
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Selector_List::cloneChildren()
Packit Service 7770af
  {
Packit Service 7770af
    for (size_t i = 0, l = length(); i < l; i++) {
Packit Service 7770af
      at(i) = SASS_MEMORY_CLONE(at(i));
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Wrapped_Selector::cloneChildren()
Packit Service 7770af
  {
Packit Service 7770af
    selector(SASS_MEMORY_CLONE(selector()));
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // remove parent selector references
Packit Service 7770af
  // basically unwraps parsed selectors
Packit Service 7770af
  void Selector_List::remove_parent_selectors()
Packit Service 7770af
  {
Packit Service 7770af
    // Check every rhs selector against left hand list
Packit Service 7770af
    for(size_t i = 0, L = length(); i < L; ++i) {
Packit Service 7770af
      if (!(*this)[i]->head()) continue;
Packit Service 7770af
      if ((*this)[i]->head()->is_empty_reference()) {
Packit Service 7770af
        // simply move to the next tail if we have "no" combinator
Packit Service 7770af
        if ((*this)[i]->combinator() == Complex_Selector::ANCESTOR_OF) {
Packit Service 7770af
          if ((*this)[i]->tail()) {
Packit Service 7770af
            if ((*this)[i]->has_line_feed()) {
Packit Service 7770af
              (*this)[i]->tail()->has_line_feed(true);
Packit Service 7770af
            }
Packit Service 7770af
            (*this)[i] = (*this)[i]->tail();
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        // otherwise remove the first item from head
Packit Service 7770af
        else {
Packit Service 7770af
          (*this)[i]->head()->erase((*this)[i]->head()->begin());
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  size_t Wrapped_Selector::hash()
Packit Service 7770af
  {
Packit Service 7770af
    if (hash_ == 0) {
Packit Service 7770af
      hash_combine(hash_, Simple_Selector::hash());
Packit Service 7770af
      if (selector_) hash_combine(hash_, selector_->hash());
Packit Service 7770af
    }
Packit Service 7770af
    return hash_;
Packit Service 7770af
  }
Packit Service 7770af
  bool Wrapped_Selector::has_parent_ref() const {
Packit Service 7770af
    // if (has_reference()) return true;
Packit Service 7770af
    if (!selector()) return false;
Packit Service 7770af
    return selector()->has_parent_ref();
Packit Service 7770af
  }
Packit Service 7770af
  bool Wrapped_Selector::has_real_parent_ref() const {
Packit Service 7770af
    // if (has_reference()) return true;
Packit Service 7770af
    if (!selector()) return false;
Packit Service 7770af
    return selector()->has_real_parent_ref();
Packit Service 7770af
  }
Packit Service 7770af
  unsigned long Wrapped_Selector::specificity() const
Packit Service 7770af
  {
Packit Service 7770af
    return selector_ ? selector_->specificity() : 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  bool Selector_List::has_parent_ref() const
Packit Service 7770af
  {
Packit Service 7770af
    for (Complex_Selector_Obj s : elements()) {
Packit Service 7770af
      if (s && s->has_parent_ref()) return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_List::has_real_parent_ref() const
Packit Service 7770af
  {
Packit Service 7770af
    for (Complex_Selector_Obj s : elements()) {
Packit Service 7770af
      if (s && s->has_real_parent_ref()) return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_Schema::has_parent_ref() const
Packit Service 7770af
  {
Packit Service 7770af
    if (String_Schema_Obj schema = Cast<String_Schema>(contents())) {
Packit Service 7770af
      return schema->length() > 0 && Cast<Parent_Selector>(schema->at(0)) != NULL;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Selector_Schema::has_real_parent_ref() const
Packit Service 7770af
  {
Packit Service 7770af
    if (String_Schema_Obj schema = Cast<String_Schema>(contents())) {
Packit Service 7770af
      Parent_Selector_Obj p = Cast<Parent_Selector>(schema->at(0));
Packit Service 7770af
      return schema->length() > 0 && p && p->is_real_parent_ref();
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Selector_List::adjust_after_pushing(Complex_Selector_Obj c)
Packit Service 7770af
  {
Packit Service 7770af
    // if (c->has_reference())   has_reference(true);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // it's a superselector if every selector of the right side
Packit Service 7770af
  // list is a superselector of the given left side selector
Packit Service 7770af
  bool Complex_Selector::is_superselector_of(Selector_List_Obj sub, std::string wrapping)
Packit Service 7770af
  {
Packit Service 7770af
    // Check every rhs selector against left hand list
Packit Service 7770af
    for(size_t i = 0, L = sub->length(); i < L; ++i) {
Packit Service 7770af
      if (!is_superselector_of((*sub)[i], wrapping)) return false;
Packit Service 7770af
    }
Packit Service 7770af
    return true;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // it's a superselector if every selector of the right side
Packit Service 7770af
  // list is a superselector of the given left side selector
Packit Service 7770af
  bool Selector_List::is_superselector_of(Selector_List_Obj sub, std::string wrapping)
Packit Service 7770af
  {
Packit Service 7770af
    // Check every rhs selector against left hand list
Packit Service 7770af
    for(size_t i = 0, L = sub->length(); i < L; ++i) {
Packit Service 7770af
      if (!is_superselector_of((*sub)[i], wrapping)) return false;
Packit Service 7770af
    }
Packit Service 7770af
    return true;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // it's a superselector if every selector on the right side
Packit Service 7770af
  // is a superselector of any one of the left side selectors
Packit Service 7770af
  bool Selector_List::is_superselector_of(Compound_Selector_Obj sub, std::string wrapping)
Packit Service 7770af
  {
Packit Service 7770af
    // Check every lhs selector against right hand
Packit Service 7770af
    for(size_t i = 0, L = length(); i < L; ++i) {
Packit Service 7770af
      if ((*this)[i]->is_superselector_of(sub, wrapping)) return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // it's a superselector if every selector on the right side
Packit Service 7770af
  // is a superselector of any one of the left side selectors
Packit Service 7770af
  bool Selector_List::is_superselector_of(Complex_Selector_Obj sub, std::string wrapping)
Packit Service 7770af
  {
Packit Service 7770af
    // Check every lhs selector against right hand
Packit Service 7770af
    for(size_t i = 0, L = length(); i < L; ++i) {
Packit Service 7770af
      if ((*this)[i]->is_superselector_of(sub)) return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Selector_List_Ptr Selector_List::unify_with(Selector_List_Ptr rhs) {
Packit Service 7770af
    std::vector<Complex_Selector_Obj> unified_complex_selectors;
Packit Service 7770af
    // Unify all of children with RHS's children, storing the results in `unified_complex_selectors`
Packit Service 7770af
    for (size_t lhs_i = 0, lhs_L = length(); lhs_i < lhs_L; ++lhs_i) {
Packit Service 7770af
      Complex_Selector_Obj seq1 = (*this)[lhs_i];
Packit Service 7770af
      for(size_t rhs_i = 0, rhs_L = rhs->length(); rhs_i < rhs_L; ++rhs_i) {
Packit Service 7770af
        Complex_Selector_Ptr seq2 = rhs->at(rhs_i);
Packit Service 7770af
Packit Service 7770af
        Selector_List_Obj result = seq1->unify_with(seq2);
Packit Service 7770af
        if( result ) {
Packit Service 7770af
          for(size_t i = 0, L = result->length(); i < L; ++i) {
Packit Service 7770af
            unified_complex_selectors.push_back( (*result)[i] );
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // Creates the final Selector_List by combining all the complex selectors
Packit Service 7770af
    Selector_List_Ptr final_result = SASS_MEMORY_NEW(Selector_List, pstate());
Packit Service 7770af
    for (auto itr = unified_complex_selectors.begin(); itr != unified_complex_selectors.end(); ++itr) {
Packit Service 7770af
      final_result->append(*itr);
Packit Service 7770af
    }
Packit Service 7770af
    return final_result;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Selector_List::populate_extends(Selector_List_Obj extendee, Subset_Map& extends)
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    Selector_List_Ptr extender = this;
Packit Service 7770af
    for (auto complex_sel : extendee->elements()) {
Packit Service 7770af
      Complex_Selector_Obj c = complex_sel;
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
      // Ignore any parent selectors, until we find the first non Selectorerence head
Packit Service 7770af
      Compound_Selector_Obj compound_sel = c->head();
Packit Service 7770af
      Complex_Selector_Obj pIter = complex_sel;
Packit Service 7770af
      while (pIter) {
Packit Service 7770af
        Compound_Selector_Obj pHead = pIter->head();
Packit Service 7770af
        if (pHead && Cast<Parent_Selector>(pHead->elements()[0]) == NULL) {
Packit Service 7770af
          compound_sel = pHead;
Packit Service 7770af
          break;
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        pIter = pIter->tail();
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      if (!pIter->head() || pIter->tail()) {
Packit Service 7770af
        error("nested selectors may not be extended", c->pstate());
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      compound_sel->is_optional(extendee->is_optional());
Packit Service 7770af
Packit Service 7770af
      for (size_t i = 0, L = extender->length(); i < L; ++i) {
Packit Service 7770af
        extends.put(compound_sel, std::make_pair((*extender)[i], compound_sel));
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  void Compound_Selector::append(Simple_Selector_Ptr element)
Packit Service 7770af
  {
Packit Service 7770af
    Vectorized<Simple_Selector_Obj>::append(element);
Packit Service 7770af
    pstate_.offset += element->pstate().offset;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Compound_Selector_Ptr Compound_Selector::minus(Compound_Selector_Ptr rhs)
Packit Service 7770af
  {
Packit Service 7770af
    Compound_Selector_Ptr result = SASS_MEMORY_NEW(Compound_Selector, pstate());
Packit Service 7770af
    // result->has_parent_reference(has_parent_reference());
Packit Service 7770af
Packit Service 7770af
    // not very efficient because it needs to preserve order
Packit Service 7770af
    for (size_t i = 0, L = length(); i < L; ++i)
Packit Service 7770af
    {
Packit Service 7770af
      bool found = false;
Packit Service 7770af
      std::string thisSelector((*this)[i]->to_string());
Packit Service 7770af
      for (size_t j = 0, M = rhs->length(); j < M; ++j)
Packit Service 7770af
      {
Packit Service 7770af
        if (thisSelector == (*rhs)[j]->to_string())
Packit Service 7770af
        {
Packit Service 7770af
          found = true;
Packit Service 7770af
          break;
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      if (!found) result->append((*this)[i]);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return result;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Compound_Selector::mergeSources(ComplexSelectorSet& sources)
Packit Service 7770af
  {
Packit Service 7770af
    for (ComplexSelectorSet::iterator iterator = sources.begin(), endIterator = sources.end(); iterator != endIterator; ++iterator) {
Packit Service 7770af
      this->sources_.insert(SASS_MEMORY_CLONE(*iterator));
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Argument_Obj Arguments::get_rest_argument()
Packit Service 7770af
  {
Packit Service 7770af
    if (this->has_rest_argument()) {
Packit Service 7770af
      for (Argument_Obj arg : this->elements()) {
Packit Service 7770af
        if (arg->is_rest_argument()) {
Packit Service 7770af
          return arg;
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return NULL;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Argument_Obj Arguments::get_keyword_argument()
Packit Service 7770af
  {
Packit Service 7770af
    if (this->has_keyword_argument()) {
Packit Service 7770af
      for (Argument_Obj arg : this->elements()) {
Packit Service 7770af
        if (arg->is_keyword_argument()) {
Packit Service 7770af
          return arg;
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return NULL;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void Arguments::adjust_after_pushing(Argument_Obj a)
Packit Service 7770af
  {
Packit Service 7770af
    if (!a->name().empty()) {
Packit Service 7770af
      if (has_keyword_argument()) {
Packit Service 7770af
        error("named arguments must precede variable-length argument", a->pstate());
Packit Service 7770af
      }
Packit Service 7770af
      has_named_arguments(true);
Packit Service 7770af
    }
Packit Service 7770af
    else if (a->is_rest_argument()) {
Packit Service 7770af
      if (has_rest_argument()) {
Packit Service 7770af
        error("functions and mixins may only be called with one variable-length argument", a->pstate());
Packit Service 7770af
      }
Packit Service 7770af
      if (has_keyword_argument_) {
Packit Service 7770af
        error("only keyword arguments may follow variable arguments", a->pstate());
Packit Service 7770af
      }
Packit Service 7770af
      has_rest_argument(true);
Packit Service 7770af
    }
Packit Service 7770af
    else if (a->is_keyword_argument()) {
Packit Service 7770af
      if (has_keyword_argument()) {
Packit Service 7770af
        error("functions and mixins may only be called with one keyword argument", a->pstate());
Packit Service 7770af
      }
Packit Service 7770af
      has_keyword_argument(true);
Packit Service 7770af
    }
Packit Service 7770af
    else {
Packit Service 7770af
      if (has_rest_argument()) {
Packit Service 7770af
        error("ordinal arguments must precede variable-length arguments", a->pstate());
Packit Service 7770af
      }
Packit Service 7770af
      if (has_named_arguments()) {
Packit Service 7770af
        error("ordinal arguments must precede named arguments", a->pstate());
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Ruleset::is_invisible() const {
Packit Service 7770af
    if (Selector_List_Ptr sl = Cast<Selector_List>(selector())) {
Packit Service 7770af
      for (size_t i = 0, L = sl->length(); i < L; ++i)
Packit Service 7770af
        if (!(*sl)[i]->has_placeholder()) return false;
Packit Service 7770af
    }
Packit Service 7770af
    return true;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Media_Block::is_invisible() const {
Packit Service 7770af
    for (size_t i = 0, L = block()->length(); i < L; ++i) {
Packit Service 7770af
      Statement_Obj stm = block()->at(i);
Packit Service 7770af
      if (!stm->is_invisible()) return false;
Packit Service 7770af
    }
Packit Service 7770af
    return true;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Number::Number(ParserState pstate, double val, std::string u, bool zero)
Packit Service 7770af
  : Value(pstate),
Packit Service 7770af
    value_(val),
Packit Service 7770af
    zero_(zero),
Packit Service 7770af
    numerator_units_(std::vector<std::string>()),
Packit Service 7770af
    denominator_units_(std::vector<std::string>()),
Packit Service 7770af
    hash_(0)
Packit Service 7770af
  {
Packit Service 7770af
    size_t l = 0;
Packit Service 7770af
    size_t r;
Packit Service 7770af
    if (!u.empty()) {
Packit Service 7770af
      bool nominator = true;
Packit Service 7770af
      while (true) {
Packit Service 7770af
        r = u.find_first_of("*/", l);
Packit Service 7770af
        std::string unit(u.substr(l, r == std::string::npos ? r : r - l));
Packit Service 7770af
        if (!unit.empty()) {
Packit Service 7770af
          if (nominator) numerator_units_.push_back(unit);
Packit Service 7770af
          else denominator_units_.push_back(unit);
Packit Service 7770af
        }
Packit Service 7770af
        if (r == std::string::npos) break;
Packit Service 7770af
        // ToDo: should error for multiple slashes
Packit Service 7770af
        // if (!nominator && u[r] == '/') error(...)
Packit Service 7770af
        if (u[r] == '/')
Packit Service 7770af
          nominator = false;
Packit Service 7770af
        l = r + 1;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    concrete_type(NUMBER);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  std::string Number::unit() const
Packit Service 7770af
  {
Packit Service 7770af
    std::string u;
Packit Service 7770af
    for (size_t i = 0, S = numerator_units_.size(); i < S; ++i) {
Packit Service 7770af
      if (i) u += '*';
Packit Service 7770af
      u += numerator_units_[i];
Packit Service 7770af
    }
Packit Service 7770af
    if (!denominator_units_.empty()) u += '/';
Packit Service 7770af
    for (size_t i = 0, S = denominator_units_.size(); i < S; ++i) {
Packit Service 7770af
      if (i) u += '*';
Packit Service 7770af
      u += denominator_units_[i];
Packit Service 7770af
    }
Packit Service 7770af
    return u;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Number::is_valid_css_unit() const
Packit Service 7770af
  {
Packit Service 7770af
    return numerator_units().size() <= 1 &&
Packit Service 7770af
           denominator_units().size() == 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Number::is_unitless() const
Packit Service 7770af
  { return numerator_units_.empty() && denominator_units_.empty(); }
Packit Service 7770af
Packit Service 7770af
  void Number::normalize(const std::string& prefered, bool strict)
Packit Service 7770af
  {
Packit Service 7770af
    // no conversion if unit is empty
Packit Service 7770af
    if (prefered.empty() && numerator_units_.size() == 0 && denominator_units_.size() == 0) return;
Packit Service 7770af
Packit Service 7770af
    // first make sure same units cancel each other out
Packit Service 7770af
    // it seems that a map table will fit nicely to do this
Packit Service 7770af
    // we basically construct exponents for each unit
Packit Service 7770af
    // has the advantage that they will be pre-sorted
Packit Service 7770af
    std::map<std::string, int> exponents;
Packit Service 7770af
Packit Service 7770af
    // initialize by summing up occurences in unit vectors
Packit Service 7770af
    for (size_t i = 0, S = numerator_units_.size(); i < S; ++i) ++ exponents[numerator_units_[i]];
Packit Service 7770af
    for (size_t i = 0, S = denominator_units_.size(); i < S; ++i) -- exponents[denominator_units_[i]];
Packit Service 7770af
Packit Service 7770af
    // the final conversion factor
Packit Service 7770af
    double factor = 1;
Packit Service 7770af
Packit Service 7770af
    // get the first entry of numerators
Packit Service 7770af
    // forward it when entry is converted
Packit Service 7770af
    std::vector<std::string>::iterator nom_it = numerator_units_.begin();
Packit Service 7770af
    std::vector<std::string>::iterator nom_end = numerator_units_.end();
Packit Service 7770af
    std::vector<std::string>::iterator denom_it = denominator_units_.begin();
Packit Service 7770af
    std::vector<std::string>::iterator denom_end = denominator_units_.end();
Packit Service 7770af
Packit Service 7770af
    // main normalization loop
Packit Service 7770af
    // should be close to optimal
Packit Service 7770af
    while (denom_it != denom_end)
Packit Service 7770af
    {
Packit Service 7770af
      // get and increment afterwards
Packit Service 7770af
      const std::string denom = *(denom_it ++);
Packit Service 7770af
      // skip already canceled out unit
Packit Service 7770af
      if (exponents[denom] >= 0) continue;
Packit Service 7770af
      // skip all units we don't know how to convert
Packit Service 7770af
      if (string_to_unit(denom) == UNKNOWN) continue;
Packit Service 7770af
      // now search for nominator
Packit Service 7770af
      while (nom_it != nom_end)
Packit Service 7770af
      {
Packit Service 7770af
        // get and increment afterwards
Packit Service 7770af
        const std::string nom = *(nom_it ++);
Packit Service 7770af
        // skip already canceled out unit
Packit Service 7770af
        if (exponents[nom] <= 0) continue;
Packit Service 7770af
        // skip all units we don't know how to convert
Packit Service 7770af
        if (string_to_unit(nom) == UNKNOWN) continue;
Packit Service 7770af
        // we now have two convertable units
Packit Service 7770af
        // add factor for current conversion
Packit Service 7770af
        factor *= conversion_factor(nom, denom, strict);
Packit Service 7770af
        // update nominator/denominator exponent
Packit Service 7770af
        -- exponents[nom]; ++ exponents[denom];
Packit Service 7770af
        // inner loop done
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // now we can build up the new unit arrays
Packit Service 7770af
    numerator_units_.clear();
Packit Service 7770af
    denominator_units_.clear();
Packit Service 7770af
Packit Service 7770af
    // build them by iterating over the exponents
Packit Service 7770af
    for (auto exp : exponents)
Packit Service 7770af
    {
Packit Service 7770af
      // maybe there is more effecient way to push
Packit Service 7770af
      // the same item multiple times to a vector?
Packit Service 7770af
      for(size_t i = 0, S = abs(exp.second); i < S; ++i)
Packit Service 7770af
      {
Packit Service 7770af
        // opted to have these switches in the inner loop
Packit Service 7770af
        // makes it more readable and should not cost much
Packit Service 7770af
        if (!exp.first.empty()) {
Packit Service 7770af
          if (exp.second < 0) denominator_units_.push_back(exp.first);
Packit Service 7770af
          else if (exp.second > 0) numerator_units_.push_back(exp.first);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // apply factor to value_
Packit Service 7770af
    // best precision this way
Packit Service 7770af
    value_ *= factor;
Packit Service 7770af
Packit Service 7770af
    // maybe convert to other unit
Packit Service 7770af
    // easier implemented on its own
Packit Service 7770af
    try { convert(prefered, strict); }
Packit Service 7770af
    catch (Exception::IncompatibleUnits& err)
Packit Service 7770af
    { error(err.what(), pstate()); }
Packit Service 7770af
    catch (...) { throw; }
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // this does not cover all cases (multiple prefered units)
Packit Service 7770af
  double Number::convert_factor(const Number& n) const
Packit Service 7770af
  {
Packit Service 7770af
Packit Service 7770af
    // first make sure same units cancel each other out
Packit Service 7770af
    // it seems that a map table will fit nicely to do this
Packit Service 7770af
    // we basically construct exponents for each unit class
Packit Service 7770af
    // std::map<std::string, int> exponents;
Packit Service 7770af
    // initialize by summing up occurences in unit vectors
Packit Service 7770af
    // for (size_t i = 0, S = numerator_units_.size(); i < S; ++i) ++ exponents[unit_to_class(numerator_units_[i])];
Packit Service 7770af
    // for (size_t i = 0, S = denominator_units_.size(); i < S; ++i) -- exponents[unit_to_class(denominator_units_[i])];
Packit Service 7770af
Packit Service 7770af
    std::vector<std::string> l_miss_nums(0);
Packit Service 7770af
    std::vector<std::string> l_miss_dens(0);
Packit Service 7770af
    // create copy since we need these for state keeping
Packit Service 7770af
    std::vector<std::string> r_nums(n.numerator_units_);
Packit Service 7770af
    std::vector<std::string> r_dens(n.denominator_units_);
Packit Service 7770af
Packit Service 7770af
    std::vector<std::string>::const_iterator l_num_it = numerator_units_.begin();
Packit Service 7770af
    std::vector<std::string>::const_iterator l_num_end = numerator_units_.end();
Packit Service 7770af
Packit Service 7770af
    bool l_unitless = is_unitless();
Packit Service 7770af
    bool r_unitless = n.is_unitless();
Packit Service 7770af
Packit Service 7770af
    // overall conversion
Packit Service 7770af
    double factor = 1;
Packit Service 7770af
Packit Service 7770af
    // process all left numerators
Packit Service 7770af
    while (l_num_it != l_num_end)
Packit Service 7770af
    {
Packit Service 7770af
      // get and increment afterwards
Packit Service 7770af
      const std::string l_num = *(l_num_it ++);
Packit Service 7770af
Packit Service 7770af
      std::vector<std::string>::iterator r_num_it = r_nums.begin();
Packit Service 7770af
      std::vector<std::string>::iterator r_num_end = r_nums.end();
Packit Service 7770af
Packit Service 7770af
      bool found = false;
Packit Service 7770af
      // search for compatible numerator
Packit Service 7770af
      while (r_num_it != r_num_end)
Packit Service 7770af
      {
Packit Service 7770af
        // get and increment afterwards
Packit Service 7770af
        const std::string r_num = *(r_num_it);
Packit Service 7770af
        // get possible converstion factor for units
Packit Service 7770af
        double conversion = conversion_factor(l_num, r_num, false);
Packit Service 7770af
        // skip incompatible numerator
Packit Service 7770af
        if (conversion == 0) {
Packit Service 7770af
          ++ r_num_it;
Packit Service 7770af
          continue;
Packit Service 7770af
        }
Packit Service 7770af
        // apply to global factor
Packit Service 7770af
        factor *= conversion;
Packit Service 7770af
        // remove item from vector
Packit Service 7770af
        r_nums.erase(r_num_it);
Packit Service 7770af
        // found numerator
Packit Service 7770af
        found = true;
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
      // maybe we did not find any
Packit Service 7770af
      // left numerator is leftover
Packit Service 7770af
      if (!found) l_miss_nums.push_back(l_num);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    std::vector<std::string>::const_iterator l_den_it = denominator_units_.begin();
Packit Service 7770af
    std::vector<std::string>::const_iterator l_den_end = denominator_units_.end();
Packit Service 7770af
Packit Service 7770af
    // process all left denominators
Packit Service 7770af
    while (l_den_it != l_den_end)
Packit Service 7770af
    {
Packit Service 7770af
      // get and increment afterwards
Packit Service 7770af
      const std::string l_den = *(l_den_it ++);
Packit Service 7770af
Packit Service 7770af
      std::vector<std::string>::iterator r_den_it = r_dens.begin();
Packit Service 7770af
      std::vector<std::string>::iterator r_den_end = r_dens.end();
Packit Service 7770af
Packit Service 7770af
      bool found = false;
Packit Service 7770af
      // search for compatible denominator
Packit Service 7770af
      while (r_den_it != r_den_end)
Packit Service 7770af
      {
Packit Service 7770af
        // get and increment afterwards
Packit Service 7770af
        const std::string r_den = *(r_den_it);
Packit Service 7770af
        // get possible converstion factor for units
Packit Service 7770af
        double conversion = conversion_factor(l_den, r_den, false);
Packit Service 7770af
        // skip incompatible denominator
Packit Service 7770af
        if (conversion == 0) {
Packit Service 7770af
          ++ r_den_it;
Packit Service 7770af
          continue;
Packit Service 7770af
        }
Packit Service 7770af
        // apply to global factor
Packit Service 7770af
        factor *= conversion;
Packit Service 7770af
        // remove item from vector
Packit Service 7770af
        r_dens.erase(r_den_it);
Packit Service 7770af
        // found denominator
Packit Service 7770af
        found = true;
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
      // maybe we did not find any
Packit Service 7770af
      // left denominator is leftover
Packit Service 7770af
      if (!found) l_miss_dens.push_back(l_den);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // check left-overs (ToDo: might cancel out)
Packit Service 7770af
    if (l_miss_nums.size() > 0 && !r_unitless) {
Packit Service 7770af
      throw Exception::IncompatibleUnits(n, *this);
Packit Service 7770af
    }
Packit Service 7770af
    if (l_miss_dens.size() > 0 && !r_unitless) {
Packit Service 7770af
      throw Exception::IncompatibleUnits(n, *this);
Packit Service 7770af
    }
Packit Service 7770af
    if (r_nums.size() > 0 && !l_unitless) {
Packit Service 7770af
      throw Exception::IncompatibleUnits(n, *this);
Packit Service 7770af
    }
Packit Service 7770af
    if (r_dens.size() > 0 && !l_unitless) {
Packit Service 7770af
      throw Exception::IncompatibleUnits(n, *this);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return factor;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // this does not cover all cases (multiple prefered units)
Packit Service 7770af
  bool Number::convert(const std::string& prefered, bool strict)
Packit Service 7770af
  {
Packit Service 7770af
    // no conversion if unit is empty
Packit Service 7770af
    if (prefered.empty()) return true;
Packit Service 7770af
Packit Service 7770af
    // first make sure same units cancel each other out
Packit Service 7770af
    // it seems that a map table will fit nicely to do this
Packit Service 7770af
    // we basically construct exponents for each unit
Packit Service 7770af
    // has the advantage that they will be pre-sorted
Packit Service 7770af
    std::map<std::string, int> exponents;
Packit Service 7770af
Packit Service 7770af
    // initialize by summing up occurences in unit vectors
Packit Service 7770af
    for (size_t i = 0, S = numerator_units_.size(); i < S; ++i) ++ exponents[numerator_units_[i]];
Packit Service 7770af
    for (size_t i = 0, S = denominator_units_.size(); i < S; ++i) -- exponents[denominator_units_[i]];
Packit Service 7770af
Packit Service 7770af
    // the final conversion factor
Packit Service 7770af
    double factor = 1;
Packit Service 7770af
Packit Service 7770af
    std::vector<std::string>::iterator denom_it = denominator_units_.begin();
Packit Service 7770af
    std::vector<std::string>::iterator denom_end = denominator_units_.end();
Packit Service 7770af
Packit Service 7770af
    // main normalization loop
Packit Service 7770af
    // should be close to optimal
Packit Service 7770af
    while (denom_it != denom_end)
Packit Service 7770af
    {
Packit Service 7770af
      // get and increment afterwards
Packit Service 7770af
      const std::string denom = *(denom_it ++);
Packit Service 7770af
      // check if conversion is needed
Packit Service 7770af
      if (denom == prefered) continue;
Packit Service 7770af
      // skip already canceled out unit
Packit Service 7770af
      if (exponents[denom] >= 0) continue;
Packit Service 7770af
      // skip all units we don't know how to convert
Packit Service 7770af
      if (string_to_unit(denom) == UNKNOWN) continue;
Packit Service 7770af
      // we now have two convertable units
Packit Service 7770af
      // add factor for current conversion
Packit Service 7770af
      factor *= conversion_factor(denom, prefered, strict);
Packit Service 7770af
      // update nominator/denominator exponent
Packit Service 7770af
      ++ exponents[denom]; -- exponents[prefered];
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    std::vector<std::string>::iterator nom_it = numerator_units_.begin();
Packit Service 7770af
    std::vector<std::string>::iterator nom_end = numerator_units_.end();
Packit Service 7770af
Packit Service 7770af
    // now search for nominator
Packit Service 7770af
    while (nom_it != nom_end)
Packit Service 7770af
    {
Packit Service 7770af
      // get and increment afterwards
Packit Service 7770af
      const std::string nom = *(nom_it ++);
Packit Service 7770af
      // check if conversion is needed
Packit Service 7770af
      if (nom == prefered) continue;
Packit Service 7770af
      // skip already canceled out unit
Packit Service 7770af
      if (exponents[nom] <= 0) continue;
Packit Service 7770af
      // skip all units we don't know how to convert
Packit Service 7770af
      if (string_to_unit(nom) == UNKNOWN) continue;
Packit Service 7770af
      // we now have two convertable units
Packit Service 7770af
      // add factor for current conversion
Packit Service 7770af
      factor *= conversion_factor(nom, prefered, strict);
Packit Service 7770af
      // update nominator/denominator exponent
Packit Service 7770af
      -- exponents[nom]; ++ exponents[prefered];
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // now we can build up the new unit arrays
Packit Service 7770af
    numerator_units_.clear();
Packit Service 7770af
    denominator_units_.clear();
Packit Service 7770af
Packit Service 7770af
    // build them by iterating over the exponents
Packit Service 7770af
    for (auto exp : exponents)
Packit Service 7770af
    {
Packit Service 7770af
      // maybe there is more effecient way to push
Packit Service 7770af
      // the same item multiple times to a vector?
Packit Service 7770af
      for(size_t i = 0, S = abs(exp.second); i < S; ++i)
Packit Service 7770af
      {
Packit Service 7770af
        // opted to have these switches in the inner loop
Packit Service 7770af
        // makes it more readable and should not cost much
Packit Service 7770af
        if (!exp.first.empty()) {
Packit Service 7770af
          if (exp.second < 0) denominator_units_.push_back(exp.first);
Packit Service 7770af
          else if (exp.second > 0) numerator_units_.push_back(exp.first);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // apply factor to value_
Packit Service 7770af
    // best precision this way
Packit Service 7770af
    value_ *= factor;
Packit Service 7770af
Packit Service 7770af
    // success?
Packit Service 7770af
    return true;
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // useful for making one number compatible with another
Packit Service 7770af
  std::string Number::find_convertible_unit() const
Packit Service 7770af
  {
Packit Service 7770af
    for (size_t i = 0, S = numerator_units_.size(); i < S; ++i) {
Packit Service 7770af
      std::string u(numerator_units_[i]);
Packit Service 7770af
      if (string_to_unit(u) != UNKNOWN) return u;
Packit Service 7770af
    }
Packit Service 7770af
    for (size_t i = 0, S = denominator_units_.size(); i < S; ++i) {
Packit Service 7770af
      std::string u(denominator_units_[i]);
Packit Service 7770af
      if (string_to_unit(u) != UNKNOWN) return u;
Packit Service 7770af
    }
Packit Service 7770af
    return std::string();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Custom_Warning::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Custom_Warning_Ptr_Const r = Cast<Custom_Warning>(&rhs)) {
Packit Service 7770af
      return message() == r->message();
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Custom_Error::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Custom_Error_Ptr_Const r = Cast<Custom_Error>(&rhs)) {
Packit Service 7770af
      return message() == r->message();
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Number::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Number_Ptr_Const r = Cast<Number>(&rhs)) {
Packit Service 7770af
      size_t lhs_units = numerator_units_.size() + denominator_units_.size();
Packit Service 7770af
      size_t rhs_units = r->numerator_units_.size() + r->denominator_units_.size();
Packit Service 7770af
      // unitless and only having one unit seems equivalent (will change in future)
Packit Service 7770af
      if (!lhs_units || !rhs_units) {
Packit Service 7770af
        return std::fabs(value() - r->value()) < NUMBER_EPSILON;
Packit Service 7770af
      }
Packit Service 7770af
      return (numerator_units_ == r->numerator_units_) &&
Packit Service 7770af
             (denominator_units_ == r->denominator_units_) &&
Packit Service 7770af
             std::fabs(value() - r->value()) < NUMBER_EPSILON;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Number::operator< (const Number& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    size_t lhs_units = numerator_units_.size() + denominator_units_.size();
Packit Service 7770af
    size_t rhs_units = rhs.numerator_units_.size() + rhs.denominator_units_.size();
Packit Service 7770af
    // unitless and only having one unit seems equivalent (will change in future)
Packit Service 7770af
    if (!lhs_units || !rhs_units) {
Packit Service 7770af
      return value() < rhs.value();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Number tmp_r(&rhs;; // copy
Packit Service 7770af
    tmp_r.normalize(find_convertible_unit());
Packit Service 7770af
    std::string l_unit(unit());
Packit Service 7770af
    std::string r_unit(tmp_r.unit());
Packit Service 7770af
    if (unit() != tmp_r.unit()) {
Packit Service 7770af
      error("cannot compare numbers with incompatible units", pstate());
Packit Service 7770af
    }
Packit Service 7770af
    return value() < tmp_r.value();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool String_Quoted::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (String_Quoted_Ptr_Const qstr = Cast<String_Quoted>(&rhs)) {
Packit Service 7770af
      return (value() == qstr->value());
Packit Service 7770af
    } else if (String_Constant_Ptr_Const cstr = Cast<String_Constant>(&rhs)) {
Packit Service 7770af
      return (value() == cstr->value());
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool String_Constant::is_invisible() const {
Packit Service 7770af
    return value_.empty() && quote_mark_ == 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool String_Constant::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (String_Quoted_Ptr_Const qstr = Cast<String_Quoted>(&rhs)) {
Packit Service 7770af
      return (value() == qstr->value());
Packit Service 7770af
    } else if (String_Constant_Ptr_Const cstr = Cast<String_Constant>(&rhs)) {
Packit Service 7770af
      return (value() == cstr->value());
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool String_Schema::is_left_interpolant(void) const
Packit Service 7770af
  {
Packit Service 7770af
    return length() && first()->is_left_interpolant();
Packit Service 7770af
  }
Packit Service 7770af
  bool String_Schema::is_right_interpolant(void) const
Packit Service 7770af
  {
Packit Service 7770af
    return length() && last()->is_right_interpolant();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool String_Schema::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (String_Schema_Ptr_Const r = Cast<String_Schema>(&rhs)) {
Packit Service 7770af
      if (length() != r->length()) return false;
Packit Service 7770af
      for (size_t i = 0, L = length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj rv = (*r)[i];
Packit Service 7770af
        Expression_Obj lv = (*this)[i];
Packit Service 7770af
        if (!lv || !rv) return false;
Packit Service 7770af
        if (!(*lv == *rv)) return false;
Packit Service 7770af
      }
Packit Service 7770af
      return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Boolean::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Boolean_Ptr_Const r = Cast<Boolean>(&rhs)) {
Packit Service 7770af
      return (value() == r->value());
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Color::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Color_Ptr_Const r = Cast<Color>(&rhs)) {
Packit Service 7770af
      return r_ == r->r() &&
Packit Service 7770af
             g_ == r->g() &&
Packit Service 7770af
             b_ == r->b() &&
Packit Service 7770af
             a_ == r->a();
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool List::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (List_Ptr_Const r = Cast<List>(&rhs)) {
Packit Service 7770af
      if (length() != r->length()) return false;
Packit Service 7770af
      if (separator() != r->separator()) return false;
Packit Service 7770af
      for (size_t i = 0, L = length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj rv = r->at(i);
Packit Service 7770af
        Expression_Obj lv = this->at(i);
Packit Service 7770af
        if (!lv || !rv) return false;
Packit Service 7770af
        if (!(*lv == *rv)) return false;
Packit Service 7770af
      }
Packit Service 7770af
      return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Map::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    if (Map_Ptr_Const r = Cast<Map>(&rhs)) {
Packit Service 7770af
      if (length() != r->length()) return false;
Packit Service 7770af
      for (auto key : keys()) {
Packit Service 7770af
        Expression_Obj lv = at(key);
Packit Service 7770af
        Expression_Obj rv = r->at(key);
Packit Service 7770af
        if (!rv || !lv) return false;
Packit Service 7770af
        if (!(*lv == *rv)) return false;
Packit Service 7770af
      }
Packit Service 7770af
      return true;
Packit Service 7770af
    }
Packit Service 7770af
    return false;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Null::operator== (const Expression& rhs) const
Packit Service 7770af
  {
Packit Service 7770af
    return rhs.concrete_type() == NULL_VAL;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  size_t List::size() const {
Packit Service 7770af
    if (!is_arglist_) return length();
Packit Service 7770af
    // arglist expects a list of arguments
Packit Service 7770af
    // so we need to break before keywords
Packit Service 7770af
    for (size_t i = 0, L = length(); i < L; ++i) {
Packit Service 7770af
      Expression_Obj obj = this->at(i);
Packit Service 7770af
      if (Argument_Ptr arg = Cast<Argument>(obj)) {
Packit Service 7770af
        if (!arg->name().empty()) return i;
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return length();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Expression_Obj Hashed::at(Expression_Obj k) const
Packit Service 7770af
  {
Packit Service 7770af
    if (elements_.count(k))
Packit Service 7770af
    { return elements_.at(k); }
Packit Service 7770af
    else { return NULL; }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  bool Binary_Expression::is_left_interpolant(void) const
Packit Service 7770af
  {
Packit Service 7770af
    return is_interpolant() || (left() && left()->is_left_interpolant());
Packit Service 7770af
  }
Packit Service 7770af
  bool Binary_Expression::is_right_interpolant(void) const
Packit Service 7770af
  {
Packit Service 7770af
    return is_interpolant() || (right() && right()->is_right_interpolant());
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  const std::string AST_Node::to_string(Sass_Inspect_Options opt) const
Packit Service 7770af
  {
Packit Service 7770af
    Sass_Output_Options out(opt);
Packit Service 7770af
    Emitter emitter(out);
Packit Service 7770af
    Inspect i(emitter);
Packit Service 7770af
    i.in_declaration = true;
Packit Service 7770af
    // ToDo: inspect should be const
Packit Service 7770af
    const_cast<AST_Node_Ptr>(this)->perform(&i);
Packit Service 7770af
    return i.get_buffer();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  const std::string AST_Node::to_string() const
Packit Service 7770af
  {
Packit Service 7770af
    return to_string({ NESTED, 5 });
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  std::string String_Quoted::inspect() const
Packit Service 7770af
  {
Packit Service 7770af
    return quote(value_, '*');
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  std::string String_Constant::inspect() const
Packit Service 7770af
  {
Packit Service 7770af
    return quote(value_, '*');
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  //////////////////////////////////////////////////////////////////////////////////////////
Packit Service 7770af
  // Additional method on Lists to retrieve values directly or from an encompassed Argument.
Packit Service 7770af
  //////////////////////////////////////////////////////////////////////////////////////////
Packit Service 7770af
  Expression_Obj List::value_at_index(size_t i) {
Packit Service 7770af
    Expression_Obj obj = this->at(i);
Packit Service 7770af
    if (is_arglist_) {
Packit Service 7770af
      if (Argument_Ptr arg = Cast<Argument>(obj)) {
Packit Service 7770af
        return arg->value();
Packit Service 7770af
      } else {
Packit Service 7770af
        return obj;
Packit Service 7770af
      }
Packit Service 7770af
    } else {
Packit Service 7770af
      return obj;
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  //////////////////////////////////////////////////////////////////////////////////////////
Packit Service 7770af
  // Convert map to (key, value) list.
Packit Service 7770af
  //////////////////////////////////////////////////////////////////////////////////////////
Packit Service 7770af
  List_Obj Map::to_list(ParserState& pstate) {
Packit Service 7770af
    List_Obj ret = SASS_MEMORY_NEW(List, pstate, length(), SASS_COMMA);
Packit Service 7770af
Packit Service 7770af
    for (auto key : keys()) {
Packit Service 7770af
      List_Obj l = SASS_MEMORY_NEW(List, pstate, 2);
Packit Service 7770af
      l->append(key);
Packit Service 7770af
      l->append(at(key));
Packit Service 7770af
      ret->append(l);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return ret;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  //////////////////////////////////////////////////////////////////////////////////////////
Packit Service 7770af
  // Copy implementations
Packit Service 7770af
  //////////////////////////////////////////////////////////////////////////////////////////
Packit Service 7770af
Packit Service 7770af
  #ifdef DEBUG_SHARED_PTR
Packit Service 7770af
Packit Service 7770af
  #define IMPLEMENT_AST_OPERATORS(klass) \
Packit Service 7770af
    klass##_Ptr klass::copy(std::string file, size_t line) const { \
Packit Service 7770af
      klass##_Ptr cpy = new klass(this); \
Packit Service 7770af
      cpy->trace(file, line); \
Packit Service 7770af
      return cpy; \
Packit Service 7770af
    } \
Packit Service 7770af
    klass##_Ptr klass::clone(std::string file, size_t line) const { \
Packit Service 7770af
      klass##_Ptr cpy = copy(file, line); \
Packit Service 7770af
      cpy->cloneChildren(); \
Packit Service 7770af
      return cpy; \
Packit Service 7770af
    } \
Packit Service 7770af
Packit Service 7770af
  #else
Packit Service 7770af
Packit Service 7770af
  #define IMPLEMENT_AST_OPERATORS(klass) \
Packit Service 7770af
    klass##_Ptr klass::copy() const { \
Packit Service 7770af
      return new klass(this); \
Packit Service 7770af
    } \
Packit Service 7770af
    klass##_Ptr klass::clone() const { \
Packit Service 7770af
      klass##_Ptr cpy = copy(); \
Packit Service 7770af
      cpy->cloneChildren(); \
Packit Service 7770af
      return cpy; \
Packit Service 7770af
    } \
Packit Service 7770af
Packit Service 7770af
  #endif
Packit Service 7770af
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Supports_Operator);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Supports_Negation);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Compound_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Complex_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Element_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Class_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Id_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Pseudo_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Wrapped_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Selector_List);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Ruleset);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Media_Block);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Custom_Warning);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Custom_Error);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(List);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Map);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Number);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Binary_Expression);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(String_Schema);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(String_Constant);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(String_Quoted);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Boolean);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Color);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Null);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Parent_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Import);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Import_Stub);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Function_Call);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Directive);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(At_Root_Block);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Supports_Block);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(While);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Each);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(For);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(If);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Mixin_Call);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Extension);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Media_Query);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Media_Query_Expression);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Debug);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Error);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Warning);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Assignment);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Return);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(At_Root_Query);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Variable);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Comment);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Attribute_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Supports_Interpolation);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Supports_Declaration);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Supports_Condition);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Parameters);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Parameter);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Arguments);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Argument);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Unary_Expression);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Function_Call_Schema);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Block);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Content);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Trace);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Keyframe_Rule);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Bubble);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Selector_Schema);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Placeholder_Selector);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Definition);
Packit Service 7770af
  IMPLEMENT_AST_OPERATORS(Declaration);
Packit Service 7770af
Packit Service 7770af
}