1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 // tokens of the CSS1 parser
38 CSS1_LENGTH
, // absolute length in 1/100 MM
39 CSS1_PIXLENGTH
, // length in pixels
57 CSS1_PAGE_SYM
, // Feature: PrintExt
67 CSS1_PAR_ACCEPTED
= 0,
74 CSS1_SELTYPE_ELEM_CLASS
,
78 CSS1_SELTYPE_PAGE
// Feature: PrintExt
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)
90 * These simple selectors are chained in a list to complete selectors
94 CSS1SelectorType m_eType
; // the type
95 OUString m_aSelector
; // the selector itself
96 CSS1Selector
*m_pNext
; // the following component
99 CSS1Selector( CSS1SelectorType eTyp
, OUString aSel
)
100 : m_eType(eTyp
), m_aSelector(std::move( aSel
)), m_pNext( nullptr )
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
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
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
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)
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
,
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 }
176 * | | | DeclP( 'text-align', 'right' )
177 * | | DeclP( 'font-weight', 'bold' )
178 * | SelP( 'H2', false )
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
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
217 std::unique_ptr
<CSS1Selector
> ParseSelector();
218 std::unique_ptr
<CSS1Expression
> ParseDeclaration( OUString
& rProperty
);
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
254 virtual void DeclarationParsed( const OUString
& rProperty
,
255 std::unique_ptr
<CSS1Expression
> pExpr
);
259 virtual ~CSS1Parser();
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */