Program: 60138
Project: 60138
Buggy source:
/** 
 * Adds the rule.
 * @param styleSheet the style sheet
 * @param rule the rule
 * @throws MalformedURLException the malformed url exception
 * @throws UnsupportedEncodingException
 */
private final void addRule(CSSStyleSheet styleSheet,CSSRule rule) throws MalformedURLException, UnsupportedEncodingException {
  HTMLDocumentImpl document=this.document;
  if (rule instanceof CSSStyleRule) {
    CSSStyleRule sr=(CSSStyleRule)rule;
    String selectorList=sr.getSelectorText();
    StringTokenizer commaTok=new StringTokenizer(selectorList,"String_Node_Str");
    while (commaTok.hasMoreTokens()) {
      String selectorPart=commaTok.nextToken().toLowerCase();
      ArrayList<SimpleSelector> simpleSelectors=null;
      String lastSelectorText=null;
      StringTokenizer tok=new StringTokenizer(selectorPart,"String_Node_Str");
      if (tok.hasMoreTokens()) {
        simpleSelectors=new ArrayList<SimpleSelector>();
        SimpleSelector prevSelector=null;
        SELECTOR_FOR:         for (; ; ) {
          String token=tok.nextToken();
          if ("String_Node_Str".equals(token)) {
            if (prevSelector != null) {
              prevSelector.setSelectorType(SimpleSelector.PARENT);
            }
            continue SELECTOR_FOR;
          }
 else           if ("String_Node_Str".equals(token)) {
            if (prevSelector != null) {
              prevSelector.setSelectorType(SimpleSelector.PRECEEDING_SIBLING);
            }
            continue SELECTOR_FOR;
          }
          int colonIdx=token.indexOf(':');
          String simpleSelectorText=colonIdx == -1 ? token : token.substring(0,colonIdx);
          pseudoElement=colonIdx == -1 ? null : token.substring(colonIdx + 1);
          prevSelector=new SimpleSelector(simpleSelectorText,pseudoElement);
          simpleSelectors.add(prevSelector);
          if (!tok.hasMoreTokens()) {
            lastSelectorText=simpleSelectorText;
            break;
          }
        }
      }
      if (lastSelectorText != null) {
        int dotIdx=lastSelectorText.indexOf('.');
        if (dotIdx != -1) {
          String elemtl=lastSelectorText.substring(0,dotIdx);
          String classtl=lastSelectorText.substring(dotIdx + 1);
          this.addClassRule(elemtl,classtl,sr,simpleSelectors);
        }
 else {
          int poundIdx=lastSelectorText.indexOf('#');
          if (poundIdx != -1) {
            String elemtl=lastSelectorText.substring(0,poundIdx);
            String idtl=lastSelectorText.substring(poundIdx + 1);
            this.addIdRule(elemtl,idtl,sr,simpleSelectors);
          }
 else {
            String elemtl=lastSelectorText;
            this.addElementRule(elemtl,sr,simpleSelectors);
          }
        }
      }
    }
    if (selectorList.contains("String_Node_Str") && selectorList.endsWith("String_Node_Str")) {
      String selector=selectorList.replace("String_Node_Str","String_Node_Str");
      int quadIdx=selector.indexOf("String_Node_Str") + 1;
      htmlElement=selector.substring(0,selector.indexOf("String_Node_Str"));
      if (selectorList.contains(OP_PIPE_EQUAL)) {
        int eqIdx=selector.indexOf(OP_PIPE_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=OP_PIPE_EQUAL;
      }
 else       if (selectorList.contains(OP_CIRCUMFLEX_EQUAL)) {
        int eqIdx=selector.indexOf(OP_CIRCUMFLEX_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=OP_CIRCUMFLEX_EQUAL;
      }
 else       if (selectorList.contains(OP_TILDE_EQUAL)) {
        int eqIdx=selector.indexOf(OP_TILDE_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=OP_TILDE_EQUAL;
      }
 else       if (selectorList.contains(OP_DOLLAR_EQUAL)) {
        int eqIdx=selector.indexOf(OP_DOLLAR_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=OP_DOLLAR_EQUAL;
      }
 else       if (selectorList.contains(OP_STAR_EQUAL)) {
        int eqIdx=selector.indexOf(OP_STAR_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=OP_STAR_EQUAL;
      }
 else       if (selectorList.contains(OP_EQUAL)) {
        int eqIdx=selector.indexOf(OP_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 1,selector.length() - 1);
        attributeOperator=OP_EQUAL;
      }
 else {
        attribute=selector.substring(quadIdx,selector.length() - 1);
        attributeValue="String_Node_Str";
        attributeOperator=OP_ALL;
      }
      this.addAttributeRule(htmlElement,attributeValue,sr,new ArrayList<SimpleSelector>());
    }
  }
 else   if (rule instanceof CSSImportRule) {
    UserAgentContext uacontext=document.getUserAgentContext();
    if (uacontext.isExternalCSSEnabled()) {
      CSSImportRule importRule=(CSSImportRule)rule;
      if (CSSUtilities.matchesMedia(importRule.getMedia(),uacontext)) {
        String href=importRule.getHref();
        try {
          CSSStyleSheet sheet=CSSUtilities.parse(href,document);
          if (sheet != null) {
            this.addStyleSheet(sheet);
          }
        }
 catch (        Exception err) {
          logger.error("String_Node_Str" + href + "String_Node_Str"+ err);
        }
      }
    }
  }
 else   if (rule instanceof CSSMediaRule) {
    CSSMediaRule mrule=(CSSMediaRule)rule;
    MediaList mediaList=mrule.getMedia();
    if (CSSUtilities.matchesMedia(mediaList,document.getUserAgentContext())) {
      CSSRuleList ruleList=mrule.getCssRules();
      int length=ruleList.getLength();
      for (int i=0; i < length; i++) {
        CSSRule subRule=ruleList.item(i);
        this.addRule(styleSheet,subRule);
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Adds the rule.
 * @param styleSheet the style sheet
 * @param rule the rule
 * @throws MalformedURLException the malformed url exception
 * @throws UnsupportedEncodingException
 */
private final void addRule(CSSStyleSheet styleSheet,CSSRule rule) throws MalformedURLException, UnsupportedEncodingException {
  HTMLDocumentImpl document=this.document;
  if (rule instanceof CSSStyleRule) {
    CSSStyleRule sr=(CSSStyleRule)rule;
    String selectorList=sr.getSelectorText();
    StringTokenizer commaTok=new StringTokenizer(selectorList,"String_Node_Str");
    while (commaTok.hasMoreTokens()) {
      String selectorPart=commaTok.nextToken().toLowerCase();
      ArrayList<SimpleSelector> simpleSelectors=null;
      String lastSelectorText=null;
      StringTokenizer tok=new StringTokenizer(selectorPart,"String_Node_Str");
      if (tok.hasMoreTokens()) {
        simpleSelectors=new ArrayList<SimpleSelector>();
        SimpleSelector prevSelector=null;
        SELECTOR_FOR:         for (; ; ) {
          String token=tok.nextToken();
          if ("String_Node_Str".equals(token)) {
            if (prevSelector != null) {
              prevSelector.setSelectorType(SimpleSelector.PARENT);
            }
            continue SELECTOR_FOR;
          }
 else           if ("String_Node_Str".equals(token)) {
            if (prevSelector != null) {
              prevSelector.setSelectorType(SimpleSelector.PRECEEDING_SIBLING);
            }
            continue SELECTOR_FOR;
          }
          int colonIdx=token.indexOf(':');
          String simpleSelectorText=colonIdx == -1 ? token : token.substring(0,colonIdx);
          pseudoElement=colonIdx == -1 ? null : token.substring(colonIdx + 1);
          prevSelector=new SimpleSelector(simpleSelectorText,pseudoElement);
          simpleSelectors.add(prevSelector);
          if (!tok.hasMoreTokens()) {
            lastSelectorText=simpleSelectorText;
            break;
          }
        }
      }
      if (lastSelectorText != null) {
        int dotIdx=lastSelectorText.indexOf('.');
        if (dotIdx != -1) {
          String elemtl=lastSelectorText.substring(0,dotIdx);
          String classtl=lastSelectorText.substring(dotIdx + 1);
          this.addClassRule(elemtl,classtl,sr,simpleSelectors);
        }
 else {
          int poundIdx=lastSelectorText.indexOf('#');
          if (poundIdx != -1) {
            String elemtl=lastSelectorText.substring(0,poundIdx);
            String idtl=lastSelectorText.substring(poundIdx + 1);
            this.addIdRule(elemtl,idtl,sr,simpleSelectors);
          }
 else {
            String elemtl=lastSelectorText;
            this.addElementRule(elemtl,sr,simpleSelectors);
          }
        }
      }
    }
    if (selectorList.contains("String_Node_Str") && selectorList.endsWith("String_Node_Str")) {
      String selector=selectorList.replace("String_Node_Str","String_Node_Str");
      int quadIdx=selector.indexOf("String_Node_Str") + 1;
      htmlElement=selector.substring(0,selector.indexOf("String_Node_Str"));
      if (selectorList.contains(SelectorMatcher.OP_PIPE_EQUAL)) {
        int eqIdx=selector.indexOf(SelectorMatcher.OP_PIPE_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=SelectorMatcher.OP_PIPE_EQUAL;
      }
 else       if (selectorList.contains(SelectorMatcher.OP_CIRCUMFLEX_EQUAL)) {
        int eqIdx=selector.indexOf(SelectorMatcher.OP_CIRCUMFLEX_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=SelectorMatcher.OP_CIRCUMFLEX_EQUAL;
      }
 else       if (selectorList.contains(SelectorMatcher.OP_TILDE_EQUAL)) {
        int eqIdx=selector.indexOf(SelectorMatcher.OP_TILDE_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=SelectorMatcher.OP_TILDE_EQUAL;
      }
 else       if (selectorList.contains(SelectorMatcher.OP_DOLLAR_EQUAL)) {
        int eqIdx=selector.indexOf(SelectorMatcher.OP_DOLLAR_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=SelectorMatcher.OP_DOLLAR_EQUAL;
      }
 else       if (selectorList.contains(SelectorMatcher.OP_STAR_EQUAL)) {
        int eqIdx=selector.indexOf(SelectorMatcher.OP_STAR_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 2,selector.length() - 1);
        attributeOperator=SelectorMatcher.OP_STAR_EQUAL;
      }
 else       if (selectorList.contains(SelectorMatcher.OP_EQUAL)) {
        int eqIdx=selector.indexOf(SelectorMatcher.OP_EQUAL);
        attribute=selector.substring(quadIdx,eqIdx);
        attributeValue=selector.substring(eqIdx + 1,selector.length() - 1);
        attributeOperator=SelectorMatcher.OP_EQUAL;
      }
 else {
        attribute=selector.substring(quadIdx,selector.length() - 1);
        attributeValue="String_Node_Str";
        attributeOperator=SelectorMatcher.OP_ALL;
      }
      this.addAttributeRule(htmlElement,attributeValue,sr,new ArrayList<SimpleSelector>());
    }
  }
 else   if (rule instanceof CSSImportRule) {
    UserAgentContext uacontext=document.getUserAgentContext();
    if (uacontext.isExternalCSSEnabled()) {
      CSSImportRule importRule=(CSSImportRule)rule;
      if (CSSUtilities.matchesMedia(importRule.getMedia(),uacontext)) {
        String href=importRule.getHref();
        try {
          CSSStyleSheet sheet=CSSUtilities.parse(href,document);
          if (sheet != null) {
            this.addStyleSheet(sheet);
          }
        }
 catch (        Exception err) {
          logger.error("String_Node_Str" + href + "String_Node_Str"+ err);
        }
      }
    }
  }
 else   if (rule instanceof CSSMediaRule) {
    CSSMediaRule mrule=(CSSMediaRule)rule;
    MediaList mediaList=mrule.getMedia();
    if (CSSUtilities.matchesMedia(mediaList,document.getUserAgentContext())) {
      CSSRuleList ruleList=mrule.getCssRules();
      int length=ruleList.getLength();
      for (int i=0; i < length; i++) {
        CSSRule subRule=ruleList.item(i);
        this.addRule(styleSheet,subRule);
      }
    }
  }
}