Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/cowboy/javascript-htmlslice
- Owner: cowboy
- License: mit
- Created: 2012-02-17T22:02:20.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2012-02-18T00:13:31.000Z (almost 13 years ago)
- Last Synced: 2024-04-14T22:47:01.491Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 93.8 KB
- Stars: 9
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
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.jsIn 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.