Search in sources :

Example 6 with CssRule

use of org.eclipse.mylyn.wikitext.parser.css.CssRule in project mylyn.docs by eclipse.

the class SpanStrategies method calculateAlternateSpanStrategy.

private SpanStrategy calculateAlternateSpanStrategy(Attributes attributes) {
    List<SpanStrategy> strategies = new ArrayList<>();
    String cssStyle = attributes.getCssStyle();
    if (cssStyle != null) {
        Iterator<CssRule> rules = new CssParser().createRuleIterator(cssStyle);
        while (rules.hasNext()) {
            CssRule rule = rules.next();
            if (rule.name.equals("font-weight") && rule.value.equals("bold")) {
                // $NON-NLS-1$ //$NON-NLS-2$
                strategies.add(calculateAlternateSpanStrategy(SpanType.BOLD));
            } else if (rule.name.equals("font-style") && rule.value.equals("italic")) {
                // $NON-NLS-1$ //$NON-NLS-2$
                strategies.add(calculateAlternateSpanStrategy(SpanType.ITALIC));
            } else if (rule.name.equals("text-decoration") && rule.value.equalsIgnoreCase("underline")) {
                // $NON-NLS-1$ //$NON-NLS-2$
                strategies.add(calculateAlternateSpanStrategy(SpanType.UNDERLINED));
            } else if (rule.name.equals("text-decoration") && rule.value.equalsIgnoreCase("line-through")) {
                // $NON-NLS-1$ //$NON-NLS-2$
                strategies.add(calculateAlternateSpanStrategy(SpanType.DELETED));
            } else if (rule.name.equals("font-family") && isFontFamilyMonospace(rule)) {
                // $NON-NLS-1$
                strategies.add(new SubstitutionWithoutCssSpanStrategy(SpanType.MONOSPACE));
            }
        }
    }
    strategies = ImmutableList.copyOf(FluentIterable.from(strategies).filter(not(isNull())));
    if (strategies.isEmpty()) {
        return null;
    } else if (strategies.size() == 1) {
        return strategies.get(0);
    }
    return new CompositeSpanStrategy(strategies);
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) ArrayList(java.util.ArrayList) CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser)

Example 7 with CssRule

use of org.eclipse.mylyn.wikitext.parser.css.CssRule in project mylyn.docs by eclipse.

the class HtmlTextPresentationParserTest method assertDefaultCssStyles.

private void assertDefaultCssStyles(String expectedStyles, final String elementName) {
    Stylesheet stylesheet = defaultStylesheet();
    final List<String> styles = new ArrayList<>();
    Receiver receiver = new Receiver() {

        @Override
        public void apply(CssRule rule) {
            styles.add(rule.name + ": " + rule.value);
        }
    };
    stylesheet.applyTo(new ElementInfo() {

        @Override
        public boolean hasId(String id) {
            return false;
        }

        @Override
        public boolean hasCssClass(String cssClass) {
            return false;
        }

        @Override
        public ElementInfo getParent() {
            return null;
        }

        @Override
        public String getLocalName() {
            return elementName;
        }
    }, receiver);
    assertEquals("element " + elementName, expectedStyles, Joiner.on("; ").join(styles));
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) ElementInfo(org.eclipse.mylyn.wikitext.parser.css.ElementInfo) ArrayList(java.util.ArrayList) Receiver(org.eclipse.mylyn.wikitext.parser.css.Stylesheet.Receiver) Stylesheet(org.eclipse.mylyn.wikitext.parser.css.Stylesheet)

Example 8 with CssRule

use of org.eclipse.mylyn.wikitext.parser.css.CssRule in project mylyn.docs by eclipse.

the class CssParserTest method testComments.

public void testComments() {
    Stylesheet stylesheet = parser.parse("tr { /* font-size: 115%; */ font-size: 100%; } /* foo { sdf: sdf; } */");
    List<Block> blocks = stylesheet.getBlocks();
    assertEquals(1, blocks.size());
    Block block = blocks.get(0);
    List<CssRule> rules = block.getRules();
    assertEquals(1, rules.size());
    CssRule rule = rules.get(0);
    assertEquals("font-size", rule.name);
    assertEquals("100%", rule.value);
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) Block(org.eclipse.mylyn.wikitext.parser.css.Block) Stylesheet(org.eclipse.mylyn.wikitext.parser.css.Stylesheet)

Example 9 with CssRule

use of org.eclipse.mylyn.wikitext.parser.css.CssRule in project mylyn.docs by eclipse.

the class CssStyleFieldEditor method doCheckState.

/**
 * validate the input
 */
@Override
protected boolean doCheckState() {
    String value = getStringValue();
    if (value != null && value.trim().length() > 0) {
        // here we validate that the value looks like it is composed of valid CSS rules
        int offset = 0;
        Iterator<CssRule> ruleIterator = new CssParser().createRuleIterator(value);
        while (ruleIterator.hasNext()) {
            CssRule rule = ruleIterator.next();
            // detect gaps between rules.  Such gaps are areas of text that weren't detected as a CSS rule.
            if (rule.offset > offset) {
                String gap = value.substring(offset, rule.offset);
                if (gap.trim().length() != 0) {
                    setErrorMessage(NLS.bind(Messages.CssStyleFieldEditor_unexpectedToken, new Object[] { gap.trim(), offset }));
                    return false;
                }
            }
            offset = rule.offset + rule.length;
            // unknown rules should create an error
            if (!cssStyleManager.isKnownRule(rule)) {
                StringBuilder recognizedNames = new StringBuilder();
                for (String recognizedName : cssStyleManager.getRecognizedRuleNames()) {
                    if (recognizedNames.length() > 0) {
                        recognizedNames.append(Messages.CssStyleFieldEditor_1);
                    }
                    recognizedNames.append(recognizedName);
                }
                setErrorMessage(NLS.bind(Messages.CssStyleFieldEditor_unsupportedRule, new Object[] { rule.name, recognizedNames }));
                return false;
            }
            if (CssStyleManager.RULE_COLOR.equals(rule.name) || CssStyleManager.RULE_BACKGROUND_COLOR.equals(rule.name)) {
                Integer rgb = CssStyleManager.cssColorRgb(rule.value);
                if (rgb == null) {
                    setErrorMessage(NLS.bind(Messages.CssStyleFieldEditor_invalidColor, new Object[] { rule.value }));
                    return false;
                }
            }
        }
        // detect trailing text that wasn't detected as a CSS rule.
        if (offset < value.length() - 1) {
            String gap = value.substring(offset, value.length());
            if (gap.trim().length() != 0) {
                setErrorMessage(NLS.bind(Messages.CssStyleFieldEditor_unexpectedToken, new Object[] { gap.trim(), offset }));
                return false;
            }
        }
    }
    return super.doCheckState();
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser)

Aggregations

CssRule (org.eclipse.mylyn.wikitext.parser.css.CssRule)9 CssParser (org.eclipse.mylyn.wikitext.parser.css.CssParser)6 ArrayList (java.util.ArrayList)3 Matcher (java.util.regex.Matcher)2 Stylesheet (org.eclipse.mylyn.wikitext.parser.css.Stylesheet)2 Element (org.jsoup.nodes.Element)2 HashMap (java.util.HashMap)1 Block (org.eclipse.mylyn.wikitext.parser.css.Block)1 ElementInfo (org.eclipse.mylyn.wikitext.parser.css.ElementInfo)1 Receiver (org.eclipse.mylyn.wikitext.parser.css.Stylesheet.Receiver)1 Node (org.jsoup.nodes.Node)1