UNPKG

7.08 kBSource Map (JSON)View Raw
1{"version":3,"file":"plist.js","names":["_options","require","_util","_dict","xmlDeclaration","xmlDoctype","Plist","XML_DECLARATION","XML_DOCTYPE","value","constructor","getValue","Error","valueCastTo","Type","castTo","valueCastAs","casted","TYPE","fromXml","xml","declaration","doctype","documentElement","xmlDecode","fromXmlElement","element","tagName","childElements","xmlElementChildElements","childElementsL","length","childFromXmlElement","a","ValueDict","toXml","options","ir","indentRoot","INDENT_ROOT","n","newlineString","NEWLINE_STRING","v","filter","s","join","exports"],"sources":["../src/plist.ts"],"sourcesContent":["import {INDENT_ROOT, IToXmlOptions, NEWLINE_STRING} from './options';\nimport {IElement, xmlDecode, xmlElementChildElements} from './util';\nimport {Value} from './value';\nimport {ValueDict} from './value/dict';\n\nconst xmlDeclaration = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>';\nconst xmlDoctype =\n\t'<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">';\n\n/**\n * Plist object.\n */\nexport class Plist {\n\t/**\n\t * Default XML declaration.\n\t */\n\tpublic static readonly XML_DECLARATION = xmlDeclaration;\n\n\t/**\n\t * Default XML doctype.\n\t */\n\tpublic static readonly XML_DOCTYPE = xmlDoctype;\n\n\t/**\n\t * XML declaration.\n\t */\n\tpublic xmlDeclaration = xmlDeclaration;\n\n\t/**\n\t * XML doctype.\n\t */\n\tpublic xmlDoctype = xmlDoctype;\n\n\t/**\n\t * Value element.\n\t */\n\tpublic value: Value | null = null;\n\n\t/**\n\t * Plist constructor.\n\t *\n\t * @param value The value.\n\t */\n\tconstructor(value: Value | null = null) {\n\t\tthis.value = value;\n\t}\n\n\t/**\n\t * Get value or throw if null.\n\t *\n\t * @returns The value.\n\t */\n\tpublic getValue() {\n\t\tconst {value} = this;\n\t\tif (!value) {\n\t\t\tthrow new Error('Value is null');\n\t\t}\n\t\treturn value;\n\t}\n\n\t/**\n\t * Cast to specific type or null.\n\t *\n\t * @param Type Type constructor.\n\t * @returns The object or null.\n\t */\n\tpublic valueCastTo<T extends typeof Value>(Type: T): T['prototype'] | null {\n\t\tconst {value} = this;\n\t\treturn value ? value.castTo(Type) : null;\n\t}\n\n\t/**\n\t * Cast to specific type or throw.\n\t *\n\t * @param Type Type constructor.\n\t * @returns The object.\n\t */\n\tpublic valueCastAs<T extends typeof Value>(Type: T): T['prototype'] {\n\t\tconst casted = this.valueCastTo(Type);\n\t\tif (!casted) {\n\t\t\tthrow new Error(`Cannot cast value to type '${Type.TYPE}'`);\n\t\t}\n\t\treturn casted;\n\t}\n\n\t/**\n\t * Decode document from string.\n\t *\n\t * @param xml XML string.\n\t */\n\tpublic fromXml(xml: string) {\n\t\tconst {declaration, doctype, documentElement} = xmlDecode(xml);\n\t\tthis.fromXmlElement(documentElement, declaration, doctype);\n\t}\n\n\t/**\n\t * Decode document from element.\n\t *\n\t * @param element XML element.\n\t * @param declaration XML declaration.\n\t * @param doctype XML doctype.\n\t */\n\tpublic fromXmlElement(\n\t\telement: Readonly<IElement>,\n\t\tdeclaration: string | null = null,\n\t\tdoctype: string | null = null\n\t) {\n\t\tconst {tagName} = element;\n\t\tif (tagName !== 'plist') {\n\t\t\tthrow new Error(`Unexpected root plist tag name: ${tagName}`);\n\t\t}\n\n\t\tconst childElements = xmlElementChildElements(element);\n\t\tconst childElementsL = childElements.length;\n\t\tif (childElementsL > 1) {\n\t\t\tthrow new Error(`Multiple root plist child tag: ${childElementsL}`);\n\t\t}\n\n\t\tthis.value = childElementsL\n\t\t\t? this.childFromXmlElement(childElements[0])\n\t\t\t: null;\n\t\tthis.xmlDeclaration = declaration || '';\n\t\tthis.xmlDoctype = doctype || '';\n\t}\n\n\t/**\n\t * Decode child element from XML element.\n\t *\n\t * @param element XML element.\n\t * @returns Value element.\n\t */\n\tpublic childFromXmlElement(element: IElement) {\n\t\tconst a = new ValueDict();\n\t\treturn a.childFromXmlElement(element);\n\t}\n\n\t/**\n\t * Encode documents to string.\n\t *\n\t * @param options Options object.\n\t * @returns XML string.\n\t */\n\tpublic toXml(options: Readonly<IToXmlOptions> | null = null) {\n\t\tconst ir = options?.indentRoot ?? INDENT_ROOT;\n\t\tconst n = options?.newlineString ?? NEWLINE_STRING;\n\n\t\tconst v = this.value;\n\t\treturn [\n\t\t\t...[this.xmlDeclaration, this.xmlDoctype].filter(s => s),\n\t\t\t'<plist version=\"1.0\">',\n\t\t\t...(v ? [v.toXml(options, ir ? 1 : 0)] : []),\n\t\t\t'</plist>',\n\t\t\t''\n\t\t].join(n);\n\t}\n}\n"],"mappings":";;;;;;AAAA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AAEA,IAAAE,KAAA,GAAAF,OAAA;AAEA,MAAMG,cAAc,GAAG,wCAAwC;AAC/D,MAAMC,UAAU,GACf,wGAAwG;;AAEzG;AACA;AACA;AACO,MAAMC,KAAK,CAAC;EAClB;AACD;AACA;EACC,OAAuBC,eAAe,GAAGH,cAAc;;EAEvD;AACD;AACA;EACC,OAAuBI,WAAW,GAAGH,UAAU;;EAE/C;AACD;AACA;EACQD,cAAc,GAAGA,cAAc;;EAEtC;AACD;AACA;EACQC,UAAU,GAAGA,UAAU;;EAE9B;AACD;AACA;EACQI,KAAK,GAAiB,IAAI;;EAEjC;AACD;AACA;AACA;AACA;EACCC,WAAWA,CAACD,KAAmB,GAAG,IAAI,EAAE;IACvC,IAAI,CAACA,KAAK,GAAGA,KAAK;EACnB;;EAEA;AACD;AACA;AACA;AACA;EACQE,QAAQA,CAAA,EAAG;IACjB,MAAM;MAACF;IAAK,CAAC,GAAG,IAAI;IACpB,IAAI,CAACA,KAAK,EAAE;MACX,MAAM,IAAIG,KAAK,CAAC,eAAe,CAAC;IACjC;IACA,OAAOH,KAAK;EACb;;EAEA;AACD;AACA;AACA;AACA;AACA;EACQI,WAAWA,CAAyBC,IAAO,EAAyB;IAC1E,MAAM;MAACL;IAAK,CAAC,GAAG,IAAI;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