示例#1
0
文件: SchemaCSS.cs 项目: PavelPZ/REW
 public DomElement(XElement el) {
   name = el.Name.LocalName;
   cls = el.AttributeValue("class");
   id = el.AttributeValue("id");
   if (name != typeof(body).Name) parent = new DomElement(el.Parent);
   uniqueId = cnt++.ToString();
   this.el = el;
 }
示例#2
0
文件: SchemaCSS.cs 项目: PavelPZ/REW
    public static void applyCSS(ref body pg, CourseMeta.ex ex) {
      //all styles (LM and CSS) from tags
      foreach (var tg in pg.scan()) {
        var hasSheet = !string.IsNullOrEmpty(tg.styleSheet);
        if (hasSheet) pg.bodyStyle += "" + CSS.prefixTagId(tg.id, tg.styleSheet);
        tg.styleSheet = null;
      }
      //all styles (LM and CSS) from parents
      CourseMeta.data p = ex; StringBuilder sb = new StringBuilder(pg.bodyStyle);
      while (p != null) { if (!string.IsNullOrEmpty(p.styleSheet)) sb.Insert(0, p.styleSheet.Trim() + " "); p = p.parent; }
      sb.Insert(0, CourseMeta.ex.stdStyle);
      pg.bodyStyle = sb.ToString();

      //separate LM a CSS styles
      rules lmCss = separateLM_CSS_styles(pg);
      if (lmCss == null) return;

      //apply LM styles to XML
      XElement root = pg.ToElement();
      var bodyTags = root.Element("body").DescendantsAndSelf();
      foreach (var ctrl in bodyTags.Where(c => Lib.courseModelJsonMLMeta.allCSControls.Contains(c.Name.LocalName))) {
        var html = new DomElement(ctrl); var typeMeta = Lib.courseModelJsonMLMeta.types[ctrl.Name.LocalName];
        var attrsSpecs = new Dictionary<string, specificity>(); //nejlepsi hodnoty pro kazdou property
        for (var i = lmCss.lmRules.Count - 1; i >= 0; i--) { //jednotilve rule
          var spec = lmCss.lmRules[i].data.matchesSelectors(html, null); if (spec == null) continue;
          foreach (var prop in lmCss.lmRules[i].props.Where(pr => typeMeta.allProps.ContainsKey(pr.name))) { //jednotlive rule properties
            specificity sp;
            if (!attrsSpecs.TryGetValue(prop.name, out sp) || sp.I_am_worse(spec, prop.important))
              attrsSpecs[prop.name] = new specificity(spec, prop.important, prop.value); //nova nebo lepsi value
          }
        }
        //dosad values do XML
        foreach (var kv in attrsSpecs) { var at = ctrl.Attribute(kv.Key); if (at != null) continue; ctrl.Add(new XAttribute(kv.Key, kv.Value.value)); }
      }

      //zpracuj cssTagIds properties
      //foreach (var attr in bodyTags.SelectMany(el => el.Attributes()).Where(a => Lib.courseModelJsonMLMeta.allCssTagIds.Contains(a.Parent.Name.LocalName + "." + a.Name.LocalName))) {
      //  attr.Value = cssTagRx.Replace(attr.Value, m => {
      //    var ruleTxt = m.Groups["selector"].Value + " {}";
      //    var rule = CssDataFactory.parseStylesheet(ruleTxt).getRulesets(CssDataFactory.defaultMedium).First();
      //    return bodyTags.Where(el => rule.matchesSelectors(new DomElement(el), null) != null).Select(el => "#" + el.AttributeValue("id")).Where(id => id != "#").Aggregate((r, i) => r + "|" + i);
      //  });
      //}

      //body from XML
      pg = tag.FromElement<body>(root);
    }