Skip to content

Commit 0010a88

Browse files
committed
support for comma- and space-separated attributes
1 parent d596f2f commit 0010a88

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hast-util-to-dom",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Transform HAST to DOM",
55
"main": "dist/hast-util-to-dom.js",
66
"module": "dist/hast-util-to-dom.mjs",

src/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ function element(node, options = {}) {
124124
name: key,
125125
propertyName: key,
126126
};
127+
127128
const {
128129
name,
129130
propertyName,
@@ -133,10 +134,21 @@ function element(node, options = {}) {
133134
overloadedBoolean,
134135
// numeric,
135136
// positiveNumeric,
136-
// commaSeparated,
137-
// spaceSeparated,
137+
commaSeparated,
138+
spaceSeparated,
138139
} = info;
139-
const value = properties[key];
140+
141+
let value = properties[key];
142+
if (Array.isArray(value)) {
143+
if (commaSeparated) {
144+
value = value.join(', ');
145+
} else if (spaceSeparated) {
146+
value = value.join(' ');
147+
} else {
148+
value = value.join(' ');
149+
}
150+
}
151+
140152
try {
141153
if (mustUseProperty) {
142154
el[propertyName] = value;

src/index.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ describe('hast-util-to-dom', () => {
5555
}],
5656
};
5757
const htmlActual = serializeNodeToHtmlString(toDOM(tree));
58-
const htmlExpected = '<!DOCTYPE html><html><head></head><body></body></html>';
58+
let htmlExpected = '<!DOCTYPE html><html><head></head><body></body></html>';
59+
if (!bowser.x) {
60+
if (bowser.gecko) {
61+
htmlExpected = '<!DOCTYPE html>\n<html><head></head><body></body></html>';
62+
}
63+
}
5964
expect(htmlActual).toEqual(htmlExpected);
6065
});
6166

@@ -108,6 +113,26 @@ describe('hast-util-to-dom', () => {
108113
expect(htmlActual).toEqual(htmlExpected);
109114
});
110115

116+
it('handles space-separated attributes correctly', () => {
117+
const tree = h('div', { class: ['foo', 'bar'] });
118+
const htmlActual = serializeNodeToHtmlString(toDOM(tree));
119+
const htmlExpected = '<div class="foo bar"></div>';
120+
expect(htmlActual).toEqual(htmlExpected);
121+
});
122+
123+
it('handles comma-separated attributes correctly', () => {
124+
const img = 'data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=';
125+
const tree = h('img', { srcSet: [`${img} 1x`, `${img} 2x`] });
126+
const htmlActual = serializeNodeToHtmlString(toDOM(tree));
127+
let htmlExpected = `<img srcset="${img} 1x, ${img} 2x">`;
128+
if (!bowser.x) {
129+
if (bowser.webkit || bowser.blink || bowser.gecko) {
130+
htmlExpected = `<img srcset="${img} 1x, ${img} 2x" />`;
131+
}
132+
}
133+
expect(htmlActual).toEqual(htmlExpected);
134+
});
135+
111136
it('creates a doctype node', () => {
112137
const tree = {
113138
type: 'doctype',

src/utils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ export function serializeNodeToString(node) {
2626
export function serializeNodeToHtmlString(node) {
2727
const serialized = serializeNodeToString(node);
2828
// XMLSerializer puts xmlns on documentElement
29+
const { documentElement: ownerDocumentElement } = node.ownerDocument || node;
30+
const { namespaceURI: ownerNamespaceURI } = ownerDocumentElement || {};
2931
const { namespaceURI } = node.documentElement || node;
30-
if (namespaceURI) {
31-
const { namespaceURI: ownerNamespaceURI } = node.ownerDocument
32-
? node.ownerDocument.documentElement
33-
: node.documentElement;
32+
if (ownerNamespaceURI) {
3433
if (namespaceURI === ownerNamespaceURI) {
35-
return serialized.replace(` xmlns="${namespaceURI}"`, '');
34+
return serialized.replace(` xmlns="${ownerNamespaceURI}"`, '');
3635
}
36+
return serialized.replace(new RegExp(` xmlns="${ownerNamespaceURI}"`, 'g'), '');
3737
}
3838
return serialized;
3939
}

0 commit comments

Comments
 (0)