An HTML-aware String#slice.
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'
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>
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 />
.
You can take a look at the unit tests, or this animated jQuery example.
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!
(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
Copyright (c) 2012 "Cowboy" Ben Alman
Licensed under the MIT license.