htmlslice

An HTML-aware String#slice.

Usage no npm install needed!

<script type="module">
  import htmlslice from 'https://cdn.skypack.dev/htmlslice';
</script>

README

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.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.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.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

(Coming soon)

Examples

(Coming soon)

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

(Nothing yet)

License

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