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
Circle boundaries heads/master
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> pseudo_class_patterns = new Gee.ArrayList<SelectorPattern> (); 25 Gee.ArrayList<SelectorPattern> id_patterns = new Gee.ArrayList<SelectorPattern> (); 26 27 public SvgStyle style { get; set; } 28 29 public Selector (string pattern, SvgStyle style) { 30 string[] selector_patterns = pattern.split (","); 31 32 for (int i = 0; i < selector_patterns.length; i++) { 33 SelectorPattern p = new SelectorPattern (selector_patterns[i]); 34 35 if (p.has_id ()) { 36 SelectorPattern id = p.get_id_patterns (); 37 id_patterns.add (id); 38 } 39 40 if (p.has_class ()) { 41 SelectorPattern css_class = p.get_class_patterns (); 42 class_patterns.add (css_class); 43 } 44 45 if (p.has_pseudo_class ()) { 46 SelectorPattern pseudo_class = p.get_pseudo_class_patterns (); 47 pseudo_class_patterns.add (pseudo_class); 48 } 49 50 tag_patterns.add (p); 51 } 52 53 this.style = style; 54 } 55 56 public Selector.copy_constructor (Selector selector) { 57 style = selector.style.copy (); 58 59 foreach (SelectorPattern pattern in selector.tag_patterns) { 60 tag_patterns.add (pattern.copy ()); 61 } 62 63 foreach (SelectorPattern pattern in selector.class_patterns) { 64 class_patterns.add (pattern.copy ()); 65 } 66 67 foreach (SelectorPattern pattern in selector.id_patterns) { 68 id_patterns.add (pattern.copy ()); 69 } 70 71 foreach (SelectorPattern pattern in selector.pseudo_class_patterns) { 72 pseudo_class_patterns.add (pattern.copy ()); 73 } 74 } 75 76 public string to_string () { 77 StringBuilder s = new StringBuilder (); 78 79 foreach (SelectorPattern pattern in tag_patterns) { 80 if (s.str != "") { 81 s.append (", "); 82 } 83 84 s.append (pattern.to_string ()); 85 } 86 87 return s.str; 88 } 89 90 public Selector copy () { 91 return new Selector.copy_constructor (this); 92 } 93 94 public bool match_tag (XmlElement tag, string? id, string? css_class, string? pseudo_class) { 95 foreach (SelectorPattern pattern in tag_patterns) { 96 if (pattern.match (tag, id, css_class, pseudo_class)) { 97 return true; 98 } 99 } 100 101 return false; 102 } 103 104 public bool match_id (XmlElement tag, string? id, string? css_class, string? pseudo_class) { 105 foreach (SelectorPattern pattern in id_patterns) { 106 if (pattern.match (tag, id, css_class, pseudo_class)) { 107 return true; 108 } 109 } 110 111 return false; 112 } 113 114 public bool match_class (XmlElement tag, string? id, string? css_class, string? pseudo_class) { 115 foreach (SelectorPattern pattern in class_patterns) { 116 if (pattern.match (tag, id, css_class, pseudo_class)) { 117 return true; 118 } 119 } 120 121 return false; 122 } 123 124 public bool match_pseudo_class (XmlElement tag, string? id, string? css_class, string? pseudo_class) { 125 foreach (SelectorPattern pattern in pseudo_class_patterns) { 126 if (pattern.match (tag, id, css_class, pseudo_class)) { 127 return true; 128 } 129 } 130 131 return false; 132 } 133 } 134 135 } 136