/*
 * 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
 */

package com.pullenti.unitext.internal.uni;

public class UnitextImplantateHelper {

    public static com.pullenti.unitext.UnitextItem clear(com.pullenti.unitext.UnitextItem content, String id, Class<?> typeName, com.pullenti.unitext.UnitextContainerType cntTyp, com.pullenti.unitext.UnitextContainer own, com.pullenti.unisharp.Outargwrapper<Integer> cou) {
        com.pullenti.unitext.UnitextTable tab = (com.pullenti.unitext.UnitextTable)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextTable.class);
        if (tab != null) {
            for (int r = 0; r < tab.getRowsCount(); r++) {
                for (int c = 0; c < tab.getColsCount(); c++) {
                    com.pullenti.unitext.UnitextTablecell cel = tab.getCell(r, c);
                    if (cel != null && cel.content != null) 
                        cel.content = clear(cel.content, id, typeName, cntTyp, null, cou);
                }
            }
            return tab;
        }
        com.pullenti.unitext.UnitextList li = (com.pullenti.unitext.UnitextList)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextList.class);
        if (li != null) {
            for (com.pullenti.unitext.UnitextListitem l : li.items) {
                if (l.prefix != null) 
                    l.prefix = clear(l.prefix, id, typeName, cntTyp, null, cou);
                if (l.content != null) 
                    l.content = clear(l.content, id, typeName, cntTyp, null, cou);
                if (l.sublist != null) 
                    clear(l.sublist, id, typeName, cntTyp, null, cou);
            }
            return li;
        }
        com.pullenti.unitext.UnitextFootnote foot = (com.pullenti.unitext.UnitextFootnote)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextFootnote.class);
        if (foot != null) {
            if (foot.content != null) 
                foot.content = clear(foot.content, id, typeName, cntTyp, null, cou);
            return foot;
        }
        com.pullenti.unitext.UnitextHyperlink hyp = (com.pullenti.unitext.UnitextHyperlink)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextHyperlink.class);
        if (hyp != null) {
            if (hyp.content != null) 
                hyp.content = clear(hyp.content, id, typeName, cntTyp, null, cou);
            return hyp;
        }
        com.pullenti.unitext.UnitextDocblock dbl = (com.pullenti.unitext.UnitextDocblock)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextDocblock.class);
        if (dbl != null) {
            if (dbl.head != null) 
                dbl.head = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(clear(dbl.head, id, typeName, cntTyp, null, cou), com.pullenti.unitext.UnitextContainer.class);
            if (dbl.body != null) 
                dbl.body = clear(dbl.body, id, typeName, cntTyp, null, cou);
            if (dbl.tail != null) 
                dbl.tail = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(clear(dbl.tail, id, typeName, cntTyp, null, cou), com.pullenti.unitext.UnitextContainer.class);
            if (dbl.appendix != null) 
                dbl.appendix = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(clear(dbl.appendix, id, typeName, cntTyp, null, cou), com.pullenti.unitext.UnitextContainer.class);
            return dbl;
        }
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextContainer.class);
        if (cnt == null) 
            return content;
        for (int i = 0; i < cnt.children.size(); i++) {
            com.pullenti.unitext.UnitextItem cc = clear(cnt.children.get(i), id, typeName, cntTyp, cnt, cou);
            if (cc == null) {
            }
            if (cc == cnt.children.get(i) && (cc instanceof com.pullenti.unitext.UnitextContainer) && ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(cc, com.pullenti.unitext.UnitextContainer.class)).children.size() == 0) {
                if (typeName != null) {
                    if (cc.getClass() != typeName) 
                        continue;
                    if (cntTyp != com.pullenti.unitext.UnitextContainerType.UNDEFINED) {
                        if (((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(cc, com.pullenti.unitext.UnitextContainer.class)).typ != cntTyp) 
                            continue;
                    }
                }
                else if (id != null) {
                    if (com.pullenti.unisharp.Utils.stringsNe(cc.id, id)) 
                        continue;
                }
                cnt.children.remove(i);
                i--;
            }
            else 
                com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i, cc);
        }
        if (typeName != null) {
            if (cnt.getClass() != typeName) 
                return content;
            if (cntTyp != com.pullenti.unitext.UnitextContainerType.UNDEFINED) {
                if (cnt.typ != cntTyp) 
                    return content;
            }
        }
        else if (id != null) {
            if (com.pullenti.unisharp.Utils.stringsNe(cnt.id, id)) 
                return content;
        }
        if (cnt.children.size() == 0) 
            return content;
        if (cnt.children.size() == 1) {
            cnt.children.get(0).parent = cnt.parent;
            return cnt.children.get(0);
        }
        if (own == null) 
            return content;
        int ii = own.getChildIndexOf(cnt);
        if (ii < 0) 
            return content;
        own.children.remove(ii);
        com.pullenti.unisharp.Utils.insertToArrayList(own.children, cnt.children, ii);
        for (com.pullenti.unitext.UnitextItem ch : own.children) {
            ch.parent = own;
        }
        cou.value++;
        return own.children.get(ii);
    }

    private static com.pullenti.unitext.UnitextItem _splitTexts(com.pullenti.unitext.UnitextItem content, int begin, int end, com.pullenti.unitext.UnitextContainer own, int lev) {
        if (content == null) 
            return null;
        if (lev > 30) 
            return content;
        if (end < content.beginChar) 
            return content;
        if (begin > content.endChar) 
            return content;
        if (begin <= content.beginChar && end >= content.endChar) 
            return content;
        com.pullenti.unitext.UnitextTable tab = (com.pullenti.unitext.UnitextTable)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextTable.class);
        if (tab != null) {
            com.pullenti.unitext.UnitextTablecell cel = tab._findCellByPos(begin);
            if (cel != null && cel.content != null) 
                cel.content = _splitTexts(cel.content, begin, end, null, lev + 1);
            return tab;
        }
        com.pullenti.unitext.UnitextList li = (com.pullenti.unitext.UnitextList)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextList.class);
        if (li != null) {
            for (com.pullenti.unitext.UnitextListitem l : li.items) {
                if (l.prefix != null) 
                    l.prefix = _splitTexts(l.prefix, begin, end, null, lev + 1);
                if (l.content != null) 
                    l.content = _splitTexts(l.content, begin, end, null, lev + 1);
                if (l.sublist != null) 
                    _splitTexts(l.sublist, begin, end, null, lev + 1);
            }
            return li;
        }
        com.pullenti.unitext.UnitextFootnote foot = (com.pullenti.unitext.UnitextFootnote)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextFootnote.class);
        if (foot != null) {
            if (foot.content != null) 
                foot.content = _splitTexts(foot.content, begin, end, null, lev + 1);
            return foot;
        }
        com.pullenti.unitext.UnitextHyperlink hyp = (com.pullenti.unitext.UnitextHyperlink)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextHyperlink.class);
        if (hyp != null) {
            if (hyp.content != null) 
                hyp.content = _splitTexts(hyp.content, begin, end, null, lev + 1);
            return hyp;
        }
        com.pullenti.unitext.UnitextDocblock dbl = (com.pullenti.unitext.UnitextDocblock)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextDocblock.class);
        if (dbl != null) {
            if (dbl.head != null) {
                com.pullenti.unitext.UnitextContainer hh = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(_splitTexts(dbl.head, begin, end, null, lev + 1), com.pullenti.unitext.UnitextContainer.class);
                if (hh.children.contains(dbl)) {
                }
                dbl.head = hh;
            }
            if (dbl.body != null) 
                dbl.body = _splitTexts(dbl.body, begin, end, null, lev + 1);
            if (dbl.tail != null) 
                dbl.tail = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(_splitTexts(dbl.tail, begin, end, null, lev + 1), com.pullenti.unitext.UnitextContainer.class);
            if (dbl.appendix != null) 
                dbl.appendix = _splitTexts(dbl.appendix, begin, end, null, lev + 1);
            return dbl;
        }
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextContainer.class);
        if (cnt != null) {
            for (int i = 0; i < cnt.children.size(); i++) {
                com.pullenti.unitext.UnitextItem ch = cnt.children.get(i);
                if ((ch.endChar < begin) || ch.endChar == 0) 
                    continue;
                if (ch.beginChar > end) {
                    if (end == (begin - 1) && ch.beginChar <= begin) {
                    }
                    else 
                        break;
                }
                com.pullenti.unitext.UnitextItem cc = _splitTexts(ch, begin, end, cnt, lev + 1);
                if (cc == null) {
                }
                else 
                    cc.parent = cnt;
                com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i, cc);
            }
            return content;
        }
        com.pullenti.unitext.UnitextPlaintext txt = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextPlaintext.class);
        if (txt != null) {
            if (begin == txt.beginChar && end == txt.endChar) 
                return txt;
            boolean creat = false;
            if (own == null) {
                own = com.pullenti.unitext.UnitextContainer._new402(txt.parent, txt.beginChar, txt.endChar, ("cnt0_" + txt.beginChar + "_" + txt.endChar));
                own.children.add(txt);
                creat = true;
            }
            int ii = own.getChildIndexOf(content);
            if (ii < 0) 
                return content;
            int jj = ii;
            own.children.remove(ii);
            if (begin > txt.beginChar) {
                com.pullenti.unitext.UnitextPlaintext b = txt.removeStart(begin - txt.beginChar);
                if (b != null) {
                    own.children.add(jj, b);
                    jj++;
                }
            }
            if (end < txt.endChar) {
                com.pullenti.unitext.UnitextPlaintext e = txt.removeEnd(txt.endChar - end);
                if (e != null) 
                    own.children.add(jj, e);
            }
            own.children.add(jj, txt);
            txt.parent = own;
            if (creat) 
                return own;
            else 
                return own.children.get(ii);
        }
        return content;
    }

    public static com.pullenti.unitext.UnitextItem implantate(com.pullenti.unitext.UnitextItem content, com.pullenti.unitext.UnitextContainer impl, String text, com.pullenti.unitext.UnitextContainer own) {
        if (content == null || content == impl) 
            return content;
        if (impl.endChar < content.beginChar) 
            return content;
        if (impl.beginChar > content.endChar) 
            return content;
        if (impl.parent != null) 
            return content;
        if ((impl.beginChar == content.beginChar && impl.endChar == content.endChar && own == null) && !(content instanceof com.pullenti.unitext.UnitextContainer)) {
            impl.parent = content.parent;
            impl.children.add(content);
            content.parent = impl;
            return impl;
        }
        if (impl.beginChar == 0 && impl.endChar == 0 && (content instanceof com.pullenti.unitext.UnitextContainer)) {
            com.pullenti.unitext.UnitextContainer cnt1 = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextContainer.class);
            int i;
            for (i = 0; i < cnt1.children.size(); i++) {
                if (cnt1.children.get(i).endChar > 0) 
                    break;
            }
            impl.parent = cnt1;
            cnt1.children.add(i, impl);
            return impl;
        }
        content = _splitTexts(content, impl.beginChar, impl.endChar, own, 0);
        if (content == null) 
            return null;
        if (content == impl) 
            return content;
        if (impl.beginChar <= content.getPlaintextNsPos(text) && content.getPlaintextNsPos1(text) <= impl.endChar) {
            if ((content instanceof com.pullenti.unitext.UnitextDocblock) && com.pullenti.unisharp.Utils.stringsCompare((((com.pullenti.unitext.UnitextDocblock)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextDocblock.class)).typname != null ? ((com.pullenti.unitext.UnitextDocblock)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextDocblock.class)).typname : ""), "Footnote", true) == 0) {
                com.pullenti.unitext.UnitextDocblock db = (com.pullenti.unitext.UnitextDocblock)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextDocblock.class);
                if (db.body != null) {
                    db.body = implantate(db.body, impl, text, null);
                    return db.body;
                }
            }
            if (((impl.typ == com.pullenti.unitext.UnitextContainerType.NUMBER || impl.typ == com.pullenti.unitext.UnitextContainerType.NAME)) && (content instanceof com.pullenti.unitext.UnitextContainer) && ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextContainer.class)).typ == com.pullenti.unitext.UnitextContainerType.HEAD) {
                for (com.pullenti.unitext.UnitextItem ch : ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextContainer.class)).children) {
                    ch.parent = impl;
                    impl.children.add(ch);
                }
                impl.parent = content.parent;
            }
            else {
                com.pullenti.unitext.UnitextContainer cnt1 = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextContainer.class);
                if (cnt1 != null) {
                    for (int i = 0; i < cnt1.children.size(); i++) {
                        com.pullenti.unitext.UnitextItem ch = cnt1.children.get(i);
                        if (ch.endChar == 0) 
                            continue;
                        if (ch.beginChar <= impl.beginChar && impl.endChar <= ch.endChar) {
                            com.pullenti.unitext.UnitextItem ch1 = implantate(ch, impl, text, null);
                            if (ch1 == null) 
                                return null;
                            com.pullenti.unisharp.Utils.putArrayValue(cnt1.children, i, ch1);
                            ch1.parent = cnt1;
                            return content;
                        }
                    }
                }
                impl.children.add(content);
                impl.parent = content.parent;
                content.parent = impl;
            }
            if (impl.endChar > content.endChar || (impl.beginChar < content.beginChar)) {
                impl.beginChar = content.beginChar;
                impl.endChar = content.endChar;
            }
            if (own != null) {
                int ii = own.getChildIndexOf(content);
                if (ii >= 0) {
                    com.pullenti.unisharp.Utils.putArrayValue(own.children, ii, impl);
                    impl.parent = own;
                }
                else 
                    return content;
            }
            return impl;
        }
        com.pullenti.unitext.UnitextTable tab = (com.pullenti.unitext.UnitextTable)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextTable.class);
        if (tab != null) {
            com.pullenti.unitext.UnitextTablecell cel = tab._findCellByPos(impl.beginChar);
            if (cel != null) {
                if (cel.content != null) 
                    cel.content = implantate(cel.content, impl, text, null);
                else {
                    cel.content = impl;
                    impl.parent = cel;
                }
            }
            return tab;
        }
        com.pullenti.unitext.UnitextList li = (com.pullenti.unitext.UnitextList)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextList.class);
        if (li != null) {
            for (com.pullenti.unitext.UnitextListitem l : li.items) {
                if (l.beginChar <= impl.beginChar && impl.beginChar <= l.endChar) {
                    if (l.prefix != null && impl.parent == null) 
                        l.prefix = implantate(l.prefix, impl, text, null);
                    if (l.content != null && impl.parent == null) 
                        l.content = implantate(l.content, impl, text, null);
                    if (l.sublist != null && impl.parent == null) 
                        implantate(l.sublist, impl, text, null);
                }
            }
            return li;
        }
        com.pullenti.unitext.UnitextFootnote foot = (com.pullenti.unitext.UnitextFootnote)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextFootnote.class);
        if (foot != null) {
            if (foot.content != null) 
                foot.content = implantate(foot.content, impl, text, null);
            return foot;
        }
        com.pullenti.unitext.UnitextHyperlink hyp = (com.pullenti.unitext.UnitextHyperlink)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextHyperlink.class);
        if (hyp != null) {
            if (hyp.content != null) 
                hyp.content = implantate(hyp.content, impl, text, null);
            return hyp;
        }
        com.pullenti.unitext.UnitextDocblock dbl = (com.pullenti.unitext.UnitextDocblock)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextDocblock.class);
        if (dbl != null) {
            if (dbl.head != null) {
                com.pullenti.unitext.UnitextContainer hh = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(implantate(dbl.head, impl, text, null), com.pullenti.unitext.UnitextContainer.class);
                if (hh.children.contains(dbl)) {
                }
                dbl.head = hh;
            }
            if (dbl.body != null && impl.parent == null) 
                dbl.body = implantate(dbl.body, impl, text, null);
            if (dbl.tail != null && impl.parent == null) 
                dbl.tail = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(implantate(dbl.tail, impl, text, null), com.pullenti.unitext.UnitextContainer.class);
            if (dbl.appendix != null && impl.parent == null) 
                dbl.appendix = implantate(dbl.appendix, impl, text, null);
            return dbl;
        }
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextContainer.class);
        if (cnt != null) {
            for (int i = 0; i < cnt.children.size(); i++) {
                if (i == 49) {
                }
                com.pullenti.unitext.UnitextItem ch = cnt.children.get(i);
                if (ch.endChar == 0) 
                    continue;
                if (impl.endChar < ch.beginChar) {
                    if (impl.endChar == (impl.beginChar - 1) && impl.beginChar == ch.beginChar) {
                    }
                    else 
                        continue;
                }
                if (impl.beginChar > ch.endChar) 
                    continue;
                if (((ch instanceof com.pullenti.unitext.UnitextFootnote) || (ch instanceof com.pullenti.unitext.UnitextTable) || (ch instanceof com.pullenti.unitext.UnitextList)) || (ch instanceof com.pullenti.unitext.UnitextDocblock)) {
                    implantate(ch, impl, text, cnt);
                    if (impl.parent != null) 
                        break;
                    continue;
                }
                if (((ch instanceof com.pullenti.unitext.UnitextHyperlink) && ch.beginChar != impl.beginChar) || ch.endChar > impl.endChar) {
                    implantate(ch, impl, text, cnt);
                    if (impl.parent != null) 
                        break;
                    continue;
                }
                if (ch instanceof com.pullenti.unitext.UnitextComment) 
                    continue;
                if (ch.beginChar > ch.endChar) 
                    continue;
                int i1 = impl.getPlaintextNsPos(text);
                int i2 = ch.getPlaintextNsPos(text);
                int e1 = impl.getPlaintextNsPos1(text);
                int e2 = ch.getPlaintextNsPos1(text);
                if (i1 > i2 || ((i1 == i2 && (e1 < e2)))) {
                    if (ch instanceof com.pullenti.unitext.UnitextContainer) {
                        com.pullenti.unitext.UnitextItem cc = implantate(ch, impl, text, null);
                        if (cc == null) {
                        }
                        com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i, cc);
                    }
                    continue;
                }
                int j;
                for (j = i; j < cnt.children.size(); j++) {
                    ch = cnt.children.get(j);
                    if (impl.endChar < ch.beginChar) {
                        if (impl.endChar == (impl.beginChar - 1) && impl.beginChar == ch.beginChar) {
                            cnt.children.add(j, impl);
                            impl.parent = cnt;
                            return content;
                        }
                        break;
                    }
                    if (ch == impl) 
                        return content;
                    impl.children.add(ch);
                    ch.parent = impl;
                }
                if (j > i) {
                    com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i, impl);
                    impl.parent = cnt;
                    if (j > (i + 1)) 
                        for(int jjj = i + 1 + j - i - 1 - 1, mmm = i + 1; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                    impl.endChar = impl.children.get(impl.children.size() - 1).endChar;
                    impl.beginChar = impl.children.get(0).beginChar;
                }
                break;
            }
            return content;
        }
        impl.children.add(content);
        impl.parent = content.parent;
        content.parent = impl;
        if (own != null) {
            int ii = own.getChildIndexOf(content);
            if (ii >= 0) {
                com.pullenti.unisharp.Utils.putArrayValue(own.children, ii, impl);
                impl.parent = own;
            }
            else 
                return content;
        }
        return impl;
    }

    public static boolean _isTableChar(char ch) {
        return ((int)ch) == 7 || ((int)ch) == 0x1E || ((int)ch) == 0x1F;
    }

    public static com.pullenti.unitext.UnitextItem implantateBlock(com.pullenti.unitext.UnitextItem content, int beginHead, int beginBody, int beginTail, int beginAppendix, int end, String text, com.pullenti.unitext.UnitextContainer own, com.pullenti.unisharp.Outargwrapper<com.pullenti.unitext.UnitextDocblock> res) {
        if (content == null) 
            return null;
        if (end < content.beginChar) 
            return content;
        if (beginHead > content.endChar) 
            return content;
        if ((beginHead < beginBody) && (beginBody < end)) 
            content = _splitTexts(content, beginHead, beginBody - 1, own, 0);
        com.pullenti.unitext.UnitextTable tab = (com.pullenti.unitext.UnitextTable)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextTable.class);
        if (tab != null) {
            int bh = beginHead;
            for (; bh < text.length(); bh++) {
                if (!_isTableChar(text.charAt(bh))) 
                    break;
            }
            int bb = beginBody;
            if (bb < bh) 
                bb = bh;
            for (int r = 0; r < tab.getRowsCount(); r++) {
                for (int c = 0; c < tab.getColsCount(); c++) {
                    com.pullenti.unitext.UnitextTablecell cel = tab.getCell(r, c);
                    if (cel == null || cel.content == null) 
                        continue;
                    if (cel.getColEnd() > c) 
                        c = cel.getColEnd();
                    if (cel.beginChar <= bh && bh <= cel.endChar && cel.getRowBegin() == cel.getRowEnd()) {
                        if ((bh < bb) && cel.endChar > bh && bb > cel.endChar) {
                            com.pullenti.unitext.UnitextDocblock res0 = com.pullenti.unitext.UnitextDocblock._new403(bh, cel.endChar);
                            res0.head = com.pullenti.unitext.UnitextContainer._new94(com.pullenti.unitext.UnitextContainerType.HEAD);
                            ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(res0.head, com.pullenti.unitext.UnitextContainer.class)).children.add(cel.content);
                            res0.head.beginChar = bh;
                            res0.head.endChar = cel.endChar;
                            cel.content = res0;
                            if (res.value == null) 
                                res.value = res0;
                            return tab;
                        }
                        cel.content = implantateBlock(cel.content, beginHead, beginBody, beginTail, beginAppendix, end, text, null, res);
                    }
                    else if (cel.beginChar <= bb && bb <= cel.endChar) {
                        if ((bh < bb) && cel.endChar > bh && bb > cel.endChar) {
                            com.pullenti.unitext.UnitextDocblock res0 = com.pullenti.unitext.UnitextDocblock._new403(bh, cel.endChar);
                            res0.body = com.pullenti.unitext.UnitextContainer._new94(com.pullenti.unitext.UnitextContainerType.HEAD);
                            ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(res0.body, com.pullenti.unitext.UnitextContainer.class)).children.add(cel.content);
                            res0.body.beginChar = bb;
                            res0.body.endChar = cel.endChar;
                            cel.content = res0;
                            if (res.value == null) 
                                res.value = res0;
                            return tab;
                        }
                        cel.content = implantateBlock(cel.content, beginHead, beginBody, beginTail, beginAppendix, end, text, null, res);
                    }
                }
            }
            return tab;
        }
        com.pullenti.unitext.UnitextList li = (com.pullenti.unitext.UnitextList)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextList.class);
        if (li != null) {
            for (com.pullenti.unitext.UnitextListitem l : li.items) {
                if (l.beginChar <= beginHead && beginHead <= l.endChar) {
                    if (l.content != null) 
                        l.content = implantateBlock(l.content, beginHead, beginBody, beginTail, beginAppendix, end, text, null, res);
                    if (l.sublist != null) 
                        implantateBlock(l.sublist, beginHead, beginBody, beginTail, beginAppendix, end, text, null, res);
                }
            }
            return li;
        }
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(content, com.pullenti.unitext.UnitextContainer.class);
        if (cnt != null) {
            for (int i = 0; i < cnt.children.size(); i++) {
                com.pullenti.unitext.UnitextItem ch = cnt.children.get(i);
                if (ch.isWhitespaces()) 
                    continue;
                if (end < ch.beginChar) 
                    break;
                if ((ch instanceof com.pullenti.unitext.UnitextTable) || (ch instanceof com.pullenti.unitext.UnitextList) || (ch instanceof com.pullenti.unitext.UnitextDocblock)) {
                }
                if (beginHead > ch.getPlaintextNsPos(text)) {
                    if ((beginHead < ch.endChar) && (ch instanceof com.pullenti.unitext.UnitextTable)) {
                        implantateBlock(ch, beginHead, beginBody, beginTail, beginAppendix, end, text, null, res);
                        return content;
                    }
                    continue;
                }
                if (ch != cnt.children.get(i)) {
                }
                ch = cnt.children.get(i);
                if (ch instanceof com.pullenti.unitext.UnitextDocblock) 
                    continue;
                if ((ch instanceof com.pullenti.unitext.UnitextList) && (beginHead < beginBody) && ch.getPlaintextNsPos(text) == beginHead) {
                    com.pullenti.unitext.UnitextList list = (com.pullenti.unitext.UnitextList)com.pullenti.unisharp.Utils.cast(ch, com.pullenti.unitext.UnitextList.class);
                    int jj = i + 1;
                    for (com.pullenti.unitext.UnitextListitem it : list.items) {
                        if (it.prefix != null) {
                            it.prefix.parent = cnt;
                            cnt.children.add(jj++, it.prefix);
                            com.pullenti.unitext.UnitextPlaintext sp = com.pullenti.unitext.UnitextPlaintext._new57(" ");
                            sp.beginChar = it.prefix.endChar + 1;
                            sp.endChar = sp.beginChar;
                            sp.parent = cnt;
                            cnt.children.add(jj++, sp);
                        }
                        if (it.content != null) {
                            it.content.parent = cnt;
                            cnt.children.add(jj++, it.content);
                        }
                        if (it == list.items.get(list.items.size() - 1) && it.sublist == null) 
                            continue;
                        com.pullenti.unitext.UnitextNewline nl = new com.pullenti.unitext.UnitextNewline();
                        nl.beginChar = cnt.children.get(jj - 1).endChar + 1;
                        nl.endChar = nl.beginChar;
                        nl.parent = cnt;
                        cnt.children.add(jj++, nl);
                        if (it.sublist != null) {
                            it.sublist.parent = cnt;
                            cnt.children.add(jj++, it.sublist);
                        }
                    }
                    cnt.children.remove(i);
                    i--;
                    continue;
                }
                if ((ch instanceof com.pullenti.unitext.UnitextTable) && ch.beginChar <= beginHead && (end < ch.endChar)) {
                    implantateBlock(ch, beginHead, beginBody, beginTail, beginAppendix, end, text, null, res);
                    return content;
                }
                res.value = com.pullenti.unitext.UnitextDocblock._new403(beginHead, end);
                com.pullenti.unitext.UnitextContainer head = com.pullenti.unitext.UnitextContainer._new94(com.pullenti.unitext.UnitextContainerType.HEAD);
                com.pullenti.unitext.UnitextContainer body = new com.pullenti.unitext.UnitextContainer();
                com.pullenti.unitext.UnitextContainer tail = com.pullenti.unitext.UnitextContainer._new94(com.pullenti.unitext.UnitextContainerType.TAIL);
                com.pullenti.unitext.UnitextContainer apps = new com.pullenti.unitext.UnitextContainer();
                int j;
                for (j = i; j < cnt.children.size(); j++) {
                    if (beginHead == beginBody) 
                        break;
                    ch = cnt.children.get(j);
                    if (!ch.isWhitespaces()) {
                        int ppp = ch.getPlaintextNsPos1(text);
                        if (beginBody < ppp) 
                            break;
                        if (beginBody == ppp && (beginBody < end)) 
                            break;
                    }
                    head.children.add(ch);
                    ch.parent = head;
                }
                for (; j < cnt.children.size(); j++) {
                    if (beginBody == beginTail || beginBody == beginAppendix) 
                        break;
                    ch = cnt.children.get(j);
                    if (!ch.isWhitespaces()) {
                        if (beginTail < ch.getPlaintextNsPos1(text)) 
                            break;
                    }
                    body.children.add(ch);
                    ch.parent = body;
                }
                for (; j < cnt.children.size(); j++) {
                    if (beginTail == end) 
                        break;
                    ch = cnt.children.get(j);
                    if (!ch.isWhitespaces()) {
                        if (beginAppendix < ch.getPlaintextNsPos1(text)) 
                            break;
                    }
                    tail.children.add(ch);
                    ch.parent = tail;
                }
                for (; j < cnt.children.size(); j++) {
                    if (beginAppendix == end) 
                        break;
                    ch = cnt.children.get(j);
                    if (!ch.isWhitespaces()) {
                        if (end < ch.getPlaintextNsPos1(text)) 
                            break;
                    }
                    apps.children.add(ch);
                    ch.parent = apps;
                }
                if (j > i) {
                    com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i, res.value);
                    res.value.parent = cnt;
                    if (j > (i + 1)) 
                        for(int jjj = i + 1 + j - i - 1 - 1, mmm = i + 1; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                    if (head.children.size() > 0) {
                        res.value.head = head;
                        head.parent = res.value;
                        head.beginChar = beginHead;
                        head.endChar = head.children.get(head.children.size() - 1).endChar;
                    }
                    if (body.children.size() > 0) {
                        res.value.body = body;
                        body.parent = res.value;
                        body.beginChar = beginBody;
                        if (body.children.get(0).beginChar < body.beginChar) 
                            body.beginChar = body.children.get(0).beginChar;
                        body.endChar = body.children.get(body.children.size() - 1).endChar;
                    }
                    if (tail.children.size() > 0) {
                        res.value.tail = tail;
                        tail.parent = res.value;
                        tail.beginChar = beginTail;
                        if (tail.children.get(0).beginChar < tail.beginChar) 
                            tail.beginChar = tail.children.get(0).beginChar;
                        tail.endChar = tail.children.get(tail.children.size() - 1).endChar;
                    }
                    if (apps.children.size() > 0) {
                        res.value.appendix = apps;
                        apps.parent = res.value;
                        apps.beginChar = beginAppendix;
                        apps.endChar = apps.children.get(apps.children.size() - 1).endChar;
                    }
                }
                break;
            }
            return content;
        }
        if ((content instanceof com.pullenti.unitext.UnitextPlaintext) && (((content.parent instanceof com.pullenti.unitext.UnitextTablecell) || (content.parent instanceof com.pullenti.unitext.UnitextListitem)))) {
            if ((content.beginChar == beginBody && beginBody == beginHead && end == beginTail) && end == beginAppendix && end == content.endChar) {
                res.value = com.pullenti.unitext.UnitextDocblock._new403(beginHead, end);
                res.value.body = content;
                return res.value;
            }
        }
        return content;
    }
    public UnitextImplantateHelper() {
    }
}
