use of org.w3c.css.sac.Selector in project java-to-graphviz by randomnoun.
the class StylesheetApplier method apply.
/**
* actually inline the styles
*/
public void apply() {
final Multimap<Element, StyleApplication> elementMatches = ArrayListMultimap.create();
final CSSOMParser inlineParser = new CSSOMParser();
inlineParser.setErrorHandler(new ExceptionErrorHandler());
// factor in elements' style attributes
NodeTraversor.traverse(new NodeVisitor() {
@Override
public void head(Node node, int depth) {
if (node instanceof Element && node.hasAttr("style")) {
// parse the CSS into a CSSStyleDeclaration
InputSource input = new InputSource(new StringReader(node.attr("style")));
CSSStyleDeclaration declaration = null;
try {
declaration = inlineParser.parseStyleDeclaration(input);
} catch (IOException e) {
// again, this should never happen, cuz we're just reading from a string
e.printStackTrace();
}
node.removeAttr("style");
elementMatches.put(((Element) node), new InlineStyleApplication(declaration));
}
}
@Override
public void tail(Node node, int depth) {
}
}, htmlDocument.body());
// compute which rules match which elements
CSSRuleList rules = styleSheet.getCssRules();
for (int i = 0; i < rules.getLength(); i++) {
if (rules.item(i) instanceof CSSStyleRule) {
CSSStyleRuleImpl rule = (CSSStyleRuleImpl) rules.item(i);
// for each selector in the rule... (separated by commas)
for (int j = 0; j < rule.getSelectors().getLength(); j++) {
Selector selector = rule.getSelectors().item(j);
Elements matches = null;
// selector.toString() converts a~=b to a~="b" which then includes the double quotes in the regex
// could remove this but the jsoup impl to ~= is wrong anyway ( should be performing a word search, not a regex )
matches = htmlDocument.select(selector.toString());
// for each matched element....
for (Element match : matches) {
elementMatches.put(match, new RuleStyleApplication(selector, rule.getStyle()));
}
}
}
}
Map<Element, CSSStyleDeclaration> inlinedStyles = new HashMap<Element, CSSStyleDeclaration>();
// calculate overrides
for (Element element : elementMatches.keySet()) {
CSSStyleDeclaration properties = new CSSStyleDeclarationImpl();
List<StyleApplication> matchedRules = new ArrayList<StyleApplication>(elementMatches.get(element));
Collections.sort(matchedRules, orderBySpecificity);
for (StyleApplication matchedRule : matchedRules) {
CSSStyleDeclaration cssBlock = matchedRule.getCssBlock();
for (int i = 0; i < cssBlock.getLength(); i++) {
String propertyKey = cssBlock.item(i);
CSSValue propertyValue = cssBlock.getPropertyCSSValue(propertyKey);
// TODO: !important
properties.setProperty(propertyKey, propertyValue.getCssText(), "");
}
}
inlinedStyles.put(element, properties);
}
// apply to DOM
for (Map.Entry<Element, CSSStyleDeclaration> entry : inlinedStyles.entrySet()) {
entry.getKey().attr("style", entry.getValue().getCssText());
}
}
use of org.w3c.css.sac.Selector in project org.eclipse.rap by eclipse-rap.
the class StyleSheet method createSelectorWrappers.
private void createSelectorWrappers() {
List<SelectorWrapper> selectorWrappersList = new LinkedList<>();
for (int pos = 0; pos < styleRules.length; pos++) {
StyleRule styleRule = styleRules[pos];
SelectorList selectors = styleRule.getSelectors();
StylePropertyMap properties = styleRule.getProperties();
int length = selectors.getLength();
for (int i = 0; i < length; i++) {
Selector selector = selectors.item(i);
SelectorWrapper selectorWrapper = new SelectorWrapper(selector, properties, pos);
selectorWrappersList.add(selectorWrapper);
}
}
Collections.sort(selectorWrappersList, COMPARATOR);
Collections.reverse(selectorWrappersList);
selectorWrappers = new SelectorWrapper[selectorWrappersList.size()];
selectorWrappersList.toArray(selectorWrappers);
}
use of org.w3c.css.sac.Selector in project org.eclipse.rap by eclipse-rap.
the class DocumentHandlerImpl method toString.
private static String toString(SelectorList patterns) {
StringBuilder buffer = new StringBuilder();
buffer.append("[");
int length = patterns.getLength();
for (int i = 0; i < length; i++) {
buffer.append(" ");
Selector selector = patterns.item(i);
buffer.append(selector.toString());
}
buffer.append(" ]");
return buffer.toString();
}
use of org.w3c.css.sac.Selector in project org.eclipse.rap by eclipse-rap.
the class Parser method parseSelector.
/**
* Parses a selector.
*/
protected Selector parseSelector() {
SimpleSelector ss = parseSimpleSelector();
Selector result = ss;
pseudoElement = null;
loop: for (; ; ) {
switch(current) {
default:
break loop;
case LexicalUnits.IDENTIFIER:
case LexicalUnits.ANY:
case LexicalUnits.HASH:
case LexicalUnits.DOT:
case LexicalUnits.LEFT_BRACKET:
case LexicalUnits.COLON:
result = selectorFactory.createDescendantSelector(result, parseSimpleSelector());
break;
case LexicalUnits.PLUS:
nextIgnoreSpaces();
result = selectorFactory.createDirectAdjacentSelector((short) 1, result, parseSimpleSelector());
break;
case LexicalUnits.PRECEDE:
nextIgnoreSpaces();
result = selectorFactory.createChildSelector(result, parseSimpleSelector());
}
}
if (pseudoElement != null) {
result = selectorFactory.createChildSelector(result, selectorFactory.createPseudoElementSelector(null, pseudoElement));
}
return result;
}
use of org.w3c.css.sac.Selector in project org.eclipse.rap by eclipse-rap.
the class ThemeTestBase method processTestStyleRule.
private static void processTestStyleRule(StyleRule styleRule) {
SelectorList selectors = styleRule.getSelectors();
StylePropertyMap properties = styleRule.getProperties();
int length = selectors.getLength();
for (int i = 0; i < length; i++) {
Selector selector = selectors.item(i);
SelectorExt selectorExt = (SelectorExt) selector;
checkProperties(selectorExt, properties);
}
}
Aggregations