-
Notifications
You must be signed in to change notification settings - Fork 97
Oriol.sprite fun #320
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
Open
osenan
wants to merge
16
commits into
develop
Choose a base branch
from
oriol.sprite-fun
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Oriol.sprite fun #320
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3b62437
Feat: New functions addsvg_sprite and rmsvg_sprite to modify sprite maps
osenan 4b2c05e
Update documentation for sprite map functions
osenan 536c288
Update DESCRIPTION
osenan dc2bab2
Update NAMESPACE
osenan 2efa17b
fix: Reduce size of svg examples
osenan 3697232
Update last examples in documentation
osenan b4af868
include unit tests for addsvg_sprite and rmsvg_sprite
osenan 11f21e5
Fix: Typo in rmsvg_sprite documentation
osenan 267129a
Fix:
osenan d33882d
Fix: Add message when id not found
osenan 8dbe91f
first example app version
osenan cd2becc
Feat: add new examples to add_svg_sprite and remove_svg_sprite
osenan bf91b4f
update NAMESPACE
osenan 65ec290
update documentation
osenan 5bcd517
add name as contributor to DESCRIPTION
osenan cc966a1
include svg files for add_svg_sprite examples
osenan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,5 +46,6 @@ Suggests: | |
| DT, | ||
| covr, | ||
| leaflet, | ||
| plotly | ||
| plotly, | ||
| xml2 | ||
| RoxygenNote: 7.1.1 | ||
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,76 @@ | ||
| idExists <- function(id, sprite) { | ||
| sprite_sym <- xml2::xml_text(xml2::xml_find_all( | ||
| sprite, "/*[name()='svg']/*[name()='symbol']/@id")) | ||
| if(id %in% sprite_sym) return(TRUE) else return(FALSE) | ||
|
jchojna marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| #' Add svg to a sprite map | ||
| #' | ||
| #' @param newsvg \code{xml_document} object with svg that will be added | ||
| #' @param sprite \code{xml_document} object with the sprite map | ||
| #' @param id sets the id of the new svg inside the sprite map, if is | ||
| #' NULL it looks for the id inside the \code{newsvg} object. | ||
| #' | ||
| #' @return invisible copy of new sprite map | ||
| #' | ||
| #' @details the sprite map object is modified in place | ||
| #' | ||
| #' @examples | ||
| #' library(xml2) | ||
| #' sprite <- xml_new_root("svg", "version" = "1.1", "xmlns" = "http://www.w3.org/2000/svg") | ||
| #' newsvg <- read_xml("<svg version='1.1' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 68'> | ||
| #' <path d='M55.096 10.462c1.338-1.374.14-3.699-1.322-3.136-1.482.572-1'/> | ||
| #' </svg>") | ||
| #' addsvg_sprite(newsvg, sprite, "new-icon") | ||
| #' @export | ||
| addsvg_sprite <- function(newsvg, sprite, id = NULL) { | ||
| if(!requireNamespace("xml2", quietly = TRUE)) { | ||
| stop("Package \"xml2\" needed for this function to work. | ||
| Please install it", call. = FALSE) | ||
| } | ||
| if(is.null(id)) { | ||
| id <- xml2::xml_attr(newsvg, "id") | ||
| if(is.na(id)) stop("id is NULL and newsvg does not have id") | ||
| } | ||
| stopifnot(!idExists(id, sprite)) | ||
| viewBox <- xml2::xml_attr(newsvg, "viewBox") | ||
| root_newsvg <- xml2::xml_new_root("symbol", "id" = id, | ||
| "viewBox" = viewBox) | ||
| xml2::xml_add_child(root_newsvg, xml2::xml_child(newsvg)) | ||
| xml2::xml_add_child(sprite, | ||
| xml2::xml_find_first(root_newsvg, "/*[name()='symbol']")) | ||
| invisible(sprite) | ||
| } | ||
|
|
||
|
|
||
| #' Remove svg from a sprite map | ||
| #' | ||
| #' @param id symbol id of the svg to be removed | ||
| #' @param sprite \code{xml_document} object with the sprite map | ||
| #' | ||
| #' @return invisible copy of new sprite map | ||
| #' | ||
| #' @details sprite map is modifyed on place | ||
| #' | ||
| #' @examples | ||
| #' library(xml2) | ||
| #' sprite <- xml_new_root("svg", "version" = "1.1", "xmlns" = "http://www.w3.org/2000/svg") | ||
| #' newsvg <- read_xml("<svg version='1.1' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 68'> | ||
| #' <path d='M55.096 10.462c1.338-1.374.14-3.699-1.322-3.136-1.482.572-1'/> | ||
| #' </svg>") | ||
| #' addsvg_sprite(newsvg, sprite, "new-icon") | ||
| #' rmsvg_sprite("new-icon", sprite) | ||
| #' @export | ||
| rmsvg_sprite <- function(id, sprite) { | ||
|
jchojna marked this conversation as resolved.
Outdated
|
||
| if(!requireNamespace("xml2", quietly = TRUE)) { | ||
| stop("Package \"xml2\" needed for this function to work. | ||
| Please install it", call. = FALSE) | ||
| } | ||
| stopifnot(idExists(id, sprite)) | ||
|
jchojna marked this conversation as resolved.
Outdated
|
||
| sym2remove <- xml2::xml_find_first(sprite, | ||
| paste0("/*[name()='svg']/*[name()='symbol'][@id='",id,"']")) | ||
| xml2::xml_remove(sym2remove) | ||
| invisible(sprite) | ||
| } | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,26 @@ | ||
| context("sprite") | ||
|
|
||
| test_that("Function addsvg_sprite add an svg to current sprite",{ | ||
| require(xml2) | ||
| sprite <- xml_new_root("svg", "version" = "1.1", "xmlns" = "http://www.w3.org/2000/svg") | ||
| newsvg <- read_xml("<svg version='1.1' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 68'> | ||
| <path d='M55.096 10.462c1.338-1.374.14-3.699-1.322-3.136-1.482.572-1'/> | ||
| </svg>") | ||
|
|
||
| addsvg_sprite(newsvg, sprite, "shiny-logo") | ||
| expect_equal(xml_attr(xml_child(sprite), "id"), "shiny-logo") | ||
| expect_error(addsvg_sprite(newsvg, sprite, "shiny-logo")) | ||
| }) | ||
|
|
||
| test_that("Function rmsvg_sprite removes the \"id\" svg",{ | ||
| require(xml2) | ||
| sprite <- xml_new_root("svg", "version" = "1.1", "xmlns" = "http://www.w3.org/2000/svg") | ||
| newsvg <- read_xml("<svg version='1.1' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 68'> | ||
| <path d='M55.096 10.462c1.338-1'/> | ||
| </svg>") | ||
|
|
||
| addsvg_sprite(newsvg, sprite, "shiny-logo") | ||
| rmsvg_sprite("shiny-logo", sprite) | ||
| expect_equal(length(xml_children(sprite)), 0) | ||
| expect_error(rmsvg_sprite("shiny-logo", sprite)) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.