admin管理员组文章数量:1415653
I need to get the contents of an external css file in order to add it to an svg.
What I'm looking for is in effect a toString method for the css. Is this possible? I haven't been able to find a solution yet.
Here is the solution xml I'm looking for for the svg :
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
".1/DTD/svg11.dtd">
<svg xmlns="" version="1.1"
width="10cm" height="5cm" viewBox="0 0 1000 500">
<defs>
<style type="text/css"><![CDATA[
rect {
fill: red;
stroke: blue;
stroke-width: 3
}
]]></style>
</defs>
<rect x="200" y="100" width="600" height="300"/>
</svg>
I tried using an external link to it but it didn't render the styles :
<?xml version="1.0" standalone="no"?>
<?xml-stylesheet href="mystyle.css" type="text/css"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
".1/DTD/svg11.dtd">
<svg xmlns="" version="1.1"
width="10cm" height="5cm" viewBox="0 0 1000 500">
<rect x="200" y="100" width="600" height="300"/>
</svg>
Edit : I should have added this origionally I have the svg rendering fine within the browser as it has access to the svg.
What I need to do is send the svg to a server for further processing (convert format, save to database..). The problem is that the style is lost as the css is not included in the svg xml. I need to add the css to the svg itself like the first block of code above so I can keep the stylings.
I need to get the contents of an external css file in order to add it to an svg.
What I'm looking for is in effect a toString method for the css. Is this possible? I haven't been able to find a solution yet.
Here is the solution xml I'm looking for for the svg :
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3/2000/svg" version="1.1"
width="10cm" height="5cm" viewBox="0 0 1000 500">
<defs>
<style type="text/css"><![CDATA[
rect {
fill: red;
stroke: blue;
stroke-width: 3
}
]]></style>
</defs>
<rect x="200" y="100" width="600" height="300"/>
</svg>
I tried using an external link to it but it didn't render the styles :
<?xml version="1.0" standalone="no"?>
<?xml-stylesheet href="mystyle.css" type="text/css"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3/2000/svg" version="1.1"
width="10cm" height="5cm" viewBox="0 0 1000 500">
<rect x="200" y="100" width="600" height="300"/>
</svg>
Edit : I should have added this origionally I have the svg rendering fine within the browser as it has access to the svg.
What I need to do is send the svg to a server for further processing (convert format, save to database..). The problem is that the style is lost as the css is not included in the svg xml. I need to add the css to the svg itself like the first block of code above so I can keep the stylings.
Share Improve this question edited Jun 28, 2013 at 9:54 Travis asked Jun 28, 2013 at 9:37 TravisTravis 7057 silver badges30 bronze badges 3- use ajax and then get the style? – gmaliar Commented Jun 28, 2013 at 9:42
- The external link should work provided you serve it with the correct mime type. – Robert Longson Commented Jun 28, 2013 at 9:50
- Are you already able to retrive the content of the css file and you only don't know how to extract the specific part? is the external css of the same domain? – t.niese Commented Jun 28, 2013 at 9:52
1 Answer
Reset to default 7If you are able to use JavaScript you can get the contents of your stylesheet as follows:
[].slice.call(document.styleSheets[1].cssRules).forEach(function(rule){
console.log('rule:', rule.cssText)
})
Where document.styleSheets[1]
is your stylesheet.
本文标签:
版权声明:本文标题:Is it possible to get the contents of an external css file using javascript to add to an svg file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745235250a2649006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论