Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit 633fe09

Browse files
author
Aaron Leung
committed
Working on full-blown selector comparison (necessary for full-blown inheritance).
1 parent 46fd6b3 commit 633fe09

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

node.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <sstream>
2+
#include <algorithm>
23
#include "node.hpp"
34
#include "error.hpp"
45

@@ -91,10 +92,13 @@ namespace Sass {
9192
Type lhs_type = type();
9293
Type rhs_type = rhs.type();
9394

95+
// comparing atomic numbers
9496
if ((lhs_type == number && rhs_type == number) ||
9597
(lhs_type == numeric_percentage && rhs_type == numeric_percentage)) {
9698
return numeric_value() < rhs.numeric_value();
9799
}
100+
101+
// comparing numbers with units
98102
else if (lhs_type == numeric_dimension && rhs_type == numeric_dimension) {
99103
if (unit() == rhs.unit()) {
100104
return numeric_value() < rhs.numeric_value();
@@ -103,23 +107,48 @@ namespace Sass {
103107
throw Error(Error::evaluation, path(), line(), "incompatible units");
104108
}
105109
}
106-
else if ((type() >= selector_group && type() <=selector_schema) &&
110+
111+
// comparing colors
112+
else if (lhs_type == numeric_color && rhs_type == numeric_color) {
113+
return lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
114+
}
115+
116+
// comparing identifiers and strings (treat them as comparable)
117+
else if ((lhs_type == identifier || lhs_type == string_constant || lhs_type == value) &&
118+
(rhs_type == identifier || lhs_type == string_constant || rhs_type == value)) {
119+
return token().unquote() < rhs.token().unquote();
120+
}
121+
122+
// COMPARING SELECTORS -- IMPORTANT FOR INHERITANCE
123+
else if ((type() >= selector_group && type() <=selector_schema) &&
107124
(rhs.type() >= selector_group && rhs.type() <=selector_schema)) {
108-
if (type() != rhs.type()) {
109-
return type() < rhs.type();
110-
}
125+
126+
// if they're not the same kind, just compare type tags
127+
if (type() != rhs.type()) return type() < rhs.type();
128+
129+
// otherwise we have to do more work
111130
switch (type())
112131
{
113-
case simple_selector: {
132+
case simple_selector:
133+
case pseudo: {
114134
return token() < rhs.token();
115135
} break;
116136

137+
case attribute_selector: {
138+
return lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
139+
} break;
140+
141+
142+
117143
default: {
118144
return false;
119145
} break;
120146

121147
}
122148
}
149+
// END OF SELECTOR COMPARISON
150+
151+
// catch-all
123152
else {
124153
throw Error(Error::evaluation, path(), line(), "incomparable types");
125154
}

node.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ namespace Sass {
178178
Node& operator<<(Node n);
179179
Node& operator+=(Node n);
180180

181-
vector<Node>::iterator begin();
182-
vector<Node>::iterator end();
181+
vector<Node>::iterator begin() const;
182+
vector<Node>::iterator end() const;
183183
void insert(vector<Node>::iterator position,
184184
vector<Node>::iterator first,
185185
vector<Node>::iterator last);
@@ -355,9 +355,9 @@ namespace Sass {
355355
return *this;
356356
}
357357

358-
inline vector<Node>::iterator Node::begin()
358+
inline vector<Node>::iterator Node::begin() const
359359
{ return ip_->children.begin(); }
360-
inline vector<Node>::iterator Node::end()
360+
inline vector<Node>::iterator Node::end() const
361361
{ return ip_->children.end(); }
362362
inline void Node::insert(vector<Node>::iterator position,
363363
vector<Node>::iterator first,

test_node_factory.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string>
33
#include <tr1/unordered_map>
44
#include <map>
5+
#include <algorithm>
56

67
#ifndef SASS_NODE_INCLUDED
78
#include "node.hpp"
@@ -75,6 +76,17 @@ int main()
7576

7677
cout << dict[m] << " " << dict[n] << endl;
7778

79+
cout << "Lexicographical comparison: " << endl;
80+
cout << lexicographical_compare(num2.begin(), num2.end(),
81+
num3.begin(), num3.end())
82+
<< endl;
83+
cout << lexicographical_compare(num.begin(), num.end(),
84+
num2.begin(), num2.end())
85+
<< endl;
86+
cout << lexicographical_compare(num3.begin(), num3.end(),
87+
num.begin(), num.end())
88+
<< endl << endl;
89+
7890

7991

8092

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy