Skip to content

Commit

Permalink
Fix Safari svg whitespace (#4294)
Browse files Browse the repository at this point in the history
* fix-whitespace

* fixed tests

* fixed doc
  • Loading branch information
asturur authored Sep 7, 2017
1 parent 0d36e56 commit 545686e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/mixins/itext.svg_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
* @private
*/
_createTextCharSpan: function(_char, styleDecl, left, top) {
var styleProps = this.getSvgSpanStyles(styleDecl, false),
var styleProps = this.getSvgSpanStyles(styleDecl, _char !== _char.trim()),
fillStyles = styleProps ? 'style="' + styleProps + '"' : '';

return [
Expand Down Expand Up @@ -212,8 +212,12 @@
};
},

/**
* Returns styles-string for svg-export
* @param {Boolean} skipShadow a boolean to skip shadow filter output
* @return {String}
*/
getSvgStyles: function(skipShadow) {

var svgStyle = fabric.Object.prototype.getSvgStyles.call(this, skipShadow);
return svgStyle + ' white-space: pre;';
},
Expand Down
6 changes: 4 additions & 2 deletions src/mixins/object.svg_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@

/**
* Returns styles-string for svg-export
* @param {Boolean} skipShadow a boolean to skip shadow filter output
* @param {Object} style style properties for the span a boolean to skip shadow filter output
* @param {Boolean} useWhiteSpace a boolean to include an additional attribute in the style.
* @return {String}
*/
getSvgSpanStyles: function(style) {
getSvgSpanStyles: function(style, useWhiteSpace) {
var strokeWidth = style.strokeWidth ? 'stroke-width: ' + style.strokeWidth + '; ' : '',
fontFamily = style.fontFamily ? 'font-family: ' + style.fontFamily.replace(/"/g, '\'') + '; ' : '',
fontSize = style.fontSize ? 'font-size: ' + style.fontSize + '; ' : '',
Expand All @@ -82,6 +83,7 @@
fontWeight,
textDecoration,
fill,
useWhiteSpace ? 'white-space: pre; ' : ''
].join('');
},

Expand Down
2 changes: 1 addition & 1 deletion test/unit/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
};

var TEXT_SVG = '\t<g transform="translate(10.5 26.72)">\n\t\t<text xml:space="preserve" font-family="Times New Roman" font-size="40" font-style="normal" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1; white-space: pre;" >\n\t\t\t<tspan x="-10" y="12.57" >x</tspan>\n\t\t</text>\n\t</g>\n';
var TEXT_SVG_JUSTIFIED = '\t<g transform="translate(50.5 26.72)">\n\t\t<text xml:space="preserve" font-family="Times New Roman" font-size="40" font-style="normal" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1; white-space: pre;" >\n\t\t\t<tspan x=\"-60\" y=\"-13.65\" >xxxxxx</tspan>\n\t\t\t<tspan x=\"-60\" y=\"38.78\" >x </tspan>\n\t\t\t<tspan x=\"40\" y=\"38.78\" >y</tspan>\n\t\t</text>\n\t</g>\n';
var TEXT_SVG_JUSTIFIED = '\t<g transform="translate(50.5 26.72)">\n\t\t<text xml:space="preserve" font-family="Times New Roman" font-size="40" font-style="normal" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1; white-space: pre;" >\n\t\t\t<tspan x="-60" y="-13.65" >xxxxxx</tspan>\n\t\t\t<tspan x="-60" y="38.78" style="white-space: pre; ">x </tspan>\n\t\t\t<tspan x=\"40\" y=\"38.78\" >y</tspan>\n\t\t</text>\n\t</g>\n';

test('constructor', function() {
ok(fabric.Text);
Expand Down

0 comments on commit 545686e

Please sign in to comment.