Skip to content

Commit

Permalink
Allow plugins to expose their features too.
Browse files Browse the repository at this point in the history
Plugins can call Sass.add_feature(feature_name) to expose their features
and in doing so get the same feature detection benefits that Sass itself
gets from this capability. Plugin features must begin with a dash to
distinguish them from official features and ensure our namespace is not
polluted.
  • Loading branch information
chriseppstein committed Jun 11, 2013
1 parent 106a915 commit 1b45a55
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 4 additions & 1 deletion doc-src/SASS_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ Thanks to Alexander Pavlov for implementing this.
* You can now detect the presence of a Sass feature using the new function
`sass-supports($feature-name)`. There are no detectable features in this
release, this is provided so that subsequent releases can begin to
use it.
use it. Additionally, plugins can now expose their functionality
through `sass-supports` by calling `Sass.add_feature(feature_name)`. Features
exposed by plugins must begin with a dash to distinguish them from
official features.

### Backwards Incompatibilities -- Must Read!

Expand Down
20 changes: 20 additions & 0 deletions lib/sass/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ module Features
def has_feature?(feature_name)
KNOWN_FEATURES.include?(feature_name)
end

# Add a feature to Sass. Plugins can use this to easily expose their
# availability to end users. Plugins must prefix their feature
# names with a dash to distinguish them from official features.
#
# @example
# Sass.add_feature("-import-globbing")
# Sass.add_feature("-math-cos")
#
#

This comment has been minimized.

Copy link
@nex3

nex3 Jun 14, 2013

Contributor

Nit: extra newline.

# @param feature_name [String] The case sensitive name of the feature to
# check if it exists in this version of Sass. Must begin with a dash.

This comment has been minimized.

Copy link
@nex3

nex3 Jun 14, 2013

Contributor

This is the documentation for has_feature?.

#
# @return [Boolean] whether the feature of that name exists.

This comment has been minimized.

Copy link
@nex3

nex3 Jun 14, 2013

Contributor

Leftover from copying the has_feature? definition.

def add_feature(feature_name)
unless feature_name[0] == ?-
raise ArgumentError.new("Plugin feature names must begin with a dash")
end
KNOWN_FEATURES << feature_name
end
end

extend Features
Expand Down
9 changes: 6 additions & 3 deletions test/sass/functions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -978,10 +978,13 @@ def test_type_of
end

def test_sass_supports
Sass::Features::KNOWN_FEATURES << "my-test-feature"
assert_equal("true", evaluate("sass-supports(my-test-feature)"))
assert_raises ArgumentError do
Sass.add_feature("my-test-feature")
end
Sass.add_feature("-my-test-feature")
assert_equal("true", evaluate("sass-supports(-my-test-feature)"))
assert_equal("false", evaluate("sass-supports(whatisthisidontevenknow)"))
assert_equal("true", evaluate("sass-supports($feature: my-test-feature)"))
assert_equal("true", evaluate("sass-supports($feature: -my-test-feature)"))
end

def test_unit
Expand Down

2 comments on commit 1b45a55

@chriseppstein
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While writing my test, I needed to add a feature to ensure the test was working and it occurred to me that allowing plugins to expose their features good for the ecosystem. However, I now question whether sass-supports is the right name for this function. Perhaps feature-exists, feature-present, or has-feature would be better.

@nex3
Copy link
Contributor

@nex3 nex3 commented on 1b45a55 Jun 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how sass-supports reads like "if sass supports blah", but I agree that it's a little weird with external features. I think I like feature-exists best of the alternatives.

Please sign in to comment.