Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including htmlslice with all npm packages installed. Try it out:

var htmlslice = require("htmlslice")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

htmlslice v0.1.1

An HTML-aware String#slice.

htmlslice

An HTML-aware String#slice.

Getting Started

On the server

Install the module with: npm install htmlslice

var HtmlSlice = require('htmlslice').HtmlSlice;

var h = new HtmlSlice('sample <a href="#">link <br/><i>test</i></a> foo<br/>bar');
h.length        // 25
h.slice(0, 13)  // 'sample <a href="#">link <br/></a>'
h.slice(8, 14)  // '<a href="#">ink <br/><i>t</i></a>'
h.slice(12, 22) // '<a href="#"><br/><i>test</i></a> foo<br/>'
h.slice(16, 23) // '<a href="#"><i>t</i></a> foo<br/>b'

In the browser

Download the production version or the development version.

In your web page:

<script src="dist/htmlslice.min.js"></script>
<script>
var h = new HtmlSlice('sample <a href="#">link <br/><i>test</i></a> foo<br/>bar');
h.length        // 25
h.slice(0, 13)  // 'sample <a href="#">link <br/></a>'
h.slice(8, 14)  // '<a href="#">ink <br/><i>t</i></a>'
h.slice(12, 22) // '<a href="#"><br/><i>test</i></a> foo<br/>'
h.slice(16, 23) // '<a href="#"><i>t</i></a> foo<br/>b'
</script>

In your code, you can attach htmlslice's methods to any object.

<script>
this.exports = Bocoup.utils;
</script>
<script src="dist/htmlslice.min.js"></script>
<script>
var h = new Bocoup.utils.HtmlSlice('sample <a href="#">link <br/><i>test</i></a> foo<br/>bar');
h.length        // 25
h.slice(0, 13)  // 'sample <a href="#">link <br/></a>'
h.slice(8, 14)  // '<a href="#">ink <br/><i>t</i></a>'
h.slice(12, 22) // '<a href="#"><br/><i>test</i></a> foo<br/>'
h.slice(16, 23) // '<a href="#"><i>t</i></a> foo<br/>b'
</script>

Documentation

Create a new HtmlSlice(htmlString) object. The new is optional. Use its .length property and .slice(begin [, end]) method like you would a String.

Note: tags must be balanced, and self-closing tags must end in />.

Examples

You can take a look at the unit tests, or this animated jQuery example.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Also, please don't edit files in the "dist" subdirectory as they are generated via grunt. You'll find source code in the "lib" subdirectory!

Release History

(for some reason, GitHub displays the following list incorrectly unless there is content here)

  • 0.1.1 - 2012/02/17 - Fixing minor issue with \n characters.
  • 0.1.0 - 2012/02/17 - Initial Release

License

Copyright (c) 2012 "Cowboy" Ben Alman
Licensed under the MIT license.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free