
br.com.objectos.css.io.DescendantSelector Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2016 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.css.io;
import java.util.stream.Stream;
import br.com.objectos.codereader.Keyword;
import br.com.objectos.core.lang.Strings;
import br.com.objectos.css.Selectable;
import br.com.objectos.css.Selector;
/**
* @author [email protected] (Marcio Endo)
*/
class DescendantSelector extends AbstractCombinatorSelector {
private static final SelectorAction ACTION = new AbstractKeywordSelectorAction(Keyword.whitespace()) {
@Override
public void execute(SelectorReader reader) {
String maybeName = reader.get();
if (!Strings.isNullOrEmpty(maybeName)) {
reader.add(CssSelectors.tag(maybeName));
}
reader.acceptBuilderFinalizer(b -> b.descendant());
}
};
private static SelectorAction FINALIZER = new SelectorAction() {
@Override
public boolean matches(SelectorReader reader) {
return reader.empty();
}
@Override
public void execute(SelectorReader reader) {
reader.acceptBuilderFinalizer(b -> b.descendant());
}
};
public DescendantSelector(Selector first, Selector second) {
super(first, second);
}
public static SelectorAction action() {
return ACTION;
}
public static SelectorAction finalizerAction() {
return FINALIZER;
}
@Override
public boolean matches(Selectable element) {
return secondMatches(element)
&& parentStream(element)
.filter(el -> firstMatches(el))
.findAny()
.isPresent();
}
@Override
String toStringSeparator() {
return " ";
}
private Stream extends Selectable> parentStream(Selectable element) {
return element.parent()
.map(el -> Stream.concat(Stream.of(el), parentStream(el)))
.orElse(Stream.of());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy