<commit_before>/*
 * Copyright 2011, The gwtquery team.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.query.client.plugins.widgets;

import static com.google.gwt.query.client.GQuery.$;

import com.google.gwt.dom.client.Element;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.GqUi;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class WidgetsUtils {

  static final String[] appendingTags = {
    "td", "th", "li"};

  
  /**
   * Test if the tag name of the element is one of tag names given in parameter
   * 
   * @param tagNames
   * @return
   */
  public static boolean matchesTags(Element e, String... tagNames) {

    assert e != null : "Element cannot be null";

    StringBuilder regExp = new StringBuilder("^(");
    int tagNameLenght = tagNames != null ? tagNames.length : 0;
    for (int i = 0; i < tagNameLenght; i++) {
      regExp.append(tagNames[i].toUpperCase());
      if (i < tagNameLenght - 1) {
        regExp.append("|");
      }
    }
    regExp.append(")$");

    return e.getTagName().toUpperCase().matches(regExp.toString());

  }

  /**
   * If the <code>oldElement</code> is a td, th, li tags, the new element will replaced its content.
   * In other cases, the <code>oldElement</code> will be replaced by the <code>newElement</code>
   *  and the old element classes will be copied to the new element.
   */
   private static void replaceOrAppend(Element oldElement, Element newElement) {
    assert oldElement != null && newElement != null;
    
    if(matchesTags(oldElement, appendingTags)){
      GQuery.$(oldElement).html("").append(newElement);
    }else{
      GQuery.$(oldElement).replaceWith(newElement);
  
      //copy class
      String c = oldElement.getClassName();
      if (!c.isEmpty()) {
        newElement.addClassName(c);
      }
      //copy id
      newElement.setId(oldElement.getId());
      //ensure no duplicate id
      oldElement.setId("");
      
    }
   }
   
   private static void hideAndAfter(Element oldElement, Element newElement) {
     assert oldElement != null && newElement != null;
     GQuery.$(oldElement).hide().after(newElement);
     String c = oldElement.getClassName();
     if (!c.isEmpty()) {
       newElement.addClassName(c);
     }
   }
   
   private static void replaceWidget(Widget oldWidget, Widget newWidget, boolean remove) {
     Widget parent = oldWidget.getParent();
     // TODO: handle tables
     if (parent instanceof HTMLPanel) {
       ((HTMLPanel) parent).addAndReplaceElement(newWidget, oldWidget.getElement().<com.google.gwt.dom.client.Element>cast());
     } else if (parent instanceof ComplexPanel) {
       ((ComplexPanel) parent).add(newWidget);
     } else if (parent instanceof SimplePanel) {
       ((SimplePanel) parent).setWidget(newWidget);
     } else if (parent instanceof Panel) {
       ((Panel) parent).add(newWidget);
     } else {
       assert false : "Can not replace an attached widget whose parent is a " + parent.getClass().getName();
     }
     if (remove) {
       oldWidget.removeFromParent();
     } else {
       oldWidget.setVisible(false);
     }
   }
   
   /**
    * Replace a dom element by a widget.
    * Old element classes will be copied to the new widget.
    */
   public static void replaceOrAppend(Element e, Widget widget)  {
     assert e != null && widget != null;
     if ($(e).widget() != null) {
       replaceWidget($(e).widget(), widget, true);
     } else {
       GqUi.detachWidget(widget);
       replaceOrAppend(e, widget.getElement());
       GqUi.attachWidget(widget);
     }
   }

   /**
    * Append a widget to a dom element, and hide it.
    * Element classes will be copied to the new widget.
    */
   public static void hideAndAfter(Element e, Widget widget)  {
     assert e != null && widget != null;
     if ($(e).widget() != null) {
       replaceWidget($(e).widget(), widget, false);
     } else {
       GqUi.detachWidget(widget);
       hideAndAfter(e, widget.getElement());
       GqUi.attachWidget(widget);
     }
   }
}<commit_msg>Add the old widget element when its parent is HTMLPanel and we want maintain it hidden in the dom<commit_after>/*
 * Copyright 2011, The gwtquery team.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.query.client.plugins.widgets;

import static com.google.gwt.query.client.GQuery.$;

import com.google.gwt.dom.client.Element;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.GqUi;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class WidgetsUtils {

  static final String[] appendingTags = {
    "td", "th", "li"};

  
  /**
   * Test if the tag name of the element is one of tag names given in parameter
   * 
   * @param tagNames
   * @return
   */
  public static boolean matchesTags(Element e, String... tagNames) {

    assert e != null : "Element cannot be null";

    StringBuilder regExp = new StringBuilder("^(");
    int tagNameLenght = tagNames != null ? tagNames.length : 0;
    for (int i = 0; i < tagNameLenght; i++) {
      regExp.append(tagNames[i].toUpperCase());
      if (i < tagNameLenght - 1) {
        regExp.append("|");
      }
    }
    regExp.append(")$");

    return e.getTagName().toUpperCase().matches(regExp.toString());

  }

  /**
   * If the <code>oldElement</code> is a td, th, li tags, the new element will replaced its content.
   * In other cases, the <code>oldElement</code> will be replaced by the <code>newElement</code>
   *  and the old element classes will be copied to the new element.
   */
   private static void replaceOrAppend(Element oldElement, Element newElement) {
    assert oldElement != null && newElement != null;
    
    if(matchesTags(oldElement, appendingTags)){
      GQuery.$(oldElement).html("").append(newElement);
    }else{
      GQuery.$(oldElement).replaceWith(newElement);
  
      //copy class
      String c = oldElement.getClassName();
      if (!c.isEmpty()) {
        newElement.addClassName(c);
      }
      //copy id
      newElement.setId(oldElement.getId());
      //ensure no duplicate id
      oldElement.setId("");
      
    }
   }
   
   private static void hideAndAfter(Element oldElement, Element newElement) {
     assert oldElement != null && newElement != null;
     GQuery.$(oldElement).hide().after(newElement);
     String c = oldElement.getClassName();
     if (!c.isEmpty()) {
       newElement.addClassName(c);
     }
   }
   
   private static void replaceWidget(Widget oldWidget, Widget newWidget, boolean remove) {
     Widget parent = oldWidget.getParent();
     boolean removed = false;
     // TODO: handle tables
     if (parent instanceof HTMLPanel) {
       ((HTMLPanel) parent).addAndReplaceElement(newWidget, oldWidget.getElement().<com.google.gwt.dom.client.Element>cast());
       if (!remove) {
         $(newWidget).before($(oldWidget));
       }
       removed = true;
     } else if (parent instanceof ComplexPanel) {
       ((ComplexPanel) parent).add(newWidget);
     } else if (parent instanceof SimplePanel) {
       ((SimplePanel) parent).setWidget(newWidget);
     } else if (parent instanceof Panel) {
       ((Panel) parent).add(newWidget);
     } else {
       assert false : "Can not replace an attached widget whose parent is a " + parent.getClass().getName();
     }
     if (remove && !removed && oldWidget.isAttached()) {
       oldWidget.removeFromParent();
     } else {
       oldWidget.setVisible(false);
     }
   }
   
   /**
    * Replace a dom element by a widget.
    * Old element classes will be copied to the new widget.
    */
   public static void replaceOrAppend(Element e, Widget widget)  {
     assert e != null && widget != null;
     if ($(e).widget() != null && $(e).widget().isAttached()) {
       replaceWidget($(e).widget(), widget, true);
     } else {
       GqUi.detachWidget(widget);
       replaceOrAppend(e, widget.getElement());
       GqUi.attachWidget(widget);
     }
   }

   /**
    * Append a widget to a dom element, and hide it.
    * Element classes will be copied to the new widget.
    */
   public static void hideAndAfter(Element e, Widget widget)  {
     assert e != null && widget != null;
     if ($(e).widget() != null && $(e).widget().isAttached()) {
       replaceWidget($(e).widget(), widget, false);
     } else {
       GqUi.detachWidget(widget);
       hideAndAfter(e, widget.getElement());
       GqUi.attachWidget(widget);
     }
   }
}
{'fraction_non_alphanumeric': 0.12611607142857142, 'fraction_numerical': 0.002232142857142857, 'mean_word_length': 2.9522854851643947, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5048209', 'n_tokens_mistral': 2915, 'n_tokens_neox': 2883, 'n_words': 850}