import logging


def replaceHTMLBlock(html, commentIdentifier, newContent):
    """ Replaces html content in block of <!-- commentIdentifier -->Old content<!-- end of commentIdentifier --> by new value.

    :param html: source html containing section(s) to be replaced
    :param commentIdentifier: identifier of section to be replaced
    :param newContent: new content of identified section
    :return: resulting html

    >>> html = "<html><body><h1>Title</h1><p><!-- content -->Here should be page content<!-- end of content --></p></body></html>"
    >>> html = replaceHTMLBlock(html, "content", "My content of page.")
    >>> print html
    <html><body><h1>Title</h1><p>My content of page.</p></body></html>
    """
    commentIdentifier = commentIdentifier.strip()
    startId = ("<!-- %s -->" % commentIdentifier).upper()
    endId = ("<!-- END OF %s -->" % commentIdentifier).upper()
    while html.upper().find(startId) >= 0:
        upperCase = html.upper()
        startPos = upperCase.find(startId)

        endPos = upperCase.find(endId)
        if endPos < 0:
            logging.error("replaceHTMLBlock endPos(%d) < 0" % (endPos))
            return html

        endCutPos = upperCase.find("-->", endPos) + 3
        if endCutPos < 3:
            return html

        if startPos>=0 and endCutPos>=0:
            html = html[:startPos] + newContent + html[endCutPos:]

    return html