Skip to content
This repository
Browse code

Add esc

  • Loading branch information...
commit c27918d76bc518b493771bfc471a04d4d1e16e1b 1 parent 6e25c2e
Paul Vorbach authored February 16, 2012
1  node_modules/esc/.npmignore
... ...
@@ -0,0 +1 @@
  1
+test.js
18  node_modules/esc/LICENSE.mkd
Source Rendered
... ...
@@ -0,0 +1,18 @@
  1
+Copyright © 2011 Paul Vorbach <paul@vorb.de>
  2
+
  3
+Permission is hereby granted, free of charge, to any person obtaining a copy of
  4
+this software and associated documentation files (the “Software”), to deal in
  5
+the Software without restriction, including without limitation the rights to
  6
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7
+the Software, and to permit persons to whom the Software is furnished to do so,
  8
+subject to the following conditions:
  9
+
  10
+The above copyright notice and this permission notice shall be included in all
  11
+copies or substantial portions of the Software.
  12
+
  13
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  15
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE
  18
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17  node_modules/esc/README.mkd
Source Rendered
... ...
@@ -0,0 +1,17 @@
  1
+**esc** allows you to quickly escape HTML strings. It is capable of
  2
+automatically extending URLs in the HTML for use in feeds.
  3
+
  4
+## Example
  5
+
  6
+```javascript
  7
+var esc = require('esc');
  8
+
  9
+var html = '<a href="../some.html">link</a>';
  10
+console.log(esc(html, { uri: 'http://example.com/path/to/document.html' }));
  11
+```
  12
+
  13
+This will print:
  14
+
  15
+```
  16
+&lt;a href=&quot;http://example.com/path/some.html&quot;&gt;
  17
+```
57  node_modules/esc/esc.js
... ...
@@ -0,0 +1,57 @@
  1
+var append = require('append'),
  2
+    url = require('url'),
  3
+    path = require('path');
  4
+
  5
+module.exports = function(str, opt) {
  6
+  if (typeof str == 'undefined')
  7
+    str = '';
  8
+  if (typeof str != 'string')
  9
+    str = str.toString();
  10
+
  11
+  // default options
  12
+  var def = {
  13
+    mode: 'html',
  14
+    uri: false
  15
+  };
  16
+
  17
+  // append given options to `def`
  18
+  opt = append(def, opt);
  19
+
  20
+  // If a URI is defined, put the prefix in front of URLs.
  21
+  if (opt.uri) {
  22
+    var uri = url.parse(opt.uri);
  23
+    var host = uri.protocol + '//' + uri.host;
  24
+    var dir = path.dirname(uri.pathname)+'/';
  25
+    var all = opt.uri;
  26
+
  27
+    //             attr        (protocol:? // host)|document
  28
+    var pattern = /(href|src)="(([^:"]+:)?\/\/[^"]+|[^"]+)"/gi;
  29
+
  30
+    // Add 'http://foo.bar/' to URLs
  31
+    str = str.replace(pattern,
  32
+        function(match, key, value, protocol, offset, string) {
  33
+      // If the URL hasn't got a protocol and doesn't start with '//'
  34
+      if (typeof protocol == 'undefined' && value[1] != '/')
  35
+        // If the URL is absolute
  36
+      if (value[0] == '/')
  37
+        return key+'="'+host+value+'"';
  38
+      // If the URL is a hash tag
  39
+      else if (value[0] == '#')
  40
+        return key+'="'+all+value+'"';
  41
+      // If the URL is relative
  42
+      else {
  43
+        return key+'="'+host+path.normalize(dir+value)+'"';
  44
+      }
  45
+    });
  46
+  }
  47
+
  48
+  // Escape HTML output
  49
+  if (opt.mode == 'html') {
  50
+    str = str.replace(/&/gi, '&amp;');
  51
+    str = str.replace(/</gi, '&lt;');
  52
+    str = str.replace(/>/gi, '&gt;');
  53
+    str = str.replace(/"/gi, '&quot;');
  54
+  }
  55
+
  56
+  return str;
  57
+};
18  node_modules/esc/package.json
... ...
@@ -0,0 +1,18 @@
  1
+{
  2
+  "author": "Paul Vorbach <paul@vorb.de> (http://vorb.de/)",
  3
+  "name": "esc",
  4
+  "description": "escapes HTML",
  5
+  "version": "0.0.2",
  6
+  "repository": {
  7
+    "type": "git",
  8
+    "url": "git://github.com/pvorb/node-esc.git"
  9
+  },
  10
+  "main": "esc.js",
  11
+  "engines": {
  12
+    "node": "*"
  13
+  },
  14
+  "tags": [ "html", "escape" ],
  15
+  "dependencies": {
  16
+    "append": ">=0.1.0"
  17
+  }
  18
+}
1  node_modules/esc/test/test-1.html
... ...
@@ -0,0 +1 @@
  1
+&lt;html&gt;&lt;head&gt;&lt;meta charset=&quot;utf-8&quot;&gt;&lt;title&gt;example&lt;/title&gt;&lt;/head&gt;&lt;body id=&quot;top&quot;&gt;&lt;a href=&quot;http://example.com/some/other/file.html&quot;&gt;Link&lt;/a&gt;&lt;a undefined&gt;&lt;img src=&quot;http://example.com/some/image.png&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://example.com/another-document&quot;&gt;another doc&lt;/a&gt;&lt;a href=&quot;http://example.com#top&quot;&gt;top&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;
1  node_modules/esc/test/test-2.html
... ...
@@ -0,0 +1 @@
  1
+&lt;html&gt;&lt;head&gt;&lt;meta charset=&quot;utf-8&quot;&gt;&lt;title&gt;example&lt;/title&gt;&lt;/head&gt;&lt;body id=&quot;top&quot;&gt;&lt;a href=&quot;http://example.com/some/other/file.html&quot;&gt;Link&lt;/a&gt;&lt;a undefined&gt;&lt;img src=&quot;http://example.com/some/image.png&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://example.com/another-document&quot;&gt;another doc&lt;/a&gt;&lt;a href=&quot;http://example.com/#top&quot;&gt;top&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;
1  node_modules/esc/test/test-3.html
... ...
@@ -0,0 +1 @@
  1
+&lt;html&gt;&lt;head&gt;&lt;meta charset=&quot;utf-8&quot;&gt;&lt;title&gt;example&lt;/title&gt;&lt;/head&gt;&lt;body id=&quot;top&quot;&gt;&lt;a href=&quot;http://example.com/some/other/file.html&quot;&gt;Link&lt;/a&gt;&lt;a undefined&gt;&lt;img src=&quot;http://example.com/some/image.png&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://example.com/dir/another-document&quot;&gt;another doc&lt;/a&gt;&lt;a href=&quot;http://example.com/dir/doc.html#top&quot;&gt;top&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;

0 notes on commit c27918d

Please sign in to comment.
Something went wrong with that request. Please try again.