Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit b92d92c490fb5c56f02932cfa8e62d3e288e013b 0 parents
@pvorb authored
1  .gitignore
@@ -0,0 +1 @@
+*.html
1  .npmignore
@@ -0,0 +1 @@
+test.js
18 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.
17 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;
+```
55 esc.js
@@ -0,0 +1,55 @@
+var append = require('append'),
+ url = require('url'),
+ path = require('path');
+
+module.exports = function(str, opt) {
+ 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;
+};
15 package.json
@@ -0,0 +1,15 @@
+{
+ "author": "Paul Vorbach <paul@vorb.de> (http://vorb.de/)",
+ "name": "esc",
+ "description": "escapes HTML",
+ "version": "0.0.0",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/pvorb/node-esc.git"
+ },
+ "main": "esc.js",
+ "engines": {
+ "node": "*"
+ },
+ "tags": [ "html", "escape" ]
+}
10 test.js
@@ -0,0 +1,10 @@
+var esc = require('./'),
+ fs = require('fs');
+
+var html = '<html><head><meta charset="utf-8"><title>example</title></head><body id="top"><a href="/some/other/file.html">Link</a><a href="http://example.com"><img src="/some/image.png"></a><a href="another-document">another doc</a><a href="#top">top</a></body></html>';
+
+fs.writeFileSync('test-1.html', esc(html, { 'uri': 'http://example.com' }));
+
+fs.writeFileSync('test-2.html', esc(html, { 'uri': 'http://example.com/' }));
+
+fs.writeFileSync('test-3.html', esc(html, { 'uri': 'http://example.com/dir/doc.html' }));
Please sign in to comment.
Something went wrong with that request. Please try again.