summaryrefslogtreecommitdiff
path: root/libs/litehtml/src/css_length.cpp
blob: 798eb0bd37d0ccec27330f76187c035264c057fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "html.h"
#include "css_length.h"

namespace litehtml
{

bool css_length::from_token(const css_token& token, int options, const string& keywords)
{
	if ((options & f_positive) && is_one_of(token.type, NUMBER, DIMENSION, PERCENTAGE) && token.n.number < 0)
		return false;

	if (token.type == IDENT)
	{
		int idx = value_index(lowcase(token.name), keywords);
		if (idx == -1) return false;
		m_predef = idx;
		m_is_predefined = true;
		return true;
	}
	else if (token.type == DIMENSION)
	{
		if (!(options & f_length)) return false;

		int idx = value_index(lowcase(token.unit), css_units_strings);
		// note: 1none and 1\% are invalid
		if (idx == -1 || idx == css_units_none || idx == css_units_percentage)
			return false;
		
		m_value = token.n.number;
		m_units = (css_units)idx;
		m_is_predefined = false;
		return true;
	}
	else if (token.type == PERCENTAGE)
	{
		if (!(options & f_percentage)) return false;

		m_value = token.n.number;
		m_units = css_units_percentage;
		m_is_predefined = false;
		return true;
	}
	else if (token.type == NUMBER)
	{
		// if token is a nonzero number and neither f_number nor f_integer is specified in the options
		if (!(options & (f_number | f_integer)) && token.n.number != 0)
			return false;
		// if token is a zero number and neither of f_number, f_integer or f_length are specified in the options
		if (!(options & (f_number | f_integer | f_length)) && token.n.number == 0)
			return false;
		if ((options & f_integer) && token.n.number_type != css_number_integer)
			return false;
		
		m_value = token.n.number;
		m_units = css_units_none;
		m_is_predefined = false;
		return true;
	}
	return false;
}

string css_length::to_string() const
{
	if(m_is_predefined)
	{
		return "def(" + std::to_string(m_predef) + ")";
	}
	return std::to_string(m_value) + "{" + index_value(m_units, css_units_strings) + "}";
}

css_length css_length::predef_value(int val)
{
	css_length len;
	len.predef(val);
	return len;
}

} // namespace litehtml