├── ChangeLog.txt ├── LICENSE ├── Makefile ├── Makefile.win32 ├── README.md └── cssparser ├── CSSParser.cpp ├── CSSParser.h ├── CSSProperties.cpp ├── CSSProperties.h ├── CSSUtils.cpp ├── CSSUtils.h └── main.cpp /ChangeLog.txt: -------------------------------------------------------------------------------- 1 | 2 | Version 1.0 3 | ----------- 4 | - Extracted the css parser portions of the code from CSSTidy and reowrked it 5 | into a standalone CSS Parser in C++ 6 | 7 | - Added support for nesting of @media queries and proper pretty printing 8 | 9 | - Created a token based interface and serializer 10 | 11 | - Added support for @supports 12 | 13 | - Added support for simple validation 14 | 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | 3 | Version 2.1, February 1999 4 | 5 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 6 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 7 | Everyone is permitted to copy and distribute verbatim copies 8 | of this license document, but changing it is not allowed. 9 | 10 | [This is the first released version of the Lesser GPL. It also counts 11 | as the successor of the GNU Library Public License, version 2, hence 12 | the version number 2.1.] 13 | 14 | Preamble 15 | 16 | The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. 17 | 18 | This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. 19 | 20 | When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. 21 | 22 | To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. 23 | 24 | For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. 25 | 26 | We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. 27 | 28 | To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. 29 | 30 | Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. 31 | 32 | Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. 33 | 34 | When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. 35 | 36 | We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. 37 | 38 | For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. 39 | 40 | In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. 41 | 42 | Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. 43 | 44 | The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. 45 | 46 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 47 | 48 | 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". 49 | 50 | A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. 51 | 52 | The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) 53 | 54 | "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. 55 | 56 | Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 57 | 58 | 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. 59 | 60 | You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 61 | 62 | 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: 63 | 64 | * a) The modified work must itself be a software library. 65 | * b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. 66 | * c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. 67 | * d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. 68 | 69 | (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) 70 | 71 | These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. 72 | 73 | Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. 74 | 75 | In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 76 | 77 | 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. 78 | 79 | Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. 80 | 81 | This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 82 | 83 | 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. 84 | 85 | If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 86 | 87 | 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. 88 | 89 | However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. 90 | 91 | When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. 92 | 93 | If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) 94 | 95 | Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 96 | 97 | 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. 98 | 99 | You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: 100 | 101 | * a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) 102 | * b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. 103 | * c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. 104 | * d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. 105 | * e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. 106 | 107 | For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. 108 | 109 | It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 110 | 111 | 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: 112 | 113 | * a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. 114 | * b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 115 | 116 | 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 117 | 118 | 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 119 | 120 | 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 121 | 122 | 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. 123 | 124 | If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. 125 | 126 | It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. 127 | 128 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 129 | 130 | 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 131 | 132 | 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 133 | 134 | Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 135 | 136 | 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. 137 | 138 | NO WARRANTY 139 | 140 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 141 | 142 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 143 | 144 | END OF TERMS AND CONDITIONS 145 | How to Apply These Terms to Your New Libraries 146 | 147 | If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). 148 | 149 | To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. 150 | 151 | one line to give the library's name and an idea of what it does. 152 | Copyright (C) year name of author 153 | 154 | This library is free software; you can redistribute it and/or 155 | modify it under the terms of the GNU Lesser General Public 156 | License as published by the Free Software Foundation; either 157 | version 2.1 of the License, or (at your option) any later version. 158 | 159 | This library is distributed in the hope that it will be useful, 160 | but WITHOUT ANY WARRANTY; without even the implied warranty of 161 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 162 | Lesser General Public License for more details. 163 | 164 | You should have received a copy of the GNU Lesser General Public 165 | License along with this library; if not, write to the Free Software 166 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 167 | 168 | Also add information on how to contact you by electronic and paper mail. 169 | 170 | You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: 171 | 172 | Yoyodyne, Inc., hereby disclaims all copyright interest in 173 | the library `Frob' (a library for tweaking knobs) written 174 | by James Random Hacker. 175 | 176 | signature of Ty Coon, 1 April 1990 177 | Ty Coon, President of Vice 178 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | name=cssparser 2 | WORKINGDIR=$(shell pwd) 3 | DIRNAME=$(shell basename `pwd`) 4 | HOSTCC=gcc 5 | HOSTCXX=g++ 6 | OPTFLAGS=-O2 -fomit-frame-pointer 7 | HOSTCFLAGS=$(OPTFLAGS) 8 | HOSTLD=ld 9 | HOSTLDFLAGS= 10 | CFLAGS=$(HOSTCFLAGS) -funsigned-char 11 | CC=$(HOSTCC) 12 | EXTRA_CFLAGS= 13 | LD=$(HOSTLD) 14 | LDFLAGS=$(HOSTLDFLAGS) 15 | STRIP_ARGS=-s -R .note -R .comment 16 | STRIP=strip $(STRIP_ARGS) 17 | RM=/bin/rm 18 | CP=/bin/cp -PpR 19 | MV=/bin/mv 20 | LN=/bin/ln 21 | 22 | RELEASE_DIR=release 23 | 24 | CSSPARSEROBJS= \ 25 | cssparser/CSSUtils.o \ 26 | cssparser/CSSParser.o \ 27 | cssparser/CSSProperties.o \ 28 | cssparser/main.o 29 | 30 | .cpp.o: 31 | $(CXX) -o $(RELEASE_DIR)/$@ -c $(CFLAGS) $< 32 | 33 | .PHONY: clean cssparser 34 | 35 | all: releasedir $(CSSPARSEROBJS) cssparser 36 | 37 | cssparser: 38 | cd $(RELEASE_DIR); $(CXX) $(LDCFLAGS) -o $(name)/$(name) $(CSSPARSEROBJS) 39 | 40 | releasedir: 41 | mkdir -p $(RELEASE_DIR)/$(name) 42 | 43 | clean:: 44 | $(RM) -rf $(RELEASE_DIR) 45 | 46 | -------------------------------------------------------------------------------- /Makefile.win32: -------------------------------------------------------------------------------- 1 | name=cssparser 2 | EXECUTABLE_NAME=$(name).exe 3 | INTERMEDIATE_DIR=.\intermediate 4 | RELEASE_DIR=.\release 5 | 6 | SRC_FILES = \ 7 | $(name)\main.cpp \ 8 | $(name)\CSSParser.cpp \ 9 | $(name)\CSSProperties.cpp \ 10 | $(name)\CSSUtils.cpp 11 | 12 | all: clean create_dirs $(name) 13 | 14 | $(EXECUTABLE_NAME) : $(SRC_FILES) 15 | cl /EHsc /Fe$(RELEASE_DIR)\$(name)\$(EXECUTABLE_NAME) /I$(name) $(SRC_FILES) 16 | copy *.obj $(INTERMEDIATE_DIR) 17 | del *.obj 18 | 19 | $(name): $(EXECUTABLE_NAME) 20 | 21 | create_dirs: 22 | @if not exist $(RELEASE_DIR)\$(name) mkdir $(RELEASE_DIR)\$(name) 23 | @if not exist $(INTERMEDIATE_DIR) mkdir $(INTERMEDIATE_DIR) 24 | 25 | clean:: 26 | @if exist $(RELEASE_DIR) rmdir /S /Q $(RELEASE_DIR) 27 | @if exist $(INTERMEDIATE_DIR) rmdir /S /Q $(INTERMEDIATE_DIR) 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CSSParser - A Simple C++ CSS Parser =================================== 2 | 3 | CSSParser is a free, open source, multi-platform simpole C++ based CSS Parser 4 | that only uses the std:: library. 5 | 6 | It's code was initially extracted from CSSTidy and then reworked. 7 | 8 | CSSTidy (https://github.com/csstidy-c/csstidy) 9 | 10 | CSSTidy Portions Copyright: 11 | * Florian Schmitz 12 | * Thierry Charbonnel 13 | * Will Mitchell 14 | * Brett Zamir 15 | * sined_ 16 | * Dmitry Leskov 17 | * Kevin Coyner 18 | * Tuukka Pasanen 19 | * Frank W. Bergmann 20 | * Frank Dana 21 | 22 | The CSSTidy codebase is itself is Available under the LGPL 2.1 23 | 24 | 25 | Interface 26 | ========= 27 | 28 | The best way to understand the parser interface is to examine main.cpp 29 | which is excerpted below: 30 | 31 | [CODE SNIPPETS] 32 | 33 | CSSParser csst; 34 | 35 | css_file = CSSUtils::file_get_contents(argv[1]); 36 | 37 | // valid css levels are "CSS1.0", "CSS2.0", "CSS2.1", "CSS3.0" 38 | csst.set_level("CSS3.0"); 39 | 40 | // do the actual parsing 41 | csst.parse_css(css_file); 42 | 43 | // check for any parse errors 44 | std::vector errors = csst.get_parse_errors(); 45 | 46 | // check for any parse warnings 47 | std::vector warnings = csst.get_parse_warnings(); 48 | 49 | // check for any parse information messages 50 | std::vector infos = csst.get_parse_info(); 51 | 52 | // get any @charset without having to walk the csstokens list 53 | std::string cset = csst.get_charset(); 54 | 55 | // get all @import without having to walk the csstokens list 56 | std::vector imports = csst.get_import(); 57 | 58 | // get any @namespace without having to walk the csstokens list 59 | std::string ns = csst.get_namespace(); 60 | 61 | // enum token_type 62 | // { 63 | // CHARSET, IMPORT, NAMESP, AT_START, AT_END, SEL_START, SEL_END, PROPERTY, VALUE, COMMENT, CSS_END 64 | // }; 65 | 66 | // struct token 67 | // { 68 | // token_type type; 69 | // int pos; // position in input string 70 | // int line; // line number in input string 71 | // std::string data; 72 | // }; 73 | 74 | // now walk the sequence of parsed tokens 75 | // if you know the location of the token you want in the sequence (starting with 0) 76 | // simply pass start_ptr in get_next_token set to a valid starting point in the token sequence 77 | 78 | CSSParser::token atoken = csst.get_next_token(); 79 | 80 | while(atoken.type != CSSParser::CSS_END) { 81 | 82 | std::string ttype = csst.get_type_name(atoken.type); 83 | 84 | std::cout << "Pos: " << atoken.pos 85 | << " Line: " << atoken.line 86 | << " Type: " << ttype 87 | <<" Data: " << atoken.data << std::endl; 88 | 89 | atoken = csst.get_next_token(); 90 | } 91 | 92 | // serialize back to CSS 93 | std::string cssout; 94 | cssout = csst.serialize_css(); 95 | 96 | [/CODE SNIPPETS] 97 | 98 | 99 | Build and Install 100 | ================= 101 | 102 | There is a very simplistic Linux/MacOS Makefile and a Windows Makefile using nmake 103 | 104 | 105 | License 106 | ======= 107 | 108 | Like the CSSTidy codebase it was derived from, CSSParser is licensed under 109 | the LGPLv2.1 or later. The complete license is located in LICENSE. 110 | -------------------------------------------------------------------------------- /cssparser/CSSParser.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | ** 3 | ** CSSParser 4 | ** A Simple C++ CSS Parser 5 | ** 6 | ** Copyright (C) 2021 Kevin B. Hendricks, Stratford, Ontario, Canada 7 | ** 8 | ** License: LGPL v2.1 or later 9 | ** 10 | ** Extracted and modified from: 11 | ** CSSTidy (https://github.com/csstidy-c/csstidy) 12 | ** 13 | ** CSSTidy Portions Copyright: 14 | ** Florian Schmitz 15 | ** Thierry Charbonnel 16 | ** Will Mitchell 17 | ** Brett Zamir 18 | ** sined_ 19 | ** Dmitry Leskov 20 | ** Kevin Coyner 21 | ** Tuukka Pasanen 22 | ** Frank W. Bergmann 23 | ** Frank Dana 24 | ** 25 | ** CSSTidy itself is Available under the LGPL 2.1 26 | ** 27 | ** You should have received a copy of the GNU Lesser General Public License 2.1 28 | ** along with this program. If not, see . 29 | ** 30 | *************************************************************************/ 31 | 32 | #include "CSSProperties.h" 33 | #include "CSSParser.h" 34 | 35 | /* PIS = in selector 36 | * PIP = in property 37 | * PIV = in value 38 | * PINSTR = in string (-> ",',( => ignore } and ; etc.) 39 | * PIC = in comment (ignore everything) 40 | * PAT = in @-block 41 | */ 42 | 43 | CSSParser::CSSParser() 44 | { 45 | tokens = "{};:()@='\"/,\\!$%&*+.<>?[]^`|~"; 46 | 47 | // Used for serializing parsed css 48 | csstemplate.push_back(" "); // 0 - standard indentation 49 | csstemplate.push_back(" {\n"); // 1 - bracket after @-rule 50 | csstemplate.push_back(""); // 2 - unused 51 | csstemplate.push_back(" {\n"); // 3 - bracket after selector was "\n{\n" 52 | csstemplate.push_back(""); // 4 - unused 53 | csstemplate.push_back(""); // 5 - string after property before value 54 | csstemplate.push_back(";\n"); // 6 - string after value 55 | csstemplate.push_back("}"); // 7 - closing bracket - selector 56 | csstemplate.push_back("\n\n"); // 8 - space between blocks {...} 57 | csstemplate.push_back("}\n\n"); // 9 - closing bracket @-rule 58 | csstemplate.push_back(""); // 10 - unused 59 | csstemplate.push_back(""); // 11 - before comment 60 | csstemplate.push_back("\n"); // 12 - after comment 61 | csstemplate.push_back("\n"); // 13 - after last line @-rule 62 | 63 | // at_rule to parser state map 64 | at_rules["page"] = PIS; 65 | at_rules["font-face"] = PIS; 66 | at_rules["charset"] = PIV; 67 | at_rules["import"] = PIV; 68 | at_rules["namespace"] = PIV; 69 | at_rules["media"] = PAT; 70 | at_rules["keyframes"] = PAT; 71 | at_rules["supports"] = PAT; 72 | at_rules["-moz-keyframes"] = PAT; 73 | at_rules["-ms-keyframes"] = PAT; 74 | at_rules["-o-keyframes"] = PAT; 75 | at_rules["-webkit-keyframes"] = PAT; 76 | 77 | // descriptive names for each token type 78 | token_type_names.push_back("CHARSET"); 79 | token_type_names.push_back("IMPORT"); 80 | token_type_names.push_back("NAMESP"); 81 | token_type_names.push_back("AT_START"); 82 | token_type_names.push_back("AT_END"); 83 | token_type_names.push_back("SEL_START"); 84 | token_type_names.push_back("SEL_END"); 85 | token_type_names.push_back("PROPERTY"); 86 | token_type_names.push_back("VALUE"); 87 | token_type_names.push_back("COMMENT"); 88 | token_type_names.push_back("CSS_END"); 89 | 90 | css_level = "CSS3.0"; 91 | } 92 | 93 | 94 | void CSSParser::set_level(std::string level) 95 | { 96 | if ((level == "CSS1.0") || (level == "CSS2.0") || 97 | (level == "CSS2.1") || (level == "CSS3.0")) 98 | { 99 | css_level = level; 100 | } 101 | } 102 | 103 | 104 | void CSSParser::reset_parser() 105 | { 106 | token_ptr = 0; 107 | charset = ""; 108 | namesp = ""; 109 | selector_nest_level = 0; 110 | line = 1; 111 | import.clear(); 112 | csstokens.clear(); 113 | cur_selector.clear(); 114 | cur_at.clear(); 115 | cur_property.clear(); 116 | cur_function.clear(); 117 | cur_sub_value.clear(); 118 | cur_value.clear(); 119 | cur_string.clear(); 120 | cur_selector.clear(); 121 | sel_separate.clear(); 122 | } 123 | 124 | 125 | std::string CSSParser::get_charset() 126 | { 127 | return charset; 128 | } 129 | 130 | std::string CSSParser::get_namespace() 131 | { 132 | return namesp; 133 | } 134 | 135 | std::vector CSSParser::get_import() 136 | { 137 | return import; 138 | } 139 | 140 | 141 | CSSParser::token CSSParser::get_next_token(int start_ptr) 142 | { 143 | if ((start_ptr >= 0) && (start_ptr < csstokens.size())) 144 | { 145 | token_ptr = start_ptr; 146 | } 147 | 148 | token atoken; 149 | atoken.type = CSS_END; 150 | atoken.data = ""; 151 | 152 | if (token_ptr < csstokens.size()) 153 | { 154 | atoken = csstokens[token_ptr]; 155 | token_ptr++; 156 | } 157 | return atoken; 158 | } 159 | 160 | 161 | std::string CSSParser::get_type_name(CSSParser::token_type t) 162 | { 163 | return token_type_names[t]; 164 | } 165 | 166 | 167 | void CSSParser::add_token(const token_type ttype, const std::string data, const bool force) 168 | { 169 | token temp; 170 | temp.type = ttype; 171 | temp.pos = spos; 172 | temp.line = sline; 173 | temp.data = (ttype == COMMENT) ? data : CSSUtils::trim(data); 174 | csstokens.push_back(temp); 175 | if (ttype == SEL_START) selector_nest_level++; 176 | if (ttype == SEL_END) selector_nest_level--; 177 | } 178 | 179 | 180 | void CSSParser::log(const std::string msg, const message_type type, int iline) 181 | { 182 | message new_msg; 183 | new_msg.m = msg; 184 | new_msg.t = type; 185 | if(iline == 0) 186 | { 187 | iline = line; 188 | } 189 | if(logs.count(line) > 0) 190 | { 191 | for(int i = 0; i < logs[line].size(); ++i) 192 | { 193 | if(logs[line][i].m == new_msg.m && logs[line][i].t == new_msg.t) 194 | { 195 | return; 196 | } 197 | } 198 | } 199 | logs[line].push_back(new_msg); 200 | } 201 | 202 | std::string CSSParser::unicode(std::string& istring, int& i) 203 | { 204 | ++i; 205 | std::string add = ""; 206 | bool replaced = false; 207 | 208 | while(i < istring.length() && (CSSUtils::ctype_xdigit(istring[i]) || 209 | CSSUtils::ctype_space(istring[i])) && add.length()< 6) 210 | { 211 | add += istring[i]; 212 | 213 | if(CSSUtils::ctype_space(istring[i])) { 214 | break; 215 | } 216 | i++; 217 | } 218 | 219 | if ((CSSUtils::hexdec(add) > 47 && CSSUtils::hexdec(add) < 58) || 220 | (CSSUtils::hexdec(add) > 64 && CSSUtils::hexdec(add) < 91) || 221 | (CSSUtils::hexdec(add) > 96 && CSSUtils::hexdec(add) < 123)) 222 | { 223 | std::string msg = "Replaced unicode notation: Changed \\" + CSSUtils::rtrim(add) + " to "; 224 | add = static_cast(CSSUtils::hexdec(add)); 225 | msg += add; 226 | log(msg,Information); 227 | replaced = true; 228 | } else { 229 | add = CSSUtils::trim("\\" + add); 230 | } 231 | 232 | if ((CSSUtils::ctype_xdigit(istring[i+1]) && CSSUtils::ctype_space(istring[i]) 233 | && !replaced) || !CSSUtils::ctype_space(istring[i])) 234 | { 235 | i--; 236 | } 237 | 238 | if (1) { 239 | return add; 240 | } 241 | return ""; 242 | } 243 | 244 | 245 | bool CSSParser::is_token(std::string& istring, const int i) 246 | { 247 | return (CSSUtils::in_str_array(tokens, istring[i]) && !CSSUtils::escaped(istring,i)); 248 | } 249 | 250 | 251 | void CSSParser::explode_selectors() 252 | { 253 | sel_separate = std::vector(); 254 | } 255 | 256 | 257 | int CSSParser::_seeknocomment(const int key, int move) 258 | { 259 | int go = (move > 0) ? 1 : -1; 260 | for (int i = key + 1; abs(key-i)-1 < abs(move); i += go) 261 | { 262 | if (i < 0 || i >= csstokens.size()) { 263 | return -1; 264 | } 265 | if (csstokens[i].type == COMMENT) { 266 | move += 1; 267 | continue; 268 | } 269 | return csstokens[i].type; 270 | } 271 | // FIXME: control can reach end of non-void function 272 | return -1; 273 | } 274 | 275 | 276 | std::string CSSParser::serialize_css(std::string filename, bool tostdout) 277 | { 278 | if(charset == "" && namesp == "" && import.empty() && csstokens.empty()) 279 | { 280 | std::cout << "Warning: empty CSS output!" << std::endl; 281 | } 282 | 283 | std::ofstream file_output; 284 | if(filename != "") 285 | { 286 | file_output.open(filename.c_str(),std::ios::binary); 287 | if(file_output.bad()) 288 | { 289 | std::cout << "Error when trying to save the output file!" << std::endl; 290 | return ""; 291 | } 292 | } 293 | 294 | std::stringstream output; 295 | int lvl = 0; 296 | std::string indent = ""; 297 | 298 | for (int i = 0; i < csstokens.size(); ++i) 299 | { 300 | switch (csstokens[i].type) 301 | { 302 | case CHARSET: 303 | output << "@charset " << csstemplate[5] << csstokens[i].data << csstemplate[6]; 304 | break; 305 | 306 | case IMPORT: 307 | indent = CSSUtils::indent(lvl, csstemplate[0]); 308 | output << indent << "@import " << csstemplate[5] << csstokens[i].data << csstemplate[6]; 309 | break; 310 | 311 | case NAMESP: 312 | output << "@namespace " << csstemplate[5] << csstokens[i].data << csstemplate[6]; 313 | break; 314 | 315 | case AT_START: 316 | indent = CSSUtils::indent(lvl, csstemplate[0]); 317 | output << indent << csstokens[i].data << csstemplate[1]; 318 | lvl++; 319 | break; 320 | 321 | case SEL_START: 322 | indent = CSSUtils::indent(lvl, csstemplate[0]); 323 | output << indent << csstokens[i].data << csstemplate[3]; 324 | lvl++; 325 | break; 326 | 327 | case PROPERTY: 328 | indent = CSSUtils::indent(lvl, csstemplate[0]); 329 | output << indent << csstokens[i].data << ":" << csstemplate[5]; 330 | break; 331 | 332 | case VALUE: 333 | output << csstokens[i].data << csstemplate[6]; 334 | break; 335 | 336 | case SEL_END: 337 | lvl--; if (lvl < 0) lvl = 0; 338 | indent = CSSUtils::indent(lvl, csstemplate[0]); 339 | output << indent + csstemplate[7]; 340 | if(_seeknocomment(i, 1) != AT_END) output << csstemplate[8]; 341 | break; 342 | 343 | case AT_END: 344 | lvl--; if (lvl < 0) lvl = 0; 345 | indent = CSSUtils::indent(lvl, csstemplate[0]); 346 | output << csstemplate[13] << indent << csstemplate[9]; 347 | break; 348 | 349 | case COMMENT: 350 | output << csstemplate[11] << "/*" << csstokens[i].data << "*/" << csstemplate[12]; 351 | break; 352 | 353 | case CSS_END: 354 | break; 355 | } 356 | } 357 | 358 | std::string output_string = CSSUtils::trim(output.str()); 359 | 360 | if(tostdout) 361 | { 362 | std::cout << output_string << "\n"; 363 | } 364 | if(filename != "") 365 | { 366 | file_output << output_string; 367 | file_output.close(); 368 | } 369 | return output_string; 370 | } 371 | 372 | 373 | void CSSParser::parseInAtBlock(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom) 374 | { 375 | if(is_token(css_input,i)) 376 | { 377 | if(css_input[i] == '/' && CSSUtils::s_at(css_input,i+1) == '*') 378 | { 379 | astatus = PIC; i += 2; 380 | afrom = PAT; 381 | } 382 | else if(css_input[i] == '{') 383 | { 384 | astatus = PIS; 385 | add_token(AT_START, cur_at); 386 | } 387 | else if(css_input[i] == ',') 388 | { 389 | cur_at = CSSUtils::trim(cur_at) + ","; 390 | } 391 | else if(css_input[i] == '\\') 392 | { 393 | cur_at += unicode(css_input,i); 394 | } 395 | else /*if((css_input[i] == '(') || (css_input[i] == ':') || (css_input[i] == ')') || (css_input[i] == '.'))*/ 396 | { 397 | if(!CSSUtils::in_char_arr("():/.", css_input[i])) 398 | { 399 | // Strictly speaking, these are only permitted in @media rules 400 | log("Unexpected symbol '" + std::string(css_input, i, 1) + "' in @-rule", Warning); 401 | } 402 | cur_at += css_input[i]; /* append tokens after media selector */ 403 | } 404 | } 405 | else 406 | { 407 | // Skip excess whitespace 408 | int lastpos = cur_at.length()-1; 409 | if(lastpos == -1 || !( (CSSUtils::ctype_space(cur_at[lastpos]) || 410 | (is_token(cur_at,lastpos) && cur_at[lastpos] == ',')) && 411 | CSSUtils::ctype_space(css_input[i]))) 412 | { 413 | cur_at += css_input[i]; 414 | } 415 | } 416 | } 417 | 418 | 419 | void CSSParser::parseInSelector(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 420 | bool& invalid_at, char& str_char, int str_size) 421 | { 422 | if(is_token(css_input,i)) 423 | { 424 | // if(css_input[i] == '/' && CSSUtils::s_at(css_input,i+1) == '*' && 425 | // trim(cur_selector) == "") selector as dep doesn't make any sense here, huh? 426 | if(css_input[i] == '/' && CSSUtils::s_at(css_input,i+1) == '*') 427 | { 428 | astatus = PIC; ++i; 429 | afrom = PIS; 430 | } 431 | else if(css_input[i] == '@' && CSSUtils::trim(cur_selector) == "") 432 | { 433 | // Check for at-rule 434 | invalid_at = true; 435 | for(std::map::iterator j = at_rules.begin(); j != at_rules.end(); ++j ) 436 | { 437 | if(CSSUtils::strtolower(css_input.substr(i+1,j->first.length())) == j->first) 438 | { 439 | (j->second == PAT) ? cur_at = "@" + j->first : cur_selector = "@" + j->first; 440 | astatus = j->second; 441 | i += j->first.length(); 442 | invalid_at = false; 443 | } 444 | } 445 | if (invalid_at) 446 | { 447 | cur_selector = "@"; 448 | std::string invalid_at_name = ""; 449 | for(int j = i+1; j < str_size; ++j) 450 | { 451 | if(!CSSUtils::ctype_alpha(css_input[j])) 452 | { 453 | return; 454 | } 455 | invalid_at_name += css_input[j]; 456 | } 457 | log("Invalid @-rule: " + invalid_at_name + " (removed)",Warning); 458 | } 459 | } 460 | else if(css_input[i] == '"' || css_input[i] == '\'') 461 | { 462 | cur_string = css_input[i]; 463 | astatus = PINSTR; 464 | str_char = css_input[i]; 465 | afrom = PIS; 466 | } 467 | else if(invalid_at && css_input[i] == ';') 468 | { 469 | invalid_at = false; 470 | astatus = PIS; 471 | } 472 | else if (css_input[i] == '{') 473 | { 474 | astatus = PIP; 475 | add_token(SEL_START, cur_selector); 476 | } 477 | else if (css_input[i] == '}') 478 | { 479 | add_token(AT_END, cur_at); 480 | cur_at = ""; 481 | cur_selector = ""; 482 | sel_separate = std::vector(); 483 | } 484 | else if(css_input[i] == ',') 485 | { 486 | cur_selector = CSSUtils::trim(cur_selector) + ","; 487 | sel_separate.push_back(cur_selector.length()); 488 | } 489 | else if (css_input[i] == '\\') 490 | { 491 | cur_selector += unicode(css_input,i); 492 | } 493 | // remove unnecessary universal selector, FS#147 494 | else if(!(css_input[i] == '*' && (CSSUtils::s_at(css_input,i+1) == '.' || CSSUtils::s_at(css_input,i+1) == '[' || CSSUtils::s_at(css_input,i+1) == ':' || CSSUtils::s_at(css_input,i+1) == '#'))) 495 | { 496 | cur_selector += css_input[i]; 497 | } 498 | } 499 | else 500 | { 501 | int lastpos = cur_selector.length()-1; 502 | if((lastpos == -1) || !( (CSSUtils::ctype_space(cur_selector[lastpos]) || 503 | (is_token(cur_selector,lastpos) && cur_selector[lastpos] == ',')) && 504 | CSSUtils::ctype_space(css_input[i]))) 505 | { 506 | cur_selector += css_input[i]; 507 | } 508 | } 509 | } 510 | 511 | 512 | void CSSParser::parseInProperty(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 513 | bool& invalid_at) 514 | { 515 | 516 | if (is_token(css_input,i)) 517 | { 518 | if (css_input[i] == ':' || (css_input[i] == '=' && cur_property != "")) 519 | { 520 | astatus = PIV; 521 | bool valid = true || (CSSProperties::instance()->contains(cur_property) && 522 | CSSProperties::instance()->levels(cur_property).find(css_level,0) != std::string::npos); 523 | if(valid) { 524 | add_token(PROPERTY, cur_property); 525 | } 526 | } 527 | else if(css_input[i] == '/' && CSSUtils::s_at(css_input,i+1) == '*' && cur_property == "") 528 | { 529 | astatus = PIC; ++i; 530 | afrom = PIP; 531 | } 532 | else if(css_input[i] == '}') 533 | { 534 | explode_selectors(); 535 | astatus = PIS; 536 | invalid_at = false; 537 | add_token(SEL_END, cur_selector); 538 | cur_selector = ""; 539 | cur_property = ""; 540 | } 541 | else if(css_input[i] == ';') 542 | { 543 | cur_property = ""; 544 | } 545 | else if(css_input[i] == '\\') 546 | { 547 | cur_property += unicode(css_input,i); 548 | } 549 | else if(css_input[i] == '*') 550 | { 551 | // IE7 and below recognize properties that begin with '*' 552 | if (cur_property == "") 553 | { 554 | cur_property += css_input[i]; 555 | log("IE7- hack detected: property name begins with '*'", Warning); 556 | } 557 | } 558 | else 559 | { 560 | log("Unexpected character '" + std::string(1, css_input[i]) + "'in property name", Error); 561 | } 562 | } 563 | else if(!CSSUtils::ctype_space(css_input[i])) 564 | { 565 | if(css_input[i] == '_' && cur_property == "") 566 | { 567 | // IE6 and below recognize properties that begin with '_' 568 | log("IE6 hack detected: property name begins with '_'", Warning); 569 | } 570 | // TODO: Check for invalid characters 571 | cur_property += css_input[i]; 572 | } 573 | // TODO: Check for whitespace inside property names 574 | } 575 | 576 | 577 | void CSSParser::parseInValue(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 578 | bool& invalid_at, char& str_char, bool& pn, int str_size) 579 | { 580 | pn = (((css_input[i] == '\n' || css_input[i] == '\r') && property_is_next(css_input,i+1)) || i == str_size-1); 581 | if(pn) 582 | { 583 | log("Added semicolon to the end of declaration",Warning); 584 | } 585 | if(is_token(css_input,i) || pn) 586 | { 587 | if((css_input[i] == '{') && (cur_selector == "@import" || 588 | cur_selector == "@charset" || 589 | cur_selector == "@namespace")) 590 | { 591 | log("Unexpected character '" + std::string(1, css_input[i]) + "' in " + cur_selector, Error); 592 | } 593 | if(css_input[i] == '/' && CSSUtils::s_at(css_input,i+1) == '*') 594 | { 595 | astatus = PIC; ++i; 596 | afrom = PIV; 597 | } 598 | else if(css_input[i] == '"' || css_input[i] == '\'' || 599 | (css_input[i] == '(' && cur_sub_value == "url") ) 600 | { 601 | str_char = (css_input[i] == '(') ? ')' : css_input[i]; 602 | cur_string = css_input[i]; 603 | astatus = PINSTR; 604 | afrom = PIV; 605 | } 606 | else if(css_input[i] == '(') 607 | { 608 | // function call or an open parenthesis in a calc() expression 609 | // url() is a special case that should have been handled above 610 | assert(cur_sub_value != "url"); 611 | 612 | // cur_sub_value should contain the name of the function, if any 613 | cur_sub_value = CSSUtils::trim(cur_sub_value + "("); 614 | // set current function name and push it onto the stack 615 | cur_function = cur_sub_value; 616 | cur_function_arr.push_back(cur_sub_value); 617 | cur_sub_value_arr.push_back(cur_sub_value); 618 | cur_sub_value = ""; 619 | } 620 | else if(css_input[i] == '\\') 621 | { 622 | cur_sub_value += unicode(css_input,i); 623 | } 624 | else if(css_input[i] == ';' || pn) 625 | { 626 | if(cur_selector.substr(0,1) == "@" && 627 | at_rules.count(cur_selector.substr(1)) > 0 && 628 | at_rules[cur_selector.substr(1)] == PIV) 629 | { 630 | cur_sub_value_arr.push_back(CSSUtils::trim(cur_sub_value)); 631 | astatus = PIS; 632 | 633 | if(cur_selector == "@charset") 634 | { 635 | charset = cur_sub_value_arr[0]; 636 | add_token(CHARSET, charset); 637 | } 638 | else if(cur_selector == "@import") 639 | { 640 | std::string aimport = CSSUtils::build_value(cur_sub_value_arr); 641 | add_token(IMPORT, aimport); 642 | import.push_back(aimport); 643 | } 644 | else if(cur_selector == "@namespace") 645 | { 646 | namesp = CSSUtils::implode(" ",cur_sub_value_arr); 647 | add_token(NAMESP, namesp); 648 | } 649 | cur_sub_value_arr.clear(); 650 | cur_sub_value = ""; 651 | cur_selector = ""; 652 | sel_separate = std::vector(); 653 | } 654 | else 655 | { 656 | astatus = PIP; 657 | } 658 | } 659 | else if (css_input[i] == '!') 660 | { 661 | cur_sub_value_arr.push_back(CSSUtils::trim(cur_sub_value)); 662 | cur_sub_value = "!"; 663 | } 664 | else if (css_input[i] == ',' || css_input[i] == ')') 665 | { 666 | // store the current subvalue, if any 667 | cur_sub_value = CSSUtils::trim(cur_sub_value); 668 | if(cur_sub_value != "") 669 | { 670 | cur_sub_value_arr.push_back(cur_sub_value); 671 | cur_sub_value = ""; 672 | } 673 | bool drop = false; 674 | if (css_input[i] == ')') 675 | { 676 | if (cur_function_arr.empty()) 677 | { 678 | // No matching open parenthesis, drop this closing one 679 | log("Unexpected closing parenthesis, dropping", Warning); 680 | drop = true; 681 | } 682 | else 683 | { 684 | // Pop function from the stack 685 | cur_function_arr.pop_back(); 686 | cur_function = cur_function_arr.empty() ? "" : cur_function_arr.back(); 687 | 688 | } 689 | } 690 | if (!drop) { 691 | cur_sub_value_arr.push_back(std::string(1,css_input[i])); 692 | } 693 | } 694 | else if(css_input[i] != '}') 695 | { 696 | cur_sub_value += css_input[i]; 697 | } 698 | if( (css_input[i] == '}' || css_input[i] == ';' || pn) && !cur_selector.empty()) 699 | { 700 | // End of value: normalize, optimize and store property 701 | if(cur_at == "") 702 | { 703 | cur_at = "standard"; 704 | } 705 | 706 | // Kill all whitespace 707 | cur_at = CSSUtils::trim(cur_at); 708 | cur_selector = CSSUtils::trim(cur_selector); 709 | cur_value = CSSUtils::trim(cur_value); 710 | cur_property = CSSUtils::trim(cur_property); 711 | cur_sub_value = CSSUtils::trim(cur_sub_value); 712 | 713 | cur_property = CSSUtils::strtolower(cur_property); 714 | 715 | if(cur_sub_value != "") 716 | { 717 | cur_sub_value_arr.push_back(cur_sub_value); 718 | cur_sub_value = ""; 719 | } 720 | 721 | // Check for leftover open parentheses 722 | if (!cur_function_arr.empty()) 723 | { 724 | std::vector::reverse_iterator rit; 725 | for (rit = cur_function_arr.rbegin(); rit != cur_function_arr.rend(); ++rit) 726 | { 727 | log("Closing parenthesis missing for '" + *rit + "', inserting", Warning); 728 | cur_sub_value_arr.push_back(")"); 729 | } 730 | } 731 | 732 | cur_value = CSSUtils::build_value(cur_sub_value_arr); 733 | 734 | 735 | bool valid = (CSSProperties::instance()->contains(cur_property) && 736 | CSSProperties::instance()->levels(cur_property).find(css_level,0) != std::string::npos); 737 | if(1) 738 | { 739 | //add(cur_at,cur_selector,cur_property,cur_value); 740 | add_token(VALUE, cur_value); 741 | } 742 | if(!valid) 743 | { 744 | log("Invalid property in " + CSSUtils::strtoupper(css_level) + ": " + cur_property,Warning); 745 | } 746 | 747 | //Split multiple selectors here if necessary 748 | cur_property = ""; 749 | cur_sub_value_arr.clear(); 750 | cur_value = ""; 751 | } 752 | if(css_input[i] == '}') 753 | { 754 | explode_selectors(); 755 | add_token(SEL_END, cur_selector); 756 | astatus = PIS; 757 | invalid_at = false; 758 | cur_selector = ""; 759 | } 760 | } 761 | else if(!pn) 762 | { 763 | cur_sub_value += css_input[i]; 764 | 765 | if(CSSUtils::ctype_space(css_input[i])) 766 | { 767 | if(CSSUtils::trim(cur_sub_value) != "") 768 | { 769 | cur_sub_value_arr.push_back(CSSUtils::trim(cur_sub_value)); 770 | } 771 | cur_sub_value = ""; 772 | } 773 | } 774 | } 775 | 776 | 777 | void CSSParser::parseInComment(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 778 | std::string& cur_comment) 779 | { 780 | 781 | if(css_input[i] == '*' && CSSUtils::s_at(css_input,i+1) == '/') 782 | { 783 | astatus = afrom; 784 | ++i; 785 | add_token(COMMENT, cur_comment); 786 | cur_comment = ""; 787 | } 788 | else 789 | { 790 | cur_comment += css_input[i]; 791 | } 792 | } 793 | 794 | 795 | void CSSParser::parseInString(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 796 | char& str_char, bool& str_in_str) 797 | { 798 | 799 | if(str_char == ')' && (css_input[i] == '"' || css_input[i] == '\'') && 800 | str_in_str == false && !CSSUtils::escaped(css_input,i)) 801 | { 802 | str_in_str = true; 803 | } 804 | else if(str_char == ')' && (css_input[i] == '"' || css_input[i] == '\'') && 805 | str_in_str == true && !CSSUtils::escaped(css_input,i)) 806 | { 807 | str_in_str = false; 808 | } 809 | std::string temp_add = ""; temp_add += css_input[i]; 810 | if( (css_input[i] == '\n' || css_input[i] == '\r') && 811 | !(css_input[i-1] == '\\' && !CSSUtils::escaped(css_input,i-1)) ) 812 | { 813 | temp_add = "\\A "; 814 | log("Fixed incorrect newline in string",Warning); 815 | } 816 | if (!(str_char == ')' && 817 | CSSUtils::char2str(css_input[i]).find_first_of(" \n\t\r\0xb") != std::string::npos && !str_in_str)) 818 | { 819 | cur_string += temp_add; 820 | } 821 | if(css_input[i] == str_char && !CSSUtils::escaped(css_input,i) && str_in_str == false) 822 | { 823 | astatus = afrom; 824 | if (cur_function == "" && 825 | cur_string.find_first_of(" \n\t\r\0xb") == std::string::npos && 826 | cur_property != "content" && cur_sub_value != "format") 827 | { 828 | // If the string is not inside a function call, contains no whitespace, 829 | // and the current property is not 'content', it may be safe to remove quotes. 830 | // TODO: Are there any properties other than 'content' where this is unsafe? 831 | // TODO: What if the string contains a comma or slash, and the property is a list or shorthand? 832 | if (str_char == '"' || str_char == '\'') 833 | { 834 | // If the string is in double or single quotes, remove them 835 | // FIXME: once url() is handled separately, this may always be the case. 836 | cur_string = cur_string.substr(1, cur_string.length() - 2); 837 | } else if (cur_string.length() > 3 && (cur_string[1] == '"' || cur_string[1] == '\'')) /* () */ 838 | { 839 | cur_string = cur_string[0] + cur_string.substr(2, cur_string.length() - 4) + cur_string[cur_string.length()-1]; 840 | } 841 | } 842 | if(afrom == PIV) 843 | { 844 | cur_sub_value += cur_string; 845 | } 846 | else if(afrom == PIS) 847 | { 848 | cur_selector += cur_string; 849 | } 850 | } 851 | } 852 | 853 | 854 | void CSSParser::parse_css(std::string css_input) 855 | { 856 | reset_parser(); 857 | css_input = CSSUtils::str_replace("\r\n","\n",css_input); // Replace newlines 858 | css_input += "\n"; 859 | parse_status astatus = PIS, afrom; 860 | parse_status old_status = PIS; 861 | record_position(PIS, PIS, css_input, 0, true); 862 | std::string cur_comment; 863 | 864 | cur_sub_value_arr.clear(); 865 | cur_function_arr.clear(); // Stack of nested function calls 866 | char str_char; 867 | bool str_in_str = false; 868 | bool invalid_at = false; 869 | bool pn = false; 870 | 871 | int str_size = css_input.length(); 872 | for(int i = 0; i < str_size; ++i) 873 | { 874 | if(css_input[i] == '\n' || css_input[i] == '\r') 875 | { 876 | ++line; 877 | } 878 | 879 | 880 | // record current position for selected state transitions 881 | if (old_status != astatus) 882 | { 883 | record_position(old_status, astatus, css_input, i); 884 | } 885 | old_status = astatus; 886 | 887 | 888 | switch(astatus) 889 | { 890 | /* Case in-at-block */ 891 | case PAT: 892 | parseInAtBlock(css_input, i, astatus, afrom); 893 | break; 894 | 895 | /* Case in-selector */ 896 | case PIS: 897 | parseInSelector(css_input, i, astatus, afrom, invalid_at, str_char, str_size); 898 | break; 899 | 900 | /* Case in-property */ 901 | case PIP: 902 | parseInProperty(css_input, i, astatus, afrom, invalid_at); 903 | break; 904 | 905 | /* Case in-value */ 906 | case PIV: 907 | parseInValue(css_input, i, astatus, afrom, invalid_at, str_char, pn, str_size); 908 | break; 909 | 910 | /* Case in-string */ 911 | case PINSTR: 912 | parseInString(css_input, i, astatus, afrom, str_char, str_in_str); 913 | break; 914 | 915 | /* Case in-comment */ 916 | case PIC: 917 | parseInComment(css_input, i, astatus, afrom, cur_comment); 918 | break; 919 | } 920 | } 921 | // validate that every selector start had its own selector end 922 | if (selector_nest_level != 0) 923 | { 924 | log("Unbalanced selector braces in style sheet", Error, line); 925 | } 926 | } 927 | 928 | 929 | bool CSSParser::property_is_next(std::string istring, int pos) 930 | { 931 | istring = istring.substr(pos,istring.length()-pos); 932 | pos = istring.find_first_of(':',0); 933 | if(pos == std::string::npos) 934 | { 935 | return false; 936 | } 937 | istring = CSSUtils::strtolower(CSSUtils::trim(istring.substr(0,pos))); 938 | return CSSProperties::instance()->contains(istring); 939 | } 940 | 941 | 942 | std::vector CSSParser::get_parse_errors() 943 | { 944 | return get_logs(CSSParser::Error); 945 | } 946 | 947 | 948 | std::vector CSSParser::get_parse_warnings() 949 | { 950 | return get_logs(CSSParser::Warning); 951 | } 952 | 953 | 954 | std::vector CSSParser::get_parse_info() 955 | { 956 | return get_logs(CSSParser::Information); 957 | } 958 | 959 | 960 | std::vector CSSParser::get_logs(CSSParser::message_type mt) 961 | { 962 | std::vector res; 963 | if(logs.size() > 0) 964 | { 965 | for(std::map >::iterator j = logs.begin(); j != logs.end(); j++ ) 966 | { 967 | for(int i = 0; i < j->second.size(); ++i) 968 | { 969 | if (j->second[i].t == mt) { 970 | res.push_back(std::to_string(j->first) + ": " + j->second[i].m); 971 | } 972 | } 973 | } 974 | } 975 | return res; 976 | } 977 | 978 | 979 | // allow a new set of csstokens to be loaded and then serialized 980 | void CSSParser::set_csstokens(const std::vector &ntokens) 981 | { 982 | csstokens.clear(); 983 | for(int i = 0; i < ntokens.size(); i++) 984 | { 985 | token atemp = ntokens[i]; 986 | csstokens.push_back(atemp); 987 | } 988 | } 989 | 990 | 991 | // update the position only for selected state transitions 992 | void CSSParser::record_position(parse_status old_status, parse_status new_status, 993 | std::string &css_input, int i, bool force) 994 | { 995 | // to reach here old_status must be != new_status 996 | bool record = false; 997 | 998 | // any state into a comment 999 | if (new_status == PIC) record = true; 1000 | 1001 | // start of a property 1002 | if ((old_status == PIS) && (new_status == PIP)) record = true; 1003 | if ((old_status == PIV) && (new_status == PIP)) record = true; 1004 | 1005 | // from properties to values or from @charset, @namespace, and @import into values 1006 | if ((old_status == PAT) && (new_status == PIV)) record = true; 1007 | if ((old_status == PIP) && (new_status == PIV)) record = true; 1008 | 1009 | // starting a new selector 1010 | if ((old_status == PAT) && (new_status == PIS)) record = true; 1011 | if ((old_status == PIV) && (new_status == PIS)) record = true; 1012 | if ((old_status == PIP) && (new_status == PIS)) record = true; 1013 | 1014 | if (record || force) { 1015 | spos = css_input.find_first_not_of(" \n\t\r\0xb", i); 1016 | sline = line; 1017 | for(int j = i+i; j <= spos; j++) 1018 | { 1019 | if (css_input[j] == '\n') sline++; 1020 | } 1021 | } 1022 | } 1023 | -------------------------------------------------------------------------------- /cssparser/CSSParser.h: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | ** 3 | ** CSSParser 4 | ** A Simple C++ CSS Parser 5 | ** 6 | ** Copyright (C) 2021 Kevin B. Hendricks, Stratford, Ontario, Canada 7 | ** 8 | ** License: LGPL v2.1 or later 9 | ** 10 | ** Extracted and modified from: 11 | ** CSSTidy (https://github.com/csstidy-c/csstidy) 12 | ** 13 | ** CSSTidy Portions Copyright: 14 | ** Florian Schmitz 15 | ** Thierry Charbonnel 16 | ** Will Mitchell 17 | ** Brett Zamir 18 | ** sined_ 19 | ** Dmitry Leskov 20 | ** Kevin Coyner 21 | ** Tuukka Pasanen 22 | ** Frank W. Bergmann 23 | ** Frank Dana 24 | ** 25 | ** CSSTidy itself is Available under the LGPL 2.1 26 | ** 27 | ** You should have received a copy of the GNU Lesser General Public License 2.1 28 | ** along with this program. If not, see . 29 | ** 30 | *************************************************************************/ 31 | 32 | #ifndef HEADER_CSSPARSER 33 | #define HEADER_CSSPARSER 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include "CSSProperties.h" 49 | #include "CSSUtils.h" 50 | 51 | class CSSParser 52 | { 53 | 54 | public: 55 | 56 | enum parse_status 57 | { 58 | PIS, PIP, PIV, PINSTR, PIC, PAT 59 | }; 60 | 61 | enum message_type 62 | { 63 | Information,Warning,Error 64 | }; 65 | 66 | enum token_type 67 | { 68 | CHARSET, IMPORT, NAMESP, AT_START, AT_END, SEL_START, SEL_END, PROPERTY, VALUE, COMMENT, CSS_END 69 | }; 70 | 71 | struct token 72 | { 73 | token_type type; 74 | int pos; 75 | int line; 76 | std::string data; 77 | }; 78 | 79 | struct message 80 | { 81 | std::string m; 82 | message_type t; 83 | }; 84 | 85 | CSSParser(); 86 | 87 | // valid levels are "CSS1.0", "CSS2.0", "CSS2.1", "CSS3.0" 88 | void set_level(std::string level = "CSS3.0"); 89 | 90 | void parse_css(std::string css_input); 91 | 92 | void reset_parser(); 93 | 94 | // serialize the current list of csstokens back to css 95 | std::string serialize_css(std::string filename = "", bool tostdout = true); 96 | 97 | // access charset, namespace and imports without having to walk csstokens 98 | std::string get_charset(); 99 | std::vector get_import(); 100 | std::string get_namespace(); 101 | 102 | // walk the csstokens list, token by token 103 | // set start_ptr to the position you woud like to start at in the list 104 | // leaving it as -1 will simply start at 0 and increment 105 | // last token is a dummy token with type set to CSS_END 106 | token get_next_token(int start_ptr = -1); 107 | 108 | // covert token type enum value to a descriptive string 109 | std::string get_type_name(token_type t); 110 | 111 | // this routine allows external modifications to the csstokens 112 | // to be brought back into CSSParser in order to serialize them 113 | // with serialize_css 114 | void set_csstokens(const std::vector &ntokens); 115 | 116 | // get errors, warnings, and information 117 | std::vector get_parse_errors(); 118 | std::vector get_parse_warnings(); 119 | std::vector get_parse_info(); 120 | 121 | 122 | private: 123 | 124 | void parseInAtBlock(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom); 125 | 126 | void parseInSelector(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 127 | bool& invalid_at, char& str_char, int str_size); 128 | 129 | void parseInProperty(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 130 | bool& invalid_at); 131 | 132 | void parseInValue(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 133 | bool& invalid_at, char& str_char, bool& pn, int str_size ); 134 | 135 | void parseInComment(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 136 | std::string& cur_comment); 137 | 138 | void parseInString(std::string& css_input, int& i, parse_status& astatus, parse_status& afrom, 139 | char& str_char, bool& str_in_str); 140 | 141 | 142 | // filter the logs by message type 143 | std::vector get_logs(message_type t); 144 | 145 | // Pparses unicode notations 146 | std::string unicode(std::string& istring, int& i); 147 | 148 | // checks if the chat in istring at i is a token 149 | bool is_token(std::string& istring, const int i); 150 | 151 | void add_token(const token_type ttype, const std::string data, const bool force = false); 152 | 153 | // Add a message to the message log 154 | void log(const std::string msg, const message_type type, int iline = 0); 155 | 156 | // records token position information 157 | void record_position(parse_status old_status, parse_status new_status, 158 | std::string &css_input, int i, bool force=false); 159 | 160 | int _seeknocomment(const int key, const int move); 161 | 162 | void explode_selectors(); 163 | 164 | static bool property_is_next(std::string istring, int pos); 165 | 166 | // private member variables 167 | std::vector token_type_names; 168 | std::map at_rules; 169 | std::vector csstemplate; 170 | std::string css_level; 171 | std::string tokens; 172 | int token_ptr; 173 | 174 | std::map > logs; 175 | int line; 176 | int spos; 177 | int sline; 178 | int selector_nest_level; 179 | 180 | std::string charset; 181 | std::string namesp; 182 | std::vector import; 183 | std::vector csstokens; 184 | std::string cur_selector; 185 | std::string cur_at; 186 | std::string cur_property; 187 | std::string cur_function; 188 | std::string cur_sub_value; 189 | std::string cur_value; 190 | std::string cur_string; 191 | std::vector sel_separate; 192 | std::vector cur_sub_value_arr; 193 | std::vector cur_function_arr; 194 | 195 | }; 196 | 197 | #endif // HEADER_CSSPARSER 198 | -------------------------------------------------------------------------------- /cssparser/CSSProperties.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | ** 3 | ** CSSParser 4 | ** A Simple C++ CSS Parser 5 | ** 6 | ** Copyright (C) 2021 Kevin B. Hendricks, Stratford, Ontario, Canada 7 | ** 8 | ** License: LGPL v2.1 or later 9 | ** 10 | ** Extracted and modified from: 11 | ** CSSTidy (https://github.com/csstidy-c/csstidy) 12 | ** 13 | ** CSSTidy Portions Copyright: 14 | ** Florian Schmitz 15 | ** Thierry Charbonnel 16 | ** Will Mitchell 17 | ** Brett Zamir 18 | ** sined_ 19 | ** Dmitry Leskov 20 | ** Kevin Coyner 21 | ** Tuukka Pasanen 22 | ** Frank W. Bergmann 23 | ** Frank Dana 24 | ** 25 | ** CSSTidy itself is Available under the LGPL 2.1 26 | ** 27 | ** You should have received a copy of the GNU Lesser General Public License 2.1 28 | ** along with this program. If not, see . 29 | ** 30 | *************************************************************************/ 31 | 32 | #include 33 | #include "CSSProperties.h" 34 | 35 | CSSProperties *CSSProperties::m_instance = 0; 36 | 37 | CSSProperties *CSSProperties::instance() 38 | { 39 | if (m_instance == 0) { 40 | m_instance = new CSSProperties(); 41 | } 42 | return m_instance; 43 | } 44 | 45 | 46 | bool CSSProperties::contains(std::string pname) 47 | { 48 | return m_all_properties.count(pname) == 1; 49 | } 50 | 51 | 52 | std::string CSSProperties::levels(std::string pname) 53 | { 54 | if (contains(pname)) { 55 | return m_all_properties[pname]; 56 | } 57 | return ""; 58 | } 59 | 60 | 61 | CSSProperties::CSSProperties() 62 | { 63 | if (m_all_properties.empty()) { 64 | m_all_properties["alignment-adjust"] = "CSS3.0"; 65 | m_all_properties["alignment-baseline"] = "CSS3.0"; 66 | m_all_properties["animation"] = "CSS3.0"; 67 | m_all_properties["animation-delay"] = "CSS3.0"; 68 | m_all_properties["animation-direction"] = "CSS3.0"; 69 | m_all_properties["animation-duration"] = "CSS3.0"; 70 | m_all_properties["animation-iteration-count"] = "CSS3.0"; 71 | m_all_properties["animation-name"] = "CSS3.0"; 72 | m_all_properties["animation-play-state"] = "CSS3.0"; 73 | m_all_properties["animation-timing-function"] = "CSS3.0"; 74 | m_all_properties["appearance"] = "CSS3.0"; 75 | m_all_properties["azimuth"] = "CSS2.0,CSS2.1,CSS3.0"; 76 | m_all_properties["backface-visibility"] = "CSS3.0"; 77 | m_all_properties["background"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 78 | m_all_properties["background-attachment"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 79 | m_all_properties["background-clip"] = "CSS3.0"; 80 | m_all_properties["background-color"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 81 | m_all_properties["background-image"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 82 | m_all_properties["background-origin"] = "CSS3.0"; 83 | m_all_properties["background-position"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 84 | m_all_properties["background-repeat"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 85 | m_all_properties["background-size"] = "CSS3.0"; 86 | m_all_properties["baseline-shift"] = "CSS3.0"; 87 | m_all_properties["binding"] = "CSS3.0"; 88 | m_all_properties["bleed"] = "CSS3.0"; 89 | m_all_properties["bookmark-label"] = "CSS3.0"; 90 | m_all_properties["bookmark-level"] = "CSS3.0"; 91 | m_all_properties["bookmark-state"] = "CSS3.0"; 92 | m_all_properties["bookmark-target"] = "CSS3.0"; 93 | m_all_properties["border"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 94 | m_all_properties["border-bottom"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 95 | m_all_properties["border-bottom-color"] = "CSS2.0,CSS2.1,CSS3.0"; 96 | m_all_properties["border-bottom-left-radius"] = "CSS3.0"; 97 | m_all_properties["border-bottom-right-radius"] = "CSS3.0"; 98 | m_all_properties["border-bottom-style"] = "CSS2.0,CSS2.1,CSS3.0"; 99 | m_all_properties["border-bottom-width"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 100 | m_all_properties["border-collapse"] = "CSS2.0,CSS2.1,CSS3.0"; 101 | m_all_properties["border-color"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 102 | m_all_properties["border-image"] = "CSS3.0"; 103 | m_all_properties["border-image-outset"] = "CSS3.0"; 104 | m_all_properties["border-image-repeat"] = "CSS3.0"; 105 | m_all_properties["border-image-slice"] = "CSS3.0"; 106 | m_all_properties["border-image-source"] = "CSS3.0"; 107 | m_all_properties["border-image-width"] = "CSS3.0"; 108 | m_all_properties["border-left"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 109 | m_all_properties["border-left-color"] = "CSS2.0,CSS2.1,CSS3.0"; 110 | m_all_properties["border-left-style"] = "CSS2.0,CSS2.1,CSS3.0"; 111 | m_all_properties["border-left-width"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 112 | m_all_properties["border-radius"] = "CSS3.0"; 113 | m_all_properties["border-right"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 114 | m_all_properties["border-right-color"] = "CSS2.0,CSS2.1,CSS3.0"; 115 | m_all_properties["border-right-style"] = "CSS2.0,CSS2.1,CSS3.0"; 116 | m_all_properties["border-right-width"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 117 | m_all_properties["border-spacing"] = "CSS2.0,CSS2.1,CSS3.0"; 118 | m_all_properties["border-style"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 119 | m_all_properties["border-top"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 120 | m_all_properties["border-top-color"] = "CSS2.0,CSS2.1,CSS3.0"; 121 | m_all_properties["border-top-left-radius"] = "CSS3.0"; 122 | m_all_properties["border-top-right-radius"] = "CSS3.0"; 123 | m_all_properties["border-top-style"] = "CSS2.0,CSS2.1,CSS3.0"; 124 | m_all_properties["border-top-width"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 125 | m_all_properties["border-width"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 126 | m_all_properties["bottom"] = "CSS2.0,CSS2.1,CSS3.0"; 127 | m_all_properties["box-decoration-break"] = "CSS3.0"; 128 | m_all_properties["box-shadow"] = "CSS3.0"; 129 | m_all_properties["box-sizing"] = "CSS3.0"; 130 | m_all_properties["break-after"] = "CSS3.0"; 131 | m_all_properties["break-before"] = "CSS3.0"; 132 | m_all_properties["break-inside"] = "CSS3.0"; 133 | m_all_properties["caption-side"] = "CSS2.0,CSS2.1,CSS3.0"; 134 | m_all_properties["clear"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 135 | m_all_properties["clip"] = "CSS2.0,CSS2.1,CSS3.0"; 136 | m_all_properties["color"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 137 | m_all_properties["color-profile"] = "CSS3.0"; 138 | m_all_properties["column-count"] = "CSS3.0"; 139 | m_all_properties["column-fill"] = "CSS3.0"; 140 | m_all_properties["column-gap"] = "CSS3.0"; 141 | m_all_properties["column-rule"] = "CSS3.0"; 142 | m_all_properties["column-rule-color"] = "CSS3.0"; 143 | m_all_properties["column-rule-style"] = "CSS3.0"; 144 | m_all_properties["column-rule-width"] = "CSS3.0"; 145 | m_all_properties["column-span"] = "CSS3.0"; 146 | m_all_properties["column-width"] = "CSS3.0"; 147 | m_all_properties["columns"] = "CSS3.0"; 148 | m_all_properties["content"] = "CSS2.0,CSS2.1,CSS3.0"; 149 | m_all_properties["counter-increment"] = "CSS2.0,CSS2.1,CSS3.0"; 150 | m_all_properties["counter-reset"] = "CSS2.0,CSS2.1,CSS3.0"; 151 | m_all_properties["crop"] = "CSS3.0"; 152 | m_all_properties["cue"] = "CSS2.0,CSS2.1,CSS3.0"; 153 | m_all_properties["cue-after"] = "CSS2.0,CSS2.1,CSS3.0"; 154 | m_all_properties["cue-before"] = "CSS2.0,CSS2.1,CSS3.0"; 155 | m_all_properties["cursor"] = "CSS2.0,CSS2.1,CSS3.0"; 156 | m_all_properties["direction"] = "CSS2.0,CSS2.1,CSS3.0"; 157 | m_all_properties["display"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 158 | m_all_properties["dominant-baseline"] = "CSS3.0"; 159 | m_all_properties["drop-initial-after-adjust"] = "CSS3.0"; 160 | m_all_properties["drop-initial-after-align"] = "CSS3.0"; 161 | m_all_properties["drop-initial-before-adjust"] = "CSS3.0"; 162 | m_all_properties["drop-initial-before-align"] = "CSS3.0"; 163 | m_all_properties["drop-initial-size"] = "CSS3.0"; 164 | m_all_properties["drop-initial-value"] = "CSS3.0"; 165 | m_all_properties["elevation"] = "CSS2.0,CSS2.1,CSS3.0"; 166 | m_all_properties["empty-cells"] = "CSS2.0,CSS2.1,CSS3.0"; 167 | m_all_properties["fit"] = "CSS3.0"; 168 | m_all_properties["fit-position"] = "CSS3.0"; 169 | m_all_properties["flex-align"] = "CSS3.0"; 170 | m_all_properties["flex-flow"] = "CSS3.0"; 171 | m_all_properties["flex-line-pack"] = "CSS3.0"; 172 | m_all_properties["flex-order"] = "CSS3.0"; 173 | m_all_properties["flex-pack"] = "CSS3.0"; 174 | m_all_properties["float"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 175 | m_all_properties["float-offset"] = "CSS3.0"; 176 | m_all_properties["font"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 177 | m_all_properties["font-family"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 178 | m_all_properties["font-format"] = "CSS3.0"; 179 | m_all_properties["font-size"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 180 | m_all_properties["font-size-adjust"] = "CSS2.0,CSS3.0"; 181 | m_all_properties["font-stretch"] = "CSS2.0,CSS3.0"; 182 | m_all_properties["font-style"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 183 | m_all_properties["font-variant"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 184 | m_all_properties["font-weight"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 185 | m_all_properties["grid-columns"] = "CSS3.0"; 186 | m_all_properties["grid-rows"] = "CSS3.0"; 187 | m_all_properties["hanging-punctuation"] = "CSS3.0"; 188 | m_all_properties["height"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 189 | m_all_properties["hyphenate-after"] = "CSS3.0"; 190 | m_all_properties["hyphenate-before"] = "CSS3.0"; 191 | m_all_properties["hyphenate-character"] = "CSS3.0"; 192 | m_all_properties["hyphenate-lines"] = "CSS3.0"; 193 | m_all_properties["hyphenate-resource"] = "CSS3.0"; 194 | m_all_properties["hyphens"] = "CSS3.0"; 195 | m_all_properties["icon"] = "CSS3.0"; 196 | m_all_properties["image-orientation"] = "CSS3.0"; 197 | m_all_properties["image-rendering"] = "CSS3.0"; 198 | m_all_properties["image-resolution"] = "CSS3.0"; 199 | m_all_properties["inline-box-align"] = "CSS3.0"; 200 | m_all_properties["left"] = "CSS2.0,CSS2.1,CSS3.0"; 201 | m_all_properties["letter-spacing"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 202 | m_all_properties["line-break"] = "CSS3.0"; 203 | m_all_properties["line-height"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 204 | m_all_properties["line-stacking"] = "CSS3.0"; 205 | m_all_properties["line-stacking-ruby"] = "CSS3.0"; 206 | m_all_properties["line-stacking-shift"] = "CSS3.0"; 207 | m_all_properties["line-stacking-strategy"] = "CSS3.0"; 208 | m_all_properties["list-style"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 209 | m_all_properties["list-style-image"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 210 | m_all_properties["list-style-position"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 211 | m_all_properties["list-style-type"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 212 | m_all_properties["margin"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 213 | m_all_properties["margin-bottom"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 214 | m_all_properties["margin-left"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 215 | m_all_properties["margin-right"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 216 | m_all_properties["margin-top"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 217 | m_all_properties["marker-offset"] = "CSS2.0,CSS3.0"; 218 | m_all_properties["marks"] = "CSS2.0,CSS3.0"; 219 | m_all_properties["marquee-direction"] = "CSS3.0"; 220 | m_all_properties["marquee-loop"] = "CSS3.0"; 221 | m_all_properties["marquee-play-count"] = "CSS3.0"; 222 | m_all_properties["marquee-speed"] = "CSS3.0"; 223 | m_all_properties["marquee-style"] = "CSS3.0"; 224 | m_all_properties["max-height"] = "CSS2.0,CSS2.1,CSS3.0"; 225 | m_all_properties["max-width"] = "CSS2.0,CSS2.1,CSS3.0"; 226 | m_all_properties["min-height"] = "CSS2.0,CSS2.1,CSS3.0"; 227 | m_all_properties["min-width"] = "CSS2.0,CSS2.1,CSS3.0"; 228 | m_all_properties["move-to"] = "CSS3.0"; 229 | m_all_properties["nav-down"] = "CSS3.0"; 230 | m_all_properties["nav-index"] = "CSS3.0"; 231 | m_all_properties["nav-left"] = "CSS3.0"; 232 | m_all_properties["nav-right"] = "CSS3.0"; 233 | m_all_properties["nav-up"] = "CSS3.0"; 234 | m_all_properties["opacity"] = "CSS3.0"; 235 | m_all_properties["orphans"] = "CSS2.0,CSS2.1,CSS3.0"; 236 | m_all_properties["outline"] = "CSS2.0,CSS2.1,CSS3.0"; 237 | m_all_properties["outline-color"] = "CSS2.0,CSS2.1,CSS3.0"; 238 | m_all_properties["outline-offset"] = "CSS3.0"; 239 | m_all_properties["outline-style"] = "CSS2.0,CSS2.1,CSS3.0"; 240 | m_all_properties["outline-width"] = "CSS2.0,CSS2.1,CSS3.0"; 241 | m_all_properties["overflow"] = "CSS2.0,CSS2.1,CSS3.0"; 242 | m_all_properties["overflow-style"] = "CSS3.0"; 243 | m_all_properties["overflow-wrap"] = "CSS3.0"; 244 | m_all_properties["overflow-x"] = "CSS3.0"; 245 | m_all_properties["overflow-y"] = "CSS3.0"; 246 | m_all_properties["padding"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 247 | m_all_properties["padding-bottom"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 248 | m_all_properties["padding-left"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 249 | m_all_properties["padding-right"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 250 | m_all_properties["padding-top"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 251 | m_all_properties["page"] = "CSS2.0,CSS3.0"; 252 | m_all_properties["page-break-after"] = "CSS2.0,CSS2.1,CSS3.0"; 253 | m_all_properties["page-break-before"] = "CSS2.0,CSS2.1,CSS3.0"; 254 | m_all_properties["page-break-inside"] = "CSS2.0,CSS2.1,CSS3.0"; 255 | m_all_properties["page-policy"] = "CSS3.0"; 256 | m_all_properties["pause"] = "CSS2.0,CSS2.1,CSS3.0"; 257 | m_all_properties["pause-after"] = "CSS2.0,CSS2.1,CSS3.0"; 258 | m_all_properties["pause-before"] = "CSS2.0,CSS2.1,CSS3.0"; 259 | m_all_properties["perspective"] = "CSS3.0"; 260 | m_all_properties["perspective-origin"] = "CSS3.0"; 261 | m_all_properties["phonemes"] = "CSS3.0"; 262 | m_all_properties["pitch"] = "CSS2.0,CSS2.1,CSS3.0"; 263 | m_all_properties["pitch-range"] = "CSS2.0,CSS2.1,CSS3.0"; 264 | m_all_properties["play-during"] = "CSS2.0,CSS2.1,CSS3.0"; 265 | m_all_properties["position"] = "CSS2.0,CSS2.1,CSS3.0"; 266 | m_all_properties["presentation-level"] = "CSS3.0"; 267 | m_all_properties["punctuation-trim"] = "CSS3.0"; 268 | m_all_properties["quotes"] = "CSS2.0,CSS2.1,CSS3.0"; 269 | m_all_properties["rendering-intent"] = "CSS3.0"; 270 | m_all_properties["resize"] = "CSS3.0"; 271 | m_all_properties["rest"] = "CSS3.0"; 272 | m_all_properties["rest-after"] = "CSS3.0"; 273 | m_all_properties["rest-before"] = "CSS3.0"; 274 | m_all_properties["richness"] = "CSS2.0,CSS2.1,CSS3.0"; 275 | m_all_properties["right"] = "CSS2.0,CSS2.1,CSS3.0"; 276 | m_all_properties["rotation"] = "CSS3.0"; 277 | m_all_properties["rotation-point"] = "CSS3.0"; 278 | m_all_properties["ruby-align"] = "CSS3.0"; 279 | m_all_properties["ruby-overhang"] = "CSS3.0"; 280 | m_all_properties["ruby-position"] = "CSS3.0"; 281 | m_all_properties["ruby-span"] = "CSS3.0"; 282 | m_all_properties["size"] = "CSS2.0,CSS3.0"; 283 | m_all_properties["speak"] = "CSS2.0,CSS2.1,CSS3.0"; 284 | m_all_properties["speak-header"] = "CSS2.0,CSS2.1,CSS3.0"; 285 | m_all_properties["speak-numeral"] = "CSS2.0,CSS2.1,CSS3.0"; 286 | m_all_properties["speak-punctuation"] = "CSS2.0,CSS2.1,CSS3.0"; 287 | m_all_properties["speech-rate"] = "CSS2.0,CSS2.1,CSS3.0"; 288 | m_all_properties["src"] = "CSS3.0"; 289 | m_all_properties["stress"] = "CSS2.0,CSS2.1,CSS3.0"; 290 | m_all_properties["string-set"] = "CSS3.0"; 291 | m_all_properties["tab-size"] = "CSS3.0"; 292 | m_all_properties["table-layout"] = "CSS2.0,CSS2.1,CSS3.0"; 293 | m_all_properties["target"] = "CSS3.0"; 294 | m_all_properties["target-name"] = "CSS3.0"; 295 | m_all_properties["target-new"] = "CSS3.0"; 296 | m_all_properties["target-position"] = "CSS3.0"; 297 | m_all_properties["text-align"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 298 | m_all_properties["text-align-last"] = "CSS3.0"; 299 | m_all_properties["text-decoration"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 300 | m_all_properties["text-decoration-color"] = "CSS3.0"; 301 | m_all_properties["text-decoration-line"] = "CSS3.0"; 302 | m_all_properties["text-decoration-skip"] = "CSS3.0"; 303 | m_all_properties["text-decoration-style"] = "CSS3.0"; 304 | m_all_properties["text-emphasis"] = "CSS3.0"; 305 | m_all_properties["text-emphasis-color"] = "CSS3.0"; 306 | m_all_properties["text-emphasis-position"] = "CSS3.0"; 307 | m_all_properties["text-emphasis-style"] = "CSS3.0"; 308 | m_all_properties["text-height"] = "CSS3.0"; 309 | m_all_properties["text-indent"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 310 | m_all_properties["text-justify"] = "CSS3.0"; 311 | m_all_properties["text-outline"] = "CSS3.0"; 312 | m_all_properties["text-shadow"] = "CSS2.0,CSS3.0"; 313 | m_all_properties["text-space-collapse"] = "CSS3.0"; 314 | m_all_properties["text-transform"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 315 | m_all_properties["text-underline-position"] = "CSS3.0"; 316 | m_all_properties["text-wrap"] = "CSS3.0"; 317 | m_all_properties["top"] = "CSS2.0,CSS2.1,CSS3.0"; 318 | m_all_properties["transform"] = "CSS3.0"; 319 | m_all_properties["transform-origin"] = "CSS3.0"; 320 | m_all_properties["transform-style"] = "CSS3.0"; 321 | m_all_properties["transition"] = "CSS3.0"; 322 | m_all_properties["transition-delay"] = "CSS3.0"; 323 | m_all_properties["transition-duration"] = "CSS3.0"; 324 | m_all_properties["transition-property"] = "CSS3.0"; 325 | m_all_properties["transition-timing-function"] = "CSS3.0"; 326 | m_all_properties["unicode-bidi"] = "CSS2.0,CSS2.1,CSS3.0"; 327 | m_all_properties["vertical-align"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 328 | m_all_properties["visibility"] = "CSS2.0,CSS2.1,CSS3.0"; 329 | m_all_properties["voice-balance"] = "CSS3.0"; 330 | m_all_properties["voice-duration"] = "CSS3.0"; 331 | m_all_properties["voice-family"] = "CSS2.0,CSS2.1,CSS3.0"; 332 | m_all_properties["voice-pitch"] = "CSS3.0"; 333 | m_all_properties["voice-pitch-range"] = "CSS3.0"; 334 | m_all_properties["voice-rate"] = "CSS3.0"; 335 | m_all_properties["voice-stress"] = "CSS3.0"; 336 | m_all_properties["voice-volume"] = "CSS3.0"; 337 | m_all_properties["volume"] = "CSS2.0,CSS2.1,CSS3.0"; 338 | m_all_properties["white-space"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 339 | m_all_properties["widows"] = "CSS2.0,CSS2.1,CSS3.0"; 340 | m_all_properties["width"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 341 | m_all_properties["word-break"] = "CSS3.0"; 342 | m_all_properties["word-spacing"] = "CSS1.0,CSS2.0,CSS2.1,CSS3.0"; 343 | m_all_properties["word-wrap"] = "CSS3.0"; 344 | m_all_properties["z-index"] = "CSS2.0,CSS2.1,CSS3.0"; 345 | m_all_properties["zoom"] = "CSS3.0"; 346 | } 347 | } 348 | -------------------------------------------------------------------------------- /cssparser/CSSProperties.h: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | ** 3 | ** CSSParser 4 | ** A Simple C++ CSS Parser 5 | ** 6 | ** Copyright (C) 2021 Kevin B. Hendricks, Stratford, Ontario, Canada 7 | ** 8 | ** License: LGPL v2.1 or later 9 | ** 10 | ** Extracted and modified from: 11 | ** CSSTidy (https://github.com/csstidy-c/csstidy) 12 | ** 13 | ** CSSTidy Portions Copyright: 14 | ** Florian Schmitz 15 | ** Thierry Charbonnel 16 | ** Will Mitchell 17 | ** Brett Zamir 18 | ** sined_ 19 | ** Dmitry Leskov 20 | ** Kevin Coyner 21 | ** Tuukka Pasanen 22 | ** Frank W. Bergmann 23 | ** Frank Dana 24 | ** 25 | ** CSSTidy itself is Available under the LGPL 2.1 26 | ** 27 | ** You should have received a copy of the GNU Lesser General Public License 2.1 28 | ** along with this program. If not, see . 29 | ** 30 | *************************************************************************/ 31 | 32 | #pragma once 33 | #ifndef CSS_PROPERTIES_H 34 | #define CSS_PROPERTIES_H 35 | 36 | #include 37 | #include 38 | #include 39 | 40 | /** 41 | * Singleton. 42 | * 43 | * CSSProperties 44 | */ 45 | 46 | 47 | class CSSProperties 48 | { 49 | 50 | public: 51 | 52 | static CSSProperties *instance(); 53 | 54 | bool contains(std::string pname); 55 | 56 | std::string levels(std::string pname); 57 | 58 | private: 59 | 60 | CSSProperties(); 61 | 62 | std::map m_all_properties; 63 | static CSSProperties *m_instance; 64 | }; 65 | 66 | #endif // CSS_PROPERTIES_H 67 | -------------------------------------------------------------------------------- /cssparser/CSSUtils.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | ** 3 | ** CSSParser 4 | ** A Simple C++ CSS Parser 5 | ** 6 | ** Copyright (C) 2021 Kevin B. Hendricks, Stratford, Ontario, Canada 7 | ** 8 | ** License: LGPL v2.1 or later 9 | ** 10 | ** Extracted and modified from: 11 | ** CSSTidy (https://github.com/csstidy-c/csstidy) 12 | ** 13 | ** CSSTidy Portions Copyright: 14 | ** Florian Schmitz 15 | ** Thierry Charbonnel 16 | ** Will Mitchell 17 | ** Brett Zamir 18 | ** sined_ 19 | ** Dmitry Leskov 20 | ** Kevin Coyner 21 | ** Tuukka Pasanen 22 | ** Frank W. Bergmann 23 | ** Frank Dana 24 | ** 25 | ** CSSTidy itself is Available under the LGPL 2.1 26 | ** 27 | ** You should have received a copy of the GNU Lesser General Public License 2.1 28 | ** along with this program. If not, see . 29 | ** 30 | *************************************************************************/ 31 | 32 | #include 33 | #include "CSSUtils.h" 34 | 35 | std::string CSSUtils::strtolower(std::string istring) 36 | { 37 | int str_size = istring.length(); 38 | for(int i = 0; i < str_size; i++) 39 | { 40 | istring[i] = chartolower(istring[i]); 41 | } 42 | return istring; 43 | } 44 | 45 | 46 | char CSSUtils::chartolower(const char c) 47 | { 48 | switch(c) 49 | { 50 | case 'A': return 'a'; 51 | case 'B': return 'b'; 52 | case 'C': return 'c'; 53 | case 'D': return 'd'; 54 | case 'E': return 'e'; 55 | case 'F': return 'f'; 56 | case 'G': return 'g'; 57 | case 'H': return 'h'; 58 | case 'I': return 'i'; 59 | case 'J': return 'j'; 60 | case 'K': return 'k'; 61 | case 'L': return 'l'; 62 | case 'M': return 'm'; 63 | case 'N': return 'n'; 64 | case 'O': return 'o'; 65 | case 'P': return 'p'; 66 | case 'Q': return 'q'; 67 | case 'R': return 'r'; 68 | case 'S': return 's'; 69 | case 'T': return 't'; 70 | case 'U': return 'u'; 71 | case 'V': return 'v'; 72 | case 'W': return 'w'; 73 | case 'X': return 'x'; 74 | case 'Y': return 'y'; 75 | case 'Z': return 'z'; 76 | default: return c; 77 | } 78 | } 79 | 80 | 81 | std::string CSSUtils::strtoupper(std::string istring) 82 | { 83 | int str_size = istring.length(); 84 | for(int i = 0; i < str_size; i++) 85 | { 86 | istring[i] = chartoupper(istring[i]); 87 | } 88 | return istring; 89 | } 90 | 91 | 92 | char CSSUtils::chartoupper(const char c) 93 | { 94 | switch(c) 95 | { 96 | case 'a': return 'A'; 97 | case 'b': return 'B'; 98 | case 'c': return 'C'; 99 | case 'd': return 'D'; 100 | case 'e': return 'E'; 101 | case 'f': return 'F'; 102 | case 'g': return 'G'; 103 | case 'h': return 'H'; 104 | case 'i': return 'I'; 105 | case 'j': return 'J'; 106 | case 'k': return 'K'; 107 | case 'l': return 'L'; 108 | case 'm': return 'M'; 109 | case 'n': return 'N'; 110 | case 'o': return 'O'; 111 | case 'p': return 'P'; 112 | case 'q': return 'Q'; 113 | case 'r': return 'R'; 114 | case 's': return 'S'; 115 | case 't': return 'T'; 116 | case 'u': return 'U'; 117 | case 'v': return 'V'; 118 | case 'w': return 'W'; 119 | case 'x': return 'X'; 120 | case 'y': return 'Y'; 121 | case 'z': return 'Z'; 122 | default: return c; 123 | } 124 | } 125 | 126 | 127 | double CSSUtils::hexdec(std::string istring) 128 | { 129 | double ret = 0; 130 | istring = trim(istring); 131 | for(int i = istring.length()-1; i >= 0; --i) 132 | { 133 | int num = 0; 134 | switch(tolower(istring[i])) 135 | { 136 | case 'a': num = 10; break; 137 | case 'b': num = 11; break; 138 | case 'c': num = 12; break; 139 | case 'd': num = 13; break; 140 | case 'e': num = 14; break; 141 | case 'f': num = 15; break; 142 | case '1': num = 1; break; 143 | case '2': num = 2; break; 144 | case '3': num = 3; break; 145 | case '4': num = 4; break; 146 | case '5': num = 5; break; 147 | case '6': num = 6; break; 148 | case '7': num = 7; break; 149 | case '8': num = 8; break; 150 | case '9': num = 9; break; 151 | case '0': num = 0; break; 152 | } 153 | ret += num*pow((double) 16, (double) istring.length()-i-1); 154 | } 155 | return ret; 156 | } 157 | 158 | 159 | std::string CSSUtils::char2str(const char c) 160 | { 161 | std::string ret = ""; 162 | ret += c; 163 | return ret; 164 | } 165 | 166 | 167 | std::string CSSUtils::char2str(const char *c) 168 | { 169 | std::stringstream sstream; 170 | sstream << c; 171 | return sstream.str(); 172 | } 173 | 174 | 175 | std::string CSSUtils::file_get_contents(const std::string filename) 176 | { 177 | std::ifstream file_input(filename.c_str(),std::ios::binary); 178 | std::string line, file_contents = ""; 179 | 180 | if(file_input.bad()) 181 | { 182 | return ""; 183 | } 184 | else 185 | { 186 | while(file_input.good()) 187 | { 188 | getline(file_input,line); 189 | file_contents += (line + "\n"); 190 | } 191 | } 192 | file_input.close(); 193 | 194 | return file_contents; 195 | } 196 | 197 | 198 | bool CSSUtils::file_exists(const char *filename) 199 | { 200 | std::ifstream file_input(filename); 201 | 202 | if(file_input.is_open()) 203 | { 204 | file_input.close(); 205 | return true; 206 | } 207 | 208 | file_input.close(); 209 | return false; 210 | } 211 | 212 | 213 | bool CSSUtils::escaped(const std::string &istring, const int pos) 214 | { 215 | return !(s_at(istring,pos-1) != '\\' || escaped(istring,pos-1)); 216 | } 217 | 218 | 219 | // Safe replacement for .at() 220 | char CSSUtils::s_at(const std::string &istring, const int pos) 221 | { 222 | if(pos > (istring.length()-1) || pos < 0) 223 | { 224 | return 0; 225 | } 226 | else 227 | { 228 | return istring[pos]; 229 | } 230 | } 231 | 232 | 233 | std::vector CSSUtils::explode(const std::string e, std::string s, const bool check) 234 | { 235 | std::vector ret; 236 | int iPos = s.find(e, 0); 237 | int iPit = e.length(); 238 | 239 | while(iPos > -1) 240 | { 241 | if(iPos != 0 || check) 242 | { 243 | ret.push_back(s.substr(0,iPos)); 244 | } 245 | s.erase(0,iPos+iPit); 246 | iPos = s.find(e, 0); 247 | } 248 | 249 | if(s != "" || check) 250 | { 251 | ret.push_back(s); 252 | } 253 | return ret; 254 | } 255 | 256 | 257 | std::string CSSUtils::implode(const std::string e, const std::vector s) 258 | { 259 | std::string ret; 260 | for(int i = 0; i < s.size(); i++) 261 | { 262 | ret += s[i]; 263 | if(i != (s.size()-1)) ret += e; 264 | } 265 | return ret; 266 | } 267 | 268 | 269 | std::string CSSUtils::build_value(const std::vector subvalues) 270 | { 271 | std::string ret; 272 | for(int i = 0; i < subvalues.size(); i++) 273 | { 274 | ret += subvalues[i]; 275 | if(i != (subvalues.size()-1)) 276 | { 277 | char last = s_at(subvalues[i], subvalues[i].length()-1); 278 | char next = s_at(subvalues[i+1], 0); 279 | if (strchr("(,=:", last) != NULL || strchr("),=:", next) != NULL) 280 | { 281 | continue; 282 | } 283 | ret += " "; 284 | } 285 | } 286 | return ret; 287 | } 288 | 289 | 290 | std::string CSSUtils::str_replace(const std::string find, const std::string replace, std::string str) 291 | { 292 | int len = find.length(); 293 | int replace_len = replace.length(); 294 | int pos = str.find(find); 295 | 296 | while(pos != std::string::npos) 297 | { 298 | str.replace(pos, len, replace); 299 | pos = str.find(find, pos + replace_len); 300 | } 301 | return str; 302 | } 303 | 304 | 305 | 306 | bool CSSUtils::in_char_arr(const char* haystack, const char needle) 307 | { 308 | for(int i = 0; i < strlen(haystack); ++i) 309 | { 310 | if(haystack[i] == needle) 311 | { 312 | return true; 313 | } 314 | } 315 | return false; 316 | } 317 | 318 | 319 | bool CSSUtils::in_str_array(const std::string& haystack, const char needle) 320 | { 321 | return (haystack.find_first_of(needle,0) != std::string::npos); 322 | } 323 | 324 | 325 | bool CSSUtils::ctype_space(const char c) 326 | { 327 | return (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11); 328 | } 329 | 330 | 331 | bool CSSUtils::ctype_digit(const char c) 332 | { 333 | return (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'); 334 | } 335 | 336 | 337 | bool CSSUtils::ctype_xdigit(char c) 338 | { 339 | c = chartolower(c); 340 | return (ctype_digit(c) || c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'); 341 | } 342 | 343 | bool CSSUtils::ctype_alpha(char c) 344 | { 345 | c = chartolower(c); 346 | return (c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || 347 | c == 'g' || c == 'h' || c == 'i' || c == 'j' || c == 'k' || c == 'l' || 348 | c == 'm' || c == 'n' || c == 'o' || c == 'p' || c == 'q' || c == 'r' || 349 | c == 's' || c == 't' || c == 'u' || c == 'v' || c == 'w' || c == 'x' || 350 | c == 'y' || c == 'z'); 351 | } 352 | 353 | 354 | const std::string CSSUtils::trim(const std::string istring) 355 | { 356 | std::string::size_type first = istring.find_first_not_of(" \n\t\r\0xb"); 357 | if (first == std::string::npos) { 358 | return std::string(); 359 | } 360 | else 361 | { 362 | std::string::size_type last = istring.find_last_not_of(" \n\t\r\0xb"); 363 | return istring.substr( first, last - first + 1); 364 | } 365 | } 366 | 367 | 368 | const std::string CSSUtils::ltrim(const std::string istring) 369 | { 370 | std::string::size_type first = istring.find_first_not_of(" \n\t\r\0xb"); 371 | if (first == std::string::npos) { 372 | return std::string(); 373 | } 374 | else 375 | { 376 | return istring.substr( first ); 377 | } 378 | } 379 | 380 | 381 | const std::string CSSUtils::rtrim(const std::string istring) 382 | { 383 | std::string::size_type last = istring.find_last_not_of(" \n\t\r\0xb"); /// must succeed 384 | return istring.substr( 0, last + 1); 385 | } 386 | 387 | 388 | const std::string CSSUtils::rtrim(const std::string istring, const std::string chars) 389 | { 390 | std::string::size_type last = istring.find_last_not_of(chars); /// must succeed 391 | return istring.substr( 0, last + 1); 392 | } 393 | 394 | const std::string CSSUtils::indent(int lvl, const std::string &base) 395 | { 396 | std::string ind = ""; 397 | for (int i = 0; i < lvl; i++) { 398 | ind = ind + base; 399 | } 400 | return ind; 401 | } 402 | -------------------------------------------------------------------------------- /cssparser/CSSUtils.h: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | ** 3 | ** CSSParser 4 | ** A Simple C++ CSS Parser 5 | ** 6 | ** Copyright (C) 2021 Kevin B. Hendricks, Stratford, Ontario, Canada 7 | ** 8 | ** License: LGPL v2.1 or later 9 | ** 10 | ** Extracted and modified from: 11 | ** CSSTidy (https://github.com/csstidy-c/csstidy) 12 | ** 13 | ** CSSTidy Portions Copyright: 14 | ** Florian Schmitz 15 | ** Thierry Charbonnel 16 | ** Will Mitchell 17 | ** Brett Zamir 18 | ** sined_ 19 | ** Dmitry Leskov 20 | ** Kevin Coyner 21 | ** Tuukka Pasanen 22 | ** Frank W. Bergmann 23 | ** Frank Dana 24 | ** 25 | ** CSSTidy itself is Available under the LGPL 2.1 26 | ** 27 | ** You should have received a copy of the GNU Lesser General Public License 2.1 28 | ** along with this program. If not, see . 29 | ** 30 | *************************************************************************/ 31 | 32 | #ifndef HEADER_CSSUTILS 33 | #define HEADER_CSSUTILS 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | class CSSUtils 47 | { 48 | 49 | public: 50 | 51 | // Returns the lowercase version of a string 52 | static std::string strtolower(std::string istring); 53 | 54 | // Apparently faster replacement for tolower 55 | static char chartolower(const char c); 56 | 57 | // Returns the uppercase version of a string 58 | static std::string strtoupper(std::string istring); 59 | static char chartoupper(const char c); 60 | 61 | // Converts a hexadecimal number (string) to a decimal number 62 | static double hexdec(std::string istring); 63 | 64 | // Converts a char to a string 65 | static std::string char2str(const char c); 66 | 67 | // Converts a char to a string 68 | static std::string char2str(const char* c); 69 | 70 | // Get contents of a file 71 | static std::string file_get_contents(const std::string filename); 72 | 73 | // Checks if a file exists 74 | static bool file_exists(const char *filename); 75 | 76 | // Checks if a charcter is escaped 77 | static bool escaped(const std::string &istring, int pos); 78 | 79 | // Returns a char of a std::string at pos but checks the std::string-length before 80 | static char s_at(const std::string &istring, int pos); 81 | 82 | // Implodes a vector of std::string e (string join with e as seprator)) 83 | static std::string implode(const std::string e, const std::vector s); 84 | 85 | // Explodes a std::string into a vector of std::strings, splitting at s 86 | static std::vector explode(const std::string e, std::string s, const bool check); 87 | 88 | // Builds a compact value std::string, inserting spaces only where necessary 89 | static std::string build_value(const std::vector subvalues); 90 | 91 | // Replaces with in 92 | static std::string str_replace(const std::string find, const std::string replace, std::string str); 93 | 94 | // Checks if a std::string exists in a std::string-array 95 | static bool in_char_arr(const char* haystack, const char needle); 96 | static bool in_str_array(const std::string& haystack, const char needle); 97 | 98 | // isspace() and isdigit() do not work correctly with UTF-8 std::strings 99 | static bool ctype_space(const char c); 100 | static bool ctype_digit(const char c); 101 | static bool ctype_xdigit(char c); 102 | static bool ctype_alpha(char c); 103 | 104 | // trims whitespace at the specific or both ends of a std::string 105 | static const std::string trim(const std::string istring); 106 | static const std::string rtrim(const std::string istring); 107 | static const std::string rtrim(const std::string istring, const std::string chars); 108 | static const std::string ltrim(const std::string istring); 109 | 110 | // creates proper indent level 111 | static const std::string indent(int lvl, const std::string &base); 112 | }; 113 | #endif // HEADER_CSSUTILS 114 | -------------------------------------------------------------------------------- /cssparser/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | ** 3 | ** CSSParser 4 | ** A Simple C++ CSS Parser 5 | ** 6 | ** Copyright (C) 2021 Kevin B. Hendricks, Stratford, Ontario, Canada 7 | ** 8 | ** License: LGPL v2.1 or later 9 | ** 10 | ** Extracted and modified from: 11 | ** CSSTidy (https://github.com/csstidy-c/csstidy) 12 | ** 13 | ** CSSTidy Portions Copyright: 14 | ** Florian Schmitz 15 | ** Thierry Charbonnel 16 | ** Will Mitchell 17 | ** Brett Zamir 18 | ** sined_ 19 | ** Dmitry Leskov 20 | ** Kevin Coyner 21 | ** Tuukka Pasanen 22 | ** Frank W. Bergmann 23 | ** Frank Dana 24 | ** 25 | ** CSSTidy itself is Available under the LGPL 2.1 26 | ** 27 | ** You should have received a copy of the GNU Lesser General Public License 2.1 28 | ** along with this program. If not, see . 29 | ** 30 | *************************************************************************/ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include "CSSProperties.h" 46 | #include "CSSUtils.h" 47 | #include "CSSParser.h" 48 | 49 | int main(int argc, char *argv[]) 50 | { 51 | CSSParser csst; 52 | 53 | if(argc > 1) 54 | { 55 | std::string filein = argv[1]; 56 | if(filein != "-" && !CSSUtils::file_exists(argv[1])) 57 | { 58 | std::cout << "The file \"" << filein << "\" does not exist." << std::endl; 59 | return EXIT_FAILURE; 60 | } 61 | 62 | std::string output_filename; 63 | 64 | for(int i = 2; i < argc; ++i) 65 | { 66 | bool output_file = true; 67 | if(output_file) 68 | { 69 | output_filename = CSSUtils::trim(argv[i]); 70 | } 71 | } 72 | 73 | std::string css_file; 74 | if(filein == "-") 75 | { 76 | std::string temp; 77 | do { 78 | getline(std::cin, temp, '\n'); 79 | css_file += (temp + "\n"); 80 | } while(std::cin); 81 | } 82 | else 83 | { 84 | css_file = CSSUtils::file_get_contents(argv[1]); 85 | } 86 | 87 | // The interface 88 | 89 | 90 | // valid css levels are "CSS1.0", "CSS2.0", "CSS2.1", "CSS3.0" 91 | csst.set_level("CSS3.0"); 92 | 93 | // do the actual parsing 94 | csst.parse_css(css_file); 95 | 96 | // check for any parse errors 97 | std::vector errors = csst.get_parse_errors(); 98 | std::cout << "Errors: " << errors.size() << std::endl; 99 | for(int i = 0; i < errors.size(); i++) { 100 | std::cout << " Error: " << errors[i] << std::endl; 101 | } 102 | 103 | // check for any parse warnings 104 | std::vector warnings = csst.get_parse_warnings(); 105 | std::cout << "Warnings: " << warnings.size() << std::endl; 106 | for(int i = 0; i < warnings.size(); i++) { 107 | std::cout << " Warning: " << warnings[i] << std::endl; 108 | } 109 | 110 | // check for any parse information messages 111 | std::vector infos = csst.get_parse_info(); 112 | std::cout << "Information: " << infos.size() << std::endl; 113 | for(int i = 0; i < infos.size(); i++) { 114 | std::cout << " Information: " << infos[i] << std::endl; 115 | } 116 | 117 | // get any @charset without having to walk the csstokens list 118 | std::string cset = csst.get_charset(); 119 | if (!cset.empty()) std::cout << "charset: " << cset << std::endl; 120 | 121 | // get all @import without having to walk the csstokens list 122 | std::vector imports = csst.get_import(); 123 | for(int i = 0; i < imports.size(); i++) { 124 | std::cout << "import: " << imports[i] << std::endl; 125 | } 126 | 127 | // get any @namespace without having to walk the csstokens list 128 | std::string ns = csst.get_namespace(); 129 | if (!ns.empty()) std::cout << "namespace: " << ns << std::endl; 130 | 131 | // The possible token type are an enum: 132 | // enum token_type: 133 | // 134 | // CHARSET = 0 135 | // IMPORT = 1 136 | // NAMESP = 2 137 | // AT_START = 3 138 | // AT_END = 4 139 | // SEL_START = 5 140 | // SEL_END = 6 141 | // PROPERTY = 7 142 | // VALUE = 8 143 | // COMMENT = 9 144 | // CSS_END = 10 145 | 146 | // now walk the sequence of parsed tokens 147 | // if you know the location of the token you want in the sequence (starting with 0) 148 | // simply pass start_ptr in get_next_token set to a valid starting point in the token sequence 149 | 150 | CSSParser::token atoken = csst.get_next_token(); 151 | while(atoken.type != CSSParser::CSS_END) { 152 | std::string ttype = csst.get_type_name(atoken.type); 153 | std::cout << "Pos: " << atoken.pos << " Line: " << atoken.line << " Type: " << ttype 154 | <<" Data: " << atoken.data << std::endl; 155 | atoken = csst.get_next_token(); 156 | } 157 | 158 | // serialize CSS to stdout if no output file is specified 159 | std::string cssout; 160 | if(output_filename == "") 161 | { 162 | cssout = csst.serialize_css(); 163 | } 164 | else 165 | { 166 | cssout = csst.serialize_css(output_filename, false); 167 | } 168 | 169 | return EXIT_SUCCESS; 170 | } 171 | 172 | return EXIT_SUCCESS; 173 | } 174 | --------------------------------------------------------------------------------