"use strict";
/**
 * Liza Context content parser
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework
 *
 *  Liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextParser = void 0;
var ContextParser = /** @class */ (function () {
    function ContextParser() {
    }
    /**
     * Parses HTML content into content associated only with
     * the bucket name, so that a FieldContext can be created
     * with it's associated content
     *
     * @param element_id - ID of the element (bucket name)
     * @param content    - HTML content
     */
    ContextParser.prototype.parse = function (element_id, content) {
        if (content === null) {
            return null;
        }
        return (content.querySelector('#qcontainer_' + element_id) ||
            this._fallbackQuery(content, '[data-field-name="' + element_id + '"]') ||
            this._fallbackQuery(content, '#' + element_id) ||
            this._fallbackQuery(content, '#q_' + element_id + '_0') ||
            null);
    };
    /**
     * Find the sibling label content if it exists
     *
     * This sibling content and its logic could be removed if the HTML structure
     * changed so that fields and labels have unique container elements.
     */
    ContextParser.prototype.findSiblingContent = function (content) {
        var sibling = content.previousElementSibling;
        return sibling !== null &&
            content.tagName === 'DD' &&
            sibling.tagName === 'DT'
            ? sibling
            : null;
    };
    /**
     * Fallback query for sub-questions,
     * displays, answers, stand-alone labels
     * and static elements
     *
     * If the content query returns an element,
     * return the parent of the element
     * unless it's a stand-alone label (DT)
     *
     * @param content - HTML content
     * @param query - query string
     */
    ContextParser.prototype._fallbackQuery = function (content, query) {
        var element = content.querySelector(query);
        if (element === null) {
            return null;
        }
        // Some elements are standalone labels
        if (element.tagName === 'DT') {
            return element;
        }
        return this._getContentParent(element);
    };
    /**
     * Get the content parent (either a DD or a TD)
     *
     * @param element - HTML element
     */
    ContextParser.prototype._getContentParent = function (element) {
        var parent = element.parentElement;
        if (parent === null) {
            return null;
        }
        switch (parent.tagName) {
            case 'DD':
            case 'TD':
                return parent;
        }
        // otherwise, keep looking
        return this._getContentParent(parent);
    };
    return ContextParser;
}());
exports.ContextParser = ContextParser;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ29udGV4dFBhcnNlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIkNvbnRleHRQYXJzZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBbUJHOzs7QUFJSDtJQUFBO0lBeUZBLENBQUM7SUF4RkM7Ozs7Ozs7T0FPRztJQUNILDZCQUFLLEdBQUwsVUFBTSxVQUFrQixFQUFFLE9BQXVCO1FBQy9DLElBQUksT0FBTyxLQUFLLElBQUksRUFBRTtZQUNwQixPQUFPLElBQUksQ0FBQztTQUNiO1FBRUQsT0FBTyxDQUNMLE9BQU8sQ0FBQyxhQUFhLENBQUMsY0FBYyxHQUFHLFVBQVUsQ0FBQztZQUNsRCxJQUFJLENBQUMsY0FBYyxDQUFDLE9BQU8sRUFBRSxvQkFBb0IsR0FBRyxVQUFVLEdBQUcsSUFBSSxDQUFDO1lBQ3RFLElBQUksQ0FBQyxjQUFjLENBQUMsT0FBTyxFQUFFLEdBQUcsR0FBRyxVQUFVLENBQUM7WUFDOUMsSUFBSSxDQUFDLGNBQWMsQ0FBQyxPQUFPLEVBQUUsS0FBSyxHQUFHLFVBQVUsR0FBRyxJQUFJLENBQUM7WUFDdkQsSUFBSSxDQUNMLENBQUM7SUFDSixDQUFDO0lBRUQ7Ozs7O09BS0c7SUFDSCwwQ0FBa0IsR0FBbEIsVUFBbUIsT0FBdUI7UUFDeEMsSUFBTSxPQUFPLEdBQUc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