-
Notifications
You must be signed in to change notification settings - Fork 166
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
feat: add Svg component #15540
Merged
Merged
feat: add Svg component #15540
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6d10c6f
Added Svg component
mstahv 9600479
Refined implementation, test and comment to Html javadocs
mstahv 257c637
Merge branch 'master' into feature/add-svg-component
mstahv 2aac105
formatter:format
mstahv 26a498f
removed somewhat obsolete test
mstahv 3a32eea
Merge branch 'master' into feature/add-svg-component
taefi b4007ee
cleanup
mstahv 997b5d0
cleanup
mstahv 95e3619
Test updates/clean up
mstahv 48bf7b0
formatter:format
mstahv 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 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
134 changes: 134 additions & 0 deletions
134
flow-server/src/main/java/com/vaadin/flow/component/Svg.java
This file contains 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,134 @@ | ||
/* | ||
* Copyright 2000-2022 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import org.jsoup.helper.DataUtil; | ||
|
||
import com.vaadin.flow.dom.Element; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
/** | ||
* A component that displays a given SVG image. | ||
* <p> | ||
* Note that it is the developer's responsibility to sanitize and remove any | ||
* dangerous parts of the SVG before sending it to the user through this | ||
* component. Passing raw input data to the user will possibly lead to | ||
* cross-site scripting attacks. | ||
* <p> | ||
* Note, because of implementation details, we currently wrap the SVG in a div | ||
* element. This might change in the future. | ||
* | ||
* @author Vaadin Ltd | ||
* @since 24.0 | ||
*/ | ||
public class Svg extends Component { | ||
|
||
/** | ||
* Creates an instance based on the given SVG input. The string must have | ||
* exactly one root element. | ||
* | ||
* @param stream | ||
* the SVG to display | ||
*/ | ||
public Svg(InputStream stream) { | ||
this(); | ||
setSvg(stream); | ||
} | ||
|
||
/** | ||
* Creates an instance based on the given SVG string. The string must have | ||
* exactly one root element. | ||
* | ||
* @param svg | ||
* the SVG to display | ||
*/ | ||
public Svg(String svg) { | ||
this(); | ||
setSvg(svg); | ||
} | ||
|
||
/** | ||
* Creates an empty Svg. | ||
*/ | ||
public Svg() { | ||
super(new Element("div")); | ||
} | ||
|
||
/** | ||
* Sets the graphics shown in this component. | ||
* | ||
* @param svg | ||
* the SVG string | ||
*/ | ||
public void setSvg(String svg) { | ||
if (svg == null || svg.isEmpty()) { | ||
setInnerHtml(""); | ||
} else { | ||
validateAndSet(svg); | ||
} | ||
} | ||
mstahv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Sets the graphics shown in this component. | ||
* | ||
* @param stream | ||
* the input stream where SVG is read from | ||
*/ | ||
public void setSvg(InputStream stream) { | ||
validateAndSet(readSvgStreamAsString(stream)); | ||
} | ||
|
||
private String readSvgStreamAsString(InputStream stream) { | ||
if (stream == null) { | ||
throw new IllegalArgumentException("SVG stream cannot be null"); | ||
} | ||
try { | ||
/* | ||
* Cannot use any of the methods that accept a stream since they all | ||
* parse as a document rather than as a body fragment. The logic for | ||
* reading a stream into a String is the same that is used | ||
* internally by JSoup if you strip away all the logic to guess an | ||
* encoding in case one isn't defined. | ||
*/ | ||
return UTF_8.decode(DataUtil.readToByteBuffer(stream, 0)) | ||
.toString(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Unable to read SVG from stream", e); | ||
} | ||
} | ||
|
||
private void validateAndSet(String svgInput) { | ||
if (!svgInput.startsWith("<svg")) { | ||
// remove possible xml header & doc types | ||
int startIndex = svgInput.indexOf("<svg"); | ||
if (startIndex == -1) { | ||
throw new IllegalArgumentException( | ||
"The content don't appear to be SVG"); | ||
} | ||
svgInput = svgInput.substring(startIndex); | ||
} | ||
setInnerHtml(svgInput); | ||
} | ||
|
||
private void setInnerHtml(String html) { | ||
getElement().setProperty("innerHTML", html); | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
flow-server/src/test/java/com/vaadin/flow/component/SvgTest.java
This file contains 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,97 @@ | ||
/* | ||
* Copyright 2000-2022 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class SvgTest { | ||
|
||
@Test | ||
public void attachedToElement() { | ||
// This will throw an assertion error if the element is not attached to | ||
// the component | ||
new Svg("<svg></svg>").getParent(); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void nullStream() { | ||
new Svg((InputStream) null); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void text() { | ||
new Svg("hello"); | ||
} | ||
|
||
static String TRIVIAL_SVG = """ | ||
<svg> | ||
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /> | ||
</svg>"""; | ||
|
||
static String TRIVIAL_SVG2 = """ | ||
<svg height="140" width="500"> | ||
<ellipse cx="200" cy="80" rx="100" ry="50" | ||
style="fill:yellow;stroke:purple;stroke-width:2" /> | ||
</svg>"""; | ||
|
||
static String SVG_WITH_DOCTYPE_ET_AL = """ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> | ||
<svg width="391" height="391" viewBox="-70.5 -70.5 391 391" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
<rect fill="#fff" stroke="#000" x="-70" y="-70" width="390" height="390"/> | ||
<g opacity="0.8"> | ||
<rect x="25" y="25" width="200" height="200" fill="lime" stroke-width="4" stroke="pink" /> | ||
<circle cx="125" cy="125" r="75" fill="orange" /> | ||
<polyline points="50,150 50,200 200,200 200,100" stroke="red" stroke-width="4" fill="none" /> | ||
<line x1="50" y1="50" x2="200" y2="200" stroke="blue" stroke-width="4" /> | ||
</g> | ||
</svg>"""; | ||
|
||
@Test | ||
public void simpleSvg() { | ||
Svg svg = new Svg(TRIVIAL_SVG); | ||
Assert.assertEquals(TRIVIAL_SVG, getSvgDocumentBody(svg)); | ||
} | ||
|
||
@Test | ||
public void withDocType() { | ||
Svg svg = new Svg(SVG_WITH_DOCTYPE_ET_AL); | ||
Assert.assertTrue(getSvgDocumentBody(svg).startsWith("<svg")); | ||
} | ||
|
||
@Test | ||
public void resetSvg() { | ||
Svg svg = new Svg(TRIVIAL_SVG); | ||
Assert.assertEquals(TRIVIAL_SVG, getSvgDocumentBody(svg)); | ||
svg.setSvg(TRIVIAL_SVG2); | ||
Assert.assertEquals(TRIVIAL_SVG2, getSvgDocumentBody(svg)); | ||
} | ||
|
||
@Test | ||
public void fromStream() { | ||
Svg svg = new Svg(new ByteArrayInputStream(TRIVIAL_SVG.getBytes())); | ||
Assert.assertEquals(TRIVIAL_SVG, getSvgDocumentBody(svg)); | ||
} | ||
|
||
private static String getSvgDocumentBody(Svg svg) { | ||
return svg.getElement().getProperty("innerHTML"); | ||
} | ||
|
||
} |
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.
I suggest to rename the setters for the content of the Svg component to
setContent
, since having asvg
variable and calling asetSvg
method on it is verbose and even a little confusing.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.
I don't agree. That method name would confuse people to pass in only content of the top level svg element.
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.
In Image we have setSrc. Don't like that either, especially in that shorthand form.
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.
I get your concern about a situation that someone might pass children contents of a
svg
instead of including the<svg ...> </svg>
itself as well, but we can address that issue by a good documentation. Also, given the fact that the implementation of thevalidateAndSet
is taking care of the presence of additional contents such as<?xml ...>
or<!DOCTYPE svg... >
, or not having the<svg ...>
in the input, I don't see a reason to add the verbosity.Btw, imagining myself reading/reviewing a code/documentation/tutorial (having a
Svg svg = new Svg();
in the context), I still prefer:over
Maybe this is only my personal taste.