Skip to content

Commit

Permalink
Check if valueOf is defined. Fixes #176
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Apr 25, 2018
1 parent 943557d commit d970429
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/Utility.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ isPlainObject = (val) ->
(ctor instanceof ctor) and
(Function.prototype.toString.call(ctor) is Function.prototype.toString.call(Object))

# Gets the primitive value of an object
getValue = (obj) ->
if isFunction obj.valueOf
obj.valueOf()
else
obj

module.exports.assign = assign
module.exports.isFunction = isFunction
module.exports.isObject = isObject
module.exports.isArray = isArray
module.exports.isEmpty = isEmpty
module.exports.isPlainObject = isPlainObject
module.exports.getValue = getValue

12 changes: 6 additions & 6 deletions src/XMLDocumentCB.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ isObject, isFunction, isPlainObject } = require './Utility'
{ isObject, isFunction, isPlainObject, getValue } = require './Utility'

XMLElement = require './XMLElement'
XMLCData = require './XMLCData'
Expand Down Expand Up @@ -82,10 +82,10 @@ module.exports = class XMLDocumentCB

@openCurrent()

name = name.valueOf()
name = getValue name

attributes ?= {}
attributes = attributes.valueOf()
attributes = getValue attributes
# swap argument order: text <-> attributes
if not isObject attributes
[text, attributes] = [attributes, text]
Expand Down Expand Up @@ -121,7 +121,7 @@ module.exports = class XMLDocumentCB
if not @currentNode or @currentNode.children
throw new Error "att() can only be used immediately after an ele() call in callback mode"

name = name.valueOf() if name?
name = getValue(name) if name?

if isObject name # expand if object
for own attName, attValue of name
Expand Down Expand Up @@ -191,8 +191,8 @@ module.exports = class XMLDocumentCB
instruction: (target, value) ->
@openCurrent()

target = target.valueOf() if target?
value = value.valueOf() if value?
target = getValue(target) if target?
value = getValue(value) if value?

if Array.isArray target # expand if array
for insTarget in target
Expand Down
6 changes: 3 additions & 3 deletions src/XMLElement.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ isObject, isFunction } = require './Utility'
{ isObject, isFunction, getValue } = require './Utility'

XMLNode = require './XMLNode'
XMLAttribute = require './XMLAttribute'
Expand Down Expand Up @@ -59,7 +59,7 @@ module.exports = class XMLElement extends XMLNode
# `name` attribute name
# `value` attribute value
attribute: (name, value) ->
name = name.valueOf() if name?
name = getValue(name) if name?

if isObject name # expand if object
for own attName, attValue of name
Expand All @@ -78,7 +78,7 @@ module.exports = class XMLElement extends XMLNode
removeAttribute: (name) ->
if not name?
throw new Error "Missing attribute name"
name = name.valueOf()
name = getValue name

if Array.isArray name # expand if array
for attName in name
Expand Down
14 changes: 7 additions & 7 deletions src/XMLNode.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ isObject, isFunction, isEmpty } = require './Utility'
{ isObject, isFunction, isEmpty, getValue } = require './Utility'

XMLElement = null
XMLCData = null
Expand Down Expand Up @@ -45,12 +45,12 @@ module.exports = class XMLNode
lastChild = null

attributes ?= {}
attributes = attributes.valueOf()
attributes = getValue attributes
# swap argument order: text <-> attributes
if not isObject attributes
[text, attributes] = [attributes, text]

name = name.valueOf() if name?
name = getValue(name) if name?
# expand if array
if Array.isArray name
lastChild = @element item for item in name
Expand Down Expand Up @@ -176,10 +176,10 @@ module.exports = class XMLNode
# `attributes` an object containing name/value pairs of attributes
# `text` element text
node: (name, attributes, text) ->
name = name.valueOf() if name?
name = getValue(name) if name?

attributes or= {}
attributes = attributes.valueOf()
attributes = getValue attributes
# swap argument order: text <-> attributes
if not isObject attributes
[text, attributes] = [attributes, text]
Expand Down Expand Up @@ -265,8 +265,8 @@ module.exports = class XMLNode
# `target` instruction target
# `value` instruction value
instruction: (target, value) ->
target = target.valueOf() if target?
value = value.valueOf() if value?
target = getValue(target) if target?
value = getValue(value) if value?

if Array.isArray target # expand if array
for insTarget in target
Expand Down
11 changes: 11 additions & 0 deletions test/issues/176.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
suite 'Tests specific to issues:', ->
test 'Issue #176 (valueOf fails for Object.Create(null)', ->
obj = Object.create(null)
obj.root = 'node'

eq(
xml(obj).end()
'<?xml version="1.0"?>' +
'<root>node</root>'
)

0 comments on commit d970429

Please sign in to comment.