Permalink
Browse files

Add esc

  • Loading branch information...
1 parent 6e25c2e commit c27918d76bc518b493771bfc471a04d4d1e16e1b @pvorb committed Feb 16, 2012
View
1 node_modules/esc/.npmignore
@@ -0,0 +1 @@
+test.js
View
18 node_modules/esc/LICENSE.mkd
@@ -0,0 +1,18 @@
+Copyright © 2011 Paul Vorbach <paul@vorb.de>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the “Software”), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
17 node_modules/esc/README.mkd
@@ -0,0 +1,17 @@
+**esc** allows you to quickly escape HTML strings. It is capable of
+automatically extending URLs in the HTML for use in feeds.
+
+## Example
+
+```javascript
+var esc = require('esc');
+
+var html = '<a href="../some.html">link</a>';
+console.log(esc(html, { uri: 'http://example.com/path/to/document.html' }));
+```
+
+This will print:
+
+```
+&lt;a href=&quot;http://example.com/path/some.html&quot;&gt;
+```
View
57 node_modules/esc/esc.js
@@ -0,0 +1,57 @@
+var append = require('append'),
+ url = require('url'),
+ path = require('path');
+
+module.exports = function(str, opt) {
+ if (typeof str == 'undefined')
+ str = '';
+ if (typeof str != 'string')
+ str = str.toString();
+
+ // default options
+ var def = {
+ mode: 'html',
+ uri: false
+ };
+
+ // append given options to `def`
+ opt = append(def, opt);
+
+ // If a URI is defined, put the prefix in front of URLs.
+ if (opt.uri) {
+ var uri = url.parse(opt.uri);
+ var host = uri.protocol + '//' + uri.host;
+ var dir = path.dirname(uri.pathname)+'/';
+ var all = opt.uri;
+
+ // attr (protocol:? // host)|document
+ var pattern = /(href|src)="(([^:"]+:)?\/\/[^"]+|[^"]+)"/gi;
+
+ // Add 'http://foo.bar/' to URLs
+ str = str.replace(pattern,
+ function(match, key, value, protocol, offset, string) {
+ // If the URL hasn't got a protocol and doesn't start with '//'
+ if (typeof protocol == 'undefined' && value[1] != '/')
+ // If the URL is absolute
+ if (value[0] == '/')
+ return key+'="'+host+value+'"';
+ // If the URL is a hash tag
+ else if (value[0] == '#')
+ return key+'="'+all+value+'"';
+ // If the URL is relative
+ else {
+ return key+'="'+host+path.normalize(dir+value)+'"';
+ }
+ });
+ }
+
+ // Escape HTML output
+ if (opt.mode == 'html') {
+ str = str.replace(/&/gi, '&amp;');
+ str = str.replace(/</gi, '&lt;');
+ str = str.replace(/>/gi, '&gt;');
+ str = str.replace(/"/gi, '&quot;');
+ }
+
+ return str;
+};
View
18 node_modules/esc/package.json
@@ -0,0 +1,18 @@
+{
+ "author": "Paul Vorbach <paul@vorb.de> (http://vorb.de/)",
+ "name": "esc",
+ "description": "escapes HTML",
+ "version": "0.0.2",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/pvorb/node-esc.git"
+ },
+ "main": "esc.js",
+ "engines": {
+ "node": "*"
+ },
+ "tags": [ "html", "escape" ],
+ "dependencies": {
+ "append": ">=0.1.0"
+ }
+}
View
1 node_modules/esc/test/test-1.html
@@ -0,0 +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;
View
1 node_modules/esc/test/test-2.html
@@ -0,0 +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;
View
1 node_modules/esc/test/test-3.html
@@ -0,0 +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 comments on commit c27918d

Please sign in to comment.