diff options
-rw-r--r-- | src/css.cc | 76 | ||||
-rw-r--r-- | src/css.hh | 34 | ||||
-rw-r--r-- | src/cssparser.cc | 75 | ||||
-rw-r--r-- | src/cssparser.hh | 10 | ||||
-rw-r--r-- | src/styleengine.cc | 15 |
5 files changed, 127 insertions, 83 deletions
@@ -18,9 +18,8 @@ using namespace dw::core::style; -/** \todo dummy only */ -CssPropertyList *CssPropertyList::parse (const char *buf) { - return NULL; +void CssProperty::print () { + fprintf (stderr, "%s - %d\n", Css_property_info[name].symbol, value.intVal); } void CssPropertyList::set (CssProperty::Name name, CssProperty::Value value) { @@ -40,9 +39,9 @@ void CssPropertyList::apply (CssPropertyList *props) { props->set (getRef (i)->name, getRef (i)->value); } -/** \todo dummy only */ -CssSelector *CssSelector::parse (const char *buf) { - return NULL; +void CssPropertyList::print () { + for (int i = 0; i < size (); i++) + getRef (i)->print (); } /** \todo implement all selection option CSS offers */ @@ -63,17 +62,31 @@ bool CssSelector::match (Doctree *docTree) { return true; } +void CssSelector::print () { + fprintf (stderr, "Element %d, class %s, pseudo %s, id %s\n", + element, klass, pseudo, id); +} + void CssRule::apply (CssPropertyList *props, Doctree *docTree) { if (selector->match (docTree)) this->props->apply (props); } -void CssStyleSheet::addRule (CssSelector *selector, CssPropertyList *props) { - CssRule *rule = new CssRule (selector, props); +void CssRule::print () { + selector->print (); + props->print (); +} + +void CssStyleSheet::addRule (CssRule *rule) { increase (); set (size () - 1, rule); } +void CssStyleSheet::addRule (CssSelector *selector, CssPropertyList *props) { + CssRule *rule = new CssRule (selector, props); + addRule (rule); +} + void CssStyleSheet::apply (CssPropertyList *props, Doctree *docTree) { for (int i = 0; i < size (); i++) get (i)->apply (props, docTree); @@ -84,13 +97,20 @@ CssStyleSheet *CssContext::userStyle; CssStyleSheet *CssContext::userImportantStyle; CssContext::CssContext () { - for (int o = CSS_PRIMARY_USER_AGENT; o <= CSS_PRIMARY_USER_IMPORTANT; o++) + for (int o = CSS_PRIMARY_USER_AGENT; o < CSS_PRIMARY_LAST; o++) sheet[o] = NULL; if (userAgentStyle == NULL) { - userAgentStyle = buildUserAgentStyle (); - userStyle = buildUserStyle (false); - userImportantStyle = buildUserStyle (true); + userAgentStyle = new CssStyleSheet (); + userStyle = new CssStyleSheet (); + userImportantStyle = new CssStyleSheet (); + + sheet[CSS_PRIMARY_USER_AGENT] = userAgentStyle; + sheet[CSS_PRIMARY_USER] = userStyle; + sheet[CSS_PRIMARY_USER_IMPORTANT] = userImportantStyle; + + buildUserAgentStyle (); + buildUserStyle (); } sheet[CSS_PRIMARY_USER_AGENT] = userAgentStyle; @@ -116,18 +136,28 @@ void CssContext::apply (CssPropertyList *props, Doctree *docTree, tagStyle->apply (props); } -CssStyleSheet * CssContext::buildUserAgentStyle () { - char *cssBuf = - "body {background: 0xdcd1ba; font-family: DejaVu Sans; color: black; margin-left: 5; \ - margin-top: 5; margin-bottom: 5; margin-right: 5; } \ - :link {color: blue; textdecoration: underline; cursor: pointer; } \ - :visited {color: green; textdecoration: underline; cursor: pointer; } \ - b, strong {fontweight: bolder; }"; +void CssContext::addRule (CssRule *rule, CssPrimaryOrder order) { + if (sheet[order] == NULL) + sheet[order] = new CssStyleSheet (); - a_Css_parse (this, cssBuf, strlen (cssBuf), 0, CSS_ORIGIN_USER_AGENT); + sheet[order]->addRule (rule); + fprintf(stderr, "Adding Rule (%d)\n", order); + rule->print (); +} +void CssContext::buildUserAgentStyle () { + char *cssBuf = + "body {background-color: 0xdcd1ba; font-family: helvetica; color: black; margin-left: 5px; \ + margin-top: 5px; margin-bottom: 5px; margin-right: 5px; } \ + :link {color: blue; text-decoration: underline; cursor: pointer; } \ + :visited {color: green; text-decoration: underline; cursor: pointer; } \ + b, strong {font-weight: bolder; } \ + h1 {font-size: 20em;} "; + a_Css_parse (this, cssBuf, strlen (cssBuf), 0, CSS_ORIGIN_USER_AGENT); + +#if 0 CssStyleSheet *s = new CssStyleSheet (); CssPropertyList *props; @@ -210,9 +240,12 @@ CssStyleSheet * CssContext::buildUserAgentStyle () { s->addRule (new CssSelector(a_Html_tag_index("td")), props); return s; +#endif } -CssStyleSheet * CssContext::buildUserStyle (bool important) { +void CssContext::buildUserStyle () { + +#if 0 CssStyleSheet *s = new CssStyleSheet (); CssPropertyList *props; @@ -243,4 +276,5 @@ CssStyleSheet * CssContext::buildUserStyle (bool important) { } return s; +#endif } @@ -4,6 +4,16 @@ #include "dw/core.hh" #include "doctree.hh" +/* Origin and weight. Used only internally.*/ +typedef enum { + CSS_PRIMARY_USER_AGENT, + CSS_PRIMARY_USER, + CSS_PRIMARY_AUTHOR, + CSS_PRIMARY_AUTHOR_IMPORTANT, + CSS_PRIMARY_USER_IMPORTANT, + CSS_PRIMARY_LAST, +} CssPrimaryOrder; + class CssProperty { public: typedef union { @@ -120,6 +130,8 @@ class CssProperty { Name name; Value value; + + void print (); }; class CssPropertyList : public lout::misc::SimpleVector <CssProperty> { @@ -133,7 +145,6 @@ class CssPropertyList : public lout::misc::SimpleVector <CssProperty> { refCount = 0; }; - static CssPropertyList *parse (const char *buf); void set (CssProperty::Name name, CssProperty::Value value); void set (CssProperty::Name name, const char *value) { CssProperty::Value v; @@ -146,6 +157,7 @@ class CssPropertyList : public lout::misc::SimpleVector <CssProperty> { set (name, v); }; void apply (CssPropertyList *props); + void print (); inline void ref () { refCount++; } inline void unref () { if(--refCount == 0) delete this; } @@ -166,9 +178,8 @@ class CssSelector { this->id = id; }; - static CssSelector *parse (const char *buf); - bool match (Doctree *dt); + void print (); }; class CssRule { @@ -184,39 +195,32 @@ class CssRule { ~CssRule (); void apply (CssPropertyList *props, Doctree *docTree); + void print (); }; class CssStyleSheet : lout::misc::SimpleVector <CssRule*> { public: CssStyleSheet() : lout::misc::SimpleVector <CssRule*> (1) {}; + void addRule (CssRule *rule); void addRule (CssSelector *selector, CssPropertyList *props); void apply (CssPropertyList *props, Doctree *docTree); }; class CssContext { - public: - typedef enum { - CSS_PRIMARY_USER_AGENT, - CSS_PRIMARY_USER, - CSS_PRIMARY_AUTHOR, - CSS_PRIMARY_AUTHOR_IMPORTANT, - CSS_PRIMARY_USER_IMPORTANT - } PrimaryOrder; - private: static CssStyleSheet *userAgentStyle; static CssStyleSheet *userStyle; static CssStyleSheet *userImportantStyle; CssStyleSheet *sheet[CSS_PRIMARY_USER_IMPORTANT + 1]; - CssStyleSheet *buildUserAgentStyle (); - CssStyleSheet *buildUserStyle (bool important); + void buildUserAgentStyle (); + void buildUserStyle (); public: CssContext (); ~CssContext (); - void addRule (CssRule *rule, PrimaryOrder order); + void addRule (CssRule *rule, CssPrimaryOrder order); void apply (CssPropertyList *props, Doctree *docTree, CssPropertyList *tagStyle, CssPropertyList *nonCss); diff --git a/src/cssparser.cc b/src/cssparser.cc index f9f3168b..eaf3ecfe 100644 --- a/src/cssparser.cc +++ b/src/cssparser.cc @@ -621,7 +621,7 @@ static bool Css_parse_value (CssParser *parser, val->intVal = CSS_CREATE_LENGTH (fval, lentype); } else if (parser->ttype == CSS_TK_SYMBOL && strcmp (parser->tval, "auto") == 0) { - val->intVal = CSS_LENGTH_TYPE_AUTO; + val->intVal = LENGTH_AUTO; } break; @@ -727,25 +727,8 @@ static int Css_shorthand_info_cmp (const void *a, const void *b) ((CssShorthandInfo*)b)->symbol); } - -static void Css_add_declarations ( - CssContext *context, - lout::misc::SimpleVector <CssSelector*> *selectors, - CssProperty::Name prop, - CssProperty::Value val, - int order_count, - CssOrigin origin, - bool weight) { - - for (int i = 0; i < selectors->size (); i++) { - CssSelector *s = selectors->get (i); - fprintf (stderr, "elem %d, class %s, pseudo %s, id %s -> %s, %d\n", - s->element, s->klass, s->pseudo, s->id, Css_property_info[prop].symbol, val.intVal); - } -} - static void Css_parse_declaration (CssParser *parser, - lout::misc::SimpleVector <CssSelector*> *selectors) + CssPropertyList *props, CssPropertyList *importantProps) { CssPropertyInfo pi, *pip; CssShorthandInfo si, *sip; @@ -771,9 +754,10 @@ static void Css_parse_declaration (CssParser *parser, Css_next_token (parser); if (Css_parse_value (parser, prop, &val)) { weight = Css_parse_weight (parser); - Css_add_declarations (parser->context, selectors, prop, val, - parser->order_count, parser->origin, - weight); + if (weight) + importantProps->set (prop, val); + else + props->set (prop, val); } } } else { @@ -807,11 +791,14 @@ static void Css_parse_declaration (CssParser *parser, Css_shorthand_info[sh_index] .properties[i], &val)) { weight = Css_parse_weight (parser); - Css_add_declarations ( - parser->context, selectors, - Css_shorthand_info[sh_index].properties[i], - val, parser->order_count, parser->origin, - weight); + if (weight) + importantProps->set ( + Css_shorthand_info[sh_index].properties[i], + val); + else + props->set ( + Css_shorthand_info[sh_index].properties[i], + val); } } } while (found); @@ -836,12 +823,14 @@ static void Css_parse_declaration (CssParser *parser, weight = Css_parse_weight (parser); if (n > 0) { for (i = 0; i < 4; i++) - Css_add_declarations (parser->context, selectors, - Css_shorthand_info[sh_index] + if (weight) + importantProps->set (Css_shorthand_info[sh_index] + .properties[i], + dir_vals[dir_set[n - 1][i]]); + else + props->set (Css_shorthand_info[sh_index] .properties[i], - dir_vals[dir_set[n - 1][i]], - parser->order_count, - parser->origin, weight); + dir_vals[dir_set[n - 1][i]]); } else MSG_CSS("no values for shorthand proprerty '%s'\n", Css_shorthand_info[sh_index].symbol); @@ -874,6 +863,7 @@ static void Css_parse_declaration (CssParser *parser, static void Css_parse_ruleset (CssParser *parser) { lout::misc::SimpleVector <CssSelector*> *list; + CssPropertyList *props, *importantProps; CssSelector *selector; const char *p, **pp; @@ -964,20 +954,35 @@ static void Css_parse_ruleset (CssParser *parser) } DEBUG_MSG (DEBUG_PARSE_LEVEL, "end of %s\n", "selectors"); + + props = new CssPropertyList (); + importantProps = new CssPropertyList (); /* Read block. ('{' has already been read.) */ if (parser->ttype != CSS_TK_END) { parser->within_block = true; Css_next_token (parser); do - Css_parse_declaration (parser, list); + Css_parse_declaration (parser, props, importantProps); while (!(parser->ttype == CSS_TK_END || (parser->ttype == CSS_TK_CHAR && parser->tval[0] == '}'))); parser->within_block = false; } - for (int i = 0; i < list->size (); i++) - delete list->get (i); + for (int i = 0; i < list->size (); i++) { + CssSelector *s = list->get (i); + + if (parser->origin == CSS_ORIGIN_USER_AGENT) { + parser->context->addRule (new CssRule (s, props), CSS_PRIMARY_USER_AGENT); + } else if (parser->origin == CSS_ORIGIN_USER) { + parser->context->addRule (new CssRule (s, props), CSS_PRIMARY_USER); + parser->context->addRule (new CssRule (s, importantProps), CSS_PRIMARY_USER_IMPORTANT); + } else if (parser->origin == CSS_ORIGIN_AUTHOR) { + parser->context->addRule (new CssRule (s, props), CSS_PRIMARY_AUTHOR); + parser->context->addRule (new CssRule (s, importantProps), CSS_PRIMARY_AUTHOR_IMPORTANT); + } + } + delete list; if (parser->ttype == CSS_TK_CHAR && parser->tval[0] == '}') diff --git a/src/cssparser.hh b/src/cssparser.hh index 4403a4b9..c5b69da0 100644 --- a/src/cssparser.hh +++ b/src/cssparser.hh @@ -40,16 +40,6 @@ typedef enum { CSS_ORIGIN_AUTHOR, } CssOrigin; -/* Origin and weight. Used only internally.*/ -typedef enum { - CSS_PRIMARY_USER_IMPORTANT, - CSS_PRIMARY_AUTHOR_IMPORTANT, - CSS_PRIMARY_AUTHOR, - CSS_PRIMARY_USER, - CSS_PRIMARY_USER_AGENT, - CSS_PRIMARY_LAST, -} CssPrimaryOrder; - typedef struct { char *symbol; CssValueType type; diff --git a/src/styleengine.cc b/src/styleengine.cc index 50cd6570..76f23777 100644 --- a/src/styleengine.cc +++ b/src/styleengine.cc @@ -168,6 +168,18 @@ void StyleEngine::apply (StyleAttrs *attrs, CssPropertyList *props) { case CssProperty::CSS_PROPERTY_MARGIN: attrs->margin.setVal (p->value.intVal); break; + case CssProperty::CSS_PROPERTY_MARGIN_BOTTOM: + attrs->margin.bottom = p->value.intVal; + break; + case CssProperty::CSS_PROPERTY_MARGIN_LEFT: + attrs->margin.left = p->value.intVal; + break; + case CssProperty::CSS_PROPERTY_MARGIN_RIGHT: + attrs->margin.right = p->value.intVal; + break; + case CssProperty::CSS_PROPERTY_MARGIN_TOP: + attrs->margin.top = p->value.intVal; + break; case CssProperty::CSS_PROPERTY_PADDING: attrs->padding.setVal (p->value.intVal); break; @@ -205,8 +217,7 @@ void StyleEngine::apply (StyleAttrs *attrs, CssPropertyList *props) { */ Style * StyleEngine::style0 (CssPropertyList *nonCssProperties) { CssPropertyList props; - CssPropertyList *tagStyleProps = CssPropertyList::parse ( - stack->getRef (stack->size () - 1)->styleAttribute); + CssPropertyList *tagStyleProps = NULL; /** \todo implementation */ // get previous style from the stack StyleAttrs attrs = *stack->getRef (stack->size () - 2)->style; |