cz.vutbr.web.css.CSSFactory Java Examples

The following examples show how to use cz.vutbr.web.css.CSSFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testAttributePresence() throws CSSException, IOException {

	StyleSheet ss = CSSFactory.parseString(TEST_ATTRIB_PRESENCE, null);
	assertEquals("One rule is set", 1, ss.size());

	List<CombinedSelector> cslist = SelectorsUtil.appendCS(null);
	SelectorsUtil.appendSimpleSelector(cslist, "*", null, rf
			.createAttribute(null, false, Selector.Operator.NO_OPERATOR,
					"href"));

	assertArrayEquals("Rule 1 contains one combined selector *[href]", cslist.toArray(),
			((RuleSet) ss.get(0)).getSelectors());

	List<Term<?>> terms = DeclarationsUtil.appendTerm(null, null, tf
			.createIdent("Verdana"));
	DeclarationsUtil.appendCommaTerm(terms, tf.createIdent("monospace"));

	assertEquals(
			"Rule contains one declaration { text-decoration: underline }",
			DeclarationsUtil.appendDeclaration(null, "text-decoration", tf
					.createIdent("underline")), ((RuleSet) ss.get(0))
					.asList());

}
 
Example #2
Source File: Viewport.java    From CSSBox with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
    * Creates a new Viewport with the given initial size. The actual size may be increased during the layout. 
    *  
    * @param e The anonymous element representing the viewport.
    * @param ctx
    * @param factory The factory used for creating the child boxes.
    * @param root The root element of the rendered document.
    * @param width Preferred (minimal) width.
    * @param height Preferred (minimal) height.
    */
   public Viewport(Element e, VisualContext ctx, BoxFactory factory, Element root, float width, float height)
{
	super(e, ctx);
	ctx.setViewport(this);
	this.factory = factory;
	this.root = root;
	style = CSSFactory.createNodeData(); //Viewport starts with an empty style
       nested = new Vector<Box>();
       startChild = 0;
       endChild = 0;
	this.width = width;
	this.height = height;
       isblock = true;
       contblock = true;
       root = null;
       visibleRect = new Rectangle(0, 0, width, height);
}
 
Example #3
Source File: ProfilerEntryPointAnalyzer.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        final String src = "/profiling/slate.html";
        //final String src = "/profiling/lidovky2.html";
        
        Date start = new Date();
        DOMSource ds = new DOMSource(AnalyzerTest.class.getResourceAsStream(src));
        Document doc = ds.parse();
        
        Date analyze = new Date();
        StyleMap decl = CSSFactory.assignDOM(doc, null, ProfilerEntryPointAnalyzer.class.getResource(src), "screen", true);
        
        Date end = new Date();
        System.out.println("DOM parsing: " + (analyze.getTime() - start.getTime()) + " ms");
        System.out.println("CSS analysis: " + (end.getTime() - analyze.getTime()) + " ms");
        System.out.println("Obtained " + decl.size() + " node styles");
    }
 
Example #4
Source File: TranslateImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public TermList setValue(List<Term<?>> value)
{
    super.setValue(value);
    List<Term<?>> args = getSeparatedValues(DEFAULT_ARG_SEP, false);
    if (args != null)
    {
        if (args.size() == 2 
                && (translateX = getLengthOrPercentArg(args.get(0))) != null
                && (translateY = getLengthOrPercentArg(args.get(1))) != null) {
            setValid(true);
        } else if (size() == 1 && (translateX = getLengthOrPercentArg(args.get(0))) != null) {
            translateY = CSSFactory.getTermFactory().createLength(0.0f);
            setValid(true);
        }
    }
    return this;
}
 
Example #5
Source File: PseudoClassTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void pseudoClassDirect() throws SAXException, IOException {  
    
    DOMSource ds = new DOMSource(getClass().getResourceAsStream("/simple/pseudo.html"));
    Document doc = ds.parse();
    ElementMap elements = new ElementMap(doc);
    
    MatchConditionOnElements cond = new MatchConditionOnElements("a", PseudoClassType.LINK);
    cond.addMatch(elements.getElementById("l2"), PseudoClassType.HOVER);
    cond.addMatch(elements.getElementById("l3"), PseudoClassType.VISITED);
    CSSFactory.registerDefaultMatchCondition(cond);
    
    StyleSheet style = CSSFactory.getUsedStyles(doc, null, createBaseFromFilename("data/simple/selectors.html"),"screen");
    DirectAnalyzer da = new DirectAnalyzer(style);

    NodeData l1 = getStyleById(elements, da, "l1");
    NodeData l2 = getStyleById(elements, da, "l2");
    NodeData l3 = getStyleById(elements, da, "l3");
    
    assertThat(l1.getValue(TermColor.class, "color"), is(tf.createColor(0,255,0)));
    assertThat(l2.getValue(TermColor.class, "color"), is(tf.createColor(0,255,255)));
    assertThat(l3.getValue(TermColor.class, "color"), is(tf.createColor(0,0,170)));
}
 
Example #6
Source File: SkewImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public TermList setValue(List<Term<?>> value)
{
    super.setValue(value);
    List<Term<?>> args = getSeparatedValues(DEFAULT_ARG_SEP, false);
    if (args != null)
    {
        if (args.size() == 2 
                && (skewX = getAngleArg(args.get(0))) != null
                && (skewY = getAngleArg(args.get(1))) != null) {
            setValid(true);
        } else if (size() == 1 && (skewX = getAngleArg(args.get(0))) != null) {
            skewY = CSSFactory.getTermFactory().createAngle(0.0f);
            setValid(true);
        }
    }
    return this;
}
 
Example #7
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testAdjacent() throws CSSException, IOException {

	StyleSheet ss = CSSFactory.parseString(TEST_ADJACENT, null);
	assertEquals("One rule is set", 1, ss.size());

	RuleSet rule = (RuleSet) ss.get(0);

	List<CombinedSelector> cslist = SelectorsUtil.appendCS(null);
	SelectorsUtil.appendSimpleSelector(cslist, "DIV", null);
	SelectorsUtil.appendAdjacent(cslist, "P");

	assertArrayEquals("Rule contains one combined selectors DIV+P ", cslist.toArray(),
			rule.getSelectors());

	assertEquals("Rule contains one declaration {color:blue;}",
			DeclarationsUtil.appendDeclaration(null, "color", tf
					.createColor(0, 0, 255)), rule.asList());

}
 
Example #8
Source File: FontFaceTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testFFMultiSrc() throws IOException, CSSException {
    log.info("input:\n\n\n" + TEST_STRING2 + "\n\n\n");
    StyleSheet ss;

    ss = CSSFactory.parseString(TEST_STRING2, null);

    assertEquals("One rule is set", 1, ss.size());

    RuleFontFace rule = (RuleFontFace) ss.get(0);
    assertEquals("Rule contains 2 declarations ", 2, rule.size());
    assertEquals("Rule contains font-family declaration", "font-family: 'MyWebFont';\n", rule.get(0).toString());
    assertEquals("Rule contains scr declaration",
            "src: url('myfont.woff2') format('woff2'), url('myfont.woff') format('woff'), url('myfont.ttf') format('truetype');\n",
            rule.get(1).toString());

}
 
Example #9
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testEscapedID() throws CSSException, IOException {

    StyleSheet ss = CSSFactory.parseString(TEST_ESCAPED_ID, null);
    assertEquals("One rule is set", 1, ss.size());

    RuleSet rule = (RuleSet) ss.get(0);

    List<CombinedSelector> cslist = SelectorsUtil.appendCS(null);
    SelectorsUtil.appendSimpleSelector(cslist, null, null, rf
            .createID("1krysa"));

    assertArrayEquals("Rule contains one ID selector #1krysa", cslist.toArray(), rule
            .getSelectors());

    assertEquals("Rule contains one declaration { font-size: 100px;}",
            DeclarationsUtil.appendDeclaration(null, "font-size", tf
                    .createLength(100.0f).setUnit(TermNumeric.Unit.px)),
            rule.asList());
}
 
Example #10
Source File: BaseNodeDataImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Term<?> getSpecifiedValue(String name) {
    Term<?> ret = getValue(name, true);
    if (ret == null)
        ret = css.getDefaultValue(name);
    
    if (ret != null) {
        if (ret instanceof TermColor && ((TermColor) ret).getKeyword() == Keyword.CURRENT_COLOR) {
            //clone the value fot setting the current color
            final TermFactory tf = CSSFactory.getTermFactory();
            ret = tf.createColor(tf.createIdent("currentColor"));
            //set the current color value
            TermColor cvalue = getValue(TermColor.class, "color", true);
            if (cvalue == null)
                cvalue = (TermColor) css.getDefaultValue("color");
            ((TermColor) ret).setValue(cvalue.getValue());
        }
    }
    
    return ret;
}
 
Example #11
Source File: PseudoClassTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void nonlegacyPseudoElementSupport() throws CSSException, IOException {
    StyleSheet style = CSSFactory.parseString(TEST_PSEUDO_2, null);
    assertEquals("There are 4 rules", 4, style.size());
    
    List<CombinedSelector> sel1 = SelectorsUtil.appendSimpleSelector(null, "p", null, rf.createPseudoElement("first-line"));
    List<CombinedSelector> sel2 = SelectorsUtil.appendSimpleSelector(null, "p", null, rf.createPseudoElement("first-letter"));
    List<CombinedSelector> sel3 = SelectorsUtil.appendSimpleSelector(null, "p", null, rf.createPseudoElement("before"));
    List<CombinedSelector> sel4 = SelectorsUtil.appendSimpleSelector(null, "p", null, rf.createPseudoElement("after"));

    RuleSet rule1 = (RuleSet) style.get(0);
    assertArrayEquals("Rule contains one selector p::first-line", sel1.toArray(), rule1.getSelectors());
    assertEquals("Rule contains one declaration", 1, rule1.size());
    
    RuleSet rule2 = (RuleSet) style.get(1);
    assertArrayEquals("Rule contains one selector p::first-letter", sel2.toArray(), rule2.getSelectors());
    assertEquals("Rule contains one declaration", 1, rule2.size());
    
    RuleSet rule3 = (RuleSet) style.get(2);
    assertArrayEquals("Rule contains one selector p::before", sel3.toArray(), rule3.getSelectors());
    assertEquals("Rule contains one declaration", 1, rule3.size());
    
    RuleSet rule4 = (RuleSet) style.get(3);
    assertArrayEquals("Rule contains one selector p::after", sel4.toArray(), rule4.getSelectors());
    assertEquals("Rule contains one declaration", 1, rule4.size());
}
 
Example #12
Source File: FontFaceTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
    public void testFFMultiSrc2() throws IOException, CSSException {

        log.info("input:\n\n\n" + TEST_STRING3 + "\n\n\n");
        StyleSheet ss;

        ss = CSSFactory.parseString(TEST_STRING3, null);

        assertEquals("Two rules are set", 2, ss.size());

        RuleFontFace rule = (RuleFontFace) ss.get(0);
        assertEquals("Rule contains 3 declarations ", 3, rule.size());
        assertEquals("Rule contains font-family declaration", "font-family: 'MyWebFont';\n", rule.get(0).toString());
//        assertEquals("Rule contains scr declaration",
//                "src: url('myfont.woff2') format('woff2'), url('myfont.woff') format('woff'), url('myfont.ttf') format('truetype');\n",
//                rule.get(1).toString());

    }
 
Example #13
Source File: DOMAssignTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void combinators() throws SAXException, IOException {  
    
    DOMSource ds = new DOMSource(getClass().getResourceAsStream("/simple/selectors3.html"));
    Document doc = ds.parse();
    ElementMap elements = new ElementMap(doc);
    
    StyleMap decl = CSSFactory.assignDOM(doc, null, getClass().getResource("/simple/selectors3.html"),"screen", true);
    
    NodeData i1 = getStyleById(elements, decl, "i1");
    NodeData i2 = getStyleById(elements, decl, "i2");
    NodeData i3 = getStyleById(elements, decl, "i3");
    NodeData i4 = getStyleById(elements, decl, "i4");

    assertThat("Descendant combinator", i1.getValue(TermColor.class, "color"), is(tf.createColor(0,128,0)));
    assertThat("Child combinator", i2.getValue(TermColor.class, "color"), is(tf.createColor(0,128,0)));
    assertThat("Adjacent sibling combinator", i3.getValue(TermColor.class, "color"), is(tf.createColor(0,128,0)));
    assertThat("Generic sibling combinator", i4.getValue(TermColor.class, "color"), is(tf.createColor(0,128,0)));
}
 
Example #14
Source File: DOMAssignTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void inherit() throws SAXException, IOException {  
    DOMSource ds = new DOMSource(getClass().getResourceAsStream("/advanced/inherit.html"));
    Document doc = ds.parse();
    ElementMap elements = new ElementMap(doc);
    
    StyleMap decl = CSSFactory.assignDOM(doc, null,
    		getClass().getResource("/advanced/inherit.html"),"screen", true);
    
    NodeData data = decl.get(elements.getElementById("item1"));
    assertNotNull("Data for #item1 exist", data);
    assertThat(data.getValue(TermLength.class, "border-top-width"), is(tf.createLength(1.0f, Unit.px)));
    assertThat(data.getValue(TermColor.class, "border-top-color"), is(tf.createColor(0, 128, 0)));
    assertThat((CSSProperty.BorderStyle) data.getProperty("border-top-style"), is(CSSProperty.BorderStyle.SOLID));
    assertThat(data.getValue(TermLength.class, "margin-top"), is(tf.createLength(1.0f, Unit.em)));
    assertThat(data.getValue(TermLength.class, "margin-bottom"), is(tf.createLength(3.0f, Unit.em)));
    
    data = decl.get(elements.getElementById("item2"));
    assertNotNull("Data for #item2 exist", data);
    assertThat(data.getValue(TermLength.class, "border-top-width"), is(tf.createLength(5.0f, Unit.px)));
    assertThat(data.getValue(TermColor.class, "border-top-color"), is(tf.createColor(0, 128, 0)));
    assertThat((CSSProperty.BorderStyle) data.getProperty("border-top-style"), is(CSSProperty.BorderStyle.DOTTED));
    assertThat(data.getValue(TermLength.class, "margin-top"), is(tf.createLength(1.0f, Unit.em)));
    assertNull(data.getValue(TermLength.class, "margin-bottom"));
    
}
 
Example #15
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testPreceding() throws CSSException, IOException {

    StyleSheet ss = CSSFactory.parseString(TEST_PRECEDING, null);
    assertEquals("One rule is set", 1, ss.size());

    RuleSet rule = (RuleSet) ss.get(0);

    List<CombinedSelector> cslist = SelectorsUtil.appendCS(null);
    SelectorsUtil.appendSimpleSelector(cslist, "DIV", null);
    SelectorsUtil.appendPreceding(cslist, "P");

    assertArrayEquals("Rule contains one combined selectors DIV~P ", cslist.toArray(),
            rule.getSelectors());

    assertEquals("Rule contains one declaration {color:blue;}",
            DeclarationsUtil.appendDeclaration(null, "color", tf
                    .createColor(0, 0, 255)), rule.asList());

}
 
Example #16
Source File: GradientTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testMultipleGradients() throws IOException, CSSException {
    StyleSheet ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_MULTIPLE, null);
    assertEquals("One rule is set", 1, ss.size());

    RuleSet rule = (RuleSet) ss.get(0);
    assertArrayEquals("Rule contains one selector div ", SelectorsUtil.createSelectors("div"), rule.getSelectors());
    assertEquals("Rule contains one declaration for background ", "background", rule.get(0).getProperty());
    assertEquals("Rule contains two terms ", 2, rule.get(0).size());

    TermFunction func = (TermFunction) rule.get(0).get(0);
    List<List<Term<?>>> separatedArguments = func.getSeparatedArgs(tf.createOperator(','));
    assertEquals("First function name is linear-gradient ", "linear-gradient", func.getFunctionName());
    assertEquals("First function has 2 comma-separated arguments ", 2, separatedArguments.size());
    assertEquals("The first argument is red ", Arrays.asList(tf.createColor(tf.createIdent("red"))), separatedArguments.get(0));
    assertEquals("The second argument is blue ", Arrays.asList(tf.createColor(tf.createIdent("blue"))), separatedArguments.get(1));

    func = (TermFunction) rule.get(0).get(1);
    separatedArguments = func.getSeparatedArgs(tf.createOperator(','));
    assertEquals("Second function name is linear-gradient ", "linear-gradient", func.getFunctionName());
    assertEquals("Second function has 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is to right ", Arrays.asList(tf.createIdent("to"), tf.createIdent("right")), separatedArguments.get(0));
    assertEquals("The second argument is yellow ", Arrays.asList(tf.createColor(tf.createIdent("yellow"))), separatedArguments.get(1));
    assertEquals("The third argument is green ", Arrays.asList(tf.createColor(tf.createIdent("green"))), separatedArguments.get(2));
}
 
Example #17
Source File: GradientTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testSampleGradients() throws IOException, CSSException {
    StyleSheet ss = CSSFactory.parseString(SAMPLE_LINEAR_GRADIENT_1, null);
    assertEquals("One rule is set", 1, ss.size());

    RuleSet rule1 = (RuleSet) ss.get(0);
    assertArrayEquals("Rule contains one selector div ", SelectorsUtil.createSelectors("div"), rule1.getSelectors());
    assertEquals("Rule contains one declaration for background ", "background", rule1.get(0).getProperty());
    assertEquals("Rule contains one term ", 1, rule1.get(0).size());
    assertTrue("Rule contains a TermFunction ", rule1.get(0).get(0) instanceof TermFunction);

    TermFunction func = (TermFunction) rule1.get(0).get(0);
    assertEquals("Function name is linear-gradient ", "linear-gradient", func.getFunctionName());

    List<List<Term<?>>> separatedArguments = func.getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is to bottom ", Arrays.asList(tf.createIdent("to"), tf.createIdent("bottom")), separatedArguments.get(0));
    assertEquals("The first argument is pretty-printed as \"to bottom\" ", "linear-gradient(to bottom,", func.toString().substring(0, "linear-gradient(to bottom,".length()));
    assertEquals("The second argument is rgba(245,245,245,1) 0% ", Arrays.asList(tf.createColor(245, 245, 245, 255), tf.createPercent(0f)), separatedArguments.get(1));
    assertEquals("The second argument is rgba(221,221,221,1) 100% ", Arrays.asList(tf.createColor(221, 221, 221, 255), tf.createPercent(100f)), separatedArguments.get(2));
}
 
Example #18
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testAsterisk() throws CSSException, IOException {

	StyleSheet ss = CSSFactory.parseString(TEST_ASTERISK, null);
	assertEquals("One rule is set", 1, ss.size());

	List<CombinedSelector> cslist = SelectorsUtil.appendCS(null);
	SelectorsUtil.appendSimpleSelector(cslist, "*", null, rf
			.createClass("home"));

	assertArrayEquals("Rule 1 contains one combined selector *.home", cslist.toArray(),
			((RuleSet) ss.get(0)).getSelectors());

	List<Term<?>> terms = DeclarationsUtil.appendTerm(null, null, tf
			.createIdent("Verdana"));
	DeclarationsUtil.appendCommaTerm(terms, tf.createIdent("monospace"));

	assertEquals(
			"Rule contains one declaration { font-family: Verdana, monospace }",
			DeclarationsUtil.appendDeclaration(null, "font-family", terms),
			((RuleSet) ss.get(0)).asList());
}
 
Example #19
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test 
public void testRGBFunction1() throws IOException, CSSException   {
	
	StyleSheet ss = CSSFactory.parseString(TEST_RGBFUNCTION1, null);
	assertEquals("One rule is set", 1, ss.size());
	
	RuleSet rule = (RuleSet) ss.get(0);				
	
	assertArrayEquals("Rule contains one selector BODY ", 
			SelectorsUtil.createSelectors("BODY"), 
			rule.getSelectors());
	
	assertEquals("Rule contains one declaration {color: #00aa85;}",
			DeclarationsUtil.appendDeclaration(null, "color", 
					tf.createColor(192, 64, 32)),
			rule.asList());
	
}
 
Example #20
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test 
public void testRGBFunctionInvalid() throws IOException, CSSException   {
    
    StyleSheet ss = CSSFactory.parseString(TEST_RGBFUNCTION_INVALID, null);
    assertEquals("One rule is set", 1, ss.size());
    
    RuleSet rule = (RuleSet) ss.get(0);             
    
    assertArrayEquals("Rule contains one selector BODY ", 
            SelectorsUtil.createSelectors("BODY"), 
            rule.getSelectors());
    
    assertEquals("Rule contains one declaration", 1, rule.size());
    Term<?> value = rule.get(0).get(0);
    assertTrue("Assigned value is TermFunction (not TermColor)", value instanceof TermFunction);
}
 
Example #21
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test 
public void testHSLFunction1() throws IOException, CSSException   {
    
    StyleSheet ss = CSSFactory.parseString(TEST_HSLFUNCTION1, null);
    assertEquals("One rule is set", 1, ss.size());
    
    RuleSet rule = (RuleSet) ss.get(0);             
    
    assertArrayEquals("Rule contains one selector BODY ", 
            SelectorsUtil.createSelectors("BODY"), 
            rule.getSelectors());
    
    assertEquals("Rule contains one declaration with color",
            DeclarationsUtil.appendDeclaration(null, "color", 
                    tf.createColor(0, 255, 0)),
            rule.asList());
    
}
 
Example #22
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test 
public void testRGBAFunction1() throws IOException, CSSException   {
    
    StyleSheet ss = CSSFactory.parseString(TEST_RGBAFUNCTION1, null);
    assertEquals("One rule is set", 1, ss.size());
    
    RuleSet rule = (RuleSet) ss.get(0);             
    
    assertArrayEquals("Rule contains one selector BODY ", 
            SelectorsUtil.createSelectors("BODY"), 
            rule.getSelectors());
    
    assertEquals("Rule contains one declaration with color",
            DeclarationsUtil.appendDeclaration(null, "color", 
                    tf.createColor(255, 0, 0, 51)),
            rule.asList());
    
}
 
Example #23
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test 
public void testHSLAFunction1() throws IOException, CSSException   {
    
    StyleSheet ss = CSSFactory.parseString(TEST_HSLAFUNCTION1, null);
    assertEquals("One rule is set", 1, ss.size());
    
    RuleSet rule = (RuleSet) ss.get(0);             
    
    assertArrayEquals("Rule contains one selector BODY ", 
            SelectorsUtil.createSelectors("BODY"), 
            rule.getSelectors());
    
    assertEquals("Rule contains one declaration with color",
            DeclarationsUtil.appendDeclaration(null, "color", 
                    tf.createColor(0, 0, 255, 102)),
            rule.asList());
    
}
 
Example #24
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testClass() throws CSSException, IOException {

	StyleSheet ss = CSSFactory.parseString(TEST_CLASS, null);
	assertEquals("One rule is set", 1, ss.size());

	RuleSet rule = (RuleSet) ss.get(0);

	List<CombinedSelector> cslist = SelectorsUtil.appendCS(null);
	SelectorsUtil.appendSimpleSelector(cslist, null, null, rf
			.createClass("fit"));

	assertArrayEquals("Rule contains one class selector .fit", cslist.toArray(), rule
			.getSelectors());

	assertEquals("Rule contains one declaration { width: 80%;}",
			DeclarationsUtil.appendDeclaration(null, "width", tf
					.createPercent(80.0f)), rule.asList());
}
 
Example #25
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testHashColor2() throws IOException, CSSException   {
	
	StyleSheet ss = CSSFactory.parseString(TEST_HASH_COLOR2, null);
	assertEquals("One rule is set", 1, ss.size());
	
	final RuleSet rule = (RuleSet) ss.get(0);				
	
	assertArrayEquals("Rule contains two selectors DIV, P", 
			SelectorsUtil.createSelectors("DIV", "P"), 
			rule.getSelectors());
	
	assertEquals("Rule contains one declaration {color: #CCC;}",
			DeclarationsUtil.appendDeclaration(null, "color", 
					tf.createColor(204,204,204)),
			rule.asList());
}
 
Example #26
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testBracketedIdent() throws IOException, CSSException   {
    
    StyleSheet ss = CSSFactory.parseString(TEST_BRACKETED_IDENT, null);
    assertEquals("There is one rule", 1, ss.size());
    
    Declaration dec = (Declaration) ss.get(0).get(0);
    assertEquals("There is one declaration", 1, ss.get(0).size());
    assertEquals("Four terms", 4, dec.size());
    
    assertTrue("Term[0] type is BracketedIdents", dec.get(0) instanceof TermBracketedIdents);
    assertEquals("Term[0] : There is one ident", 1, ((TermBracketedIdents) dec.get(0)).size());
    assertEquals("Term[0] : Identifier value is correct", "-linename1", ((TermBracketedIdents) dec.get(0)).get(0).getValue());
    assertTrue("Term[3] type is BracketedIdents", dec.get(3) instanceof TermBracketedIdents);
    assertEquals("Term[3] : There is one ident", 1, ((TermBracketedIdents) dec.get(3)).size());
    assertEquals("Term[3] : Identifier value is correct", "linename2", ((TermBracketedIdents) dec.get(3)).get(0).getValue());
}
 
Example #27
Source File: PseudoClassTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void pseudoClassDirectNonStatic() throws SAXException, IOException {

    DOMSource ds = new DOMSource(getClass().getResourceAsStream("/simple/pseudo.html"));
    Document doc = ds.parse();
    ElementMap elements = new ElementMap(doc);

    MatchConditionOnElements cond = new MatchConditionOnElements("a", PseudoClassType.LINK);
    cond.addMatch(elements.getElementById("l2"), PseudoClassType.HOVER);
    cond.addMatch(elements.getElementById("l3"), PseudoClassType.VISITED);

    StyleSheet style = CSSFactory.getUsedStyles(doc, null, createBaseFromFilename("data/simple/selectors.html"),"screen");
    DirectAnalyzer da = new DirectAnalyzer(style);
    da.registerMatchCondition(cond);

    NodeData l1 = getStyleById(elements, da, "l1");
    NodeData l2 = getStyleById(elements, da, "l2");
    NodeData l3 = getStyleById(elements, da, "l3");

    assertThat(l1.getValue(TermColor.class, "color"), is(tf.createColor(0,255,0)));
    assertThat(l2.getValue(TermColor.class, "color"), is(tf.createColor(0,255,255)));
    assertThat(l3.getValue(TermColor.class, "color"), is(tf.createColor(0,0,170)));
}
 
Example #28
Source File: AnalyzerTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@BeforeClass
public static void init() throws IOException, CSSException, SAXException {
	log.info("\n\n\n == AnalyzerTest test at {} == \n\n\n", new Date());

       DOMSource ds = new DOMSource(AnalyzerTest.class.getResourceAsStream("/simple/data.html"));
       doc = ds.parse();
       
	sheet = CSSFactory.parse(AnalyzerTest.class.getResource("/simple/data.css"), null);

	analyzer = new Analyzer(sheet);

	NodeList list = doc.getElementsByTagName("body");
	assertEquals("There is one <body> element", 1, list.getLength());

	//walker = new TidyTreeWalker(list.item(0), NodeFilter.SHOW_ELEMENT);
	DocumentTraversal traversal = (DocumentTraversal) doc;
	walker = traversal.createTreeWalker(list.item(0), NodeFilter.SHOW_ELEMENT, null, false);
	elements = new ElementMap(doc);
}
 
Example #29
Source File: FunctionsTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void rectFunctions() throws IOException, CSSException
{
       StyleSheet ss1 = CSSFactory.parseString(TEST_RECT1, null);
       assertEquals("Two properties are accepted", 2, ss1.get(0).size());
       Declaration d1 = (Declaration) ss1.get(0).get(0);
       TermRect r1 = (TermRect) d1.get(0);
       assertEquals("The last one is a correct length", tf.createLength(2f, Unit.ch), r1.getValue().get(3));
    
       StyleSheet ss2 = CSSFactory.parseString(TEST_RECT2, null);
       assertEquals("Two properties are accepted", 2, ss2.get(0).size());
       Declaration d2 = (Declaration) ss2.get(0).get(0);
       TermRect r2 = (TermRect) d2.get(0);
       assertEquals("The last one is a correct length", tf.createLength(2f, Unit.ch), r2.getValue().get(3));
       
       StyleSheet ss3 = CSSFactory.parseString(TEST_RECT3, null);
       assertEquals("Two properties are accepted", 2, ss3.get(0).size());
       Declaration d3 = (Declaration) ss3.get(0).get(0);
       TermRect r3 = (TermRect) d3.get(0);
       assertEquals("The last one is a correct length", null, r3.getValue().get(3));
}
 
Example #30
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testID() throws CSSException, IOException {

	StyleSheet ss = CSSFactory.parseString(TEST_ID, null);
	assertEquals("One rule is set", 1, ss.size());

	RuleSet rule = (RuleSet) ss.get(0);

	List<CombinedSelector> cslist = SelectorsUtil.appendCS(null);
	SelectorsUtil.appendSimpleSelector(cslist, null, null, rf
			.createID("krysa"));

	assertArrayEquals("Rule contains one ID selector #krysa", cslist.toArray(), rule
			.getSelectors());

	assertEquals("Rule contains one declaration { font-size: 100px;}",
			DeclarationsUtil.appendDeclaration(null, "font-size", tf
					.createLength(100.0f).setUnit(TermNumeric.Unit.px)),
			rule.asList());
}