Example #1
0
  private Collection<Selector> replaceFirstAppender(
      Selector selector, List<Selector> previousSelectors) {
    if (selector.getHead().isAppender()) {
      NestedSelectorAppender appender = (NestedSelectorAppender) selector.getHead();
      Selector reminder = chopOffHead(selector);
      return joinAll(
          previousSelectors,
          reminder,
          appender.getLeadingCombinator(),
          isDirectlyAfterPreviousPart(reminder));
    }

    // appender somewhere in the middle
    NestedSelectorAppender appender = selector.findFirstAppender();
    if (appender == null)
      throw new BugHappened("This is very weird error and should not happen.", selector);

    Selector afterAppender = splitOn(selector, appender);
    List<Selector> partialResults =
        joinAll(
            selector,
            previousSelectors,
            appender.getLeadingCombinator(),
            appender.isDirectlyAfter());
    // FIXME (now) last parameter should be nide and repats cold in 146
    return joinAll(partialResults, afterAppender, null, isDirectlyAfterPreviousPart(afterAppender));
  }
Example #2
0
  public Selector indirectJoin(Selector firstI, SelectorCombinator combinator, Selector secondI) {
    // if both of them are null, something is very wrong
    if (secondI == null) return firstI.clone();

    if (firstI == null) return secondI.clone();

    Selector first = firstI.clone();
    Selector second = secondI.clone();

    return indirectJoinNoClone(first, combinator, second);
  }
Example #3
0
  private boolean isEmptySelector(Selector selector) {
    if (selector.isCombined()) return false;

    SelectorPart head = selector.getHead();
    if (head.getType() != ASTCssNodeType.SIMPLE_SELECTOR) return false;

    SimpleSelector simpleHead = (SimpleSelector) head;
    if (!simpleHead.isEmptyForm() || !simpleHead.isStar()) {
      return false;
    }

    if (simpleHead.hasSubsequent()) return false;

    return true;
  }
Example #4
0
 private Selector replaceMiddleAppendersByEmptiness(Selector selector) {
   Selector empty =
       new Selector(selector.getUnderlyingStructure(), createEmptySimpleSelector(selector));
   List<Selector> replaceAppenders = replaceAppenders(selector, Arrays.asList(empty));
   Selector replacement = replaceAppenders.get(0);
   return replacement;
 }
Example #5
0
  public Selector removeAppenders(Selector selector) {
    selector = replaceLeadingAppendersByEmptiness(selector);
    if (!selector.containsAppender()) return selector;

    Selector replacement = replaceMiddleAppendersByEmptiness(selector);
    return replacement;
  }
Example #6
0
  public Selector indirectJoinNoClone(
      Selector first,
      SelectorCombinator combinator,
      List<SelectorPart> second,
      List<Extend> extend) {
    first.addExtends(extend);

    if (second.isEmpty()) return first;

    if (combinator != null) {
      second.get(0).setLeadingCombinator(combinator);
    }
    first.addParts(second);
    first.configureParentToAllChilds();

    return first;
  }
Example #7
0
  public Selector directJoin(Selector firstI, Selector secondI) {
    // if both of them are null, something is very wrong
    if (secondI == null) return firstI.clone();

    if (firstI == null) return secondI.clone();

    Selector first = firstI.clone();
    List<SelectorPart> secondParts = ArraysUtils.deeplyClonedList(secondI.getParts());
    List<Extend> secondExtends = ArraysUtils.deeplyClonedList(secondI.getExtend());
    SelectorPart secondHead = secondParts.get(0);

    if (secondHead.isAppender())
      return indirectJoinNoClone(
          first, secondHead.getLeadingCombinator(), secondParts, secondExtends);

    /*
     * FIXME: test on old whether survives if first is not simple selector. (say, if:
     * (~"escaped") {
     *   &:pseudo() {
     *   }
     * }
     *
     */
    SelectorPart attachToHead = first.getLastPart();
    directlyJoinParts(attachToHead, secondHead);

    secondParts.remove(0);
    SelectorCombinator leadingCombinator =
        secondParts.isEmpty() ? null : secondParts.get(0).getLeadingCombinator();
    return indirectJoinNoClone(first, leadingCombinator, secondParts, secondExtends);
  }
Example #8
0
 private boolean isDirect(
     SelectorCombinator beforeAppenderCombinator,
     boolean appenderDirectlyPlaced,
     Selector afterAppender) {
   boolean result =
       beforeAppenderCombinator == null
           && appenderDirectlyPlaced
           && (afterAppender == null || !afterAppender.hasLeadingCombinator());
   return result;
 }
Example #9
0
  private Selector splitOn(Selector selector, NestedSelectorAppender appender) {
    List<SelectorPart> parts = selector.getParts();
    int indexOfAppender = parts.indexOf(appender);
    List<SelectorPart> appenderAndAfter = parts.subList(indexOfAppender, parts.size());

    // remove appender
    appenderAndAfter.remove(0);
    appender.setParent(null);

    // create selector with after appender parts
    Selector result = null;
    if (!appenderAndAfter.isEmpty()) {
      result =
          new Selector(
              selector.getUnderlyingStructure(), new ArrayList<SelectorPart>(appenderAndAfter));
      result.configureParentToAllChilds();
    }

    // leave only before appender parts in original selector
    appenderAndAfter.clear();
    return result;
  }
Example #10
0
  public List<Selector> replaceAppenders(Selector inSelector, List<Selector> replacements) {
    if (!inSelector.containsAppender()) {
      return indirectJoinAll(replacements, null, inSelector);
    }

    List<Selector> result = Arrays.asList(inSelector);
    while (result.get(0).containsAppender()) {
      List<Selector> nextRound = new ArrayList<Selector>();
      for (Selector tbch : result) {
        nextRound.addAll(replaceFirstAppender(tbch, replacements));
      }

      result = nextRound;
    }

    return result;
  }
Example #11
0
  private Selector replaceLeadingAppendersByEmptiness(Selector selector) {
    while (!selector.isEmpty() && selector.getHead().isAppender()) {
      selector.getHead().setParent(null);
      selector.removeHead();
    }

    if (selector.isEmpty()) {
      SimpleSelector empty = createEmptySimpleSelector(selector);
      selector.addPart(empty);
    }
    return selector;
  }
Example #12
0
 public Selector indirectJoinNoClone(
     Selector first, SelectorCombinator combinator, Selector second) {
   return indirectJoinNoClone(first, combinator, second.getParts(), second.getExtend());
 }
Example #13
0
  private Selector chopOffHead(Selector selector) {
    if (!selector.isCombined()) return null;

    selector.removeHead();
    return selector;
  }
Example #14
0
 private boolean isDirectlyAfterPreviousPart(Selector selector) {
   return selector == null ? false : !selector.hasLeadingCombinator();
 }
Example #15
0
 public boolean appendSelector(Selector selector) {
   for (SelectorPart part : selector.getParts()) {
     append(part);
   }
   return true;
 }