android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / filter / html / parcss1.hxx
blob99f46af8e830cf40ca30d659e30365c95b3cc007
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SW_SOURCE_FILTER_HTML_PARCSS1_HXX
21 #define INCLUDED_SW_SOURCE_FILTER_HTML_PARCSS1_HXX
23 #include <rtl/ustring.hxx>
24 #include <tools/color.hxx>
26 #include <memory>
27 #include <utility>
29 // tokens of the CSS1 parser
30 enum CSS1Token
32 CSS1_NULL,
34 CSS1_IDENT,
35 CSS1_STRING,
36 CSS1_NUMBER,
37 CSS1_PERCENTAGE,
38 CSS1_LENGTH, // absolute length in 1/100 MM
39 CSS1_PIXLENGTH, // length in pixels
40 CSS1_EMS,
41 CSS1_EMX,
42 CSS1_HEXCOLOR,
44 CSS1_DOT_W_WS,
45 CSS1_DOT_WO_WS,
46 CSS1_COLON,
47 CSS1_SLASH,
48 CSS1_PLUS,
49 CSS1_MINUS,
50 CSS1_OBRACE,
51 CSS1_CBRACE,
52 CSS1_SEMICOLON,
53 CSS1_COMMA,
54 CSS1_HASH,
56 CSS1_IMPORT_SYM,
57 CSS1_PAGE_SYM, // Feature: PrintExt
59 CSS1_IMPORTANT_SYM,
61 CSS1_URL,
62 CSS1_RGB
65 enum CSS1ParserState
67 CSS1_PAR_ACCEPTED = 0,
68 CSS1_PAR_WORKING
71 enum CSS1SelectorType
73 CSS1_SELTYPE_ELEMENT,
74 CSS1_SELTYPE_ELEM_CLASS,
75 CSS1_SELTYPE_CLASS,
76 CSS1_SELTYPE_ID,
77 CSS1_SELTYPE_PSEUDO,
78 CSS1_SELTYPE_PAGE // Feature: PrintExt
81 /** A simple selector
83 * This class represents a simple selector, e.g.
84 * - a HTML element name
85 * - a HTML element name with a class (separated by a dot)
86 * - a class (without a dot)
87 * - an ID (set with ID=xxx)
88 * - a pseudo element
90 * These simple selectors are chained in a list to complete selectors
92 class CSS1Selector
94 CSS1SelectorType m_eType; // the type
95 OUString m_aSelector; // the selector itself
96 CSS1Selector *m_pNext; // the following component
98 public:
99 CSS1Selector( CSS1SelectorType eTyp, OUString aSel )
100 : m_eType(eTyp), m_aSelector(std::move( aSel )), m_pNext( nullptr )
103 ~CSS1Selector();
105 CSS1SelectorType GetType() const { return m_eType; }
106 const OUString& GetString() const { return m_aSelector; }
108 void SetNext( CSS1Selector *pNxt ) { m_pNext = pNxt; }
109 const CSS1Selector *GetNext() const { return m_pNext; }
112 /** a subexpression of a CSS1 declaration
114 * It contains
115 * - the type of this expression (= token)
116 * - the value as string (and/or double, with algebraic sign for NUMBER and LENGTH)
117 * - the operator with that it is connected with the *predecessor* expression
119 struct CSS1Expression
121 private:
122 sal_Unicode cOp; // type of the link with its predecessor
123 CSS1Token eType; // type of the expression
124 OUString aValue; // value as string
125 double nValue; // value as number (TWIPs for LENGTH)
126 CSS1Expression *pNext; // the following component
128 public:
129 CSS1Expression( CSS1Token eTyp, OUString aVal,
130 double nVal, sal_Unicode cO = 0 )
131 : cOp(cO), eType(eTyp), aValue(std::move(aVal)), nValue(nVal), pNext(nullptr)
134 ~CSS1Expression();
136 inline void Set( CSS1Token eTyp, const OUString &rVal, double nVal );
138 CSS1Token GetType() const { return eType; }
139 const OUString& GetString() const { return aValue; }
140 double GetNumber() const { return nValue; }
141 inline sal_uInt32 GetULength() const;
142 inline sal_Int32 GetSLength() const;
143 sal_Unicode GetOp() const { return cOp; }
145 void GetURL( OUString& rURL ) const;
146 bool GetColor( Color &rRGB ) const;
148 void SetNext( CSS1Expression *pNxt ) { pNext = pNxt; }
149 const CSS1Expression *GetNext() const { return pNext; }
152 inline void CSS1Expression::Set( CSS1Token eTyp, const OUString &rVal,
153 double nVal )
155 cOp = 0; eType = eTyp; aValue = rVal; nValue = nVal; pNext = nullptr;
158 inline sal_uInt32 CSS1Expression::GetULength() const
160 return nValue < 0. ? 0UL : static_cast<sal_uInt32>(nValue + .5);
163 inline sal_Int32 CSS1Expression::GetSLength() const
165 return static_cast<sal_Int32>(nValue + (nValue < 0. ? -.5 : .5 ));
168 /** Parser of a style element/option
170 * This class parses the content of a style element or a style option and preprocesses it.
172 * The result of the parser is forwarded to derived parsers by the methods SelectorParsed()
173 * and DeclarationParsed(). Example:
174 * H1, H2 { font-weight: bold; text-align: right }
175 * | | | |
176 * | | | DeclP( 'text-align', 'right' )
177 * | | DeclP( 'font-weight', 'bold' )
178 * | SelP( 'H2', false )
179 * SelP( 'H1', true )
181 class CSS1Parser
183 bool m_bWhiteSpace : 1; // read a whitespace?
184 bool m_bEOF : 1; // is end of "file"?
186 sal_Unicode m_cNextCh; // next character
188 sal_Int32 m_nInPos; // current position in the input string
190 sal_uInt32 m_nlLineNr; // current row number
191 sal_uInt32 m_nlLinePos; // current column number
193 double m_nValue; // value of the token as number
195 CSS1ParserState m_eState; // current state of the parser
196 CSS1Token m_nToken; // the current token
198 OUString m_aIn; // the string to parse
199 OUString m_aToken; // token as string
201 /// prepare parsing
202 void InitRead( const OUString& rIn );
204 /// @returns the next character to parse
205 sal_Unicode GetNextChar();
207 /// @returns the next token to parse
208 CSS1Token GetNextToken();
210 /// Is the parser still working?
211 bool IsParserWorking() const { return CSS1_PAR_WORKING == m_eState; }
213 bool IsEOF() const { return m_bEOF; }
215 // parse parts of the grammar
216 void ParseRule();
217 std::unique_ptr<CSS1Selector> ParseSelector();
218 std::unique_ptr<CSS1Expression> ParseDeclaration( OUString& rProperty );
220 protected:
221 void ParseStyleSheet();
223 /** parse the content of a HTML style element
225 * For each selector and each declaration the methods SelectorParsed()
226 * or DeclarationParsed() need to be called afterwards
228 * @param rIn the style element as string
230 void ParseStyleSheet( const OUString& rIn );
232 /** parse the content of a HTML style option
234 * For each selector and each declaration the methods SelectorParsed()
235 * or DeclarationParsed() need to be called afterwards.
237 * @param rIn the style option as string
238 * @return true if ???
240 void ParseStyleOption( const OUString& rIn );
242 /** Called after a selector was parsed.
244 * @param pSelector The selector that was parsed
245 * @param bFirst if true, a new declaration starts with this selector
247 virtual void SelectorParsed( std::unique_ptr<CSS1Selector> pSelector, bool bFirst );
249 /** Called after a declaration or property was parsed
251 * @param rProperty The declaration/property
252 * @param pExpr ???
254 virtual void DeclarationParsed( const OUString& rProperty,
255 std::unique_ptr<CSS1Expression> pExpr );
257 public:
258 CSS1Parser();
259 virtual ~CSS1Parser();
262 #endif
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */