habaki 0.5.1 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb31379da24aff32519428d320bfd20e20445f009c8912cdbbf940822a64a35c
4
- data.tar.gz: ea74632fd8cb95ac348fcfc95463b8aa2cb1ee452f224fdc0b5c4774d276d669
3
+ metadata.gz: 07aac3ef018685ae5ba014eae5369fffc0e74d3a0f4219d7caa005f7d359d14b
4
+ data.tar.gz: 51b5777e6cc9d2b6ead583aa259965d1c78ccfac535214cb058328263b83d2d3
5
5
  SHA512:
6
- metadata.gz: 116dbb8a23a4ec449e2544e9c9f63ce894b80d5e2c3dc5b3393fa6792baf3b616407817f0169cad20409e107942d90b699a79ce3dfa3a3f4e32d0bf4600877d4
7
- data.tar.gz: 844e6cf9e458d98a7458688a3335d4ccecd6b23cd73b798b755b575feda5ba6f1b853ac2abdd5d1cf4145f6e75c41e2ddf69f1794543b32020169d2ae6d6859a
6
+ metadata.gz: 8099f0147a9acf50cea501484328e0b4d737a02daf6c6cce6547b9eb94071fe4e98d4b06c8571c0b635244378531bddf43343ad48d30cbb320876feb77f9552b
7
+ data.tar.gz: 1fad3480648acc3247ffd822d1136dbfa20120c3f402c346200027c21da0bd4868a0f8a4b0e6a6233ae125dc9ac4e1328078e14c2d43e3ea701ab4bd7a83f824
@@ -10,7 +10,7 @@ src/katana-parser/parser.c src/katana-parser/selector.c src/katana-parser/tokeni
10
10
  src/rb_katana_array.c src/rb_katana_selector.c src/rb_katana_declaration.c src/rb_katana_rule.c src/rb_katana.c
11
11
  }
12
12
 
13
- $INCFLAGS << " -I$(srcdir)/src"
13
+ $INCFLAGS << " -std=c99 -I$(srcdir)/src"
14
14
 
15
15
  # add folder, where compiler can search source files
16
16
  $VPATH << "$(srcdir)/src"
@@ -16,6 +16,8 @@
16
16
  #include "src/katana.h"
17
17
  #include "src/selector.h"
18
18
 
19
+ #define UTF8_STR_NEW(val) rb_enc_associate_index(rb_str_new2(val), rb_enc_find_index("UTF-8"))
20
+
19
21
  /* Array */
20
22
 
21
23
  VALUE rb_array_length(VALUE array);
@@ -13,7 +13,7 @@ VALUE rb_declaration_prop(VALUE self)
13
13
  KatanaDeclaration *c_decl;
14
14
  Data_Get_Struct(self, KatanaDeclaration, c_decl);
15
15
  if (c_decl->property)
16
- return rb_str_new2(c_decl->property);
16
+ return UTF8_STR_NEW(c_decl->property);
17
17
  else
18
18
  Qnil;
19
19
  }
@@ -286,10 +286,10 @@ VALUE rb_value_value(VALUE self)
286
286
  val = rb_float_new(c_val->fValue);
287
287
  break;
288
288
  case KATANA_VALUE_IDENT:
289
- val = rb_str_new2(c_val->string);
289
+ val = UTF8_STR_NEW(c_val->string);
290
290
  break;
291
291
  case KATANA_VALUE_STRING:
292
- val = rb_str_new2(c_val->string);
292
+ val = UTF8_STR_NEW(c_val->string);
293
293
  break;
294
294
  case KATANA_VALUE_PARSER_FUNCTION:
295
295
  {
@@ -304,19 +304,19 @@ VALUE rb_value_value(VALUE self)
304
304
  // return katana_stringify_value_list(parser, value->list);
305
305
  break;
306
306
  case KATANA_VALUE_PARSER_HEXCOLOR:
307
- val = rb_str_new2(c_val->string);
307
+ val = UTF8_STR_NEW(c_val->string);
308
308
  break;
309
309
  case KATANA_VALUE_URI:
310
- val = rb_str_new2(c_val->string);
310
+ val = UTF8_STR_NEW(c_val->string);
311
311
  break;
312
312
  case KATANA_VALUE_UNICODE_RANGE:
313
- val = rb_str_new2(c_val->string);
313
+ val = UTF8_STR_NEW(c_val->string);
314
314
  break;
315
315
  case KATANA_VALUE_DIMENSION:
316
- val = rb_str_new2(c_val->string);
316
+ val = UTF8_STR_NEW(c_val->string);
317
317
  break;
318
318
  case KATANA_VALUE_UNKNOWN:
319
- val = rb_str_new2(c_val->string);
319
+ val = UTF8_STR_NEW(c_val->string);
320
320
  break;
321
321
  default:
322
322
  fprintf(stderr, "KATANA: unsupported value unit %d (%s)\n", c_val->unit, c_val->string);
@@ -371,12 +371,6 @@ void init_katana_declaration()
371
371
 
372
372
  rb_define_method(rb_Declaration, "position", rb_declaration_position, 0);
373
373
 
374
- // Declaration
375
- rb_Declaration = rb_define_class_under(rb_Katana, "Declaration", rb_cObject);
376
- rb_define_method(rb_Declaration, "property", rb_declaration_prop, 0);
377
- rb_define_method(rb_Declaration, "important", rb_declaration_important, 0);
378
- rb_define_method(rb_Declaration, "values", rb_declaration_values, 0);
379
-
380
374
  // Value
381
375
  rb_Value = rb_define_class_under(rb_Katana, "Value", rb_cObject);
382
376
  rb_define_method(rb_Value, "value", rb_value_value, 0);
@@ -477,7 +477,7 @@ VALUE rb_selector_data_value(VALUE self)
477
477
  if (c_sel->value)
478
478
  {
479
479
  // force UTF-8
480
- return rb_enc_associate_index(rb_str_new2(c_sel->value), rb_enc_find_index("UTF-8"));
480
+ return UTF8_STR_NEW(c_sel->value);
481
481
  }
482
482
  else
483
483
  return Qnil;
@@ -6,7 +6,7 @@ module Habaki
6
6
 
7
7
  # @param [String] encoding
8
8
  def initialize(encoding = nil)
9
- @encoding
9
+ @encoding = encoding
10
10
  end
11
11
 
12
12
  # @param [Formatter::Base] format
@@ -98,8 +98,8 @@ module Habaki
98
98
  props[match.reference] << match.value
99
99
  end
100
100
 
101
- props.each do |k, v|
102
- new_decl = add_by_property(k, Values.new([v].flatten))
101
+ props.each do |prop, values|
102
+ new_decl = add_by_property(prop, values)
103
103
  new_decl.position = declaration.position
104
104
  end
105
105
 
@@ -122,10 +122,10 @@ module Habaki
122
122
  border_style_properties = %w[border-width border-style border-color]
123
123
 
124
124
  border_style_properties.each do |prop|
125
- create_shorthand_properties! prop unless find_by_property(prop)
125
+ create_shorthand_properties! prop unless has_property?(prop)
126
126
  end
127
127
 
128
- create_shorthand_properties! 'border' if border_style_properties.map{|prop| find_by_property(prop)}.all?
128
+ create_shorthand_properties! 'border' if border_style_properties.map{|prop| has_property?(prop)}.all?
129
129
  end
130
130
 
131
131
  # Looks for long format CSS background properties (e.g. <tt>background-color</tt>) and
@@ -159,15 +159,37 @@ module Habaki
159
159
  create_shorthand_properties! 'list-style'
160
160
  end
161
161
 
162
+ def self.shorthand_properties(shorthand_property)
163
+ nodes = []
164
+ FormalSyntax::Tree.tree.property("--shorthand-"+shorthand_property).traverse do |node|
165
+ nodes << {type: node.type, value: node.value, next_value: node.parent&.children&.last&.value} if [:ref, :token].include?(node.type)
166
+ end
167
+ nodes
168
+ end
169
+
170
+ PRECOMPUTED_SHORTHAND_PROPS = {
171
+ "background" => shorthand_properties("background"),
172
+ "font" => shorthand_properties("font"),
173
+ "list-style" => shorthand_properties("list-style"),
174
+ "border" => shorthand_properties("border"),
175
+ "border-top" => shorthand_properties("border-top"),
176
+ "border-bottom" => shorthand_properties("border-bottom"),
177
+ "border-left" => shorthand_properties("border-left"),
178
+ "border-right" => shorthand_properties("border-right"),
179
+ "border-width" => shorthand_properties("border-width"),
180
+ "border-style" => shorthand_properties("border-style"),
181
+ "border-color" => shorthand_properties("border-color"),
182
+ }
183
+
162
184
  # Combine several properties into a shorthand one
163
185
  def create_shorthand_properties!(shorthand_property, need_all = false)
164
186
  properties_to_delete = []
165
-
166
187
  new_values = []
167
- FormalSyntax::Tree.tree.property("--shorthand-"+shorthand_property).traverse do |node|
168
- case node.type
188
+
189
+ PRECOMPUTED_SHORTHAND_PROPS[shorthand_property].each do |node|
190
+ case node[:type]
169
191
  when :ref
170
- decl = find_by_property(node.value)
192
+ decl = find_by_property(node[:value])
171
193
  if decl
172
194
  properties_to_delete << decl.property
173
195
  new_values += decl.values
@@ -176,7 +198,7 @@ module Habaki
176
198
  end
177
199
  when :token
178
200
  # only if next node property is present (line-height, background-size)
179
- new_values << Operator.new(node.value) if node.parent&.children&.last&.value && find_by_property(node.parent.children.last.value)
201
+ new_values << Operator.new(node[:value]) if node[:next_value] && has_property?(node[:next_value])
180
202
  end
181
203
  end
182
204
 
@@ -251,11 +273,6 @@ module Habaki
251
273
  decls
252
274
  end
253
275
 
254
- def push_declaration(decl)
255
- @hash[decl.property] = decl
256
- push decl
257
- end
258
-
259
276
  # Parse inline declarations and append to current declarations
260
277
  # @param [String] data
261
278
  # @return [void]
@@ -268,6 +285,12 @@ module Habaki
268
285
  end
269
286
  end
270
287
 
288
+ # Does declaration with property present ?
289
+ # @return [Boolean]
290
+ def has_property?(property)
291
+ @hash.has_key?(property)
292
+ end
293
+
271
294
  # Find declaration with property
272
295
  # @param [String] property
273
296
  # @return [Declaration]
@@ -342,5 +365,13 @@ module Habaki
342
365
  push_declaration Declaration.read_from_katana(decl)
343
366
  end
344
367
  end
368
+
369
+ private
370
+
371
+ def push_declaration(decl)
372
+ @hash[decl.property] = decl
373
+ push decl
374
+ end
375
+
345
376
  end
346
377
  end
@@ -28,7 +28,7 @@ module Habaki
28
28
  def traverse(&block)
29
29
  block.call self
30
30
  @children.each do |child|
31
- child.traverse &block
31
+ child.traverse(&block)
32
32
  end
33
33
  end
34
34
 
@@ -67,7 +67,7 @@ module Habaki
67
67
  when :token
68
68
  str += @value
69
69
  when :ref
70
- str += "'#{@value}'"
70
+ str += "<'#{@value}'>"
71
71
  when :type
72
72
  str += "<#{@value}>"
73
73
  else
@@ -111,7 +111,7 @@ module Habaki
111
111
  data.each do |k, v|
112
112
  begin
113
113
  @properties[k] = Tree.parse(v)
114
- rescue FormalSyntaxError => e
114
+ rescue FormalSyntaxError #=> e
115
115
  #STDERR.puts("#{k}: #{e}")
116
116
  end
117
117
  end
data/lib/habaki/rules.rb CHANGED
@@ -179,7 +179,7 @@ module Habaki
179
179
  end
180
180
 
181
181
  def each_rule(&block)
182
- each &block
182
+ each(&block)
183
183
  end
184
184
 
185
185
  # @param [Formatter::Base] format
@@ -4,6 +4,12 @@ module Habaki
4
4
  # @return [Symbol]
5
5
  attr_accessor :relation
6
6
 
7
+ def initialize(*args)
8
+ super(*args)
9
+
10
+ @relation = nil
11
+ end
12
+
7
13
  # does every sub selectors match {Visitor::Element} ?
8
14
  # @param [Visitor::Element] element
9
15
  # @param [Specificity, nil] specificity
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: habaki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Boulnois
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-10 00:00:00.000000000 Z
11
+ date: 2022-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -28,30 +28,30 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.14'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.14'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement