Skip to content

Commit 0e434f4

Browse files
committed
Fix tight empty SVG close after unquoted attribute
1 parent b684082 commit 0e434f4

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

lib/element.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var greaterThan = '>'
2121
var slash = '/'
2222

2323
// Stringify an element `node`.
24+
// eslint-disable-next-line complexity
2425
function element(ctx, node, index, parent) {
2526
var parentSchema = ctx.schema
2627
var name = node.tagName
@@ -31,6 +32,7 @@ function element(ctx, node, index, parent) {
3132
var root = node
3233
var content
3334
var attrs
35+
var last
3436

3537
if (parentSchema.space === 'html' && name === 'svg') {
3638
ctx.schema = svg
@@ -64,7 +66,15 @@ function element(ctx, node, index, parent) {
6466
value = lessThan + name + (attrs ? space + attrs : '')
6567

6668
if (selfClosing && close) {
67-
if (!ctx.tightClose || attrs.charAt(attrs.length - 1) === slash) {
69+
last = attrs.charAt(attrs.length - 1)
70+
if (
71+
!ctx.tightClose ||
72+
last === slash ||
73+
(ctx.schema.space === 'svg' &&
74+
last &&
75+
last !== quotationMark &&
76+
last !== apostrophe)
77+
) {
6878
value += space
6979
}
7080

test/element.js

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ test('`element`', function(t) {
3737
'should stringify voids with `/` in `closeSelfClosing` and `tightSelfClosing` mode'
3838
)
3939

40+
// This works in a browser. The `/` is not part of the `[src]`.
41+
t.deepEqual(
42+
to(h('img', {src: 'index.jpg'}), {
43+
preferUnquoted: true,
44+
closeSelfClosing: true,
45+
tightSelfClosing: true
46+
}),
47+
'<img src=index.jpg/>',
48+
'should stringify voids with `/` in `closeSelfClosing` and `tightSelfClosing` mode, without space after an unquoted attribute'
49+
)
50+
4051
t.deepEqual(
4152
to(h('img', {title: '/'}), {
4253
preferUnquoted: true,

test/svg.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,18 @@ test('svg', function(t) {
3838
tightSelfClosing: true
3939
}),
4040
'<circle/>',
41-
'should stringify voids with `/` in `closeEmptyElements` and `tightSelfClosing` mode'
41+
'should stringify empties with `/` in `closeEmptyElements` and `tightSelfClosing` mode'
42+
)
43+
44+
// `<circle cx=2 cy=2 r=1/>` does not work in browsers. Needs a space.
45+
t.deepEqual(
46+
to(s('svg', {viewBox: '0 0 4 4'}, s('circle', {cx: 2, cy: 2, r: 1})), {
47+
preferUnquoted: true,
48+
closeEmptyElements: true,
49+
tightSelfClosing: true
50+
}),
51+
'<svg viewBox="0 0 4 4"><circle cx=2 cy=2 r=1 /></svg>',
52+
'should stringify empties with `/` in `closeEmptyElements` and `tightSelfClosing` mode, *with* a space after an unquoted attribute'
4253
)
4354

4455
t.deepEqual(

0 commit comments

Comments
 (0)