Program: 77013
Project: 77013
Buggy source:
/** 
 * Inserts the HTML specified as a string at the end of the element. <p> If <code>elem</code>'s children are leaves, and the character at a <code>elem.getEndOffset() - 1</code> is a newline, this will insert before the newline so that there isn't text after the newline.</p> <p>Consider the following structure (the <code>elem</code> parameter is <b>in bold</b>).</p> <pre> &lt;body&gt; | <b>&lt;div&gt;</b> /  \ &lt;p&gt;   &lt;p&gt; </pre> <p>Invoking <code>insertBeforeEnd(elem, "&lt;ul&gt;&lt;li&gt;")</code> results in the following structure (new elements are <font style="color: red;">in red</font>).</p> <pre> &lt;body&gt; | <b>&lt;div&gt;</b> /  |  \ &lt;p&gt; &lt;p&gt; <font style="color: red;">&lt;ul&gt;</font> \ <font style="color: red;">&lt;li&gt;</font> </pre> <p>Unlike the <code>insertAfterEnd</code> method, new elements become <em>children</em> of the specified element, not siblings.</p> <p>Parameter <code>elem</code> must not be a leaf element, otherwise an <code>IllegalArgumentException</code> is thrown. If either <code>elem</code> or <code>htmlText</code> parameter is <code>null</code>, no changes are made to the document.</p> <p>For this to work correctly, the document must have an <code>HTMLEditorKit.Parser</code> set. This will be the case if the document was created from an HTMLEditorKit via the <code>createDefaultDocument</code> method.</p>
 * @param elem the element to be the root for the new text
 * @param htmlText the string to be parsed and assigned to <code>elem</code>
 * @throws IllegalArgumentException if <code>elem</code> is a leaf
 * @throws IllegalStateException if an HTMLEditorKit.Parser has notbeen set on the document
 * @since 1.3
 */
public void insertBeforeEnd(Element elem,String htmlText) throws BadLocationException, IOException {
  verifyParser();
  if (elem != null && elem.isLeaf()) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  if (elem != null) {
    int offset=elem.getEndOffset();
    if (elem.getElement(elem.getElementIndex(offset - 1)).isLeaf() && getText(offset - 1,1).charAt(0) == NEWLINE[0]) {
      offset--;
    }
    insertHTML(elem,offset,htmlText,false);
  }
}

Fixed version (ground truth):
/** 
 * Inserts the HTML specified as a string at the end of the element. <p> If <code>elem</code>'s children are leaves, and the character at a <code>elem.getEndOffset() - 1</code> is a newline, this will insert before the newline so that there isn't text after the newline.</p> <p>Consider the following structure (the <code>elem</code> parameter is <b>in bold</b>).</p> <pre> &lt;body&gt; | <b>&lt;div&gt;</b> /  \ &lt;p&gt;   &lt;p&gt; </pre> <p>Invoking <code>insertBeforeEnd(elem, "&lt;ul&gt;&lt;li&gt;")</code> results in the following structure (new elements are <font style="color: red;">in red</font>).</p> <pre> &lt;body&gt; | <b>&lt;div&gt;</b> /  |  \ &lt;p&gt; &lt;p&gt; <font style="color: red;">&lt;ul&gt;</font> \ <font style="color: red;">&lt;li&gt;</font> </pre> <p>Unlike the <code>insertAfterEnd</code> method, new elements become <em>children</em> of the specified element, not siblings.</p> <p>Parameter <code>elem</code> must not be a leaf element, otherwise an <code>IllegalArgumentException</code> is thrown. If either <code>elem</code> or <code>htmlText</code> parameter is <code>null</code>, no changes are made to the document.</p> <p>For this to work correctly, the document must have an <code>HTMLEditorKit.Parser</code> set. This will be the case if the document was created from an HTMLEditorKit via the <code>createDefaultDocument</code> method.</p>
 * @param elem the element to be the root for the new text
 * @param htmlText the string to be parsed and assigned to <code>elem</code>
 * @throws IllegalArgumentException if <code>elem</code> is a leaf
 * @throws IllegalStateException if an HTMLEditorKit.Parser has notbeen set on the document
 * @throws BadLocationException if insertion is impossible because ofa structural issue
 * @throws IOException if an I/O exception occurs
 * @since 1.3
 */
public void insertBeforeEnd(Element elem,String htmlText) throws BadLocationException, IOException {
  verifyParser();
  if (elem != null && elem.isLeaf()) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  if (elem != null) {
    int offset=elem.getEndOffset();
    if (elem.getElement(elem.getElementIndex(offset - 1)).isLeaf() && getText(offset - 1,1).charAt(0) == NEWLINE[0]) {
      offset--;
    }
    insertHTML(elem,offset,htmlText,false);
  }
}