-
-
Notifications
You must be signed in to change notification settings - Fork 204
feat: add srcset support (<img>)
#147
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2879105
Load srcset by default
ushu ed05aa5
Load srcset syntax
ushu 27f6048
Add support for multiple qualifiers: 100w 2x etc.
ushu 6b467d2
Add test
ushu 18e090b
Add doc, mostly copied from #55
ushu 5fb5372
Normalize spaces in generated require statement
ushu 4371750
Extract srcset logic into attrs/srcset.js
ushu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| var SRCSET_REGEXP = /(\s+(\d+w))?(\s+(\d+x))?\s*$/; | ||
|
|
||
| function split(start, value) { | ||
| var matches = []; | ||
|
|
||
| // allow to load several urls in srcsets, separated by commas | ||
| var subMatches = value.split(/,/); | ||
|
|
||
| for(var i = 0; i < subMatches.length; ++i) { | ||
| var subMatch = { | ||
| start: start, | ||
| value: subMatches[i], | ||
| length: subMatches[i].length | ||
| }; | ||
| // save position of the next match | ||
| var next = start + subMatch.length + 1; | ||
|
|
||
| // remove initial spacing | ||
| var space = /^\s+/.exec(subMatch.value); | ||
| if(space) { | ||
| subMatch.value = subMatch.value.substr(space[0].length) | ||
| } | ||
|
|
||
| // remove srcset qualifiers (2x, 110w, etc.) at the end | ||
| var qualifier = SRCSET_REGEXP.exec(subMatch.value); | ||
| if(qualifier) { | ||
| var qualifierLength = qualifier[0].length; | ||
| subMatch.length -= qualifierLength; | ||
| subMatch.value = subMatch.value.substr(0, subMatch.value.length - qualifierLength); | ||
| } | ||
|
|
||
| matches.push(subMatch); | ||
| start = next; | ||
| } | ||
|
|
||
| return matches; | ||
| } | ||
|
|
||
| module.exports = { | ||
| split: split | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this into its own folder && file =>
./lib/attrs/srcset.jsandrequire('./attrs/srcset.js')it