/*
*
* An XmlReader implementation for loading SGML (including HTML) converting it
* to well formed XML, by adding missing quotes, empty attribute values, ignoring
* duplicate attributes, case folding on tag names, adding missing closing tags
* based on SGML DTD information, and so on.
*
* Copyright (c) 2002 Microsoft Corporation. All rights reserved.
*
* Chris Lovett
*
*/
/* MD 2/27/13
 * This software is being re-used under the Microsoft Public License (MS-PL):
 * -----------------------------------------------------------------------------
 * This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the
 * license, do not use the software.
 *
 * 1. Definitions
 * The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
 * A "contribution" is the original software, or any additions or changes to the software. A "contributor" is any person that distributes
 * its contribution under this license. "Licensed patents" are a contributor's patent claims that read directly on its contribution.
 *
 * 2. Grant of Rights
 * (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each
 *     contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare
 *     derivative works of its contribution, and distribute its contribution or any derivative works that you create.
 * (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each
 *     contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use,
 *     sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution
 *     in the software.
 *
 * 3. Conditions and Limitations
 * (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
 * (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license
 *     from such contributor to the software ends automatically.
 * (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are
 *     present in the software.
 * (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete
 *     copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may
 *     only do so under a license that complies with this license.
 * (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
 *     You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
 *     local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
 * -----------------------------------------------------------------------------
 *
 * Note: edits have also been made to support namespace prefixes. Those changes are tagged with the following comment:
 * // MD - Added support for namespaces
 *
 * In addition, the public classes have been made internal and the namespace has been changed from Sgml to
 * Infragistics.Documents.Excel.Serialization.Excel2007.Sgml
 *
 * Also, I updated the code to work with Silverlight and WinRT.
 */
import { XmlReader } from "igniteui-angular-core";
import { Uri } from "igniteui-angular-core";
import { Node } from "./Node";
import { StringUtilitiesSgml } from "./StringUtilitiesSgml";
import { HWStack } from "./HWStack";
import { NameTable } from "igniteui-angular-core";
import { XmlNamespaceManager } from "igniteui-angular-core";
import { Entity } from "./Entity";
import { markType } from "igniteui-angular-core";
import { StringBuilder } from "igniteui-angular-core";
import { IndexOutOfRangeException } from "igniteui-angular-core";
import { InvalidOperationException } from "igniteui-angular-core";
import { StringUtilities } from "./StringUtilities";
import { StringWriter } from "igniteui-angular-core";
import { XmlTextWriter } from "igniteui-angular-core";
import { stringFormat1, stringCompare1, isLetter, startsWith1 } from "igniteui-angular-core";
import { stringEmpty } from "igniteui-angular-core";
/**
 * @hidden
 */
export class SgmlReader extends XmlReader {
    constructor() {
        super();
        this._bk = null;
        this._bc = null;
        this._bm = 0;
        this._dh = null;
        this._b2 = '\0';
        this._ca = null;
        this._be = null;
        this._bg = null;
        this._a9 = null;
        this._b3 = 0;
        this._c0 = null;
        this._cz = null;
        this._cy = null;
        this._b9 = null;
        this._bn = false;
        this._bf = null;
        this._b4 = 0;
        this._b5 = 0;
        this._bo = false;
        this._cp = null;
        this._ci = null;
        this._bd = null;
        this._cm = null;
        this._b6 = null;
        this._ct = null;
        this._cn = null;
        this._cs = null;
        this._cf = null;
        this._de = 0;
        this._bb = 0;
        this._b0 = true;
        this._dg = null;
        this.c2();
        this._dh = new NameTable();
        this._dg = new XmlNamespaceManager(this._dh);
    }
    get _bl() {
        this._c3(this._c0);
        return this._bk;
    }
    set _bl(a) {
        this._bk = a;
    }
    _c3(a) {
        if (this._bk == null) {
            if (this._bk != null && this._bk.r != null) {
                switch (this._ba) {
                    case 1:
                        this._cp = this._bk.r.toUpperCase();
                        break;
                    case 2:
                        this._cp = this._bk.r.toLowerCase();
                        break;
                    default:
                        this._cp = this._bk.r;
                        break;
                }
                this._bo = StringUtilitiesSgml.a(this._bk.r, "html");
            }
        }
    }
    get _cg() {
        return this._cf;
    }
    set _cg(a) {
        this._cf = a;
    }
    get _co() {
        return this._cn;
    }
    set _co(a) {
        this._cn = a;
    }
    get _cu() {
        return this._ct;
    }
    set _cu(a) {
        this._ct = a;
    }
    get _ck() {
        return this._cs;
    }
    set _ck(a) {
        this._cs = a;
    }
    get _b7() {
        return this._b6;
    }
    set _b7(a) {
        this._b6 = a;
        this.c2();
    }
    get _cw() {
        return this._cm;
    }
    set _cw(a) {
        this._cm = a;
    }
    _c9(a) {
        this._c0 = new Uri(0, a);
    }
    get _cj() {
        return this._ci;
    }
    set _cj(a) {
        this._ci = a;
        this.c2();
        if (this._c0 == null) {
            if (this._ci.indexOf("://") > 0) {
                this._c0 = new Uri(0, this._ci);
            }
        }
    }
    get _b1() {
        return this._b0;
    }
    set _b1(a) {
        this._b0 = a;
    }
    get _ba() {
        return this._bb;
    }
    set _ba(a) {
        this._bb = a;
    }
    get _b8() {
        return this._b9;
    }
    set _b8(a) {
        this._b9 = a;
    }
    _c4(a, ...b) {
        if (this._b8 != null) {
            let c = stringFormat1(a, ...b);
            if (this._bd != this._bc) {
                c = c + "    " + this._bc.r();
                this._bd = this._bc;
                this._b8.an("### Error:" + c);
            }
            else {
                let d = "";
                if (this._bc.ae != null) {
                    d = this._bc.ae.absolutePath;
                }
                this._b8.an("### Error in " + d + "#" + this._bc.u + ", line " + this._bc.n + ", position " + this._bc.o + ": " + c);
            }
        }
    }
    _c5(a, b) {
        this._c4(a, b.toString());
    }
    c2() {
        this._bm = 0;
        this._be = new HWStack(10);
        this._bg = this._bi(null, 9, null);
        this._bg.f = false;
        this._cz = new StringBuilder(0);
        this._cy = new StringBuilder(0);
        this._b4 = 0;
        this._bc = null;
        this._b2 = '\0';
        this._ca = null;
        this._a9 = null;
        this._b3 = 0;
        this._bf = null;
        this._b5 = 0;
        this._bn = false;
    }
    _bi(a, b, c) {
        let d = this._be.i();
        if (d == null) {
            d = new Node();
            this._be.item(this._be.c - 1, d);
        }
        d.o(a, b, c);
        this._bg = d;
        return d;
    }
    _da() {
        let a = this._be.c - 1;
        if (a > 0) {
            let b = this._be.item(a - 1);
            this._be.item(a - 1, this._be.item(a));
            this._be.item(a, b);
        }
    }
    _bh(a) {
        let b = this._bi(a.j, a.p, a.k);
        b.c = a.c;
        b.f = a.f;
        b.q = a.q;
        b.l = a.l;
        b.e = a.e;
        b.m(a);
        this._bg = b;
        return b;
    }
    _c8() {
        if (this._be.c > 1) {
            this._bg = this._be.h();
        }
    }
    _bj() {
        let a = this._be.c - 1;
        if (a > 0) {
            return this._be.item(a);
        }
        return null;
    }
    get_a7() {
        if (this._bm == 3) {
            return 2;
        }
        else if (this._bm == 4) {
            return 3;
        }
        else if (this._bm == 2 || this._bm == 7) {
            return 15;
        }
        return this._bg.p;
    }
    get a7() {
        return this.get_a7();
    }
    get_ai() {
        let a = null;
        if (this._bm == 3) {
            a = this._a9.e;
        }
        else if (this._bm == 4) {
            a = null;
        }
        else {
            a = this._bg.j;
        }
        return a;
    }
    get ai() {
        return this.get_ai();
    }
    get_ag() {
        let a = this.ai;
        let b = this.ak;
        if (b.length != 0) {
            return a.substr(b.length + 1);
        }
        return a;
    }
    get ag() {
        return this.get_ag();
    }
    get_aj() {
        if (this._bm == 3 && StringUtilitiesSgml.a(this._a9.e, "xmlns")) {
            return "http://www.w3.org/2000/xmlns/";
        }
        let a = this.ak;
        switch (a) {
            case "xmlns": return "http://www.w3.org/2000/xmlns/";
            case "xml": return "http://www.w3.org/XML/1998/namespace";
            case "": return stringEmpty();
            default: return this.ah(a);
        }
    }
    get aj() {
        return this.get_aj();
    }
    get_ak() {
        let a = this.ai;
        if (a == null) {
            return stringEmpty();
        }
        let b = a.indexOf(':');
        if (b < 0) {
            return stringEmpty();
        }
        return a.substr(0, b);
    }
    get ak() {
        return this.get_ak();
    }
    get_f() {
        if (this._bm == 3 || this._bm == 4) {
            return true;
        }
        return (this._bg.k != null);
    }
    get f() {
        return this.get_f();
    }
    get_ar() {
        if (this._bm == 3 || this._bm == 4) {
            return this._a9.f;
        }
        return this._bg.k;
    }
    get ar() {
        return this.get_ar();
    }
    get_x() {
        if (this._bm == 3) {
            return this._be.c;
        }
        else if (this._bm == 4) {
            return this._be.c + 1;
        }
        return this._be.c - 1;
    }
    get x() {
        return this.get_x();
    }
    get_z() {
        return this._c0 == null ? "" : this._c0.absoluteUri;
    }
    get z() {
        return this.get_z();
    }
    get_i() {
        if (this._bm == 1 || this._bm == 3 || this._bm == 4) {
            return this._bg.f;
        }
        return false;
    }
    get i() {
        return this.get_i();
    }
    get_h() {
        if (this._bm == 3 || this._bm == 4) {
            return this._a9.b;
        }
        return false;
    }
    get h() {
        return this.get_h();
    }
    get_v() {
        if (this._a9 != null) {
            return this._a9.c;
        }
        return '\0';
    }
    get v() {
        return this.get_v();
    }
    get_a8() {
        for (let a = this._be.c - 1; a > 1; a--) {
            let b = this._be.item(a);
            let c = b.q;
            if (c != 0) {
                return c;
            }
        }
        return 0;
    }
    get a8() {
        return this.get_a8();
    }
    get_as() {
        for (let a = this._be.c - 1; a > 1; a--) {
            let b = this._be.item(a);
            let c = b.l;
            if (c != null) {
                return c;
            }
        }
        return stringEmpty();
    }
    get as() {
        return this.get_as();
    }
    get _df() {
        return this._de;
    }
    set _df(a) {
        this._de = a;
    }
    get_w() {
        if (this._bm == 3 || this._bm == 4) {
            return 0;
        }
        if (this._bg.p == 1 || this._bg.p == 10) {
            return this._bg.h;
        }
        return 0;
    }
    get w() {
        return this.get_w();
    }
    ab(a) {
        if (this._bm != 3 && this._bm != 4) {
            let b = this._bg.i(a);
            if (b >= 0) {
                return this.aa(b);
            }
        }
        return null;
    }
    ac(a, b) {
        return this.ab(a);
    }
    aa(a) {
        if (this._bm != 3 && this._bm != 4) {
            let b = this._bg.b(a);
            if (b != null) {
                return b.f;
            }
        }
        throw new IndexOutOfRangeException(0);
    }
    item(a) {
        return this.aa(a);
    }
    item1(a) {
        return this.ab(a);
    }
    item2(a, b) {
        return this.ac(a, b);
    }
    o(a) {
        let b = this._bg.i(a);
        if (b >= 0) {
            this.aw(b);
            return true;
        }
        return false;
    }
    p(a, b) {
        return this.o(a);
    }
    aw(a) {
        let b = this._bg.b(a);
        if (b != null) {
            this._b3 = a;
            this._a9 = b;
            if (this._bm != 3) {
                this._bg.e = this._bm;
            }
            this._bm = 3;
            return;
        }
        throw new IndexOutOfRangeException(0);
    }
    r() {
        if (this._bg.h > 0) {
            this.aw(0);
            return true;
        }
        return false;
    }
    s() {
        if (this._bm != 3 && this._bm != 4) {
            return this.r();
        }
        if (this._b3 < this._bg.h - 1) {
            this.aw(this._b3 + 1);
            return true;
        }
        return false;
    }
    q() {
        if (this._bm == 3 || this._bm == 4) {
            this._bm = this._bg.e;
            this._a9 = null;
            return true;
        }
        return (this._bg.p == 1);
    }
    get _bp() {
        return this._bo;
    }
    _cx() {
        if (this._bc == null) {
            this._c6();
        }
        return this._bc.ac();
    }
    _c6() {
        this._c3(this._c0);
        if (this._cj != null) {
            this._bc = new Entity(0, "#document", null, this._ci, this._cm);
        }
        else if (this._b6 != null) {
            this._bc = new Entity(2, "#document", null, this._b6, this._cm);
        }
        else {
            throw new InvalidOperationException(1, "You must specify input either via Href or InputStream properties");
        }
        this._bc.d = this._bp;
        this._bc.ak(null, this._c0);
        if (this._bc.ae != null) {
            this._c0 = this._bc.ae;
        }
        if (this._bc.d && this._bk == null) {
            this._cf = "HTML";
            this._c3(this._c0);
        }
    }
    t() {
        if (this._bc == null) {
            this._c6();
        }
        let a = this._bm;
        if (this._bg.g) {
            this._bg.g = false;
            this._bg = this._bj();
            this._bm = this._bg.e;
            return true;
        }
        let b = false;
        while (!b) {
            let t1 = this._bm;
            L0: while (true) {
                switch (t1) {
                    case 0:
                        this._bm = 1;
                        this._bc.k();
                        t1 = 1;
                        continue L0;
                    case 11:
                        if (this._bc.b != null) {
                            this._bc.af();
                            this._bc = this._bc.b;
                        }
                        else {
                            return false;
                        }
                        break;
                    case 2:
                        if (this._ca == this._bg.j) {
                            this._c8();
                            this._bm = 1;
                            t1 = 1;
                            continue L0;
                        }
                        this._c8();
                        b = true;
                        break;
                    case 1:
                        if (this._bg.f) {
                            this._c8();
                        }
                        let c = this._bg;
                        b = this._bv();
                        break;
                    case 6:
                        this._c8();
                        this._bm = 1;
                        b = this._by(this._b2);
                        break;
                    case 10:
                        b = this._bx('<');
                        break;
                    case 7:
                        this._c8();
                        if (this._be.c <= this._b4) {
                            this._bm = 1;
                            if (this._bf != null) {
                                this._bh(this._bf);
                                this._bf = null;
                                this._bm = 1;
                            }
                            else if (this._bg.p == 9) {
                                this._bm = 11;
                                t1 = 11;
                                continue L0;
                            }
                        }
                        b = true;
                        break;
                    case 8:
                        b = this._br();
                        break;
                    case 3:
                        t1 = 4;
                        continue L0;
                    case 4:
                        this._bm = 1;
                        t1 = 1;
                        continue L0;
                    case 5:
                        this._c8();
                        t1 = 1;
                        continue L0;
                    case 9:
                        if (this._bz(this._bc.j, false)) {
                            this._bg.p = 13;
                        }
                        b = true;
                        break;
                }
                break;
            }
            if (b && this._bg.p == 13 && this._de == 2) {
                b = false;
            }
            if (!b && this._bm == 11 && this._be.c > 1) {
                this._b4 = 1;
                this._bm = 7;
                this._bg = this._bj();
                return true;
            }
        }
        if (!this._bn && (this.a7 == 1 || this.a7 == 3 || this.a7 == 4)) {
            this._bn = true;
            if (this._bp && (this.a7 != 1 || stringCompare1(this.ag, "html", StringUtilities.j) != 0)) {
                this._bg.e = this._bm;
                let d = this._bi("html", 1, null);
                this._da();
                this._bg = d;
                d.g = true;
                d.f = false;
                this._bm = 1;
            }
            return true;
        }
        return true;
    }
    _bv() {
        let a = this._bc.j;
        if (a == '<') {
            a = this._bc.k();
            return this._by(a);
        }
        else if (a != "\uffff") {
            if (this._bg.c != null && this._bg.c.e.a == 1) {
                this._b2 = '\0';
                this._bm = 8;
                return false;
            }
            else if (this._bz(a, true)) {
                this._bg.p = 13;
            }
            return true;
        }
        this._bm = 11;
        return false;
    }
    _by(a) {
        if (a == '%') {
            return this._bq();
        }
        if (a == '!') {
            a = this._bc.k();
            if (a == '-') {
                return this._bs();
            }
            else if (a == '[') {
                return this._bt();
            }
            else if (a != '_' && !isLetter(a)) {
                let b = this._bc.y(this._cz, "Recovering", ">");
                this._c4("Ignoring invalid markup '<!" + b + ">");
                return false;
            }
            else {
                let c = this._bc.z(this._cz, SgmlReader._ce, false);
                if (c == "DOCTYPE") {
                    this._c7();
                    if (this.ab("SYSTEM") == null && this.ab("PUBLIC") != null) {
                        this._bg.a("SYSTEM", "", '\"', this._bb == 0);
                    }
                    if (this._b0) {
                        return false;
                    }
                    else {
                        this._bg.p = 10;
                        return true;
                    }
                }
                else {
                    this._c4("Invalid declaration '<!{0}...'.  Expecting '<!DOCTYPE' only.", c);
                    this._bc.y(null, "Recovering", ">");
                    return false;
                }
            }
        }
        else if (a == '?') {
            this._bc.k();
            return this._bw();
        }
        else if (a == '/') {
            return this._bu();
        }
        else {
            return this._bx(a);
        }
    }
    _cr(a) {
        let b = this._bc.z(this._cz, a, false);
        switch (this._bb) {
            case 1:
                b = b.toUpperCase();
                break;
            case 2:
                b = b.toLowerCase();
                break;
        }
        return this._dh.b(b);
    }
    _bx(a) {
        let b = null;
        if (this._bm != 10) {
            if (SgmlReader._cv.indexOf(a) >= 0) {
                this._cz.c = 0;
                this._cz.h('<');
                this._bm = 9;
                return false;
            }
            b = this._cr(SgmlReader._cv);
        }
        else {
            this._bm = 1;
        }
        let c = this._bi(b, 1, null);
        c.f = false;
        this._db(c);
        a = this._bc.l();
        this._dg.ac();
        while (a != "\uffff" && a != '>') {
            if (a == '/') {
                this._dg.h();
                c.f = true;
                a = this._bc.k();
                if (a != '>') {
                    this._c5("Expected empty start tag '/>' sequence instead of '{0}'", a);
                    this._bc.y(null, "Recovering", ">");
                    return false;
                }
                break;
            }
            else if (a == '<') {
                this._c4("Start tag '{0}' is missing '>'", b);
                break;
            }
            let d = this._cr(SgmlReader._cb);
            a = this._bc.l();
            if (d == "," || d == "=" || d == ":" || d == ";") {
                continue;
            }
            let e = null;
            let f = '\0';
            if (a == '=' || a == '\"' || a == '\'') {
                if (a == '=') {
                    this._bc.k();
                    a = this._bc.l();
                }
                if (a == '\'' || a == '\"') {
                    f = a;
                    e = this._cq(this._cz, a);
                }
                else if (a != '>') {
                    let g = SgmlReader._cc;
                    e = this._bc.z(this._cz, g, false);
                }
            }
            if (d.length > 0) {
                let h = c.a(d, e, f, this._bb == 0);
                if (h == null) {
                    this._c4("Duplicate attribute '{0}' ignored", d);
                }
                else {
                    this._dc(c, h);
                    if (startsWith1(h.e, "xmlns:", StringUtilities.j)) {
                        let i = h.e.substr(6);
                        this._dg.x(i, h.f);
                    }
                }
            }
            a = this._bc.l();
        }
        if (a == "\uffff") {
            this._bc.aj("Unexpected EOF parsing start tag '{0}'", b);
        }
        else if (a == '>') {
            this._bc.k();
        }
        if (this.x == 1) {
            if (this._b5 == 1) {
                this._bm = 11;
                return false;
            }
            this._b5++;
        }
        this._dd(c);
        return true;
    }
    _bu() {
        this._dg.h();
        this._bm = 2;
        this._bc.k();
        let a = this._cr(SgmlReader._cv);
        let b = this._bc.l();
        if (b != '>') {
            this._c5("Expected empty start tag '/>' sequence instead of '{0}'", b);
            this._bc.y(null, "Recovering", ">");
        }
        this._bc.k();
        this._ca = a;
        let c = (this._bb == 0);
        this._bg = this._be.item(this._be.c - 1);
        for (let d = this._be.c - 1; d > 0; d--) {
            let e = this._be.item(d);
            if (c && stringCompare1(e.j, a, 1) == 0) {
                this._ca = e.j;
                return true;
            }
            else if (e.j == a) {
                return true;
            }
        }
        this._c4("No matching start tag for '</{0}>'", a);
        this._bm = 1;
        return false;
    }
    _bq() {
        let a = "<%" + this._bc.y(this._cz, "AspNet", "%>") + "%>";
        this._bi(null, 4, a);
        return true;
    }
    _bs() {
        let a = this._bc.k();
        if (a != '-') {
            this._c5("Expecting comment '<!--' but found {0}", a);
            this._bc.y(null, "Comment", ">");
            return false;
        }
        let b = this._bc.y(this._cz, "Comment", "-->");
        let c = b.indexOf("--");
        while (c >= 0) {
            let d = c + 2;
            while (d < b.length && b.charAt(d) == '-') {
                d++;
            }
            if (c > 0) {
                b = b.substr(0, c - 1) + "-" + b.substr(d);
            }
            else {
                b = "-" + b.substr(d);
            }
            c = b.indexOf("--");
        }
        if (b.length > 0 && b.charAt(b.length - 1) == '-') {
            b += " ";
        }
        this._bi(null, 8, b);
        return true;
    }
    _bt() {
        let a = this._bc.k();
        a = this._bc.l();
        let b = this._bc.z(this._cz, SgmlReader._cd, false);
        if (b != "CDATA") {
            this._c4("Expecting CDATA but found '{0}'", b);
            this._bc.y(null, "CDATA", ">");
            return false;
        }
        a = this._bc.l();
        if (a != '[') {
            this._c5("Expecting '[' but found '{0}'", a);
            this._bc.y(null, "CDATA", ">");
            return false;
        }
        let c = this._bc.y(this._cz, "CDATA", "]]>");
        this._bi(null, 4, c);
        return true;
    }
    _c7() {
        let a = this._bc.l();
        let b = this._cr(SgmlReader._ch);
        this._bi(b, 10, null);
        a = this._bc.l();
        if (a != '>') {
            let c = "";
            let d = "";
            let e = "";
            if (a != '[') {
                let f = this._bc.z(this._cz, SgmlReader._ch, false);
                if (f == "PUBLIC") {
                    a = this._bc.l();
                    if (a == '\"' || a == '\'') {
                        d = this._bc.x(this._cz, a);
                        this._bg.a(f, d, a, this._bb == 0);
                    }
                }
                else if (f != "SYSTEM") {
                    this._c4("Unexpected token in DOCTYPE '{0}'", f);
                    this._bc.y(null, "DOCTYPE", ">");
                }
                a = this._bc.l();
                if (a == '\"' || a == '\'') {
                    f = this._dh.b("SYSTEM");
                    e = this._bc.x(this._cz, a);
                    this._bg.a(f, e, a, this._bb == 0);
                }
                a = this._bc.l();
            }
            if (a == '[') {
                c = this._bc.y(this._cz, "Internal Subset", "]");
                this._bg.k = c;
            }
            a = this._bc.l();
            if (a != '>') {
                this._c5("Expecting end of DOCTYPE tag, but found '{0}'", a);
                this._bc.y(null, "DOCTYPE", ">");
            }
            if (this._bk == null) {
                this._cf = b;
                this._cn = d;
                this._ct = e;
                this._cs = c;
                this._c3(this._bc.ae);
            }
        }
        this._bc.k();
    }
    _bw() {
        let a = this._bc.z(this._cz, SgmlReader._cl, false);
        let b = null;
        if (this._bc.j != '?') {
            b = this._bc.y(this._cz, "Processing Instruction", ">");
        }
        else {
            b = this._bc.y(this._cz, "Processing Instruction", ">");
        }
        if (a != "xml") {
            this._bi(this._dh.b(a), 7, b);
            return true;
        }
        return false;
    }
    _bz(a, b) {
        let c = !b || this._bc.g;
        if (b) {
            this._cz.c = 0;
        }
        this._bm = 5;
        while (a != "\uffff") {
            if (a == '<') {
                a = this._bc.k();
                if (a == '/' || a == '!' || a == '?' || isLetter(a)) {
                    this._bm = 6;
                    this._b2 = a;
                    break;
                }
                else {
                    this._cz.h('<');
                    this._cz.h(a);
                    c = false;
                    a = this._bc.k();
                }
            }
            else if (a == '&') {
                this._c1(this._cz, '<');
                c = false;
                a = this._bc.j;
            }
            else {
                if (!this._bc.g) {
                    c = false;
                }
                this._cz.h(a);
                a = this._bc.k();
            }
        }
        let d = this._cz.toString();
        this._bi(null, 3, d);
        return c;
    }
    _cq(a, b) {
        a.c = 0;
        let c = this._bc.k();
        while (c != "\uffff" && c != b) {
            if (c == '&') {
                this._c1(this._cz, b);
                c = this._bc.j;
            }
            else {
                a.h(c);
                c = this._bc.k();
            }
        }
        this._bc.k();
        return a.toString();
    }
    _br() {
        let a = this._bc.g;
        this._cz.c = 0;
        let b = this._bc.j;
        if (this._b2 != '\0') {
            this._c8();
            switch (this._b2) {
                case '!':
                    this._b2 = ' ';
                    return this._bs();
                case '?':
                    this._b2 = ' ';
                    return this._bw();
                case '/':
                    this._bm = 2;
                    return true;
                case ' ': break;
            }
        }
        else {
            b = this._bc.k();
        }
        while (b != "\uffff") {
            if (b == '<') {
                b = this._bc.k();
                if (b == '!') {
                    b = this._bc.k();
                    if (b == '-') {
                        if (a) {
                            this._b2 = ' ';
                            return this._bs();
                        }
                        else {
                            this._b2 = '!';
                            break;
                        }
                    }
                    else {
                        this._cz.h('<');
                        this._cz.h('!');
                        this._cz.h(b);
                        a = false;
                    }
                }
                else if (b == '?') {
                    this._bc.k();
                    if (a) {
                        this._b2 = ' ';
                        return this._bw();
                    }
                    else {
                        this._b2 = '?';
                        break;
                    }
                }
                else if (b == '/') {
                    let c = this._cz.toString();
                    if (this._bu() && this._ca == this._bg.j) {
                        if (a || c == "") {
                            return true;
                        }
                        else {
                            this._b2 = '/';
                            this._cz.c = 0;
                            this._cz.l(c);
                            this._bm = 8;
                            break;
                        }
                    }
                    else {
                        this._cz.c = 0;
                        this._cz.l(c);
                        this._cz.l("</" + this._ca + ">");
                        a = false;
                    }
                }
                else {
                    this._cz.h('<');
                    this._cz.h(b);
                    a = false;
                }
            }
            else {
                if (!this._bc.g && a) {
                    a = false;
                }
                this._cz.h(b);
            }
            b = this._bc.k();
        }
        let d = this._cz.toString();
        this._bi(null, 4, d);
        if (this._b2 == '\0') {
            this._b2 = ' ';
        }
        return true;
    }
    _c1(a, b) {
        let c = this._bc.k();
        if (c == '#') {
            let d = this._bc.s();
            a.l(d);
            c = this._bc.j;
        }
        else {
            this._cy.c = 0;
            while (c != "\uffff" && (isLetter(c) || c == '_' || c == '-')) {
                this._cy.h(c);
                c = this._bc.k();
            }
            let e = this._cy.toString();
            if (this._bk != null && e != "") {
                let f = this._bk.f(e);
                if (f != null) {
                    if (f.e) {
                        a.l(f.t);
                        if (c != b) {
                            c = this._bc.k();
                        }
                        return;
                    }
                    else {
                        let g = new Entity(0, e, f.w, f.aa, this._bc.v);
                        f.ak(this._bc, new Uri(0, f.aa));
                        this._bc = g;
                        this._bc.k();
                        return;
                    }
                }
                else {
                    this._c4("Undefined entity '{0}'", e);
                }
            }
            a.l("&");
            a.l(e);
            if (c != b) {
                a.h(c);
                c = this._bc.k();
            }
        }
    }
    get_d() {
        return this._bm == 11;
    }
    get d() {
        return this.get_d();
    }
    au() {
        this.disposeCore(true);
    }
    disposeCore(a) {
        if (a) {
            if (this._bc != null) {
                this._bc.af();
                this._bc = null;
            }
            if (this._b9 != null) {
                this._b9.i();
                this._b9 = null;
            }
        }
    }
    get_a3() {
        if (this._bm == 0) {
            return 0;
        }
        else if (this._bm == 11) {
            return 3;
        }
        return 1;
    }
    get a3() {
        return this.get_a3();
    }
    aq() {
        if (this._bg.p == 1) {
            this._cz.c = 0;
            while (this.t()) {
                switch (this.a7) {
                    case 4:
                    case 14:
                    case 13:
                    case 3:
                        this._cz.l(this._bg.k);
                        break;
                    default: return this._cz.toString();
                }
            }
            return this._cz.toString();
        }
        return this._bg.k;
    }
    ao() {
        let a = new StringWriter(0);
        {
            let b = new XmlTextWriter(1, a);
            try {
                switch (this.a7) {
                    case 1:
                        this.t();
                        while (!this.d && this.a7 != 15) {
                            b.ac(this, true);
                        }
                        this.t();
                        break;
                    case 2:
                        a.v(this.ar);
                        break;
                    default: break;
                }
            }
            finally {
                if (b != null) {
                    b.dispose();
                }
            }
        }
        return a.toString();
    }
    ap() {
        let a = new StringWriter(0);
        {
            let b = new XmlTextWriter(1, a);
            try {
                b.ac(this, true);
            }
            finally {
                if (b != null) {
                    b.dispose();
                }
            }
        }
        return a.toString();
    }
    get_a5() {
        return this._dh;
    }
    get a5() {
        return this.get_a5();
    }
    ah(a) {
        return this._dg.lookupNamespace(a);
    }
    a1() {
        throw new InvalidOperationException(1, "Not on an entity reference.");
    }
    u() {
        if (this._bm == 3) {
            this._bm = 4;
            return true;
        }
        else if (this._bm == 4) {
            return false;
        }
        throw new InvalidOperationException(1, "Not on an attribute.");
    }
    _db(a) {
        if (this._bk != null) {
            let b = this._bk.d(a.j);
            if (b != null) {
                a.c = b;
                if (b.e.a == 3) {
                    a.f = true;
                }
            }
        }
    }
    _dc(a, b) {
        let c = a.c;
        if (c != null) {
            let d = c.c(b.e);
            if (d != null) {
                b.a = d;
            }
        }
    }
    _dd(a) {
        if (this._bk != null) {
            let b = this._dh.b(a.j.toUpperCase());
            let c = 0;
            let d = this._be.c - 2;
            if (a.c != null) {
                for (c = d; c > 0; c--) {
                    let e = this._be.item(c);
                    if (e.f) {
                        continue;
                    }
                    let f = e.c;
                    if (f != null) {
                        if (f.i == this._bk.r) {
                            break;
                        }
                        if (f.f(b, this._bk)) {
                            break;
                        }
                        else if (!f.g) {
                            break;
                        }
                    }
                    else {
                        break;
                    }
                }
            }
            if (c == 0) {
            }
            else if (c < d) {
                let g = this._be.item(d);
                if (c == d - 1 && b == g.j) {
                }
                else {
                    let h = "";
                    for (let i = d; i >= c + 1; i--) {
                        if (h != "") {
                            h += ",";
                        }
                        let j = this._be.item(i);
                        h += "<" + j.j + ">";
                    }
                    this._c4("Element '{0}' not allowed inside '{1}', closing {2}.", b, g.j, h);
                }
                this._bm = 7;
                this._bf = a;
                this._c8();
                this._b4 = c + 1;
            }
        }
    }
}
SgmlReader.$t = markType(SgmlReader, 'SgmlReader', XmlReader.$);
SgmlReader._ce = " \t\r\n><";
SgmlReader._cv = " \t\r\n=/><";
SgmlReader._cb = " \t\r\n='\"/>";
SgmlReader._cc = " \t\r\n>";
SgmlReader._cd = "\t\r\n[<>";
SgmlReader._ch = " \t\r\n>";
SgmlReader._cl = " \t\r\n?";
//# sourceMappingURL=SgmlReader.js.map