Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_SW_SOURCE_FILTER_HTML_PARCSS1_HXX
21 : #define INCLUDED_SW_SOURCE_FILTER_HTML_PARCSS1_HXX
22 :
23 : // tokens of the CSS1 parser
24 : enum CSS1Token
25 : {
26 : CSS1_NULL,
27 : CSS1_UNKNOWN,
28 :
29 : CSS1_IDENT,
30 : CSS1_STRING,
31 : CSS1_NUMBER,
32 : CSS1_PERCENTAGE,
33 : CSS1_LENGTH, // absolute length in 1/100 MM
34 : CSS1_PIXLENGTH, // length in pixels
35 : CSS1_EMS,
36 : CSS1_EMX,
37 : CSS1_HEXCOLOR,
38 :
39 : CSS1_DOT_W_WS,
40 : CSS1_DOT_WO_WS,
41 : CSS1_COLON,
42 : CSS1_SLASH,
43 : CSS1_PLUS,
44 : CSS1_MINUS,
45 : CSS1_OBRACE,
46 : CSS1_CBRACE,
47 : CSS1_SEMICOLON,
48 : CSS1_COMMA,
49 : CSS1_HASH,
50 :
51 : CSS1_IMPORT_SYM,
52 : CSS1_PAGE_SYM, // Feature: PrintExt
53 :
54 : CSS1_IMPORTANT_SYM,
55 :
56 : CSS1_URL,
57 : CSS1_RGB
58 : };
59 :
60 : enum CSS1ParserState
61 : {
62 : CSS1_PAR_ACCEPTED = 0,
63 : CSS1_PAR_WORKING,
64 : CSS1_PAR_ERROR
65 : };
66 :
67 : enum CSS1SelectorType
68 : {
69 : CSS1_SELTYPE_ELEMENT,
70 : CSS1_SELTYPE_ELEM_CLASS,
71 : CSS1_SELTYPE_CLASS,
72 : CSS1_SELTYPE_ID,
73 : CSS1_SELTYPE_PSEUDO,
74 : CSS1_SELTYPE_PAGE // Feature: PrintExt
75 : };
76 :
77 : /** A simple selector
78 : *
79 : * This class represents a simple selector, e.g.
80 : * - a HTML element name
81 : * - a HTML element name with a class (separated by a dot)
82 : * - a class (without a dot)
83 : * - an ID (set with ID=xxx)
84 : * - a pseudo element
85 : *
86 : * These simple selectors are chained in a list to complete selectors
87 : */
88 : class CSS1Selector
89 : {
90 : CSS1SelectorType eType; // the type
91 : OUString aSelector; // the selector itself
92 : CSS1Selector *pNext; // the following component
93 :
94 : public:
95 62 : CSS1Selector( CSS1SelectorType eTyp, const OUString &rSel )
96 62 : : eType(eTyp), aSelector( rSel ), pNext( 0 )
97 62 : {}
98 :
99 : ~CSS1Selector();
100 :
101 101 : CSS1SelectorType GetType() const { return eType; }
102 46 : const OUString& GetString() const { return aSelector; }
103 :
104 15 : void SetNext( CSS1Selector *pNxt ) { pNext = pNxt; }
105 50 : const CSS1Selector *GetNext() const { return pNext; }
106 : };
107 :
108 : /** a subexpression of a CSS1 declaration
109 : *
110 : * It contains
111 : * - the type of this expression (= token)
112 : * - the value as string (and/or double, with algebraic sign for NUMBER and LENGTH)
113 : * - the operator with that it is connected with the *predecessor* expression
114 : */
115 : struct CSS1Expression
116 : {
117 : sal_Unicode cOp; // type of the link with its predecessor
118 : CSS1Token eType; // type of the expression
119 : OUString aValue; // value as string
120 : double nValue; // value as number (TWIPs for LENGTH)
121 : CSS1Expression *pNext; // the following component
122 :
123 : public:
124 3975 : CSS1Expression( CSS1Token eTyp, const OUString &rVal,
125 : double nVal, sal_Unicode cO = 0 )
126 3975 : : cOp(cO), eType(eTyp), aValue(rVal), nValue(nVal), pNext(0)
127 3975 : {}
128 :
129 : ~CSS1Expression();
130 :
131 : inline void Set( CSS1Token eTyp, const OUString &rVal, double nVal,
132 : sal_Unicode cO = 0 );
133 :
134 3931 : CSS1Token GetType() const { return eType; }
135 622 : const OUString& GetString() const { return aValue; }
136 579 : double GetNumber() const { return nValue; }
137 : inline sal_uInt32 GetULength() const;
138 : inline sal_Int32 GetSLength() const;
139 1732 : sal_Unicode GetOp() const { return cOp; }
140 :
141 : bool GetURL( OUString& rURL ) const;
142 : bool GetColor( Color &rRGB ) const;
143 :
144 1143 : void SetNext( CSS1Expression *pNxt ) { pNext = pNxt; }
145 1739 : const CSS1Expression *GetNext() const { return pNext; }
146 : };
147 :
148 0 : inline void CSS1Expression::Set( CSS1Token eTyp, const OUString &rVal,
149 : double nVal, sal_Unicode cO )
150 : {
151 0 : cOp = cO; eType = eTyp; aValue = rVal; nValue = nVal; pNext = 0;
152 0 : }
153 :
154 15 : inline sal_uInt32 CSS1Expression::GetULength() const
155 : {
156 15 : return nValue < 0. ? 0UL : (sal_uInt32)(nValue + .5);
157 : }
158 :
159 2139 : inline sal_Int32 CSS1Expression::GetSLength() const
160 : {
161 2139 : return (sal_Int32)(nValue + (nValue < 0. ? -.5 : .5 ));
162 : }
163 :
164 : /** Parser of a style element/option
165 : *
166 : * This class parses the content of a style element or a style option and preprocesses it.
167 : *
168 : * The result of the parser is forwarded to derived parsers by the methods SelectorParsed()
169 : * and DeclarationParsed(). Example:
170 : * H1, H2 { font-weight: bold; text-align: right }
171 : * | | | |
172 : * | | | DeclP( 'text-align', 'right' )
173 : * | | DeclP( 'font-weight', 'bold' )
174 : * | SelP( 'H2', false )
175 : * SelP( 'H1', true )
176 : */
177 : class CSS1Parser
178 : {
179 : bool bWhiteSpace : 1; // read a whitespace?
180 : bool bEOF : 1; // is end of "file"?
181 :
182 : sal_Unicode cNextCh; // next character
183 :
184 : sal_Int32 nInPos; // current position in the input string
185 :
186 : sal_uInt32 nlLineNr; // current row number
187 : sal_uInt32 nlLinePos; // current column number
188 :
189 : double nValue; // value of the token as number
190 :
191 : CSS1ParserState eState; // current state of the parser
192 : CSS1Token nToken; // the current token
193 :
194 : OUString aIn; // the string to parse
195 : OUString aToken; // token as string
196 :
197 : /// prepare parsing
198 : void InitRead( const OUString& rIn );
199 :
200 : /// @returns the next character to parse
201 : sal_Unicode GetNextChar();
202 :
203 : /// @returns the next token to parse
204 : CSS1Token GetNextToken();
205 :
206 : /// Is the parser still working?
207 16375 : bool IsParserWorking() const { return CSS1_PAR_WORKING == eState; }
208 :
209 44082 : bool IsEOF() const { return bEOF; }
210 :
211 66 : sal_uInt32 IncLineNr() { return ++nlLineNr; }
212 68048 : sal_uInt32 IncLinePos() { return ++nlLinePos; }
213 : inline sal_uInt32 SetLinePos( sal_uInt32 nlPos ); // inline declaration below
214 :
215 : // parse parts of the grammar
216 : void ParseRule();
217 : CSS1Selector *ParseSelector();
218 : CSS1Expression *ParseDeclaration( OUString& rProperty );
219 :
220 : protected:
221 : void ParseStyleSheet();
222 :
223 : /** parse the content of a HTML style element
224 : *
225 : * For each selector and each declaration the methods SelectorParsed()
226 : * or DeclarationParsed() need to be called afterwards
227 : *
228 : * @param rIn the style element as string
229 : * @return true if ???
230 : */
231 : bool ParseStyleSheet( const OUString& rIn );
232 :
233 : /** parse the content of a HTML style option
234 : *
235 : * For each selector and each declaration the methods SelectorParsed()
236 : * or DeclarationParsed() need to be called afterwards.
237 : *
238 : * @param rIn the style option as string
239 : * @return true if ???
240 : */
241 : bool ParseStyleOption( const OUString& rIn );
242 :
243 : /** Called after a selector was parsed.
244 : *
245 : * @param pSelector The selector that was parsed
246 : * @param bFirst if true, a new declaration starts with this selector
247 : * @return If true, the selector will be deleted. (Returns always true?)
248 : */
249 : virtual bool SelectorParsed( CSS1Selector* pSelector, bool bFirst );
250 :
251 : /** Called after a declaration or property was parsed
252 : *
253 : * @param rProperty The declaration/property
254 : * @param pExpr ???
255 : * @return If true, the declaration will be deleted. (Returns always true?)
256 : */
257 : virtual bool DeclarationParsed( const OUString& rProperty,
258 : const CSS1Expression *pExpr );
259 :
260 : public:
261 : CSS1Parser();
262 : virtual ~CSS1Parser();
263 :
264 : inline sal_uInt32 GetLineNr() const { return nlLineNr; }
265 : inline sal_uInt32 GetLinePos() const { return nlLinePos; }
266 : };
267 :
268 66 : inline sal_uInt32 CSS1Parser::SetLinePos( sal_uInt32 nlPos )
269 : {
270 66 : sal_uInt32 nlOld = nlLinePos;
271 66 : nlLinePos = nlPos;
272 66 : return nlOld;
273 : }
274 :
275 : #endif
276 :
277 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|