The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Selector.vala in libsvgbird

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libsvgbird/Selector.vala.
Apply css styles
1 /* 2 Copyright (C) 2016 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using B; 16 using Math; 17 18 namespace SvgBird { 19 20 public class Selector : GLib.Object { 21 22 Gee.ArrayList<SelectorPattern> tag_patterns = new Gee.ArrayList<SelectorPattern> (); 23 Gee.ArrayList<SelectorPattern> class_patterns = new Gee.ArrayList<SelectorPattern> (); 24 Gee.ArrayList<SelectorPattern> id_patterns = new Gee.ArrayList<SelectorPattern> (); 25 26 public SvgStyle style { get; set; } 27 28 public Selector (string pattern, SvgStyle style) { 29 string[] selector_patterns = pattern.split (","); 30 31 for (int i = 0; i < selector_patterns.length; i++) { 32 SelectorPattern p = new SelectorPattern (selector_patterns[i]); 33 34 if (p.has_id ()) { 35 id_patterns.add (p); 36 } 37 38 if (p.has_class ()) { 39 class_patterns.add (p); 40 } 41 42 tag_patterns.add (p); 43 } 44 45 this.style = style; 46 } 47 48 public Selector.copy_constructor (Selector selector) { 49 style = selector.style.copy (); 50 51 foreach (SelectorPattern pattern in selector.tag_patterns) { 52 tag_patterns.add (pattern.copy ()); 53 } 54 55 foreach (SelectorPattern pattern in selector.class_patterns) { 56 class_patterns.add (pattern.copy ()); 57 } 58 59 foreach (SelectorPattern pattern in selector.id_patterns) { 60 id_patterns.add (pattern.copy ()); 61 } 62 } 63 64 public string to_string () { 65 StringBuilder s = new StringBuilder (); 66 67 foreach (SelectorPattern pattern in tag_patterns) { 68 if (s.str != "") { 69 s.append (", "); 70 } 71 72 s.append (pattern.to_string ()); 73 } 74 75 return s.str; 76 } 77 78 public Selector copy () { 79 return new Selector.copy_constructor (this); 80 } 81 82 public bool match_tag (XmlElement tag, string? id, string? css_class) { 83 foreach (SelectorPattern pattern in tag_patterns) { 84 if (pattern.match (tag, id, css_class)) { 85 return true; 86 } 87 } 88 89 return false; 90 } 91 92 public bool match_id (XmlElement tag, string? id, string? css_class) { 93 foreach (SelectorPattern pattern in id_patterns) { 94 if (pattern.match (tag, id, css_class)) { 95 return true; 96 } 97 } 98 99 return false; 100 } 101 102 public bool match_class (XmlElement tag, string? id, string? css_class) { 103 foreach (SelectorPattern pattern in class_patterns) { 104 if (pattern.match (tag, id, css_class)) { 105 return true; 106 } 107 } 108 109 return false; 110 } 111 } 112 113 } 114