@@ -47,8 +47,8 @@ namespace Sass {
47
47
// ##########################################################################
48
48
SelectorListObj Extender::extend (
49
49
SelectorListObj& selector,
50
- SelectorListObj& source,
51
- SelectorListObj& targets,
50
+ const SelectorListObj& source,
51
+ const SelectorListObj& targets,
52
52
Backtraces& traces)
53
53
{
54
54
return extendOrReplace (selector, source, targets, ExtendMode::TARGETS, traces);
@@ -60,8 +60,8 @@ namespace Sass {
60
60
// ##########################################################################
61
61
SelectorListObj Extender::replace (
62
62
SelectorListObj& selector,
63
- SelectorListObj& source,
64
- SelectorListObj& targets,
63
+ const SelectorListObj& source,
64
+ const SelectorListObj& targets,
65
65
Backtraces& traces)
66
66
{
67
67
return extendOrReplace (selector, source, targets, ExtendMode::REPLACE, traces);
@@ -73,9 +73,9 @@ namespace Sass {
73
73
// ##########################################################################
74
74
SelectorListObj Extender::extendOrReplace (
75
75
SelectorListObj& selector,
76
- SelectorListObj& source,
77
- SelectorListObj& targets,
78
- ExtendMode mode,
76
+ const SelectorListObj& source,
77
+ const SelectorListObj& targets,
78
+ const ExtendMode mode,
79
79
Backtraces& traces)
80
80
{
81
81
ExtSelExtMapEntry extenders;
@@ -87,15 +87,16 @@ namespace Sass {
87
87
88
88
for (auto complex : targets->elements ()) {
89
89
90
- if (complex->length () != 1 ) {
91
- // throw "can't extend complex selector $complex."
92
- }
90
+ // This seems superfluous, check is done before!?
91
+ // if (complex->length() != 1) {
92
+ // error("complex selectors may not be extended.", complex->pstate(), traces);
93
+ // }
93
94
94
- if (auto compound = complex->first ()->getCompound ()) {
95
+ if (const CompoundSelector* compound = complex->first ()->getCompound ()) {
95
96
96
97
ExtSelExtMap extensions;
97
98
98
- for (auto simple : compound->elements ()) {
99
+ for (const SimpleSelectorObj& simple : compound->elements ()) {
99
100
extensions.insert (std::make_pair (simple, extenders));
100
101
}
101
102
@@ -287,20 +288,19 @@ namespace Sass {
287
288
// Note: this function could need some logic cleanup
288
289
// ##########################################################################
289
290
void Extender::addExtension (
290
- SelectorListObj& extender,
291
- SimpleSelectorObj& target,
292
- // get get passed a pointer
293
- ExtendRuleObj extend,
294
- CssMediaRuleObj& mediaQueryContext)
291
+ const SelectorListObj& extender,
292
+ const SimpleSelectorObj& target,
293
+ const CssMediaRuleObj& mediaQueryContext,
294
+ bool is_optional)
295
295
{
296
296
297
297
auto rules = selectors.find (target);
298
298
bool hasRule = rules != selectors.end ();
299
299
300
300
ExtSelExtMapEntry newExtensions;
301
301
302
- auto existingExtensions = extensionsByExtender. find (target);
303
- bool hasExistingExtensions = existingExtensions != extensionsByExtender.end ();
302
+ // ToDo: we check this here first and fetch the same? item again after the loop!?
303
+ bool hasExistingExtensions = extensionsByExtender. find (target) != extensionsByExtender.end ();
304
304
305
305
ExtSelExtMapEntry& sources = extensions[target];
306
306
@@ -309,7 +309,7 @@ namespace Sass {
309
309
Extension state (complex);
310
310
// ToDo: fine-tune public API
311
311
state.target = target;
312
- state.isOptional = extend-> isOptional () ;
312
+ state.isOptional = is_optional ;
313
313
state.mediaContext = mediaQueryContext;
314
314
315
315
if (sources.hasKey (complex)) {
@@ -349,12 +349,15 @@ namespace Sass {
349
349
350
350
ExtSelExtMap newExtensionsByTarget;
351
351
newExtensionsByTarget.insert (std::make_pair (target, newExtensions));
352
- existingExtensions = extensionsByExtender.find (target);
353
- if (hasExistingExtensions && !existingExtensions->second .empty ()) {
354
- auto additionalExtensions =
355
- extendExistingExtensions (existingExtensions->second , newExtensionsByTarget);
356
- if (!additionalExtensions.empty ()) {
357
- mapCopyExts (newExtensionsByTarget, additionalExtensions);
352
+ // ToDo: do we really need to fetch again (see top off fn)
353
+ auto existingExtensions = extensionsByExtender.find (target);
354
+ if (existingExtensions != extensionsByExtender.end ()) {
355
+ if (hasExistingExtensions && !existingExtensions->second .empty ()) {
356
+ auto additionalExtensions =
357
+ extendExistingExtensions (existingExtensions->second , newExtensionsByTarget);
358
+ if (!additionalExtensions.empty ()) {
359
+ mapCopyExts (newExtensionsByTarget, additionalExtensions);
360
+ }
358
361
}
359
362
}
360
363
@@ -410,30 +413,32 @@ namespace Sass {
410
413
// ##########################################################################
411
414
ExtSelExtMap Extender::extendExistingExtensions (
412
415
// Taking in a reference here makes MSVC debug stuck!?
413
- const std::vector<Extension> oldExtensions,
414
- ExtSelExtMap& newExtensions)
416
+ const std::vector<Extension>& oldExtensions,
417
+ const ExtSelExtMap& newExtensions)
415
418
{
416
419
417
420
ExtSelExtMap additionalExtensions;
418
421
419
- for (Extension extension : oldExtensions) {
422
+ // During the loop `oldExtensions` vector might be changed.
423
+ // Callers normally pass this from `extensionsByExtender` and
424
+ // that points back to the `sources` vector from `extensions`.
425
+ for (size_t i = 0 , iL = oldExtensions.size (); i < iL; i += 1 ) {
426
+ const Extension& extension = oldExtensions[i];
420
427
ExtSelExtMapEntry& sources = extensions[extension.target ];
421
- std::vector<ComplexSelectorObj> selectors;
422
-
423
- selectors = extendComplex (
428
+ std::vector<ComplexSelectorObj> selectors (extendComplex (
424
429
extension.extender ,
425
430
newExtensions,
426
431
extension.mediaContext
427
- );
432
+ )) ;
428
433
429
434
if (selectors.empty ()) {
430
435
continue ;
431
436
}
432
437
433
438
// ToDo: "catch" error from extend
434
439
435
- bool first = false ;
436
- bool containsExtension = ObjEqualityFn (selectors.front (), extension.extender );
440
+ bool first = false , containsExtension =
441
+ ObjEqualityFn (selectors.front (), extension.extender );
437
442
for (const ComplexSelectorObj& complex : selectors) {
438
443
// If the output contains the original complex
439
444
// selector, there's no need to recreate it.
@@ -442,11 +447,11 @@ namespace Sass {
442
447
continue ;
443
448
}
444
449
445
- Extension withExtender = extension.withExtender (complex);
450
+ const Extension withExtender =
451
+ extension.withExtender (complex);
446
452
if (sources.hasKey (complex)) {
447
- Extension source = sources.get (complex);
448
453
sources.insert (complex, mergeExtension (
449
- source , withExtender));
454
+ sources. get (complex) , withExtender));
450
455
}
451
456
else {
452
457
sources.insert (complex, withExtender);
@@ -527,7 +532,7 @@ namespace Sass {
527
532
// ##########################################################################
528
533
std::vector<ComplexSelectorObj> Extender::extendComplex (
529
534
// Taking in a reference here makes MSVC debug stuck!?
530
- const ComplexSelectorObj complex,
535
+ const ComplexSelectorObj& complex,
531
536
const ExtSelExtMap& extensions,
532
537
const CssMediaRuleObj& mediaQueryContext)
533
538
{
@@ -656,7 +661,7 @@ namespace Sass {
656
661
// ##########################################################################
657
662
Extension Extender::extensionForCompound (
658
663
// Taking in a reference here makes MSVC debug stuck!?
659
- const std::vector<SimpleSelectorObj> simples) const
664
+ const std::vector<SimpleSelectorObj>& simples) const
660
665
{
661
666
CompoundSelectorObj compound = SASS_MEMORY_NEW (CompoundSelector, ParserState (" [ext]" ));
662
667
compound->concat (simples);
0 commit comments