Commit 633fe09d by Aaron Leung

Working on full-blown selector comparison (necessary for full-blown inheritance).

parent 46fd6b34
#include <sstream> #include <sstream>
#include <algorithm>
#include "node.hpp" #include "node.hpp"
#include "error.hpp" #include "error.hpp"
...@@ -91,10 +92,13 @@ namespace Sass { ...@@ -91,10 +92,13 @@ namespace Sass {
Type lhs_type = type(); Type lhs_type = type();
Type rhs_type = rhs.type(); Type rhs_type = rhs.type();
// comparing atomic numbers
if ((lhs_type == number && rhs_type == number) || if ((lhs_type == number && rhs_type == number) ||
(lhs_type == numeric_percentage && rhs_type == numeric_percentage)) { (lhs_type == numeric_percentage && rhs_type == numeric_percentage)) {
return numeric_value() < rhs.numeric_value(); return numeric_value() < rhs.numeric_value();
} }
// comparing numbers with units
else if (lhs_type == numeric_dimension && rhs_type == numeric_dimension) { else if (lhs_type == numeric_dimension && rhs_type == numeric_dimension) {
if (unit() == rhs.unit()) { if (unit() == rhs.unit()) {
return numeric_value() < rhs.numeric_value(); return numeric_value() < rhs.numeric_value();
...@@ -103,23 +107,48 @@ namespace Sass { ...@@ -103,23 +107,48 @@ namespace Sass {
throw Error(Error::evaluation, path(), line(), "incompatible units"); throw Error(Error::evaluation, path(), line(), "incompatible units");
} }
} }
else if ((type() >= selector_group && type() <=selector_schema) &&
// comparing colors
else if (lhs_type == numeric_color && rhs_type == numeric_color) {
return lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
}
// comparing identifiers and strings (treat them as comparable)
else if ((lhs_type == identifier || lhs_type == string_constant || lhs_type == value) &&
(rhs_type == identifier || lhs_type == string_constant || rhs_type == value)) {
return token().unquote() < rhs.token().unquote();
}
// COMPARING SELECTORS -- IMPORTANT FOR INHERITANCE
else if ((type() >= selector_group && type() <=selector_schema) &&
(rhs.type() >= selector_group && rhs.type() <=selector_schema)) { (rhs.type() >= selector_group && rhs.type() <=selector_schema)) {
if (type() != rhs.type()) {
return type() < rhs.type(); // if they're not the same kind, just compare type tags
} if (type() != rhs.type()) return type() < rhs.type();
// otherwise we have to do more work
switch (type()) switch (type())
{ {
case simple_selector: { case simple_selector:
case pseudo: {
return token() < rhs.token(); return token() < rhs.token();
} break; } break;
case attribute_selector: {
return lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
} break;
default: { default: {
return false; return false;
} break; } break;
} }
} }
// END OF SELECTOR COMPARISON
// catch-all
else { else {
throw Error(Error::evaluation, path(), line(), "incomparable types"); throw Error(Error::evaluation, path(), line(), "incomparable types");
} }
......
...@@ -178,8 +178,8 @@ namespace Sass { ...@@ -178,8 +178,8 @@ namespace Sass {
Node& operator<<(Node n); Node& operator<<(Node n);
Node& operator+=(Node n); Node& operator+=(Node n);
vector<Node>::iterator begin(); vector<Node>::iterator begin() const;
vector<Node>::iterator end(); vector<Node>::iterator end() const;
void insert(vector<Node>::iterator position, void insert(vector<Node>::iterator position,
vector<Node>::iterator first, vector<Node>::iterator first,
vector<Node>::iterator last); vector<Node>::iterator last);
...@@ -355,9 +355,9 @@ namespace Sass { ...@@ -355,9 +355,9 @@ namespace Sass {
return *this; return *this;
} }
inline vector<Node>::iterator Node::begin() inline vector<Node>::iterator Node::begin() const
{ return ip_->children.begin(); } { return ip_->children.begin(); }
inline vector<Node>::iterator Node::end() inline vector<Node>::iterator Node::end() const
{ return ip_->children.end(); } { return ip_->children.end(); }
inline void Node::insert(vector<Node>::iterator position, inline void Node::insert(vector<Node>::iterator position,
vector<Node>::iterator first, vector<Node>::iterator first,
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <string> #include <string>
#include <tr1/unordered_map> #include <tr1/unordered_map>
#include <map> #include <map>
#include <algorithm>
#ifndef SASS_NODE_INCLUDED #ifndef SASS_NODE_INCLUDED
#include "node.hpp" #include "node.hpp"
...@@ -75,6 +76,17 @@ int main() ...@@ -75,6 +76,17 @@ int main()
cout << dict[m] << " " << dict[n] << endl; cout << dict[m] << " " << dict[n] << endl;
cout << "Lexicographical comparison: " << endl;
cout << lexicographical_compare(num2.begin(), num2.end(),
num3.begin(), num3.end())
<< endl;
cout << lexicographical_compare(num.begin(), num.end(),
num2.begin(), num2.end())
<< endl;
cout << lexicographical_compare(num3.begin(), num3.end(),
num.begin(), num.end())
<< endl << endl;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment