Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cowboy/javascript-htmlslice

An HTML-aware String#slice.
https://github.com/cowboy/javascript-htmlslice

Last synced: 2 months ago
JSON representation

An HTML-aware String#slice.

Awesome Lists containing this project

README

        

# htmlslice

An HTML-aware String#slice.

## Getting Started
### On the server
Install the module with: `npm install htmlslice`

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

var h = new HtmlSlice('sample link
test
foo
bar');
h.length // 25
h.slice(0, 13) // 'sample link
'
h.slice(8, 14) // 'ink
t
'
h.slice(12, 22) // '
test
foo
'
h.slice(16, 23) // 't foo
b'
```

### In the browser
Download the [production version][min] or the [development version][max].

[min]: https://raw.github.com/cowboy/javascript-htmlslice/master/dist/htmlslice.min.js
[max]: https://raw.github.com/cowboy/javascript-htmlslice/master/dist/htmlslice.js

In your web page:

```html

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 your code, you can attach htmlslice's methods to any object.

```html

this.exports = Bocoup.utils;

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'

```

## 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](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/slice).

_Note: tags must be balanced, and self-closing tags must end in `/>`._

## Examples
You can take a look at the [unit tests](https://github.com/cowboy/javascript-htmlslice/blob/master/test/htmlslice_test.js), or this [animated jQuery example](http://jsfiddle.net/cowboy/2SCRK/).

## 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](https://github.com/cowboy/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.