-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from jamesmartin/handle-malformed-docs
Handle documents that do not contain SVG root elements
- Loading branch information
Showing
17 changed files
with
126 additions
and
78 deletions.
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
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
35 changes: 16 additions & 19 deletions
35
lib/inline_svg/transform_pipeline/transformations/aria_attributes.rb
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
11 changes: 5 additions & 6 deletions
11
lib/inline_svg/transform_pipeline/transformations/class_attribute.rb
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 |
---|---|---|
@@ -1,12 +1,11 @@ | ||
module InlineSvg::TransformPipeline::Transformations | ||
class ClassAttribute < Transformation | ||
def transform(doc) | ||
doc = Nokogiri::XML::Document.parse(doc.to_html) | ||
svg = doc.at_css "svg" | ||
classes = (svg["class"] || "").split(" ") | ||
classes << value | ||
svg["class"] = classes.join(" ") | ||
doc | ||
with_svg(doc) do |svg| | ||
classes = (svg["class"] || "").split(" ") | ||
classes << value | ||
svg["class"] = classes.join(" ") | ||
end | ||
end | ||
end | ||
end |
9 changes: 4 additions & 5 deletions
9
lib/inline_svg/transform_pipeline/transformations/data_attributes.rb
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
13 changes: 7 additions & 6 deletions
13
lib/inline_svg/transform_pipeline/transformations/description.rb
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
module InlineSvg::TransformPipeline::Transformations | ||
class Description < Transformation | ||
def transform(doc) | ||
doc = Nokogiri::XML::Document.parse(doc.to_html) | ||
node = Nokogiri::XML::Node.new('desc', doc) | ||
node.content = value | ||
doc.search('svg desc').each { |node| node.remove } | ||
doc.at_css('svg').prepend_child(node) | ||
doc | ||
with_svg(doc) do |svg| | ||
node = Nokogiri::XML::Node.new("desc", doc) | ||
node.content = value | ||
|
||
svg.search("desc").each { |node| node.remove } | ||
svg.prepend_child(node) | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
module InlineSvg::TransformPipeline::Transformations | ||
class Height < Transformation | ||
def transform(doc) | ||
doc = Nokogiri::XML::Document.parse(doc.to_html) | ||
svg = doc.at_css 'svg' | ||
svg['height'] = self.value | ||
doc | ||
with_svg(doc) do |svg| | ||
svg["height"] = self.value | ||
end | ||
end | ||
end | ||
end |
7 changes: 3 additions & 4 deletions
7
lib/inline_svg/transform_pipeline/transformations/id_attribute.rb
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
module InlineSvg::TransformPipeline::Transformations | ||
class IdAttribute < Transformation | ||
def transform(doc) | ||
doc = Nokogiri::XML::Document.parse(doc.to_html) | ||
svg = doc.at_css 'svg' | ||
svg['id'] = self.value | ||
doc | ||
with_svg(doc) do |svg| | ||
svg["id"] = self.value | ||
end | ||
end | ||
end | ||
end |
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
7 changes: 3 additions & 4 deletions
7
lib/inline_svg/transform_pipeline/transformations/preserve_aspect_ratio.rb
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
module InlineSvg::TransformPipeline::Transformations | ||
class PreserveAspectRatio < Transformation | ||
def transform(doc) | ||
doc = Nokogiri::XML::Document.parse(doc.to_html) | ||
svg = doc.at_css 'svg' | ||
svg['preserveAspectRatio'] = self.value | ||
doc | ||
with_svg(doc) do |svg| | ||
svg["preserveAspectRatio"] = self.value | ||
end | ||
end | ||
end | ||
end |
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
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
module InlineSvg::TransformPipeline::Transformations | ||
class Title < Transformation | ||
def transform(doc) | ||
doc = Nokogiri::XML::Document.parse(doc.to_html) | ||
node = Nokogiri::XML::Node.new('title', doc) | ||
node.content = value | ||
doc.search('svg title').each { |node| node.remove } | ||
doc.at_css('svg').prepend_child(node) | ||
doc | ||
with_svg(doc) do |svg| | ||
node = Nokogiri::XML::Node.new("title", doc) | ||
node.content = value | ||
|
||
svg.search("title").each { |node| node.remove } | ||
svg.prepend_child(node) | ||
end | ||
end | ||
end | ||
end |
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
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
module InlineSvg::TransformPipeline::Transformations | ||
class Width < Transformation | ||
def transform(doc) | ||
doc = Nokogiri::XML::Document.parse(doc.to_html) | ||
svg = doc.at_css 'svg' | ||
svg['width'] = self.value | ||
doc | ||
with_svg(doc) do |svg| | ||
svg["width"] = self.value | ||
end | ||
end | ||
end | ||
end |
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
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
36 changes: 36 additions & 0 deletions
36
spec/transformation_pipeline/transformations/transformation_spec.rb
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,36 @@ | ||
require 'inline_svg' | ||
require 'inline_svg/transform_pipeline' | ||
|
||
describe InlineSvg::TransformPipeline::Transformations::Transformation do | ||
context "#with_svg" do | ||
it "returns a Nokogiri::XML::Document representing the parsed document fragment" do | ||
document = Nokogiri::XML::Document.parse("<svg>Some document</svg>") | ||
|
||
transformation = InlineSvg::TransformPipeline::Transformations::Transformation.new(:irrelevant) | ||
expect(transformation.with_svg(document).to_html).to eq( | ||
"<svg>Some document</svg>\n" | ||
) | ||
end | ||
|
||
it "yields to the block when the document contains an SVG element" do | ||
document = Nokogiri::XML::Document.parse("<svg>Some document</svg>") | ||
svg = document.at_css("svg") | ||
|
||
transformation = InlineSvg::TransformPipeline::Transformations::Transformation.new(:irrelevant) | ||
|
||
expect do |b| | ||
transformation.with_svg(document, &b) | ||
end.to yield_with_args(svg) | ||
end | ||
|
||
it "does not yield if the document does not contain an SVG element at the root" do | ||
document = Nokogiri::XML::Document.parse("<foo>bar</foo><svg>Some document</svg>") | ||
|
||
transformation = InlineSvg::TransformPipeline::Transformations::Transformation.new(:irrelevant) | ||
|
||
expect do |b| | ||
transformation.with_svg(document, &b) | ||
end.not_to yield_control | ||
end | ||
end | ||
end |