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

127 lines 7.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElementChildTextContentSerializer = exports.ElementChildTextContentParser = void 0; exports.AssertElementChildTextContentOptions = AssertElementChildTextContentOptions; exports.ElementChildTextContent = ElementChildTextContent; 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 tag_element_mixin_1 = require("./mixins/tag-element.mixin"); const text_content_element_mixin_1 = require("./mixins/text-content-element.mixin"); const required_property_1 = require("./required-property"); const utilities_2 = require("./utilities"); /** * Asserts that the provided `options` object conforms to the `ElementChildTextContentOptions` type. * * This function checks if the given `options` object meets the criteria of being an instance of `ElementChildTextContentOptions` by verifying the presence of a `tag` property, which is essential for identifying the options as valid for elements with text content. * * @param options - The object to be validated against the `ElementChildTextContentOptions` type. * @throws {Error} Throws an error if the `options` object does not have a `tag` property, indicating it is not a valid `ElementChildTextContentOptions` object. * * @template T - The type parameter that extends the basic HTMLElement interface, specifying the type of element these options are associated with. */ function AssertElementChildTextContentOptions(options) { if (!(0, tag_element_mixin_1.IsTagElementOptions)(options)) { throw new Error('The object is not a ElementChildTextContentOptions. The required property "tag" is missing!'); } } let ElementChildTextContentParser = class ElementChildTextContentParser { 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) { if (element.hasChild(this.tag)) { const rawValue = element.getChildTextContent(this.tag, this.defaultValue, true); if (rawValue !== undefined) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore parsedElement[this.propertyKey] = this.parseValue(rawValue); } } else if (this.required) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore if (parsedElement[this.propertyKey] === undefined) { throw new error_1.RxapXmlParserValidateRequiredError(`Element <${element.name}> child <${this.tag}> text content is required!`, parsedElement.__tag); } } return parsedElement; } }; exports.ElementChildTextContentParser = ElementChildTextContentParser; exports.ElementChildTextContentParser = ElementChildTextContentParser = tslib_1.__decorate([ (0, mixin_1.Mixin)(text_content_element_mixin_1.TextContentElementMixin, tag_element_mixin_1.TagElementMixin), tslib_1.__metadata("design:paramtypes", [String, Object]) ], ElementChildTextContentParser); let ElementChildTextContentSerializer = class ElementChildTextContentSerializer { 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 child = parsedElement[this.propertyKey]; if (child !== undefined) { element.setChildTextContent(this.tag, this.serializeValue(child)); } else if (this.required) { throw new error_1.RxapXmlParserValidateRequiredError(`Element <${element.name}> child <${this.tag}> raw content is required!`, parsedElement.__tag); } } }; exports.ElementChildTextContentSerializer = ElementChildTextContentSerializer; exports.ElementChildTextContentSerializer = ElementChildTextContentSerializer = tslib_1.__decorate([ (0, mixin_1.Mixin)(text_content_element_mixin_1.TextContentElementMixin, tag_element_mixin_1.TagElementMixin), tslib_1.__metadata("design:paramtypes", [String, Object]) ], ElementChildTextContentSerializer); /** * Decorator factory that creates a decorator to parse the text content of a child element specified by a tag. * This decorator can be applied to properties within a class to automatically parse and assign the text content * of a child element from the associated DOM element of the class instance. * * @param {Partial<ElementChildTextContentParserOptions<Value>> | string} optionsOrString - Configuration options for the decorator or a string specifying the tag of the child element. If a string is provided, it is used as the tag name. If an object is provided, it can specify various parsing options including the tag name. * * The options object may include: * - `tag`: A string specifying the tag name of the child element whose text content is to be parsed. * - `required`: A boolean indicating whether the child element is required. If set to true and the child element is missing, an error will be thrown. * - Additional options specific to the parsing and handling of the element's content can also be included. * * @returns {Function} A class property decorator that, when applied, configures the property to parse and store the text content of the specified child element. * * ### Usage * * ```typescript * @ElementChildTextContent({ tag: 'span', required: true }) * public someProperty: string; * ``` * * In this example, `someProperty` will be automatically populated with the text content of a `<span>` element found within the host element of the class instance. If the `<span>` is not found and `required` is set to true, an error will be thrown. */ function ElementChildTextContent(optionsOrString) { return function (target, propertyKey) { let options = optionsOrString === undefined ? { tag: (0, utilities_1.dasherize)(propertyKey) } : typeof optionsOrString === 'string' ? { tag: optionsOrString } : optionsOrString; options = (0, utilities_1.deepMerge)(options, (0, reflect_metadata_1.getMetadata)(metadata_keys_1.ElementParserMetaData.OPTIONS, target, propertyKey) || {}); if (!options.tag) { options.tag = (0, utilities_1.dasherize)(propertyKey); } AssertElementChildTextContentOptions(options); const parser = new ElementChildTextContentParser(propertyKey, options); (0, utilities_2.AddParserToMetadata)(parser, target); const serializer = new ElementChildTextContentSerializer(propertyKey, options); (0, utilities_2.AddSerializerToMetadata)(serializer, target); if (options.required) { (0, required_property_1.RequiredProperty)()(target, propertyKey); } }; } //# sourceMappingURL=element-child-text-content.js.map