Пример #1
0
        /*
        move presentation attribs from body to style element

        background="foo" ->  body { background-image: url(foo) }
        bgcolor="foo"    ->  body { background-color: foo }
        text="foo"       ->  body { color: foo }
        link="foo"       ->  :link { color: foo }
        vlink="foo"      ->  :visited { color: foo }
        alink="foo"      ->  :active { color: foo }
        */
        private void CleanBodyAttrs(Lexer lexer, Node body)
        {
            AttVal attr;
            string bgurl = null;
            string bgcolor = null;
            string color = null;

            attr = body.GetAttrByName("background");

            if (attr != null)
            {
                bgurl = attr.Val;
                attr.Val = null;
                body.RemoveAttribute(attr);
            }

            attr = body.GetAttrByName("bgcolor");

            if (attr != null)
            {
                bgcolor = attr.Val;
                attr.Val = null;
                body.RemoveAttribute(attr);
            }

            attr = body.GetAttrByName("text");

            if (attr != null)
            {
                color = attr.Val;
                attr.Val = null;
                body.RemoveAttribute(attr);
            }

            if (bgurl != null || bgcolor != null || color != null)
            {
                lexer.AddStringLiteral(" body {\n");

                if (bgurl != null)
                {
                    lexer.AddStringLiteral("  background-image: url(");
                    lexer.AddStringLiteral(bgurl);
                    lexer.AddStringLiteral(");\n");
                }

                if (bgcolor != null)
                {
                    lexer.AddStringLiteral("  background-color: ");
                    lexer.AddStringLiteral(bgcolor);
                    lexer.AddStringLiteral(";\n");
                }

                if (color != null)
                {
                    lexer.AddStringLiteral("  color: ");
                    lexer.AddStringLiteral(color);
                    lexer.AddStringLiteral(";\n");
                }

                lexer.AddStringLiteral(" }\n");
            }

            attr = body.GetAttrByName("link");

            if (attr != null)
            {
                AddColorRule(lexer, " :link", attr.Val);
                body.RemoveAttribute(attr);
            }

            attr = body.GetAttrByName("vlink");

            if (attr != null)
            {
                AddColorRule(lexer, " :visited", attr.Val);
                body.RemoveAttribute(attr);
            }

            attr = body.GetAttrByName("alink");

            if (attr != null)
            {
                AddColorRule(lexer, " :active", attr.Val);
                body.RemoveAttribute(attr);
            }
        }
Пример #2
0
        /*
        Find style attribute in node, and replace it
        by corresponding class attribute. Search for
        class in style dictionary otherwise gensym
        new class and add to dictionary.

        Assumes that node doesn't have a class attribute
        */
        private void Style2Rule(Lexer lexer, Node node)
        {
            AttVal styleattr, classattr;
            string classname;

            styleattr = node.GetAttrByName("style");

            if (styleattr != null)
            {
                classname = FindStyle(lexer, node.Element, styleattr.Val);
                classattr = node.GetAttrByName("class");

                /*
                if there already is a class attribute
                then append class name after a space
                */
                if (classattr != null)
                {
                    classattr.Val = classattr.Val + " " + classname;
                    node.RemoveAttribute(styleattr);
                }
                else
                {
                    /* reuse style attribute for class attribute */
                    styleattr.Attribute = "class";
                    styleattr.Val = classname;
                }
            }
        }
    public virtual void CleanImgAttrs(Node node)
    {
      while (node != null)
      {
        if (node.Tag == _tt.TagImg)
        {
          string style = string.Empty;

          AttVal attr = node.GetAttrByName("border");
          if (attr != null)
          {
            style += "border-width:" + attr.Val + ";";
            attr.Val = null;
            node.RemoveAttribute(attr);
          }

          attr = node.GetAttrByName("align");
          if (attr != null)
          {
            style += "float:" + attr.Val + ";";
            attr.Val = null;
            node.RemoveAttribute(attr);
          }

          attr = node.GetAttrByName("height");
          if (attr != null)
          {
            style += "height:" + attr.Val + ";";
            attr.Val = null;
            node.RemoveAttribute(attr);
          }

          attr = node.GetAttrByName("width");
          if (attr != null)
          {
            style += "width:" + attr.Val + ";";
            attr.Val = null;
            node.RemoveAttribute(attr);
          }

          //xhtml requires the alt attribute
          attr = node.GetAttrByName("alt");
          if (attr == null)
          {
            node.AddAttribute("alt", string.Empty);
          }

          attr = node.GetAttrByName("style");
          if (style.Length > 0)
          {
            if (attr != null && !string.IsNullOrEmpty(attr.Val))
            {
              style = style + attr.Val;
              attr.Val = null;
              node.RemoveAttribute(attr);
            }
            node.AddAttribute("style", style);
          } 
        }

        if (node.Content != null)
        {
          CleanImgAttrs(node.Content);
        }

        node = node.Next;
      }
    }
    public virtual void CleanTableAttrs(Node node)
    {
			while (node != null)
			{
				if (node.Tag == _tt.TagTr || node.Tag == _tt.TagTh || node.Tag == _tt.TagTd) //TODO: add tbody?
				{

      AttVal attr;
      string bgurl = null;
      string bgcolor = null;
      string height = null;
      string width = null;
      

      attr = node.GetAttrByName("background");

      if (attr != null)
      {
        bgurl = attr.Val;
        attr.Val = null;
        node.RemoveAttribute(attr);
      }

      attr = node.GetAttrByName("bgcolor");

      if (attr != null)
      {
        bgcolor = attr.Val;
        attr.Val = null;
        node.RemoveAttribute(attr);
      }

      attr = node.GetAttrByName("width");

      if (attr != null)
      {
        width = attr.Val;
        attr.Val = null;
        node.RemoveAttribute(attr);
      }

      attr = node.GetAttrByName("height");

      if (attr != null)
      {
        height = attr.Val;
        attr.Val = null;
        node.RemoveAttribute(attr);
      }

      if (bgurl != null || bgcolor != null || width != null || height != null)
      {
        string style = string.Empty;

        if (bgurl != null)
        {
          style += "background-image:url(" + bgurl + ");";
        }

        if (bgcolor != null)
        {
          style += "background-color:"+ bgcolor +";";
        }

        if (width != null)
        {
          style += "width:" + width + ";";
        }

        if (height != null)
        {
          style += "height:" + height + ";";
        }

        node.AddAttribute("style", style);
      }
        }

        if (node.Content != null)
        {
          CleanTableAttrs(node.Content);
        }
				
        node = node.Next;
      }
    }
    public virtual void CleanAnchorTarget(Node node)
    {
      while (node != null)
      {
        if (node.Tag == _tt.TagA)
        {
          AttVal attr = node.GetAttrByName("target");
          if (attr != null)
          {
            if (attr.Val.ToLower() == "_blank")
            {
              AttVal rel = node.GetAttrByName("rel");
              if (rel == null) node.AddAttribute("rel", "external");
              else rel.Val += " external";
            }
            node.RemoveAttribute(attr);
          }
        }

        if (node.Content != null)
        {
          CleanAnchorTarget(node.Content);
        }

        node = node.Next;
      }
    }