123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- var error_1 = require("../error");
- var options_1 = require("../options");
- var validate_1 = require("../validate");
- var XmlDecl = (function () {
- function XmlDecl(parent, validation, options) {
- this._version = "1.0";
- this._validation = validation;
- this._parent = parent;
- this.encoding = options.encoding;
- this.standalone = options.standalone;
- if (!(0, validate_1.isUndefined)(options.version)) {
- this.version = options.version;
- }
- }
- Object.defineProperty(XmlDecl.prototype, "encoding", {
-
- get: function () {
- return this._encoding;
- },
-
- set: function (encoding) {
- if (this._validation && !(0, validate_1.isUndefined)(encoding)) {
- if (!validateEncoding(encoding)) {
- throw new Error((0, error_1.getContext)(this.up()) + ": declaration"
- + (" encoding attribute " + encoding + " should be a")
- + " valid encoding");
- }
- }
- this._encoding = encoding;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(XmlDecl.prototype, "standalone", {
-
- get: function () {
- return this._standalone;
- },
-
- set: function (standalone) {
- if (this._validation && !(0, validate_1.isUndefined)(standalone)) {
- if (standalone !== "yes" && standalone !== "no") {
- throw new Error((0, error_1.getContext)(this.up()) + ": declaration"
- + (" standalone attribute " + standalone + " should")
- + " be the string 'yes' or the string 'no'");
- }
- }
- this._standalone = standalone;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(XmlDecl.prototype, "version", {
-
- get: function () {
- return this._version;
- },
-
- set: function (version) {
- if (this._validation && !validateVersion(version)) {
- throw new Error((0, error_1.getContext)(this.up()) + ": declaration version"
- + (" attribute " + version + " should be a valid XML")
- + " version");
- }
- this._version = version;
- },
- enumerable: false,
- configurable: true
- });
-
- XmlDecl.prototype.toString = function (options) {
- if (options === void 0) { options = {}; }
- var optionsObj = new options_1.StringOptions(options);
- var quote = optionsObj.doubleQuotes ? '"' : "'";
- var str = "<?xml version=" + quote + this._version + quote;
- if (!(0, validate_1.isUndefined)(this._encoding)) {
- str += " encoding=" + quote + this._encoding + quote;
- }
- if (!(0, validate_1.isUndefined)(this._standalone)) {
- str += " standalone=" + quote + this._standalone + quote;
- }
- str += "?>";
- return str;
- };
-
- XmlDecl.prototype.up = function () {
- return this._parent;
- };
- return XmlDecl;
- }());
- exports.default = XmlDecl;
- function validateEncoding(str) {
- if (str.length === 0) {
- return false;
- }
- var initialChar = str.charCodeAt(0);
- if (!((initialChar >= 0x41 && initialChar <= 0x5A)
- || (initialChar >= 0x61 && initialChar <= 0x7A))) {
- return false;
- }
- for (var i = 1; i < str.length; i++) {
- var char = str.charCodeAt(i);
- if (char === 0x5F
- || char === 0x2D
- || char === 0x2E
- || (char >= 0x30 && char <= 0x39)
- || (char >= 0x41 && char <= 0x5A)
- || (char >= 0x61 && char <= 0x7A)) {
- continue;
- }
- if (i + 1 === str.length) {
- return false;
- }
- return false;
- }
- return true;
- }
- function validateVersion(str) {
- for (var i = 0; i <= 9; i++) {
- if (str === "1." + i) {
- return true;
- }
- }
- return false;
- }
|