Java Code Examples for cz.vutbr.web.css.CSSFactory#parseString()

The following examples show how to use cz.vutbr.web.css.CSSFactory#parseString() . 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 testPseudo() throws CSSException, IOException {

	StyleSheet ss = CSSFactory.parseString(TEST_PSEUDO, 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.createPseudoClass("hover"));

	assertArrayEquals("Rule contains one pseudoselector :hover", cslist.toArray(), rule
			.getSelectors());

	assertEquals(
			"Rule contains one declaration { text-decoration: underline; }",
			DeclarationsUtil.appendDeclaration(null, "text-decoration", tf
					.createIdent("underline")), rule.asList());
}
 
Example 2
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 3
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 4
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 5
Source File: GrammarRecovery2Test.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void invalidCharColon() throws IOException, CSSException 
{
    StyleSheet ss = CSSFactory.parseString(TEST_DECL3A, null);
    assertEquals("One property is accepted (second one is invalid)", 1, ss.get(0).size());
    ss = CSSFactory.parseString(TEST_DECL3B, null);
    assertEquals("One property is accepted (first one is invalid)", 1, ss.get(0).size());
}
 
Example 6
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testIntegerZIndex() throws IOException, CSSException   {
	
	StyleSheet ss = CSSFactory.parseString(TEST_INTEGER_Z_INDEX, 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());
	
	Object term = dec.get(0);
	assertTrue("Term value is Integer", term instanceof TermInteger);
	
	TermInteger zIndex = (TermInteger) term;
	assertTrue("toString should return 10", "10".equals(zIndex.toString()));
}
 
Example 7
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testURI1() throws IOException, CSSException   {
	
	StyleSheet ss = CSSFactory.parseString(TEST_URI1, 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());
	
	Object term = dec.get(0);
	assertTrue("Term value is URI", term instanceof TermURI);
	
	TermURI uri = (TermURI) term;
	assertEquals("URI has proper value", "image.jpg", uri.getValue());
}
 
Example 8
Source File: FunctionsTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void calcAngles() throws IOException, CSSException
{
    DegEvaluator eval = new DegEvaluator();
    for (int i = 0; i < TEST_CALC_A.length; i++)
    {
        StyleSheet ss = CSSFactory.parseString(TEST_CALC_A[i], null);
        assertEquals("Two properties are accepted [" + i + "]", 2, ss.get(0).size());
        Declaration d = (Declaration) ss.get(0).get(0);
        TermCalc calc = (TermCalc) d.get(0);
        Double result = calc.getArgs().evaluate(eval);
        assertEquals("Experssion result is correct [" + i + "]", 33.0, result.doubleValue(), 0.000001);
    }
}
 
Example 9
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testPSpecial() throws CSSException, IOException {

	StyleSheet ss = CSSFactory.parseString(TEST_PSPECIAL, null);
	assertEquals("Two rules are set", 2, ss.size());

	// test first rule
	List<CombinedSelector> cslist = SelectorsUtil.appendCS(null);
	SelectorsUtil.appendSimpleSelector(cslist, "P", null, 
               rf.createClass("special"), 
               rf.createPseudoElement("before"));

	assertArrayEquals("Rule 1 contains one combined selector P.special:before",
			cslist.toArray(), ((RuleSet) ss.get(0)).getSelectors());

	assertEquals(
			"Rule 2 contains one declaration { content: \"Special! \"}",
			DeclarationsUtil.appendDeclaration(null, "content", tf
					.createString("Special! ")), ((RuleSet) ss.get(0))
					.asList());

	// test second rule
	cslist = SelectorsUtil.appendCS(null);
	SelectorsUtil.appendSimpleSelector(cslist, "P", null, 
               rf.createClass("special"), 
               rf.createPseudoElement("first-letter"));

	assertArrayEquals(
			"Rule 2 contains one combined selector P.special:first-letter",
			cslist.toArray(), ((RuleSet) ss.get(1)).getSelectors());

	assertEquals("Rule 2 contains one declaration { color: #ffd800}",
			DeclarationsUtil.appendDeclaration(null, "color", tf
					.createColor(255, 216, 0)), ((RuleSet) ss.get(1))
					.asList());
}
 
Example 10
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testBracketedIdents() throws IOException, CSSException   {
    
    StyleSheet ss = CSSFactory.parseString(TEST_BRACKETED_IDENTS, 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 are two idents", 2, ((TermBracketedIdents) dec.get(0)).size());
    assertEquals("Term[0] : Identifier1 value is correct", "-linename1", ((TermBracketedIdents) dec.get(0)).get(0).getValue());
    assertEquals("Term[0] : Identifier2 value is correct", "linename2", ((TermBracketedIdents) dec.get(0)).get(1).getValue());
}
 
Example 11
Source File: FunctionsTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void contentInvalid() throws IOException, CSSException
{
    for (int i = 0; i < TEST_CONTENT_INVALID.length; i++)
    {
        StyleSheet ss = CSSFactory.parseString(TEST_CONTENT_INVALID[i], null);
        assertEquals("One rule is parset [" + i + "]", 1, ss.size());
        assertEquals("One property is set [" + i + "]", 1, ss.get(0).size());
    }        
}
 
Example 12
Source File: EscapingTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void assertSingleId(String input, String className) throws IOException, CSSException
{
    StyleSheet ss = CSSFactory.parseString(input, null);
    assertEquals("One rule is set", 1, ss.size());
    RuleSet rule = (RuleSet) ss.get(0);
    assertEquals("One combined selector", rule.getSelectors().length, 1);
    CombinedSelector cs = rule.getSelectors()[0];
    assertEquals("One selector", cs.size(), 1);
    assertEquals("The class name is correct", className, cs.get(0).getIDName());
}
 
Example 13
Source File: FunctionsTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void gradientInvalid() throws IOException, CSSException
{
    for (int i = 0; i < TEST_GRADIENT_INVALID.length; i++)
    {
        StyleSheet ss = CSSFactory.parseString(TEST_GRADIENT_INVALID[i], null);
        assertEquals("One rule is parset [" + i + "]", 1, ss.size());
        assertEquals("One property is set [" + i + "]", 1, ss.get(0).size());
    }        
}
 
Example 14
Source File: GradientTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testGradientAngles() throws IOException, CSSException {
    StyleSheet ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_ANGLE_DEG, null);
    List<List<Term<?>>> separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is 10deg ", Arrays.asList(tf.createAngle("10deg", TermNumeric.Unit.deg, 1)), separatedArguments.get(0));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_ANGLE_RAD, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is 1.3rad ", Arrays.asList(tf.createAngle("1.3rad", TermNumeric.Unit.rad, 1)), separatedArguments.get(0));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_ANGLE_GRAD, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is 671grad ", Arrays.asList(tf.createAngle("671grad", TermNumeric.Unit.grad, 1)), separatedArguments.get(0));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_ANGLE_TURN, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is 0.001turn ", Arrays.asList(tf.createAngle("0.001turn", TermNumeric.Unit.turn, 1)), separatedArguments.get(0));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_ANGLE_0, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is 0 ", Arrays.asList(tf.createInteger(0)), separatedArguments.get(0));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_ANGLE_NEGATIVE, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is -10deg ", Arrays.asList(tf.createAngle("10deg", TermNumeric.Unit.deg, -1)), separatedArguments.get(0));
}
 
Example 15
Source File: FunctionsTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void calcLengths() throws IOException, CSSException
{
 PxEvaluator eval = new PxEvaluator();
 for (int i = 0; i < TEST_CALC_L.length; i++)
 {
     StyleSheet ss = CSSFactory.parseString(TEST_CALC_L[i], null);
     assertEquals("Two properties are accepted [" + i + "]", 2, ss.get(0).size());
     Declaration d = (Declaration) ss.get(0).get(0);
     TermCalc calc = (TermCalc) d.get(0);
     Double result = calc.getArgs().evaluate(eval);
        assertEquals("Experssion result is correct [" + i + "]", 60.0, result.doubleValue(), 0.000001);
 }
}
 
Example 16
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testInvalidPseudoSelector1() throws IOException, CSSException   {
	StyleSheet ss = CSSFactory.parseString(TEST_INVALID_PSEUDO_SELECTOR1, null);
	assertEquals("Zero rule is set", 0, ss.size());
}
 
Example 17
Source File: GradientTest.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testGradientColorStops() throws IOException, CSSException {
    StyleSheet ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_STOPS_P1, null);
    List<List<Term<?>>> separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 2 comma-separated arguments ", 2, separatedArguments.size());
    assertEquals("The first argument is red 0% ", Arrays.asList(tf.createColor(tf.createIdent("red")), tf.createPercent(0f)), separatedArguments.get(0));
    assertEquals("The second argument is blue 100% ", Arrays.asList(tf.createColor(tf.createIdent("blue")), tf.createPercent(100f)), separatedArguments.get(1));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_STOPS_P2, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is red 20% ", Arrays.asList(tf.createColor(tf.createIdent("red")), tf.createPercent(20f)), separatedArguments.get(0));
    assertEquals("The second argument is blue 73% ", Arrays.asList(tf.createColor(tf.createIdent("blue")), tf.createPercent(73f)), separatedArguments.get(1));
    assertEquals("The third argument is green 81% ", Arrays.asList(tf.createColor(tf.createIdent("green")), tf.createPercent(81f)), separatedArguments.get(2));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_STOPS_L1, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 2 comma-separated arguments ", 2, separatedArguments.size());
    assertEquals("The first argument is red 0 ", Arrays.asList(tf.createColor(tf.createIdent("red")), tf.createInteger(0)), separatedArguments.get(0));
    assertEquals("The second argument is blue 10em ", Arrays.asList(tf.createColor(tf.createIdent("blue")), tf.createLength(10f, TermNumeric.Unit.em)), separatedArguments.get(1));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_STOPS_L2, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 2 comma-separated arguments ", 2, separatedArguments.size());
    assertEquals("The first argument is red 10pt ", Arrays.asList(tf.createColor(tf.createIdent("red")), tf.createLength(10f, TermNumeric.Unit.pt)), separatedArguments.get(0));
    assertEquals("The second argument is blue 120pt ", Arrays.asList(tf.createColor(tf.createIdent("blue")), tf.createLength(120f, TermNumeric.Unit.pt)), separatedArguments.get(1));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_STOPS_MIX, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
    assertEquals("The first argument is red ", Arrays.asList(tf.createColor(tf.createIdent("red"))), separatedArguments.get(0));
    assertEquals("The second argument is blue 120pt ", Arrays.asList(tf.createColor(tf.createIdent("blue")), tf.createLength(120f, TermNumeric.Unit.pt)), separatedArguments.get(1));
    assertEquals("The third argument is yellow 84% ", Arrays.asList(tf.createColor(tf.createIdent("yellow")), tf.createPercent(84f)), separatedArguments.get(2));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_STOPS_HASH, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 2 comma-separated arguments ", 2, separatedArguments.size());
    assertEquals("The first argument is #00F 10% ", Arrays.asList(tf.createColor("#00F"), tf.createPercent(10f)), separatedArguments.get(0));
    assertEquals("The second argument is #FF3 5em ", Arrays.asList(tf.createColor("#FF3"), tf.createLength(5f, TermNumeric.Unit.em)), separatedArguments.get(1));

    ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_STOPS_RGB, null);
    separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
    assertEquals("There are 2 comma-separated arguments ", 2, separatedArguments.size());
    assertEquals("The first argument is rgb(0,0,255) 10% ", Arrays.asList(tf.createColor(0, 0, 255), tf.createPercent(10f)), separatedArguments.get(0));
    assertEquals("The second argument is rgb(255,255,0) 5em ", Arrays.asList(tf.createColor(255, 255, 0), tf.createLength(5f, TermNumeric.Unit.em)), separatedArguments.get(1));
}
 
Example 18
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testString2() throws IOException, CSSException   {
	
	StyleSheet ss = CSSFactory.parseString(TEST_STRING2, null);
	assertEquals("Six rules are set", 6, ss.size());
}
 
Example 19
Source File: SelectorTest.java    From jStyleParser with GNU Lesser General Public License v3.0 3 votes vote down vote up
@Test
public void testInvalidID() throws CSSException, IOException {

    StyleSheet ss = CSSFactory.parseString(TEST_INVALID_ID, null);
    assertEquals("No rule is set", 0, ss.size());

}
 
Example 20
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 3 votes vote down vote up
@Test
public void testUnit() throws IOException, CSSException {
	StyleSheet ss = CSSFactory.parseString(TEST_UNIT, null);
	
	assertEquals("There is one rule", 1, ss.size());
	
}