/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter UniSharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

const Utils = require("./../../../unisharp/Utils");
const StringBuilder = require("./../../../unisharp/StringBuilder");

const EncodingStandard = require("./../../../util/EncodingStandard");
const EncodingWrapper = require("./../../../util/EncodingWrapper");
const UnitextHelper = require("./../uni/UnitextHelper");
const UnitextHtmlGen = require("./UnitextHtmlGen");
const HtmlParser = require("./HtmlParser");
const TextHelper = require("./../../../util/TextHelper");

class HtmlHelper {
    
    static getXmlText(fileName, fileContent = null) {
        if (fileContent === null) 
            fileContent = UnitextHelper.loadDataFromFile(fileName, 0);
        if (fileContent === null) 
            return null;
        if (fileContent.length > 10000000) 
            return null;
        let str = TextHelper.readStringFromBytes(fileContent, false, false);
        if (str === null) 
            return null;
        let i = str.indexOf("encoding=");
        if (i > 0 && (i < 100)) {
            i += 9;
            if (str[i] === '\'' || str[i] === '"') 
                i++;
            let j = 0;
            for (j = i; j < str.length; j++) {
                if (!Utils.isLetterOrDigit(str[j]) && str[j] !== '-') 
                    break;
            }
            if (j > i) {
                let encod = str.substring(i, i + j - i);
                let wr = new EncodingWrapper(EncodingStandard.UNDEFINED, encod);
                str = wr.getString(fileContent, 0, -1);
                return str;
            }
        }
        return str;
    }
    
    static createNode(fileName, fileContent = null, contentType = null) {
        if (fileContent === null) 
            fileContent = UnitextHelper.loadDataFromFile(fileName, 0);
        if (fileContent === null) 
            return null;
        let str = TextHelper.readStringFromBytes(fileContent, false, false);
        if (str === null) 
            return null;
        for (let k = 0; k < 2; k++) {
            let str0 = (k === 0 ? contentType : str);
            if (str0 === null) 
                continue;
            let i = str0.indexOf("charset=");
            if (i > 0) {
                i += 8;
                if (str0[i] === '\'' || str0[i] === '"') 
                    i++;
                let j = 0;
                for (j = i; j < str0.length; j++) {
                    if (!Utils.isLetterOrDigit(str0[j]) && str0[j] !== '-') 
                        break;
                }
                if (j > i) {
                    let encod = str0.substring(i, i + j - i);
                    let wr = new EncodingWrapper(EncodingStandard.UNDEFINED, encod);
                    str = wr.getString(fileContent, 0, -1);
                    break;
                }
            }
        }
        let tmp = new StringBuilder(str);
        let nod = HtmlParser.parse(tmp, false);
        return nod;
    }
    
    static create(nod, dirName, images, pars) {
        let gen = new UnitextHtmlGen();
        return gen.create(nod, dirName, images, pars);
    }
}


module.exports = HtmlHelper