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

64 lines (63 loc) 4.62 kB
import { RxapElement } from '../element'; import { ParsedElement } from '../elements/parsed-element'; import { XmlParserService } from '../xml-parser.service'; import { XmlSerializerService } from '../xml-serializer.service'; import { ElementParser } from './element.parser'; import { ElementSerializer } from './element.serializer'; import { TagElementOptions, TagElementMixin } from './mixins/tag-element.mixin'; import { TextContentElementOptions, TextContentElementMixin } from './mixins/text-content-element.mixin'; export interface ElementChildTextContentParserOptions<Value> extends TextContentElementOptions<Value>, TagElementOptions { } /** * 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. */ export declare function AssertElementChildTextContentOptions(options: any): asserts options is ElementChildTextContentParserOptions<any>; export interface ElementChildTextContentParser<T extends ParsedElement, Value> extends TextContentElementMixin<Value>, TagElementMixin { } export declare class ElementChildTextContentParser<T extends ParsedElement, Value> implements ElementParser<T> { readonly propertyKey: string; readonly options: ElementChildTextContentParserOptions<Value>; constructor(propertyKey: string, options: ElementChildTextContentParserOptions<Value>); parse(xmlParser: XmlParserService, element: RxapElement, parsedElement: T): T; } export interface ElementChildTextContentSerializerOptions<Value> extends TextContentElementOptions<Value>, TagElementOptions { } export interface ElementChildTextContentSerializer<T extends ParsedElement, Value> extends TextContentElementMixin<Value>, TagElementMixin { } export declare class ElementChildTextContentSerializer<T extends ParsedElement, Value> implements ElementSerializer<T> { readonly propertyKey: string; readonly options: ElementChildTextContentSerializerOptions<Value>; constructor(propertyKey: string, options: ElementChildTextContentSerializerOptions<Value>); serialize(xmlParser: XmlSerializerService, element: RxapElement, parsedElement: T): void; } /** * 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. */ export declare function ElementChildTextContent<Value>(optionsOrString?: Partial<ElementChildTextContentParserOptions<Value> & ElementChildTextContentSerializerOptions<Value>> | string): (target: any, propertyKey: string) => void;