Fixed: shorthand/longhand properties handled in wrong order

--HG--
branch : html-improvements
hg/feature/html-improvements
Nimetu 5 years ago
parent 963a9814da
commit d7c754039e

@ -31,7 +31,7 @@ namespace NLGUI
class CCssParser { class CCssParser {
public: public:
// parse style declaration, eg "color: red; font-size: 10px;" // parse style declaration, eg "color: red; font-size: 10px;"
static TStyle parseDecls(const std::string &styleString); static TStyleVec parseDecls(const std::string &styleString);
// parse css stylesheet // parse css stylesheet
void parseStylesheet(const std::string &cssString, std::vector<CCssStyle::SStyleRule> &rules); void parseStylesheet(const std::string &cssString, std::vector<CCssStyle::SStyleRule> &rules);

@ -27,6 +27,8 @@ namespace NLGUI
class CHtmlElement; class CHtmlElement;
typedef std::map<std::string, std::string> TStyle; typedef std::map<std::string, std::string> TStyle;
typedef std::pair<std::string, std::string> TStylePair;
typedef std::vector<TStylePair> TStyleVec;
/** /**
* \brief CSS style rules * \brief CSS style rules
@ -118,7 +120,7 @@ namespace NLGUI
struct SStyleRule { struct SStyleRule {
std::vector<CCssSelector> Selector; std::vector<CCssSelector> Selector;
TStyle Properties; TStyleVec Properties;
// pseudo element like ':before' // pseudo element like ':before'
std::string PseudoElement; std::string PseudoElement;
@ -153,7 +155,7 @@ namespace NLGUI
void apply(CStyleParams &style, const CStyleParams &current) const; void apply(CStyleParams &style, const CStyleParams &current) const;
// merge src into dest by overwriting key in dest // merge src into dest by overwriting key in dest
void merge(TStyle &dst, const TStyle &src) const; void merge(TStyle &dst, const TStyleVec &src) const;
// match selector to dom path // match selector to dom path
bool match(const std::vector<CCssSelector> &selector, const CHtmlElement &elm) const; bool match(const std::vector<CCssSelector> &selector, const CHtmlElement &elm) const;

@ -35,9 +35,9 @@ namespace NLGUI
// //
// key is converted to lowercase // key is converted to lowercase
// value is left as is // value is left as is
TStyle CCssParser::parseDecls(const std::string &styleString) TStyleVec CCssParser::parseDecls(const std::string &styleString)
{ {
TStyle styles; TStyleVec styles;
std::vector<std::string> elements; std::vector<std::string> elements;
NLMISC::splitString(styleString, ";", elements); NLMISC::splitString(styleString, ";", elements);
@ -49,7 +49,7 @@ namespace NLGUI
{ {
std::string key = trim(toLower(elements[i].substr(0, pos))); std::string key = trim(toLower(elements[i].substr(0, pos)));
std::string value = trim(elements[i].substr(pos+1)); std::string value = trim(elements[i].substr(pos+1));
styles[key] = value; styles.push_back(TStylePair(key, value));
} }
} }
@ -94,7 +94,7 @@ namespace NLGUI
std::vector<ucstring> selectors; std::vector<ucstring> selectors;
NLMISC::explode(selectorString, ucstring(","), selectors); NLMISC::explode(selectorString, ucstring(","), selectors);
TStyle props; TStyleVec props;
props = parseDecls(styleString.toUtf8()); props = parseDecls(styleString.toUtf8());
// duplicate props to each selector in selector list, // duplicate props to each selector in selector list,

@ -102,7 +102,9 @@ namespace NLGUI
} }
else else
{ {
elm.setPseudo(i->PseudoElement, i->Properties); TStyle props;
merge(props, i->Properties);
elm.setPseudo(i->PseudoElement, props);
} }
} }
} }
@ -110,17 +112,18 @@ namespace NLGUI
// style from "style" attribute overrides <style> // style from "style" attribute overrides <style>
if (elm.hasNonEmptyAttribute("style")) if (elm.hasNonEmptyAttribute("style"))
{ {
TStyle styles = CCssParser::parseDecls(elm.getAttribute("style")); TStyleVec styles = CCssParser::parseDecls(elm.getAttribute("style"));
merge(elm.Style, styles); merge(elm.Style, styles);
} }
} }
void CCssStyle::merge(TStyle &dst, const TStyle &src) const void CCssStyle::merge(TStyle &dst, const TStyleVec &src) const
{ {
// TODO: does not use '!important' flag // TODO: does not use '!important' flag
for(TStyle::const_iterator it = src.begin(); it != src.end(); ++it) for(TStyleVec::const_iterator it = src.begin(); it != src.end(); ++it)
{ {
dst[it->first] = it->second; dst[it->first] = it->second;
expandShorthand(it->first, it->second, dst);
} }
} }
@ -312,7 +315,10 @@ namespace NLGUI
// style.StrikeThrough; // text-decoration: line-through; text-decoration-line: line-through; // style.StrikeThrough; // text-decoration: line-through; text-decoration-line: line-through;
void CCssStyle::getStyleParams(const std::string &styleString, CStyleParams &style, const CStyleParams &current) const void CCssStyle::getStyleParams(const std::string &styleString, CStyleParams &style, const CStyleParams &current) const
{ {
TStyle styles = CCssParser::parseDecls(styleString); TStyleVec stylevec = CCssParser::parseDecls(styleString);
TStyle styles;
merge(styles, stylevec);
getStyleParams(styles, style, current); getStyleParams(styles, style, current);
} }
@ -442,10 +448,6 @@ namespace NLGUI
else else
style.DisplayBlock = (it->second == "block" || it->second == "table"); style.DisplayBlock = (it->second == "block" || it->second == "table");
} }
else
{
expandShorthand(it->first, it->second, style.StyleRules);
}
} }
} }

Loading…
Cancel
Save