comment this up the arseholes
[catagits/Web-Simple.git] / lib / XML / Tags.pm
index 480fefc..40f6926 100644 (file)
@@ -5,8 +5,6 @@ use warnings FATAL => 'all';
 
 use File::Glob ();
 
-
-
 my $IN_SCOPE = 0;
 
 sub import {
@@ -16,12 +14,21 @@ sub import {
   my $opts = shift(@args) if ref($args[0]) eq 'HASH';
   my $target = $class->_find_target(0, $opts);
   my @tags = $class->_find_tags(@args);
-  $class->_setup_glob_override;
   my $unex = $class->_export_tags_into($target => @tags);
   $class->_install_unexporter($unex);
   $IN_SCOPE = 1;
 }
 
+sub sanitize {
+  map { # string == text -> HTML, scalarref == raw HTML, other == passthrough
+    ref($_)
+      ? (ref $_ eq 'SCALAR' ? $$_ : $_)
+      : do { local $_ = $_; # copy
+          s/&/&amp;/g; s/"/&quot/g; s/</&lt;/g; s/>/&gt;/g; $_;
+        }
+  } @_
+}
+
 sub _find_tags { shift; @_ }
 
 sub _find_target {
@@ -31,34 +38,30 @@ sub _find_target {
   return (caller($level))[0];
 }
 
-{
-  my $setup;
-
-  sub _setup_glob_override {
-    return if $setup;
-    $setup = 1;
-    no warnings 'redefine';
-    *CORE::GLOBAL::glob = sub {
-      for ($_[0]) {
-        # unless it smells like </foo> or <foo bar="baz">
-        return File::Glob::glob($_[0]) unless (/^\/\w+$/ || /^\w+\s+\w+="/);
-      }
-      return '<'.$_[0].'>';
-    };
-  }
+sub _set_glob {
+  # stupid insanity. delete anything already there so we disassociated
+  # the *CORE::GLOBAL::glob typeglob. Then the compilation of the eval
+  # revivifies it - i.e. creates us a new glob, which we get a reference
+  # to, which we can then assign to.
+  # doing it without the eval doesn't - it binds to the version in scope
+  # at compile time, which means after a delete you get a nice warm segv.
+  delete ${CORE::GLOBAL::}{glob};
+  *{eval '\*CORE::GLOBAL::glob'} = $_[0];
 }
 
 sub _export_tags_into {
   my ($class, $into, @tags) = @_;
   foreach my $tag (@tags) {
     no strict 'refs';
-    tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', "<${tag}>";
+    tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
   }
+  _set_glob(sub { \('<'.$_[0].'>'); });
   return sub {
     foreach my $tag (@tags) {
       no strict 'refs';
       delete ${"${into}::"}{$tag}
     }
+    _set_glob(\&File::Glob::glob);
     $IN_SCOPE = 0;
   };
 }