UNPKG

@rxap/xml-parser

Version:

Provides a set of decorators and services for parsing and serializing XML documents into TypeScript classes. It simplifies the process of mapping XML elements and attributes to class properties, handling data validation, and serializing objects back into

110 lines 5.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElementTextContentSerializer = exports.ElementTextContentParser = void 0; exports.ElementTextContent = ElementTextContent; const tslib_1 = require("tslib"); const mixin_1 = require("@rxap/mixin"); const reflect_metadata_1 = require("@rxap/reflect-metadata"); const utilities_1 = require("@rxap/utilities"); const error_1 = require("../error"); const metadata_keys_1 = require("./metadata-keys"); const text_content_element_mixin_1 = require("./mixins/text-content-element.mixin"); const required_property_1 = require("./required-property"); const add_parser_to_metadata_1 = require("./utilities/add-parser-to-metadata"); const add_serializer_to_metadata_1 = require("./utilities/add-serializer-to-metadata"); let ElementTextContentParser = class ElementTextContentParser { constructor(propertyKey, options) { this.propertyKey = propertyKey; this.options = options; this.parse = this.parse.bind(this); Reflect.set(this.parse, 'propertyKey', propertyKey); } parse(xmlParser, element, parsedElement) { const rawValue = element.getTextContent(undefined, true); if (!(0, utilities_1.hasIndexSignature)(parsedElement)) { throw new Error('Parsed Element has no index signature!'); } let value = parsedElement[this.propertyKey]; if (typeof rawValue === 'string') { value = this.parseValue(rawValue); } if (value === undefined || value === null || value === '') { if (this.required && this.defaultValue === undefined) { throw new error_1.RxapXmlParserValidateRequiredError(`Element <${parsedElement.__tag}> text content is required!`, parsedElement.__tag); } else if (this.defaultValue !== undefined) { value = this.defaultValue; } } // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore parsedElement[this.propertyKey] = value; return parsedElement; } }; exports.ElementTextContentParser = ElementTextContentParser; exports.ElementTextContentParser = ElementTextContentParser = tslib_1.__decorate([ (0, mixin_1.Mixin)(text_content_element_mixin_1.TextContentElementMixin), tslib_1.__metadata("design:paramtypes", [String, Object]) ], ElementTextContentParser); let ElementTextContentSerializer = class ElementTextContentSerializer { constructor(propertyKey, options) { this.propertyKey = propertyKey; this.options = options; this.serialize = this.serialize.bind(this); Reflect.set(this.serialize, 'propertyKey', propertyKey); } serialize(xmlParser, element, parsedElement) { // @ts-expect-error the propertyKey is set by the property decorator const value = parsedElement[this.propertyKey]; if (value !== undefined) { element.setTextContent(this.serializeValue(value)); } else if (this.required) { throw new error_1.RxapXmlParserValidateRequiredError(`Element <${parsedElement.__tag}> text content is required!`, parsedElement.__tag); } } }; exports.ElementTextContentSerializer = ElementTextContentSerializer; exports.ElementTextContentSerializer = ElementTextContentSerializer = tslib_1.__decorate([ (0, mixin_1.Mixin)(text_content_element_mixin_1.TextContentElementMixin), tslib_1.__metadata("design:paramtypes", [String, Object]) ], ElementTextContentSerializer); /** * Decorator factory that creates a decorator to parse and inject text content from a DOM element into a class property. * * This decorator factory allows customization through `ElementTextContentOptions`. It merges user-provided options * with metadata options (if any) associated with the property. The merged options are then used to create an instance * of `ElementTextContentParser` which is responsible for the actual parsing and assignment of the text content to the * class property. * * If the `required` option is set to true, the property is also decorated with a `RequiredProperty` decorator to enforce * its presence. * * @param {ElementTextContentParserOptions<Value>} [options={}] - Optional configuration options for element text content parsing. * @returns A class property decorator that configures text content parsing based on the provided options. * * @template Value - The expected type of the property's value. * * ### Usage * * ```typescript * class MyComponent { * @ElementTextContent({ selector: '#myElement', required: true }) * public textContent: string; * } * ``` */ function ElementTextContent(options = {}) { return function (target, propertyKey) { options = (0, utilities_1.deepMerge)(options, (0, reflect_metadata_1.getMetadata)(metadata_keys_1.ElementParserMetaData.OPTIONS, target, propertyKey) || {}); const parser = new ElementTextContentParser(propertyKey, options); (0, add_parser_to_metadata_1.AddParserToMetadata)(parser, target); const serializer = new ElementTextContentSerializer(propertyKey, options); (0, add_serializer_to_metadata_1.AddSerializerToMetadata)(serializer, target); if (options.required) { (0, required_property_1.RequiredProperty)()(target, propertyKey); } }; } //# sourceMappingURL=element-text-content.js.map