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)); }
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); }
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; }
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; }
public Selector removeAppenders(Selector selector) { selector = replaceLeadingAppendersByEmptiness(selector); if (!selector.containsAppender()) return selector; Selector replacement = replaceMiddleAppendersByEmptiness(selector); return replacement; }
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; }
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); }
private boolean isDirect( SelectorCombinator beforeAppenderCombinator, boolean appenderDirectlyPlaced, Selector afterAppender) { boolean result = beforeAppenderCombinator == null && appenderDirectlyPlaced && (afterAppender == null || !afterAppender.hasLeadingCombinator()); return result; }
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; }
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; }
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; }
public Selector indirectJoinNoClone( Selector first, SelectorCombinator combinator, Selector second) { return indirectJoinNoClone(first, combinator, second.getParts(), second.getExtend()); }
private Selector chopOffHead(Selector selector) { if (!selector.isCombined()) return null; selector.removeHead(); return selector; }
private boolean isDirectlyAfterPreviousPart(Selector selector) { return selector == null ? false : !selector.hasLeadingCombinator(); }
public boolean appendSelector(Selector selector) { for (SelectorPart part : selector.getParts()) { append(part); } return true; }