berns 3.1.2 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6695ba5e81eb15efa63f6a0fe44f6b9a7815e72e2f559ea95215705e9071cb7
4
- data.tar.gz: 58abfcecba13d9629cac48c1b16a5aa76fbffc887e1d84f0d5d4374a9ab8cea0
3
+ metadata.gz: 0d9597d10ff728258b758e65aca73ec692374d3d359acb976677f0e143800978
4
+ data.tar.gz: 8eb851e88a7674a5d9e9af5f111085af87bb958db03f0539574bdd9358dad9be
5
5
  SHA512:
6
- metadata.gz: 8011681974ffdbc3ca50097da9cb78cd4eb323957fd60c16d6531c36a5fb88fce1364092351f1f9498d11480645a23e968403a69a34ac80d60b0c044b34dea8c
7
- data.tar.gz: 7076f10933b3b90efc0967b845448863adf3dc46780eecf655e917fc2face9eab42e2e779648db818589368296268dc7c06cd049677077f1d049e5132104f97f
6
+ metadata.gz: 6e62bb1798243beab62f44e1ba7a3fc211b53997ce44e4ba37e6c46303974a96d4391d3fcdb9c807f3b894f097e9d3c823831026d4dca645362b21f4cab050da
7
+ data.tar.gz: 047c09ea060864337ee852e51b85964bfed265009123e290e17d0f3b51782aca5ad3ca877ea45195536e8a93258dd5536e66c6e553d6afc5dfcbf970bccf7807
data/README.org CHANGED
@@ -91,6 +91,10 @@ The =sanitize= method strips HTML tags from strings.
91
91
  Berns.sanitize('This <span>should be clean</span>') # => 'This should be clean'
92
92
  #+end_src
93
93
 
94
+ Note that this is an extremely naive implementation of HTML sanitization that
95
+ literally just looks for "<" and ">" characters and removes the contents between
96
+ them. This should probably only be used on trusted strings.
97
+
94
98
  *** Standard and void elements
95
99
 
96
100
  All standard and void HTML elements are defined as methods on Berns, so you can
data/ext/berns/berns.c CHANGED
@@ -1,5 +1,4 @@
1
1
  #include "ruby.h"
2
- #include "extconf.h"
3
2
  #include "hescape.h"
4
3
 
5
4
  static const char *attr_close = "\"";
@@ -47,11 +46,11 @@ static const size_t sllen = 1;
47
46
  * Macro to define a "dynamic" function that generates a void element.
48
47
  */
49
48
  #define VOID_ELEMENT(element_name) \
50
- static VALUE external_##element_name##_element(int argc, VALUE* argv, RB_UNUSED_VAR(VALUE self)) { \
49
+ static VALUE external_##element_name##_element(int argc, VALUE *argv, RB_UNUSED_VAR(VALUE self)) { \
51
50
  rb_check_arity(argc, 0, 1); \
52
51
  \
53
- char *tag = #element_name; \
54
- char *string = void_element(tag, strlen(tag), &argv[0]); \
52
+ const char *tag = #element_name; \
53
+ char *string = void_element(tag, strlen(tag), argv[0]); \
55
54
  VALUE rstring = rb_utf8_str_new_cstr(string); \
56
55
  free(string); \
57
56
  \
@@ -62,12 +61,12 @@ static const size_t sllen = 1;
62
61
  * Macro to define a "dynamic" function that generates a standard element.
63
62
  */
64
63
  #define STANDARD_ELEMENT(element_name) \
65
- static VALUE external_##element_name##_element(int argc, VALUE* argv, RB_UNUSED_VAR(VALUE self)) { \
64
+ static VALUE external_##element_name##_element(int argc, VALUE *argv, RB_UNUSED_VAR(VALUE self)) { \
66
65
  rb_check_arity(argc, 0, 1); \
67
66
  \
68
67
  CONTENT_FROM_BLOCK; \
69
- char *tag = #element_name; \
70
- char *string = element(tag, strlen(tag), RSTRING_PTR(content), RSTRING_LEN(content), &argv[0]); \
68
+ const char *tag = #element_name; \
69
+ char *string = element(tag, strlen(tag), RSTRING_PTR(content), RSTRING_LEN(content), argv[0]); \
71
70
  VALUE rstring = rb_utf8_str_new_cstr(string); \
72
71
  free(string); \
73
72
  \
@@ -78,25 +77,64 @@ static const size_t sllen = 1;
78
77
  /*
79
78
  * "Safe strcpy" - https://twitter.com/hyc_symas/status/1102573036534972416?s=12
80
79
  */
81
- static char *stecpy(char *destination, const char *source, const char *end) {
80
+ static char * stecpy(char *destination, const char *source, const char *end) {
81
+ if (end) {
82
+ end--;
83
+ }
84
+
82
85
  while (*source && destination < end) {
83
86
  *destination++ = *source++;
84
87
  }
85
88
 
86
- if (destination < end) {
89
+ if (destination) {
87
90
  *destination = '\0';
88
91
  }
89
92
 
90
93
  return destination;
91
94
  }
92
95
 
96
+ /*
97
+ * The external API for Berns.sanitize
98
+ *
99
+ * string should be a string or nil, anything else will raise an error.
100
+ *
101
+ */
102
+ static VALUE external_sanitize(RB_UNUSED_VAR(VALUE self), VALUE string) {
103
+ if (TYPE(string) == T_NIL) {
104
+ return Qnil;
105
+ }
106
+
107
+ StringValue(string);
108
+
109
+ size_t slen = RSTRING_LEN(string);
110
+ char *str = RSTRING_PTR(string);
111
+
112
+ char dest[slen + 1];
113
+ int index = 0;
114
+ int open = 0;
115
+
116
+ for (unsigned int i = 0; i < slen; i++) {
117
+ if (str[i] == '<') {
118
+ open = 1;
119
+ } else if (str[i] == '>') {
120
+ open = 0;
121
+ } else if (!open) {
122
+ dest[index++] = str[i];
123
+ }
124
+ }
125
+
126
+ dest[index] = '\0';
127
+
128
+ return rb_utf8_str_new_cstr(dest);
129
+ }
130
+
93
131
  /*
94
132
  * The external API for Berns.escape_html.
95
133
  *
96
134
  * string should be a string, anything else will raise an error.
97
135
  *
98
136
  */
99
- static VALUE external_escape_html(const VALUE self, VALUE string) {
137
+ static VALUE external_escape_html(RB_UNUSED_VAR(VALUE self), VALUE string) {
100
138
  StringValue(string);
101
139
 
102
140
  uint8_t *dest = NULL;
@@ -123,7 +161,6 @@ static VALUE external_escape_html(const VALUE self, VALUE string) {
123
161
  static char * empty_value_to_attribute(const char *attr, const size_t attrlen) {
124
162
  size_t total_size = attrlen + 1;
125
163
  char *dest = malloc(total_size);
126
- char *ptr = NULL;
127
164
  char *end = dest + total_size;
128
165
 
129
166
  stecpy(dest, attr, end);
@@ -140,7 +177,6 @@ static char * string_value_to_attribute(const char *attr, const size_t attrlen,
140
177
  if (vallen == 0) {
141
178
  size_t total_size = attrlen + 1;
142
179
  char *dest = malloc(total_size);
143
- char *ptr = NULL;
144
180
  char *end = dest + total_size;
145
181
 
146
182
  stecpy(dest, attr, end);
@@ -168,21 +204,21 @@ static char * string_value_to_attribute(const char *attr, const size_t attrlen,
168
204
  }
169
205
  }
170
206
 
171
- static char * hash_value_to_attribute(char *attr, const size_t attrlen, VALUE *value) {
172
- if (TYPE(*value) == T_IMEMO) {
173
- return calloc(0, 1);
207
+ static char * hash_value_to_attribute(const char *attr, const size_t attrlen, VALUE value) {
208
+ if (TYPE(value) == T_IMEMO) {
209
+ return strdup("");
174
210
  }
175
211
 
176
- Check_Type(*value, T_HASH);
212
+ Check_Type(value, T_HASH);
177
213
 
178
- if (rb_hash_size(*value) == 1) {
179
- return calloc(0, 1);
214
+ if (rb_hash_size(value) == 1) {
215
+ return strdup("");
180
216
  }
181
217
 
182
218
  VALUE subkey;
183
219
  VALUE subvalue;
184
220
 
185
- const VALUE keys = rb_funcall(*value, rb_intern("keys"), 0);
221
+ const VALUE keys = rb_funcall(value, rb_intern("keys"), 0);
186
222
  const VALUE length = RARRAY_LEN(keys);
187
223
 
188
224
  size_t allocated = 256;
@@ -194,7 +230,7 @@ static char * hash_value_to_attribute(char *attr, const size_t attrlen, VALUE *v
194
230
 
195
231
  for (unsigned int i = 0; i < length; i++) {
196
232
  subkey = rb_ary_entry(keys, i);
197
- subvalue = rb_hash_aref(*value, subkey);
233
+ subvalue = rb_hash_aref(value, subkey);
198
234
 
199
235
  switch(TYPE(subkey)) {
200
236
  case T_STRING:
@@ -224,27 +260,29 @@ static char * hash_value_to_attribute(char *attr, const size_t attrlen, VALUE *v
224
260
 
225
261
  char subattr[subattr_len + 1];
226
262
  char *ptr = subattr;
227
- char *end = subattr + sizeof(subattr);
263
+ char *subend = subattr + subattr_len + 1;
228
264
 
229
265
  if (attrlen > 0) {
230
- ptr = stecpy(ptr, attr, end);
266
+ ptr = stecpy(ptr, attr, subend);
231
267
  }
232
268
 
233
269
  if (attrlen > 0 && subkey_len > 0) {
234
- ptr = stecpy(ptr, dash, end);
270
+ ptr = stecpy(ptr, dash, subend);
235
271
  }
236
272
 
237
- stecpy(ptr, RSTRING_PTR(subkey), end);
273
+ stecpy(ptr, RSTRING_PTR(subkey), subend);
238
274
 
239
275
  char *combined;
240
276
 
241
277
  switch(TYPE(subvalue)) {
242
278
  case T_FALSE:
243
- combined = calloc(0, 1);
279
+ combined = strdup("");
244
280
  break;
245
281
 
282
+ case T_NIL:
283
+ /* Fall through. */
246
284
  case T_TRUE:
247
- combined = string_value_to_attribute(subattr, subattr_len, "", 0);
285
+ combined = empty_value_to_attribute(subattr, subattr_len);
248
286
  break;
249
287
 
250
288
  case T_STRING:
@@ -256,13 +294,8 @@ static char * hash_value_to_attribute(char *attr, const size_t attrlen, VALUE *v
256
294
  combined = string_value_to_attribute(subattr, subattr_len, RSTRING_PTR(subvalue), RSTRING_LEN(subvalue));
257
295
  break;
258
296
 
259
- case T_NIL:
260
- subvalue = rb_utf8_str_new_cstr("");
261
- combined = string_value_to_attribute(subattr, subattr_len, RSTRING_PTR(subvalue), RSTRING_LEN(subvalue));
262
- break;
263
-
264
297
  case T_HASH:
265
- combined = hash_value_to_attribute(subattr, subattr_len, &subvalue);
298
+ combined = hash_value_to_attribute(subattr, subattr_len, subvalue);
266
299
  break;
267
300
 
268
301
  default:
@@ -313,7 +346,7 @@ static char * hash_value_to_attribute(char *attr, const size_t attrlen, VALUE *v
313
346
  /*
314
347
  * Convert an attribute name and value into a string.
315
348
  */
316
- static char * to_attribute(VALUE attr, VALUE *value) {
349
+ static char * to_attribute(VALUE attr, VALUE value) {
317
350
  switch(TYPE(attr)) {
318
351
  case T_SYMBOL:
319
352
  attr = rb_sym2str(attr);
@@ -327,26 +360,27 @@ static char * to_attribute(VALUE attr, VALUE *value) {
327
360
  char *val = NULL;
328
361
  VALUE str;
329
362
 
330
- switch(TYPE(*value)) {
363
+ switch(TYPE(value)) {
331
364
  case T_NIL:
365
+ /* Fall through. */
332
366
  case T_TRUE:
333
367
  val = empty_value_to_attribute(RSTRING_PTR(attr), RSTRING_LEN(attr));
334
368
  break;
335
369
  case T_FALSE:
336
- val = calloc(0, 1);
370
+ val = strdup("");
337
371
  break;
338
372
  case T_HASH:
339
373
  val = hash_value_to_attribute(RSTRING_PTR(attr), RSTRING_LEN(attr), value);
340
374
  break;
341
375
  case T_STRING:
342
- val = string_value_to_attribute(RSTRING_PTR(attr), RSTRING_LEN(attr), RSTRING_PTR(*value), RSTRING_LEN(*value));
376
+ val = string_value_to_attribute(RSTRING_PTR(attr), RSTRING_LEN(attr), RSTRING_PTR(value), RSTRING_LEN(value));
343
377
  break;
344
378
  case T_SYMBOL:
345
- str = rb_sym2str(*value);
379
+ str = rb_sym2str(value);
346
380
  val = string_value_to_attribute(RSTRING_PTR(attr), RSTRING_LEN(attr), RSTRING_PTR(str), RSTRING_LEN(str));
347
381
  break;
348
382
  default:
349
- str = rb_funcall(*value, rb_intern("to_s"), 0);
383
+ str = rb_funcall(value, rb_intern("to_s"), 0);
350
384
  val = string_value_to_attribute(RSTRING_PTR(attr), RSTRING_LEN(attr), RSTRING_PTR(str), RSTRING_LEN(str));
351
385
  break;
352
386
  }
@@ -372,7 +406,7 @@ static VALUE external_to_attribute(RB_UNUSED_VAR(VALUE self), VALUE attr, VALUE
372
406
 
373
407
  StringValue(attr);
374
408
 
375
- char *val = to_attribute(attr, &value);
409
+ char *val = to_attribute(attr, value);
376
410
  VALUE rstring = rb_utf8_str_new_cstr(val);
377
411
  free(val);
378
412
 
@@ -392,8 +426,8 @@ static VALUE external_to_attributes(RB_UNUSED_VAR(VALUE self), VALUE attributes)
392
426
  return rb_utf8_str_new_cstr("");
393
427
  }
394
428
 
395
- char *empty = "";
396
- char *attrs = hash_value_to_attribute(empty, 0, &attributes);
429
+ const char *empty = "";
430
+ char *attrs = hash_value_to_attribute(empty, 0, attributes);
397
431
 
398
432
  VALUE rstring = rb_utf8_str_new_cstr(attrs);
399
433
  free(attrs);
@@ -401,9 +435,9 @@ static VALUE external_to_attributes(RB_UNUSED_VAR(VALUE self), VALUE attributes)
401
435
  return rstring;
402
436
  }
403
437
 
404
- static char * void_element(char *tag, size_t tlen, VALUE *attributes) {
438
+ static char * void_element(const char *tag, size_t tlen, VALUE attributes) {
405
439
  /* T_IMEMO is what we get if an optional argument was not passed. */
406
- if (TYPE(*attributes) == T_IMEMO) {
440
+ if (TYPE(attributes) == T_IMEMO) {
407
441
  size_t total = tag_olen + tlen + tag_clen + 1;
408
442
  char *string = malloc(total);
409
443
  char *ptr;
@@ -415,7 +449,7 @@ static char * void_element(char *tag, size_t tlen, VALUE *attributes) {
415
449
 
416
450
  return string;
417
451
  } else {
418
- char *empty = "";
452
+ const char *empty = "";
419
453
  char *attrs = hash_value_to_attribute(empty, 0, attributes);
420
454
 
421
455
  size_t total = tag_olen + tlen + splen + strlen(attrs) + tag_clen + 1;
@@ -454,7 +488,7 @@ static VALUE external_void_element(int argc, VALUE *arguments, RB_UNUSED_VAR(VAL
454
488
 
455
489
  StringValue(tag);
456
490
 
457
- char *string = void_element(RSTRING_PTR(tag), RSTRING_LEN(tag), &attributes);
491
+ char *string = void_element(RSTRING_PTR(tag), RSTRING_LEN(tag), attributes);
458
492
  VALUE rstring = rb_utf8_str_new_cstr(string);
459
493
 
460
494
  free(string);
@@ -462,8 +496,8 @@ static VALUE external_void_element(int argc, VALUE *arguments, RB_UNUSED_VAR(VAL
462
496
  return rstring;
463
497
  }
464
498
 
465
- static char * element(char *tag, size_t tlen, char *content, size_t conlen, VALUE *attributes) {
466
- char *empty = "";
499
+ static char * element(const char *tag, size_t tlen, char *content, size_t conlen, VALUE attributes) {
500
+ const char *empty = "";
467
501
  char *attrs = hash_value_to_attribute(empty, 0, attributes);
468
502
  size_t alen = strlen(attrs);
469
503
 
@@ -529,129 +563,130 @@ static VALUE external_element(int argc, VALUE *arguments, RB_UNUSED_VAR(VALUE se
529
563
 
530
564
  CONTENT_FROM_BLOCK;
531
565
 
532
- char *string = element(RSTRING_PTR(tag), RSTRING_LEN(tag), RSTRING_PTR(content), RSTRING_LEN(content), &attributes);
566
+ char *string = element(RSTRING_PTR(tag), RSTRING_LEN(tag), RSTRING_PTR(content), RSTRING_LEN(content), attributes);
533
567
  VALUE rstring = rb_utf8_str_new_cstr(string);
534
568
  free(string);
535
569
 
536
570
  return rstring;
537
571
  }
538
572
 
539
- VOID_ELEMENT(area);
540
- VOID_ELEMENT(base);
541
- VOID_ELEMENT(br);
542
- VOID_ELEMENT(col);
543
- VOID_ELEMENT(embed);
544
- VOID_ELEMENT(hr);
545
- VOID_ELEMENT(img);
546
- VOID_ELEMENT(input);
547
- VOID_ELEMENT(link);
548
- VOID_ELEMENT(menuitem);
549
- VOID_ELEMENT(meta);
550
- VOID_ELEMENT(param);
551
- VOID_ELEMENT(source);
552
- VOID_ELEMENT(track);
553
- VOID_ELEMENT(wbr);
554
-
555
- STANDARD_ELEMENT(a);
556
- STANDARD_ELEMENT(abbr);
557
- STANDARD_ELEMENT(address);
558
- STANDARD_ELEMENT(article);
559
- STANDARD_ELEMENT(aside);
560
- STANDARD_ELEMENT(audio);
561
- STANDARD_ELEMENT(b);
562
- STANDARD_ELEMENT(bdi);
563
- STANDARD_ELEMENT(bdo);
564
- STANDARD_ELEMENT(blockquote);
565
- STANDARD_ELEMENT(body);
566
- STANDARD_ELEMENT(button);
567
- STANDARD_ELEMENT(canvas);
568
- STANDARD_ELEMENT(caption);
569
- STANDARD_ELEMENT(cite);
570
- STANDARD_ELEMENT(code);
571
- STANDARD_ELEMENT(colgroup);
572
- STANDARD_ELEMENT(datalist);
573
- STANDARD_ELEMENT(dd);
574
- STANDARD_ELEMENT(del);
575
- STANDARD_ELEMENT(details);
576
- STANDARD_ELEMENT(dfn);
577
- STANDARD_ELEMENT(dialog);
578
- STANDARD_ELEMENT(div);
579
- STANDARD_ELEMENT(dl);
580
- STANDARD_ELEMENT(dt);
581
- STANDARD_ELEMENT(em);
582
- STANDARD_ELEMENT(fieldset);
583
- STANDARD_ELEMENT(figcaption);
584
- STANDARD_ELEMENT(figure);
585
- STANDARD_ELEMENT(footer);
586
- STANDARD_ELEMENT(form);
587
- STANDARD_ELEMENT(h1);
588
- STANDARD_ELEMENT(h2);
589
- STANDARD_ELEMENT(h3);
590
- STANDARD_ELEMENT(h4);
591
- STANDARD_ELEMENT(h5);
592
- STANDARD_ELEMENT(h6);
593
- STANDARD_ELEMENT(head);
594
- STANDARD_ELEMENT(header);
595
- STANDARD_ELEMENT(html);
596
- STANDARD_ELEMENT(i);
597
- STANDARD_ELEMENT(iframe);
598
- STANDARD_ELEMENT(ins);
599
- STANDARD_ELEMENT(kbd);
600
- STANDARD_ELEMENT(label);
601
- STANDARD_ELEMENT(legend);
602
- STANDARD_ELEMENT(li);
603
- STANDARD_ELEMENT(main);
604
- STANDARD_ELEMENT(map);
605
- STANDARD_ELEMENT(mark);
606
- STANDARD_ELEMENT(menu);
607
- STANDARD_ELEMENT(meter);
608
- STANDARD_ELEMENT(nav);
609
- STANDARD_ELEMENT(noscript);
610
- STANDARD_ELEMENT(object);
611
- STANDARD_ELEMENT(ol);
612
- STANDARD_ELEMENT(optgroup);
613
- STANDARD_ELEMENT(option);
614
- STANDARD_ELEMENT(output);
615
- STANDARD_ELEMENT(p);
616
- STANDARD_ELEMENT(picture);
617
- STANDARD_ELEMENT(pre);
618
- STANDARD_ELEMENT(progress);
619
- STANDARD_ELEMENT(q);
620
- STANDARD_ELEMENT(rp);
621
- STANDARD_ELEMENT(rt);
622
- STANDARD_ELEMENT(ruby);
623
- STANDARD_ELEMENT(s);
624
- STANDARD_ELEMENT(samp);
625
- STANDARD_ELEMENT(script);
626
- STANDARD_ELEMENT(section);
627
- STANDARD_ELEMENT(select);
628
- STANDARD_ELEMENT(small);
629
- STANDARD_ELEMENT(span);
630
- STANDARD_ELEMENT(strong);
631
- STANDARD_ELEMENT(style);
632
- STANDARD_ELEMENT(sub);
633
- STANDARD_ELEMENT(summary);
634
- STANDARD_ELEMENT(table);
635
- STANDARD_ELEMENT(tbody);
636
- STANDARD_ELEMENT(td);
637
- STANDARD_ELEMENT(template);
638
- STANDARD_ELEMENT(textarea);
639
- STANDARD_ELEMENT(tfoot);
640
- STANDARD_ELEMENT(th);
641
- STANDARD_ELEMENT(thead);
642
- STANDARD_ELEMENT(time);
643
- STANDARD_ELEMENT(title);
644
- STANDARD_ELEMENT(tr);
645
- STANDARD_ELEMENT(u);
646
- STANDARD_ELEMENT(ul);
647
- STANDARD_ELEMENT(var);
648
- STANDARD_ELEMENT(video);
573
+ VOID_ELEMENT(area)
574
+ VOID_ELEMENT(base)
575
+ VOID_ELEMENT(br)
576
+ VOID_ELEMENT(col)
577
+ VOID_ELEMENT(embed)
578
+ VOID_ELEMENT(hr)
579
+ VOID_ELEMENT(img)
580
+ VOID_ELEMENT(input)
581
+ VOID_ELEMENT(link)
582
+ VOID_ELEMENT(menuitem)
583
+ VOID_ELEMENT(meta)
584
+ VOID_ELEMENT(param)
585
+ VOID_ELEMENT(source)
586
+ VOID_ELEMENT(track)
587
+ VOID_ELEMENT(wbr)
588
+
589
+ STANDARD_ELEMENT(a)
590
+ STANDARD_ELEMENT(abbr)
591
+ STANDARD_ELEMENT(address)
592
+ STANDARD_ELEMENT(article)
593
+ STANDARD_ELEMENT(aside)
594
+ STANDARD_ELEMENT(audio)
595
+ STANDARD_ELEMENT(b)
596
+ STANDARD_ELEMENT(bdi)
597
+ STANDARD_ELEMENT(bdo)
598
+ STANDARD_ELEMENT(blockquote)
599
+ STANDARD_ELEMENT(body)
600
+ STANDARD_ELEMENT(button)
601
+ STANDARD_ELEMENT(canvas)
602
+ STANDARD_ELEMENT(caption)
603
+ STANDARD_ELEMENT(cite)
604
+ STANDARD_ELEMENT(code)
605
+ STANDARD_ELEMENT(colgroup)
606
+ STANDARD_ELEMENT(datalist)
607
+ STANDARD_ELEMENT(dd)
608
+ STANDARD_ELEMENT(del)
609
+ STANDARD_ELEMENT(details)
610
+ STANDARD_ELEMENT(dfn)
611
+ STANDARD_ELEMENT(dialog)
612
+ STANDARD_ELEMENT(div)
613
+ STANDARD_ELEMENT(dl)
614
+ STANDARD_ELEMENT(dt)
615
+ STANDARD_ELEMENT(em)
616
+ STANDARD_ELEMENT(fieldset)
617
+ STANDARD_ELEMENT(figcaption)
618
+ STANDARD_ELEMENT(figure)
619
+ STANDARD_ELEMENT(footer)
620
+ STANDARD_ELEMENT(form)
621
+ STANDARD_ELEMENT(h1)
622
+ STANDARD_ELEMENT(h2)
623
+ STANDARD_ELEMENT(h3)
624
+ STANDARD_ELEMENT(h4)
625
+ STANDARD_ELEMENT(h5)
626
+ STANDARD_ELEMENT(h6)
627
+ STANDARD_ELEMENT(head)
628
+ STANDARD_ELEMENT(header)
629
+ STANDARD_ELEMENT(html)
630
+ STANDARD_ELEMENT(i)
631
+ STANDARD_ELEMENT(iframe)
632
+ STANDARD_ELEMENT(ins)
633
+ STANDARD_ELEMENT(kbd)
634
+ STANDARD_ELEMENT(label)
635
+ STANDARD_ELEMENT(legend)
636
+ STANDARD_ELEMENT(li)
637
+ STANDARD_ELEMENT(main)
638
+ STANDARD_ELEMENT(map)
639
+ STANDARD_ELEMENT(mark)
640
+ STANDARD_ELEMENT(menu)
641
+ STANDARD_ELEMENT(meter)
642
+ STANDARD_ELEMENT(nav)
643
+ STANDARD_ELEMENT(noscript)
644
+ STANDARD_ELEMENT(object)
645
+ STANDARD_ELEMENT(ol)
646
+ STANDARD_ELEMENT(optgroup)
647
+ STANDARD_ELEMENT(option)
648
+ STANDARD_ELEMENT(output)
649
+ STANDARD_ELEMENT(p)
650
+ STANDARD_ELEMENT(picture)
651
+ STANDARD_ELEMENT(pre)
652
+ STANDARD_ELEMENT(progress)
653
+ STANDARD_ELEMENT(q)
654
+ STANDARD_ELEMENT(rp)
655
+ STANDARD_ELEMENT(rt)
656
+ STANDARD_ELEMENT(ruby)
657
+ STANDARD_ELEMENT(s)
658
+ STANDARD_ELEMENT(samp)
659
+ STANDARD_ELEMENT(script)
660
+ STANDARD_ELEMENT(section)
661
+ STANDARD_ELEMENT(select)
662
+ STANDARD_ELEMENT(small)
663
+ STANDARD_ELEMENT(span)
664
+ STANDARD_ELEMENT(strong)
665
+ STANDARD_ELEMENT(style)
666
+ STANDARD_ELEMENT(sub)
667
+ STANDARD_ELEMENT(summary)
668
+ STANDARD_ELEMENT(table)
669
+ STANDARD_ELEMENT(tbody)
670
+ STANDARD_ELEMENT(td)
671
+ STANDARD_ELEMENT(template)
672
+ STANDARD_ELEMENT(textarea)
673
+ STANDARD_ELEMENT(tfoot)
674
+ STANDARD_ELEMENT(th)
675
+ STANDARD_ELEMENT(thead)
676
+ STANDARD_ELEMENT(time)
677
+ STANDARD_ELEMENT(title)
678
+ STANDARD_ELEMENT(tr)
679
+ STANDARD_ELEMENT(u)
680
+ STANDARD_ELEMENT(ul)
681
+ STANDARD_ELEMENT(var)
682
+ STANDARD_ELEMENT(video)
649
683
 
650
684
  void Init_berns() {
651
685
  VALUE Berns = rb_define_module("Berns");
652
686
 
653
687
  rb_define_singleton_method(Berns, "element", external_element, -1);
654
688
  rb_define_singleton_method(Berns, "escape_html", external_escape_html, 1);
689
+ rb_define_singleton_method(Berns, "sanitize", external_sanitize, 1);
655
690
  rb_define_singleton_method(Berns, "to_attribute", external_to_attribute, 2);
656
691
  rb_define_singleton_method(Berns, "to_attributes", external_to_attributes, 1);
657
692
  rb_define_singleton_method(Berns, "void", external_void_element, -1);
data/ext/berns/extconf.rb CHANGED
@@ -1,8 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
  require 'mkmf'
3
3
 
4
- $CFLAGS = '-O3 -msse4' # rubocop:disable Style/GlobalVars
4
+ dir_config 'berns'
5
+
6
+ append_cflags '-O3'
7
+ append_cflags '-Wshadow'
8
+ append_cflags '-Wstrict-overflow'
9
+ append_cflags '-flto'
10
+ append_cflags '-fno-strict-aliasing'
11
+ append_cflags '-msse4'
12
+ append_cflags '-std=c99'
5
13
 
6
- dir_config('berns')
7
- create_header
8
14
  create_makefile 'berns/berns'
data/lib/berns.rb CHANGED
@@ -1,25 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
  require 'berns/berns'
3
3
  require 'berns/version'
4
-
5
- module Berns # :nodoc:
6
- class Error < StandardError; end
7
-
8
- EMPTY = ''
9
-
10
- # Regular expression for basic HTML tag sanitizing.
11
- SANITIZE_REGEX = /<[^>]+>/.freeze
12
-
13
- # Sanitize text input by stripping HTML tags.
14
- #
15
- # @example Sanitize some text, removing HTML elements.
16
- # sanitize('This <span>should be clean</span>') # => "This should be clean"
17
- #
18
- # @param text [String]
19
- # The string to sanitize.
20
- # @return [nil, String]
21
- # nil unless a string was passed in, otherwise the sanitized string.
22
- def self.sanitize(string)
23
- string&.gsub(SANITIZE_REGEX, EMPTY)
24
- end
25
- end
data/lib/berns/berns.so CHANGED
Binary file
data/lib/berns/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Berns
3
- VERSION = '3.1.2'
3
+ VERSION = '3.2.0'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berns
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.2
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Beck
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-05-14 00:00:00.000000000 Z
12
+ date: 2021-06-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: benchmark-ips