@@ -801,120 +801,170 @@ namespace Sass {
801
801
return cpy;
802
802
}
803
803
804
+ // append another complex selector at the end
805
+ // check if we need to append some headers
806
+ // then we need to check for the combinator
807
+ // only then we can safely set the new tail
808
+ void Complex_Selector::append (Context& ctx, Complex_Selector* ss)
809
+ {
810
+
811
+ Complex_Selector* t = ss->tail ();
812
+ Combinator c = ss->combinator ();
813
+ String* r = ss->reference ();
814
+ Compound_Selector* h = ss->head ();
815
+
816
+ if (ss->has_line_feed ()) has_line_feed (true );
817
+ if (ss->has_line_break ()) has_line_break (true );
818
+
819
+ // append old headers
820
+ if (h && h->length ()) {
821
+ if (last ()->combinator () != ANCESTOR_OF && c != ANCESTOR_OF) {
822
+ error (" Invalid parent selector" , pstate_);
823
+ } else {
824
+ *last ()->head_ += h;
825
+ }
826
+ } else {
827
+ // std::cerr << "has no or empty head\n";
828
+ }
829
+
830
+ if (last ()) {
831
+ if (last ()->combinator () != ANCESTOR_OF && c != ANCESTOR_OF) {
832
+ Complex_Selector* inter = new (ctx.mem ) Complex_Selector (pstate ());
833
+ inter->reference (r);
834
+ inter->combinator (c);
835
+ inter->tail (t);
836
+ last ()->tail (inter);
837
+ } else {
838
+ if (last ()->combinator () == ANCESTOR_OF) {
839
+ last ()->combinator (c);
840
+ last ()->reference (r);
841
+ }
842
+ last ()->tail (t);
843
+ }
844
+ }
845
+
846
+
847
+ }
848
+
804
849
Selector_List* Selector_List::parentize (Selector_List* ps, Context& ctx)
805
850
{
806
851
Selector_List* ss = new (ctx.mem ) Selector_List (pstate ());
807
852
for (size_t pi = 0 , pL = ps->length (); pi < pL; ++pi) {
853
+ Selector_List* list = new (ctx.mem ) Selector_List (pstate ());
854
+ *list << (*ps)[pi];
808
855
for (size_t si = 0 , sL = this ->length (); si < sL ; ++si) {
809
- *ss << (*this )[si]->parentize ((*ps)[pi] , ctx);
856
+ *ss += (*this )[si]->parentize (list , ctx);
810
857
}
811
858
}
812
- // return selector
813
859
return ss;
814
860
}
815
861
816
- Selector_List* Selector_List ::parentize (Complex_Selector* p , Context& ctx)
862
+ Selector_List* Complex_Selector ::parentize (Selector_List* parents , Context& ctx)
817
863
{
818
- Selector_List* ss = new (ctx.mem ) Selector_List (pstate ());
819
- for (size_t i = 0 , L = this ->length (); i < L; ++i) {
820
- *ss << (*this )[i]->parentize (p, ctx);
821
- }
822
- // return selector
823
- return ss;
824
- }
825
864
826
- Complex_Selector* Complex_Selector::parentize (Context& ctx)
827
- {
828
- // create a new complex selector to return a processed copy
829
- return this ;
830
- Complex_Selector* ss = new (ctx.mem ) Complex_Selector (this ->pstate ());
831
- // ss->has_line_feed(this->has_line_feed());
832
- ss->combinator (this ->combinator ());
833
- if (this ->tail ()) {
834
- ss->tail (this ->tail ()->parentize (ctx));
835
- }
836
- if (Compound_Selector* head = this ->head ()) {
837
- // now add everything expect parent selectors to head
838
- ss->head (new (ctx.mem ) Compound_Selector (head->pstate ()));
839
- for (size_t i = 0 , L = head->length (); i < L; ++i) {
840
- if (!dynamic_cast <Parent_Selector*>((*head)[i])) {
841
- *ss->head () << (*head)[i];
865
+ Complex_Selector* tail = this ->tail ();
866
+ Compound_Selector* head = this ->head ();
867
+
868
+ // first parentize the tail (which may return an expanded list)
869
+ Selector_List* tails = tail ? tail->parentize (parents, ctx) : 0 ;
870
+
871
+ if (head && head->length () > 0 ) {
872
+
873
+ // we have a parent selector in a simple compound list
874
+ // mix parent complex selector into the compound list
875
+ if (dynamic_cast <Parent_Selector*>((*head)[0 ])) {
876
+ if (parents && parents->length ()) {
877
+ Selector_List* retval = new (ctx.mem ) Selector_List (pstate ());
878
+ if (tails && tails->length () > 0 ) {
879
+ for (size_t n = 0 , nL = tails->length (); n < nL; ++n) {
880
+ for (size_t i = 0 , iL = parents->length (); i < iL; ++i) {
881
+ Complex_Selector* t = (*tails)[n];
882
+ Complex_Selector* parent = (*parents)[i];
883
+ Complex_Selector* s = parent->cloneFully (ctx);
884
+ Complex_Selector* ss = this ->clone (ctx);
885
+ ss->tail (t ? t->clone (ctx) : 0 );
886
+ Compound_Selector* h = head_->clone (ctx);
887
+ if (h->length ()) h->erase (h->begin ());
888
+ ss->head (h->length () ? h : 0 );
889
+ s->append (ctx, ss);
890
+ *retval << s;
891
+ }
892
+ }
893
+ }
894
+ // have no tails but parents
895
+ // loop above is inside out
896
+ else {
897
+ for (size_t i = 0 , iL = parents->length (); i < iL; ++i) {
898
+ Complex_Selector* parent = (*parents)[i];
899
+ Complex_Selector* s = parent->cloneFully (ctx);
900
+ Complex_Selector* ss = this ->clone (ctx);
901
+ ss->tail (tail ? tail->clone (ctx) : 0 );
902
+ Compound_Selector* h = head_->clone (ctx);
903
+ if (h->length ()) h->erase (h->begin ());
904
+ ss->head (h->length () ? h : 0 );
905
+ // \/ IMO ruby sass bug \/
906
+ ss->has_line_feed (false );
907
+ s->append (ctx, ss);
908
+ *retval << s;
909
+ }
910
+ }
911
+ return retval;
912
+ }
913
+ // have no parent but some tails
914
+ else {
915
+ Selector_List* retval = new (ctx.mem ) Selector_List (pstate ());
916
+ if (tails && tails->length () > 0 ) {
917
+ for (size_t n = 0 , nL = tails->length (); n < nL; ++n) {
918
+ Complex_Selector* cpy = this ->clone (ctx);
919
+ cpy->tail ((*tails)[n]->cloneFully (ctx));
920
+ cpy->head (new (ctx.mem ) Compound_Selector (head->pstate ()));
921
+ for (size_t i = 1 , L = this ->head ()->length (); i < L; ++i)
922
+ *cpy->head () << (*this ->head ())[i];
923
+ if (!cpy->head ()->length ()) cpy->head (0 );
924
+ *retval << cpy->skip_empty_reference ();
925
+ }
926
+ }
927
+ // have no parent and not tails
928
+ else {
929
+ Complex_Selector* cpy = this ->clone (ctx);
930
+ cpy->head (new (ctx.mem ) Compound_Selector (head->pstate ()));
931
+ for (size_t i = 1 , L = this ->head ()->length (); i < L; ++i)
932
+ *cpy->head () << (*this ->head ())[i];
933
+ if (!cpy->head ()->length ()) cpy->head (0 );
934
+ *retval << cpy->skip_empty_reference ();
935
+ }
936
+ return retval;
842
937
}
843
938
}
844
- // if (ss->head()->empty()) ss->head(0);
845
- }
846
- // return copy
847
- return ss;
848
- }
939
+ // no parent selector in head
940
+ else {
941
+ return this ->tails (ctx, tails);
942
+ }
849
943
850
- Selector_List* Selector_List::parentize (Context& ctx)
851
- {
852
- Selector_List* ss = new (ctx.mem ) Selector_List (pstate ());
853
- for (size_t i = 0 , L = length (); i < L; ++i) {
854
- *ss << (*this )[i]->parentize (ctx);
855
944
}
856
- // return selector
857
- return ss;
858
- }
859
-
860
- Selector_List* Complex_Selector::parentize (Selector_List* ps, Context& ctx)
861
- {
862
- Selector_List* ss = new (ctx.mem ) Selector_List (pstate ());
863
- if (ps == 0 ) { *ss << this ->parentize (ctx); return ss; }
864
- for (size_t i = 0 , L = ps->length (); i < L; ++i) {
865
- *ss << this ->parentize ((*ps)[i], ctx);
945
+ // has no head
946
+ else {
947
+ return this ->tails (ctx, tails);
866
948
}
867
- // return selector
868
- return ss;
949
+
950
+ // unreachable
951
+ return 0 ;
869
952
}
870
953
871
- Complex_Selector * Complex_Selector::parentize (Complex_Selector* parent, Context& ctx)
954
+ Selector_List * Complex_Selector::tails ( Context& ctx, Selector_List* tails )
872
955
{
873
- if (!parent) return parentize (ctx);
874
- Complex_Selector* pr = 0 ;
875
- Compound_Selector* head = this ->head ();
876
- // create a new complex selector to return a processed copy
877
- Complex_Selector* ss = new (ctx.mem ) Complex_Selector (pstate ());
878
- // ss->has_line_feed(has_line_feed());
879
- ss->has_line_break (has_line_break ());
880
-
881
- // Points to last complex selector
882
- // Moved when resolving parent refs
883
- Complex_Selector* cur = ss;
884
-
885
- // check if compound selector has exactly one parent reference
886
- // if so we need to connect the parent to the current selector
887
- // then we also need to add the remaining simple selector to the new "parent"
888
- if (head) {
889
- // create a new compound and move originals if needed
890
- // we may add the simple selector to the same selector
891
- // with parent refs we may put them in different places
892
- ss->head (new (ctx.mem ) Compound_Selector (head->pstate ()));
893
- ss->head ()->has_parent_reference (head->has_parent_reference ());
894
- ss->head ()->has_line_break (head->has_line_break ());
895
- // process simple selectors sequence
896
- for (size_t i = 0 , L = head->length (); i < L; ++i) {
897
- // we have a parent selector in a simple selector list
898
- // mix parent complex selector into the compound list
899
- if (dynamic_cast <Parent_Selector*>((*head)[i])) {
900
- // clone the parent selector
901
- pr = parent->cloneFully (ctx);
902
- // assign head and tail
903
- cur->head (pr->head ());
904
- cur->tail (pr->tail ());
905
- // move forward
906
- cur = pr->last ();
907
- } else {
908
- // just add simple selector
909
- *cur->head () << (*head)[i];
910
- }
956
+ Selector_List* rv = new (ctx.mem ) Selector_List (pstate_);
957
+ if (tails && tails->length ()) {
958
+ for (size_t i = 0 , iL = tails->length (); i < iL; ++i) {
959
+ Complex_Selector* pr = this ->clone (ctx);
960
+ pr->tail ((*tails)[i]);
961
+ *rv << pr;
911
962
}
912
963
}
913
- if (cur->head ()) cur->head (cur->head ()->length () ? cur->head () : 0 );
914
- // parentize and assign trailing complex selector
915
- if (this ->tail ()) cur->tail (this ->tail ()->parentize (parent, ctx));
916
- // return selector
917
- return ss;
964
+ else {
965
+ *rv << this ;
966
+ }
967
+ return rv;
918
968
}
919
969
920
970
// return the last tail that is defined
@@ -1068,11 +1118,6 @@ namespace Sass {
1068
1118
void Selector_List::adjust_after_pushing (Complex_Selector* c)
1069
1119
{
1070
1120
if (c->has_reference ()) has_reference (true );
1071
-
1072
- #ifdef DEBUG
1073
- To_String to_string;
1074
- this ->mCachedSelector (this ->perform (&to_string));
1075
- #endif
1076
1121
}
1077
1122
1078
1123
// it's a superselector if every selector of the right side
0 commit comments