changeset 503:00b31843839b

implement various apply() methods
author Johannes Hofmann <Johannes.Hofmann@gmx.de>
date Wed, 29 Oct 2008 16:44:28 +0100
parents e1ff5d59c338
children 5776ac0306cc
files src/css.cc src/css.hh src/doctree.hh src/styleengine.cc src/styleengine.hh
diffstat 5 files changed, 80 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/css.cc	Tue Oct 28 21:28:57 2008 +0100
+++ b/src/css.cc	Wed Oct 29 16:44:28 2008 +0100
@@ -20,3 +20,44 @@
          break;
    }
 }
+
+void CssPropertyList::apply (dw::core::style::StyleAttrs *styleAttrs) {
+   for (int i = 0; i < size (); i++)
+      get (i)->apply (styleAttrs);
+}
+
+bool CssSelector::match (Doctree *docTree) {
+   return tagIndex < 0 || tagIndex == docTree->top ()->tagIndex;
+}
+
+void CssRule::apply (dw::core::style::StyleAttrs *styleAttrs,
+   Doctree *docTree) {
+
+   if (selector->match (docTree))
+      props->apply (styleAttrs);
+}
+
+void CssStyleSheet::apply (dw::core::style::StyleAttrs *styleAttrs,
+   Doctree *docTree) {
+
+   for (int i = 0; i < size (); i++)
+      get (i)->apply (styleAttrs, docTree);
+}
+
+void CssContext::addRule (CssRule *rule, PrimaryOrder order) {
+   sheet[order].increase ();
+   sheet[order].set (sheet[order].size () - 1, rule);
+};
+
+void CssContext::apply (dw::core::style::StyleAttrs *styleAttrs,
+         Doctree *docTree,
+         CssPropertyList *tagStyle, CssPropertyList *nonCss) {
+
+   sheet[USER_AGENT].apply (styleAttrs, docTree);
+   if (nonCss)
+        nonCss->apply (styleAttrs);
+   for (int o = USER; o <= USER_IMPORTANT; o++)
+      sheet[o].apply (styleAttrs, docTree);
+   if (tagStyle)
+        nonCss->apply (styleAttrs);
+}
--- a/src/css.hh	Tue Oct 28 21:28:57 2008 +0100
+++ b/src/css.hh	Wed Oct 29 16:44:28 2008 +0100
@@ -152,43 +152,33 @@
       };
       ~CssRule ();
 
-      void apply (dw::core::style::StyleAttrs *styleAttr);
+      void apply (dw::core::style::StyleAttrs *styleAttrs, Doctree *docTree);
 };
 
 class CssStyleSheet : public lout::misc::SimpleVector <CssRule*> {
    public:
       CssStyleSheet() : lout::misc::SimpleVector <CssRule*> (1) {};
-      void apply (dw::core::style::StyleAttrs *styleAttr);
+      void apply (dw::core::style::StyleAttrs *styleAttrs, Doctree *docTree);
 };
 
-typedef enum {
-   CSS_PRIMARY_USER_IMPORTANT,
-   CSS_PRIMARY_AUTHOR_IMPORTANT,
-   CSS_PRIMARY_AUTHOR,
-   CSS_PRIMARY_USER,
-   CSS_PRIMARY_USER_AGENT
-} CssPrimaryOrder;
-
 class CssContext {
    public:
       typedef enum {
-         USER_IMPORTANT,
+         USER_AGENT,
+         USER,
+         AUTHOR,
          AUTHOR_IMPORTANT,
-         AUTHOR,
-         USER,
-         USER_AGENT
+         USER_IMPORTANT
       } PrimaryOrder;
 
    private:
-      CssStyleSheet sheet[USER_AGENT + 1];
+      CssStyleSheet sheet[USER_IMPORTANT + 1];
 
    public:
-      void addRule (CssRule *rule, PrimaryOrder order) {
-         sheet[order].increase ();
-         sheet[order].set (sheet[order].size () - 1, rule);
-      };
-
-      void apply (dw::core::style::StyleAttrs *styleAttr);
+      void addRule (CssRule *rule, PrimaryOrder order);
+      void apply (dw::core::style::StyleAttrs *styleAttrs,
+         Doctree *docTree,
+         CssPropertyList *tagStyle, CssPropertyList *nonCss);
 };
 
 #endif
--- a/src/doctree.hh	Tue Oct 28 21:28:57 2008 +0100
+++ b/src/doctree.hh	Wed Oct 29 16:44:28 2008 +0100
@@ -2,10 +2,8 @@
 #define __DOCTREE_HH__
 
 class DoctreeNode {
-   private:
-      int index;
-
    public:
+      int depth;
       int tagIndex;
       const char *klass;
       const char *id;
@@ -13,7 +11,7 @@
 
 class Doctree {
    public:
-      virtual ~Doctree () = 0;
+      virtual ~Doctree () {};
       virtual const DoctreeNode *top () = 0;
       virtual const DoctreeNode *parent (const DoctreeNode *node) = 0;
 };
--- a/src/styleengine.cc	Tue Oct 28 21:28:57 2008 +0100
+++ b/src/styleengine.cc	Wed Oct 29 16:44:28 2008 +0100
@@ -13,9 +13,11 @@
 #include "styleengine.hh"
 
 StyleEngine::StyleEngine () {
+   stack = new lout::misc::SimpleVector <Node> (1);
 }
 
 StyleEngine::~StyleEngine () {
+   delete stack;
 }
 
 void
--- a/src/styleengine.hh	Tue Oct 28 21:28:57 2008 +0100
+++ b/src/styleengine.hh	Wed Oct 29 16:44:28 2008 +0100
@@ -4,17 +4,38 @@
 #include "dw/core.hh"
 #include "doctree.hh"
 
-class StyleEngine {
+class StyleEngine : public Doctree {
    private:
-      dw::core::style::Style *currentStyle;
+      class Node : public DoctreeNode {
+         public:
+            dw::core::style::Style *style;
+
+
+      };
+
+      lout::misc::SimpleVector <Node> *stack;
 
    public:
       StyleEngine ();
       ~StyleEngine ();
+   
+      /* Doctree interface */
+      const DoctreeNode *top () {
+         return stack->getRef (stack->size () - 1);
+      };
+      const DoctreeNode *parent (const DoctreeNode *n) {
+         if (n->depth > 0)
+            return stack->getRef (n->depth - 1);
+         else
+            return NULL;
+      };
 
       void startElement (int tag, const char *id, const char *klass, const char *style);
       void endElement (int tag);
-      inline dw::core::style::Style *style () { return currentStyle; };
+
+      inline dw::core::style::Style *style () {
+         return stack->getRef (stack->size () - 1)->style;
+      };
 };
 
 #endif