Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
_ = require 'underscore' | ||
|
||
# Represents an attribute list | ||
module.exports = class XMLDTDAttList | ||
|
||
|
||
# Initializes a new instance of `XMLDTDAttList` | ||
# | ||
# `parent` the parent `XMLDocType` element | ||
# `elementName` the name of the element containing this attribute | ||
# `attributeName` attribute name | ||
# `attributeType` type of the attribute (defaults to CDATA) | ||
# `defaultValueType` default value type (either #REQUIRED, #IMPLIED, #FIXED or | ||
# #DEFAULT) (defaults to #IMPLIED) | ||
# `defaultValue` default value of the attribute | ||
# (only used for #FIXED or #DEFAULT) | ||
constructor: (parent, elementName, attributeName, attributeType, defaultValueType, defaultValue) -> | ||
@stringify = parent.stringify | ||
|
||
if not elementName? | ||
throw new Error "Missing DTD element name" | ||
if not attributeName? | ||
throw new Error "Missing DTD attribute name" | ||
if not attributeType | ||
attributeType = 'CDATA' | ||
if not defaultValueType | ||
defaultValueType = '#IMPLIED' | ||
if defaultValueType.indexOf('#') != 0 | ||
defaultValueType = '#' + defaultValueType | ||
if not defaultValueType.match /^(#REQUIRED|#IMPLIED|#FIXED|#DEFAULT)$/ | ||
throw new Error "Invalid default value type; expected: #REQUIRED, #IMPLIED, #FIXED or #DEFAULT" | ||
if defaultValue and not defaultValueType.match /^(#FIXED|#DEFAULT)$/ | ||
throw new Error "Default value only applies to #FIXED or #DEFAULT" | ||
|
||
@elementName = @stringify.eleName elementName | ||
@attributeName = @stringify.attName attributeName | ||
@attributeType = @stringify.dtdAttType attributeType | ||
@defaultValue = @stringify.dtdAttDefault defaultValue | ||
@defaultValueType = defaultValueType | ||
|
||
# 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 += '<!ATTLIST ' + @elementName + ' ' + @attributeName + ' ' + @attributeType | ||
r += ' ' + @defaultValueType if @defaultValueType != '#DEFAULT' | ||
r += ' "' + @defaultValue + '"' if @defaultValue | ||
r += '>' | ||
|
||
r += newline if pretty | ||
|
||
return r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters