Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support or conjunction #144

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 189 additions & 87 deletions dist/_include-media.scss
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ $media-expressions: (
'handheld': 'handheld',
'landscape': '(orientation: landscape)',
'portrait': '(orientation: portrait)',
'retina2x': '(-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi), (min-resolution: 2dppx)',
'retina3x': '(-webkit-min-device-pixel-ratio: 3), (min-resolution: 350dpi), (min-resolution: 3dppx)'
'retina2x': ('(-webkit-min-device-pixel-ratio: 2)', '(min-resolution: 192dpi)', '(min-resolution: 2dppx)'),
'retina3x': ('(-webkit-min-device-pixel-ratio: 3)', '(min-resolution: 350dpi)', '(min-resolution: 3dppx)')
) !default;


Expand Down Expand Up @@ -171,91 +171,12 @@ $im-no-media-breakpoint: 'desktop' !default;
///
$im-no-media-expressions: ('screen', 'portrait', 'landscape') !default;

////
/// Cross-engine logging engine
/// @author Hugo Giraudel
/// @access private
////


///
/// Log a message either with `@error` if supported
/// else with `@warn`, using `feature-exists('at-error')`
/// to detect support.
///
/// @param {String} $message - Message to log
///
@function log($message) {
@if feature-exists('at-error') {
@error $message;
} @else {
@warn $message;
$_: noop();
}

@return $message;
}


///
/// Wrapper mixin for the log function so it can be used with a more friendly
/// API than `@if log('..') {}` or `$_: log('..')`. Basically, use the function
/// within functions because it is not possible to include a mixin in a function
/// and use the mixin everywhere else because it's much more elegant.
///
/// @param {String} $message - Message to log
///
@mixin log($message) {
@if log($message) {}
}


///
/// Function with no `@return` called next to `@warn` in Sass 3.3
/// to trigger a compiling error and stop the process.
///
@function noop() {}

///
/// Determines whether a list of conditions is intercepted by the static breakpoint.
///
/// @param {Arglist} $conditions - Media query conditions
///
/// @return {Boolean} - Returns true if the conditions are intercepted by the static breakpoint
///
@function im-intercepts-static-breakpoint($conditions...) {
$no-media-breakpoint-value: map-get($breakpoints, $im-no-media-breakpoint);

@if not $no-media-breakpoint-value {
@if log('`#{$im-no-media-breakpoint}` is not a valid breakpoint.') {}
}

@each $condition in $conditions {
@if not map-has-key($media-expressions, $condition) {
$operator: get-expression-operator($condition);
$prefix: get-expression-prefix($operator);
$value: get-expression-value($condition, $operator);

// scss-lint:disable SpaceAroundOperator
@if ($prefix == 'max' and $value <= $no-media-breakpoint-value) or
($prefix == 'min' and $value > $no-media-breakpoint-value) {
@return false;
}
} @else if not index($im-no-media-expressions, $condition) {
@return false;
}
}

@return true;
}

////
/// Parsing engine
/// @author Hugo Giraudel
/// @access private
////


///
/// Get operator of an expression
///
Expand All @@ -278,7 +199,6 @@ $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default;
$_: log('No operator found in `#{$expression}`.');
}


///
/// Get dimension of an expression, based on a found operator
///
Expand All @@ -299,7 +219,6 @@ $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default;
@return $dimension;
}


///
/// Get dimension prefix based on an operator
///
Expand All @@ -311,7 +230,6 @@ $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default;
@return if(index(('<', '<=', '≤'), $operator), 'max', 'min');
}


///
/// Get value of an expression, based on a found operator
///
Expand Down Expand Up @@ -350,7 +268,6 @@ $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default;
@return $value;
}


///
/// Parse an expression to return a valid media-query expression
///
Expand All @@ -373,6 +290,182 @@ $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default;
@return '(#{$prefix}-#{$dimension}: #{$value})';
}

///
/// Get a shallow list of resolved feature queries
///
/// @param {String} $queries - A nested list structure
/// @param {Boolean} $do-parse - Try to parse expressions
///
/// @return {List} - A one item deep nested list like `(a b, c d e)`
///
@function resolve-feature-queries($queries, $do-parse: true) {
$separator: list-separator($queries);
$result: if($separator == 'space', ((), ), ());

@each $query in $queries {
$chainable-queries: ();

@if (type-of($query) == 'list') {
// List item is a list itself
$chainable-queries: resolve-feature-queries($query);
} @else {
// List item is a string
$parsed-query: if($do-parse, parse-expression($query), $query);

@if (type-of($parsed-query) == 'list') {
// Parsed expression is a list
$chainable-queries: resolve-feature-queries($parsed-query, false);
} @else {
// Parsed expression is a string
$chainable-queries: ($parsed-query);
}
}

$result: append-feature-query($result, $chainable-queries, $separator);
}

@return $result;
}

///
/// Combine two query lists as a logical AND / OR operation
///
/// @param {List} $base-queries - The host list
/// @param {List} $append-queries - The list that is being appended
/// @param {String} $separator - Either `space` or `comma`
///
/// @return {List} - A one item deep nested list like `(a b, c d e)`
///
@function append-feature-query($base-queries, $append-queries, $separator) {
$result: if($separator == 'space', (), $base-queries);

@each $append-query in $append-queries {
@if ($separator == 'space') {
// Logical AND
@each $base-query in $base-queries {
$updated-query: join($base-query, $append-query, $separator);
$result: append($result, $updated-query, 'comma');
}
} @else {
// Logical OR
$result: append($result, $append-query, 'comma');
}
}

@return $result;
}

///
/// Parse a list of resolved expressions to return a valid media-query
///
/// @param {List} $queries - A one item deep nested list like `(a b, c d e)`
///
/// @return {String} - A valid media-query string
///
@function parse-media-query($queries) {
$result: null;
$flat-queries: ();
$separator: list-separator($queries);
$conjunction: if($separator == 'space', ' and ', ', ');

@if (type-of($queries) == 'string') {
@return $queries;
}

@each $query in $queries {
@if (type-of($query) == 'list') {
$flat-queries: append($flat-queries, parse-media-query($query));
} @else {
$flat-queries: append($flat-queries, $query);
}
}

@for $i from 1 through length($flat-queries) {
$e: nth($flat-queries, $i);
$result: unquote('#{$result}#{$e}#{if($i != length($flat-queries), $conjunction, '')}');
}

@return $result;
}

////
/// Cross-engine logging engine
/// @author Hugo Giraudel
/// @access private
////


///
/// Log a message either with `@error` if supported
/// else with `@warn`, using `feature-exists('at-error')`
/// to detect support.
///
/// @param {String} $message - Message to log
///
@function log($message) {
@if feature-exists('at-error') {
@error $message;
} @else {
@warn $message;
$_: noop();
}

@return $message;
}


///
/// Wrapper mixin for the log function so it can be used with a more friendly
/// API than `@if log('..') {}` or `$_: log('..')`. Basically, use the function
/// within functions because it is not possible to include a mixin in a function
/// and use the mixin everywhere else because it's much more elegant.
///
/// @param {String} $message - Message to log
///
@mixin log($message) {
@if log($message) {}
}


///
/// Function with no `@return` called next to `@warn` in Sass 3.3
/// to trigger a compiling error and stop the process.
///
@function noop() {}

///
/// Determines whether a list of conditions is intercepted by the static breakpoint.
///
/// @param {Arglist} $conditions - Media query conditions
///
/// @return {Boolean} - Returns true if the conditions are intercepted by the static breakpoint
///
@function im-intercepts-static-breakpoint($conditions...) {
$no-media-breakpoint-value: map-get($breakpoints, $im-no-media-breakpoint);

@if not $no-media-breakpoint-value {
@if log('`#{$im-no-media-breakpoint}` is not a valid breakpoint.') {}
}

@each $condition in $conditions {
@if not map-has-key($media-expressions, $condition) {
$operator: get-expression-operator($condition);
$prefix: get-expression-prefix($operator);
$value: get-expression-value($condition, $operator);

// scss-lint:disable SpaceAroundOperator
@if ($prefix == 'max' and $value <= $no-media-breakpoint-value) or
($prefix == 'min' and $value > $no-media-breakpoint-value) {
@return false;
}
} @else if not index($im-no-media-expressions, $condition) {
@return false;
}
}

@return true;
}

///
/// Slice `$list` between `$start` and `$end` indexes
///
Expand Down Expand Up @@ -554,12 +647,21 @@ $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default;
/// @include media('>=350px', '<tablet', 'retina3x') { }
///
@mixin media($conditions...) {
$is-list-mode: (length($conditions) == 1 and type-of(nth($conditions, 1)) == 'list');

// scss-lint:disable SpaceAroundOperator
@if ($im-media-support and length($conditions) == 0) or
(not $im-media-support and im-intercepts-static-breakpoint($conditions...)) {
(not $im-media-support and im-intercepts-static-breakpoint($conditions...) and not $is-list-mode) {
@content;
} @else if ($im-media-support and $is-list-mode) {
// List mode with AND / OR conditions
@media #{unquote(parse-media-query(resolve-feature-queries(nth($conditions, 1))))} {
@content;
}

} @else if ($im-media-support and length($conditions) > 0) {
@media #{unquote(parse-expression(nth($conditions, 1)))} {
// Legacy mode
@media #{unquote(parse-media-query(parse-expression(nth($conditions, 1))))} {
// Recursive call
@include media(slice($conditions, 2)...) {
@content;
Expand Down
4 changes: 2 additions & 2 deletions src/_config.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ $media-expressions: (
'handheld': 'handheld',
'landscape': '(orientation: landscape)',
'portrait': '(orientation: portrait)',
'retina2x': '(-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi), (min-resolution: 2dppx)',
'retina3x': '(-webkit-min-device-pixel-ratio: 3), (min-resolution: 350dpi), (min-resolution: 3dppx)'
'retina2x': ('(-webkit-min-device-pixel-ratio: 2)', '(min-resolution: 192dpi)', '(min-resolution: 2dppx)'),
'retina3x': ('(-webkit-min-device-pixel-ratio: 3)', '(min-resolution: 350dpi)', '(min-resolution: 3dppx)')
) !default;


Expand Down
13 changes: 11 additions & 2 deletions src/_media.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@
/// @include media('>=350px', '<tablet', 'retina3x') { }
///
@mixin media($conditions...) {
$is-list-mode: (length($conditions) == 1 and type-of(nth($conditions, 1)) == 'list');

// scss-lint:disable SpaceAroundOperator
@if ($im-media-support and length($conditions) == 0) or
(not $im-media-support and im-intercepts-static-breakpoint($conditions...)) {
(not $im-media-support and im-intercepts-static-breakpoint($conditions...) and not $is-list-mode) {
@content;
} @else if ($im-media-support and $is-list-mode) {
// List mode with AND / OR conditions
@media #{unquote(parse-media-query(resolve-feature-queries(nth($conditions, 1))))} {
@content;
}

} @else if ($im-media-support and length($conditions) > 0) {
@media #{unquote(parse-expression(nth($conditions, 1)))} {
// Legacy mode
@media #{unquote(parse-media-query(parse-expression(nth($conditions, 1))))} {
// Recursive call
@include media(slice($conditions, 2)...) {
@content;
Expand Down
Loading