// Path: src/main/java/jp/gr/java_conf/daisy/ajax_mutator/mutatable/DOMSelection.java
// public class DOMSelection extends Mutatable {
//     private SelectionMethod method;
//     private AstNode range;
//     private AstNode selector;
// 
//     public DOMSelection(AstNode node, AstNode range, SelectionMethod method,
//             AstNode selector) {
//         super(node);
//         this.range = range;
//         this.method = method;
//         this.selector = selector;
//     }
// 
//     public enum SelectionMethod {
//         ID, NAME, CLASS, TAG_NAME, JQUERY
//     }
// 
//     public SelectionMethod getSelectionMethod() {
//         return method;
//     }
// 
//     public AstNode getRange() {
//         return range;
//     }
// 
//     public AstNode getSelector() {
//         return selector;
//     }
// 
//     @Override
//     public String toString() {
//         StringBuilder builder = new StringBuilder();
//         builder.append(super.toString()).append('\n');
//         builder.append("  DOM selection: [from:")
//                 .append(Util.oneLineStringOf(range)).append(", by ")
//                 .append(method).append(": ")
//                 .append(Util.oneLineStringOf(selector)).append("]");
//         return builder.toString();
//     }
// }


import java.util.List;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.FunctionCall;
import org.mozilla.javascript.ast.Name;
import org.mozilla.javascript.ast.PropertyGet;
import jp.gr.java_conf.daisy.ajax_mutator.detector.AbstractDetector;
import jp.gr.java_conf.daisy.ajax_mutator.mutatable.DOMSelection;
package jp.gr.java_conf.daisy.ajax_mutator.detector.jquery;




/**
 * detector that detect jQuery's dom selection like $("#hoge") or
 * elm.children("#hoge")
 * 
 * jQuery provide rich selectors such as $("#prev ~ div"), but we donot detect
 * how they select elements, we just regards all of them as 'selected by
 * SelectionMethod.JQUERY'.
 * 
 * @author Kazuki Nishiura
 */
public class JQueryDOMSelectionDetector extends AbstractDetector<DOMSelection> {
    @Override
    public DOMSelection detect(AstNode node) {
        return detectFromFunctionCall(node, true);
    }

    @Override
    protected DOMSelection detectFromFunctionCall(FunctionCall functionCall,
            AstNode target, List<AstNode> arguments) {
        if (target instanceof Name
                && "$".equals(((Name) target).getIdentifier())) {
            return new DOMSelection(functionCall, null,
                    DOMSelection.SelectionMethod.JQUERY, arguments.get(0));
        }

        if (target instanceof PropertyGet) {
// Path: src/main/java/jp/gr/java_conf/daisy/ajax_mutator/mutatable/DOMSelection.java
// public class DOMSelection extends Mutatable {
//     private SelectionMethod method;
//     private AstNode range;
//     private AstNode selector;
// 
//     public DOMSelection(AstNode node, AstNode range, SelectionMethod method,
//             AstNode selector) {
//         super(node);
//         this.range = range;
//         this.method = method;
//         this.selector = selector;
//     }
// 
//     public enum SelectionMethod {
//         ID, NAME, CLASS, TAG_NAME, JQUERY
//     }
// 
//     public SelectionMethod getSelectionMethod() {
//         return method;
//     }
// 
//     public AstNode getRange() {
//         return range;
//     }
// 
//     public AstNode getSelector() {
//         return selector;
//     }
// 
//     @Override
//     public String toString() {
//         StringBuilder builder = new StringBuilder();
//         builder.append(super.toString()).append('\n');
//         builder.append("  DOM selection: [from:")
//                 .append(Util.oneLineStringOf(range)).append(", by ")
//                 .append(method).append(": ")
//                 .append(Util.oneLineStringOf(selector)).append("]");
//         return builder.toString();
//     }
// }


// Path: src/main/java/jp/gr/java_conf/daisy/ajax_mutator/detector/jquery/JQueryDOMSelectionDetector.java
import java.util.List;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.FunctionCall;
import org.mozilla.javascript.ast.Name;
import org.mozilla.javascript.ast.PropertyGet;
import jp.gr.java_conf.daisy.ajax_mutator.detector.AbstractDetector;
import jp.gr.java_conf.daisy.ajax_mutator.mutatable.DOMSelection;

package jp.gr.java_conf.daisy.ajax_mutator.detector.jquery;




/**
 * detector that detect jQuery's dom selection like $("#hoge") or
 * elm.children("#hoge")
 * 
 * jQuery provide rich selectors such as $("#prev ~ div"), but we donot detect
 * how they select elements, we just regards all of them as 'selected by
 * SelectionMethod.JQUERY'.
 * 
 * @author Kazuki Nishiura
 */
public class JQueryDOMSelectionDetector extends AbstractDetector<DOMSelection> {
    @Override
    public DOMSelection detect(AstNode node) {
        return detectFromFunctionCall(node, true);
    }

    @Override
    protected DOMSelection detectFromFunctionCall(FunctionCall functionCall,
            AstNode target, List<AstNode> arguments) {
        if (target instanceof Name
                && "$".equals(((Name) target).getIdentifier())) {
            return new DOMSelection(functionCall, null,
                    DOMSelection.SelectionMethod.JQUERY, arguments.get(0));
        }

        if (target instanceof PropertyGet) {
            if ("children".equals((((PropertyGet) target).getProperty())