1- 'use strict'
2-
3- var xtend = require ( 'xtend' )
4- var svg = require ( 'property-information/svg' )
5- var find = require ( 'property-information/find' )
6- var spaces = require ( 'space-separated-tokens' )
7- var commas = require ( 'comma-separated-tokens' )
8- var entities = require ( 'stringify-entities' )
9- var ccount = require ( 'ccount' )
10- var all = require ( './all' )
11- var constants = require ( './constants' )
12-
13- module . exports = serializeElement
14-
15- function serializeElement ( ctx , node , index , parent ) {
1+ import { svg , find } from 'property-information'
2+ import { stringify as spaces } from 'space-separated-tokens'
3+ import { stringify as commas } from 'comma-separated-tokens'
4+ import { stringifyEntities } from 'stringify-entities'
5+ import { ccount } from 'ccount'
6+ import { all } from './all.js'
7+ import { constants } from './constants.js'
8+
9+ // eslint-disable-next-line complexity
10+ export function element ( ctx , node , index , parent ) {
1611 var schema = ctx . schema
1712 var omit = schema . space === 'svg' ? false : ctx . omit
1813 var parts = [ ]
1914 var selfClosing =
2015 schema . space === 'svg'
2116 ? ctx . closeEmpty
22- : ctx . voids . indexOf ( node . tagName . toLowerCase ( ) ) > - 1
17+ : ctx . voids . includes ( node . tagName . toLowerCase ( ) )
2318 var attrs
2419 var content
2520 var last
@@ -79,7 +74,7 @@ function serializeAttributes(ctx, props) {
7974 var last
8075
8176 for ( key in props ) {
82- if ( props [ key ] != null ) {
77+ if ( props [ key ] !== undefined && props [ key ] !== null ) {
8378 value = serializeAttribute ( ctx , key , props [ key ] )
8479 if ( value ) values . push ( value )
8580 }
@@ -97,6 +92,7 @@ function serializeAttributes(ctx, props) {
9792 return values . join ( '' )
9893}
9994
95+ // eslint-disable-next-line complexity
10096function serializeAttribute ( ctx , key , value ) {
10197 var info = find ( ctx . schema , key )
10298 var quote = ctx . quote
@@ -113,16 +109,17 @@ function serializeAttribute(ctx, key, value) {
113109 }
114110
115111 if (
116- value == null ||
112+ value === undefined ||
113+ value === null ||
117114 value === false ||
118- ( typeof value === 'number' && value !== value )
115+ ( typeof value === 'number' && Number . isNaN ( value ) )
119116 ) {
120117 return ''
121118 }
122119
123- name = entities (
120+ name = stringifyEntities (
124121 info . attribute ,
125- xtend ( ctx . entities , {
122+ Object . assign ( { } , ctx . entities , {
126123 // Always encode without parse errors in non-HTML.
127124 subset :
128125 constants . name [ ctx . schema . space === 'html' ? ctx . valid : 1 ] [ ctx . safe ]
@@ -152,7 +149,7 @@ function serializeAttribute(ctx, key, value) {
152149 typeof value === 'object' && 'length' in value
153150 ? // `spaces` doesn’t accept a second argument, but it’s given here just to
154151 // keep the code cleaner.
155- ( info . commaSeparated ? commas . stringify : spaces . stringify ) ( value , {
152+ ( info . commaSeparated ? commas : spaces ) ( value , {
156153 padLeft : ! ctx . tightLists
157154 } )
158155 : String ( value )
@@ -161,9 +158,9 @@ function serializeAttribute(ctx, key, value) {
161158
162159 // Check unquoted value.
163160 if ( ctx . unquoted ) {
164- result = entities (
161+ result = stringifyEntities (
165162 value ,
166- xtend ( ctx . entities , {
163+ Object . assign ( { } , ctx . entities , {
167164 subset : constants . unquoted [ ctx . valid ] [ ctx . safe ] ,
168165 attribute : true
169166 } )
@@ -180,9 +177,9 @@ function serializeAttribute(ctx, key, value) {
180177
181178 result =
182179 quote +
183- entities (
180+ stringifyEntities (
184181 value ,
185- xtend ( ctx . entities , {
182+ Object . assign ( { } , ctx . entities , {
186183 // Always encode without parse errors in non-HTML.
187184 subset : ( quote === "'" ? constants . single : constants . double ) [
188185 ctx . schema . space === 'html' ? ctx . valid : 1
0 commit comments