Permalink
Browse files

Added notations to dtd

  • Loading branch information...
1 parent 4b3beda commit c42251a7f7e0934a4898cc8bf9e90e19552843f8 @oozcitak committed Dec 22, 2013
Showing with 84 additions and 0 deletions.
  1. +62 −0 src/XMLDTDNotation.coffee
  2. +16 −0 src/XMLDocType.coffee
  3. +6 −0 test/doctype.coffee
View
@@ -0,0 +1,62 @@
+_ = require 'underscore'
+
+# Represents a NOTATION entry in the DTD
+module.exports = class XMLDTDNotation
+
+
+ # Initializes a new instance of `XMLDTDNotation`
+ #
+ # `parent` the parent `XMLDocType` element
+ # `name` the name of the notation
+ # `identifierType` type of the identifier (either PUBLIC or SYSTEM)
+ # (defaults to SYSTEM)
+ # `pubID` public identifier
+ # `sysID` system identifier
+ constructor: (parent, name, identifierType, pubID, sysID) ->
+ @stringify = parent.stringify
+
+ if not name?
+ throw new Error "Missing notation name"
+ if not identifierType?
+ throw new Error "Missing identifier"
+ if not pubID? and not sysID?
+ pubID = identifierType
+ identifierType = 'SYSTEM'
+ if not identifierType.match /^(PUBLIC|SYSTEM)$/
+ throw new Error "Invalid identifier type; expected: PUBLIC or SYSTEM"
+ if identifierType.indexOf('SYSTEM') == 0
+ sysID = pubID if not sysID
+ pubID = undefined
+ if not pubID and not sysID
+ throw new Error "Missing identifier"
+
+ @name = @stringify.eleName name
+ @type = identifierType
+ @pubID = @stringify.dtdPubID pubID if pubID?
+ @sysID = @stringify.dtdSysID sysID if sysID?
+
+ # Converts the XML fragment to string
+ #
+ # `options.pretty` pretty prints the result
+ # `options.indent` indentation for pretty print
+ # `options.newline` newline sequence for pretty print
+ toString: (options, level) ->
+ pretty = options?.pretty or false
+ indent = options?.indent or ' '
+ newline = options?.newline or '\n'
+ level or= 0
+
+ space = new Array(level).join(indent)
+
+ r = ''
+
+ r += space if pretty
+
+ r += '<!NOTATION ' + @name + ' ' + @type
+ r += ' "' + @pubID + '"' if @pubID
+ r += ' "' + @sysID + '"' if @sysID
+ r += '>'
+
+ r += newline if pretty
+
+ return r
View
@@ -50,6 +50,20 @@ module.exports = class XMLDocType
return @
+ # Creates a NOTATION declaration
+ #
+ # `name` the name of the notation
+ # `identifierType` type of the identifier (either PUBLIC or SYSTEM)
+ # (defaults to SYSTEM)
+ # `pubID` public identifier
+ # `sysID` system identifier
+ notation: (name, identifierType, pubID, sysID) ->
+ XMLDTDNotation = require './XMLDTDNotation'
+ child = new XMLDTDNotation @, name, identifierType, pubID, sysID
+ @children.push child
+ return @
+
+
# Creates a CDATA node
#
# `value` element text without CDATA delimiters
@@ -137,6 +151,8 @@ module.exports = class XMLDocType
ele: (name, value) -> @element name, value
att: (elementName, attributeName, attributeType, defaultValueType, defaultValue) ->
@attList elementName, attributeName, attributeType, defaultValueType, defaultValue
+ not: (name, identifierType, pubID, sysID) ->
+ @notation name, identifierType, pubID, sysID
dat: (value) -> @cdata value
com: (value) -> @comment value
ins: (target, value) -> @instruction target, value
View
@@ -48,6 +48,9 @@ vows
.com('Image attributes follow')
.att('img', 'height', 'CDATA', '#REQUIRED')
.att('img', 'visible', '(yes|no)', '#DEFAULT', "yes")
+ .not('fs', 'FS Reader 1.0')
+ .not('fs-nt', 'PUBLIC', 'FS Network Reader 1.0', 'http://my.fs.com/reader')
+ .att('img', 'src', 'NOTATION (fs|fs-nt)', '#REQUIRED')
.dat('<owner>John</owner>')
.ele('node')
.root()
@@ -61,6 +64,9 @@ vows
'<!-- Image attributes follow -->' +
'<!ATTLIST img height CDATA #REQUIRED>' +
'<!ATTLIST img visible (yes|no) "yes">' +
+ '<!NOTATION fs SYSTEM "FS Reader 1.0">' +
+ '<!NOTATION fs-nt PUBLIC "FS Network Reader 1.0" "http://my.fs.com/reader">' +
+ '<!ATTLIST img src NOTATION (fs|fs-nt) #REQUIRED>' +
'<![CDATA[<owner>John</owner>]]>' +
'<!ELEMENT node (#PCDATA)>' +
']>' +

0 comments on commit c42251a

Please sign in to comment.