Skip to content

ded/bonzo

Repository files navigation

Bonzo

A library agnostic extensible DOM utility. Nothing else.

Bonzo is designed to live in any host library, such as Ender, or simply as a stand-alone tool for the majority of your DOM-related tasks.

It looks like this:

bonzo(elements)
  .hide()
  .addClass('foo')
  .append('<p>the happs</p>')
  .css({
    color: 'red',
    'background-color': 'white'
  })
  .show()


Use with a selector engine

A great way to use Bonzo is with a selector engine, like Qwery. You could wrap Bonzo up and augment your wrapper to inherit the same methods:

function $(selector) {
  return bonzo(qwery(selector))
}

This now allows you to write the following code:

$('#content a[rel~="bookmark"]').after('√').css('text-decoration', 'none')

See bonzo.setQueryEngine() for more details.

Bonzo extension API

One of the greatest parts about Bonzo is its simplicity to hook into the internal chain to create custom methods. For example you can create a method called color() like this:

bonzo.aug({
  color: function (c) {
    return this.css('color', c);
  }
})

// you can now do the following
$('p').color('aqua')

Complete Bonzo API

Instance methods

Static methods

Added in the Ender bridge:


bonzo(DOMElement | ArrayLikeDOMElementCollection)

Factory function for bonzo objects. Takes in either a single DOMElement, or an array-like object or array of them. Returns an array-like Bonzo object possessing all of the instance methods documented below.

var elem = document.getElementById('foo');
var $elem = bonzo(elem);
// $elem now has all the special powers listed below...

bonzo().get(index)

Returns the raw DOMElement held at index. Because Bonzo objects are array-like, this is identical to saying bonzo()[index].

var elem = document.getElementById('bar');
var $elem = bonzo(elem);
var sameElem = $elem.get(0);
var sameElemAgain = $elem[0];
// elem === sameElem && sameElem === sameElemAgain

bonzo().each(fn[, scope])

Allows you to iterate over the raw elements contained in bonzo collections. fn gets called once for each element in the collection, with each element, in turn, as its first argument. If the optional scope argument is supplied, then it is used as the this value of the function. Otherwise, the same element that is passed as the first argument is used. The index of the element is passed as the second argument, and the collection itself is passed as the third.


bonzo().deepEach(fn[, scope])

deepEach() ...


bonzo().map(fn[, rejectFn])

map() ...


bonzo().html([content])

bonzo.html() either sets or gets the elements' innerHTML to content, depending if the optional content argument is pased in. If called without the argument, .html() returns the element's innerHTML.

  • content is an optional argument. If it is passed in, it will set the innerHTML of a given element and return a Bonzo object.

Examples

bonzo(element).html('<p>foo</p>');
bonzo(element).html(); // <p>foo</p>

bonzo().text([content])

bonzo.text() is very similar to .html, but uses the elements' textContent instead of innerHTML when setting the content. Thus, the content will not get parsed as markup.

This method either gets or sets the text of a given element, depending if the optional content argument is passed in.

  • content is an optional argument. If it is passed in, it will set the text value of a given element and return a Bonzo object.

If no content is specified, the .text() method will return the text that makes up that element.

If the element has children (i.e. a ul containing several li children), the children's text is included in the return value.

Examples

bonzo("<h1>hello, world</h1>").text()
  // →  returns "hello, world"

bonzo("<h1>i'm going to change</h1>").text("changed you!")
  // the <h1> now says "changed you!"
  // →  returns a Bonzo object

bonzo("<ul><li>one</li><li>two</li></ul>").text()
  // →  returns "one
  // two"

bonzo("<ul><li>one</li><li>two</li></ul>").text('hello')
  // the html is now <ul>hello</ul>
  // →  returns a Bonzo object

bonzo().addClass(class | classList)

bonzo.addClass(class | classList) adds the specified class to the given element. It returns a Bonzo object.

  • class is a required argument. It is the name of the class you wish to add to the given element.

    • If you'd like to add multiple classes at once, simply use a space-separated string, a classList (i.e. "classOne classTwo").

Examples

bonzo("<h1>hello, world</h1>").addClass('big')
  // the html is now <h1 class="big">hello, world</h1>
  // →  returns a Bonzo object

bonzo("<h1>hello, world</h1>").addClass()
  //  throws an error, since the argument is required

bonzo("<p>i want lots of classes</p>").addClass("one two three")
  // the html is now <p class="one two three">i want lots of classes</p>
  // →  returns a Bonzo object

bonzo().removeClass(class | classList)

bonzo.removeClass(class) removes the specified class from the given element. It returns a Bonzo object.

  • class is a required argument. It is the name of the class you wish to remove from the given element.

    • If you'd like to remove multiple classes at once, simply use a space-separated string, a classList (i.e. "classOne classTwo").

Examples

bonzo("<h1 class='small'>hello, world</h1>").removeClass('small')
  // the html is now <h1 class>hello, world</h1>
  // →  returns a Bonzo object

bonzo("<h1 class='removeMe'>hello, world</h1>").removeClass()
  //  throws an error, since the argument is required

bonzo("<p class='one two three'>i have lots of classes</p>").removeClass("one two three")
  // the html is now <p>i have lots of classes</p>
  // →  returns a Bonzo object

bonzo("<h1 class='error'>hello, world</h1>").removeClass('does_not_exist')
  // →  since the argument does not match a classlist the <h1> has, nothing happens and a Bonzo object is returned

bonzo().hasClass(class | classList)

bonzo.hasClass(class) returns true or false, based on whether or not the specified element has a given class. It returns true if the specified element does have the class, and returns false if the specified element does not have the class.

  • class is a required argument. It is the name of the class you wish to check for in a given element.

    • NOTE: if you pass in a space-separated classList like you can in addClass and removeClass, this method will return true if any of the space-separated classList classes are present in the element.

Examples

bonzo("<p class='alert'>something went wrong</p>").hasClass('alert')
  // →  returns true

bonzo("<p class='alert'>something went wrong</p>").hasClass('normal')
  // →  returns false

bonzo("<p class='one'>something went wrong</p>").hasClass('one two three')
  // →  returns true

bonzo("<p class='one'>something went wrong</p>").hasClass('three two one')
  // →  returns true

bonzo("<p class='large'>something went wrong</p>").hasClass('small tiny')
  // →  returns false

bonzo().toggleClass(class | classList)

bonzo.toggleClass(class) either adds or removes a specified class to the given element, depending on whether or not the given element already has a class with that class or not.

If the element does have a class named class, calling toggleClass() will remove the class class from it. If the element does not have a class with the specified class, calling toggleClass() will add a class with that class.

  • class is a required argument. It is the name of the class you wish to toggle.

    • If you'd like to toggle multiple classes at once, simply use a space-separated string, a classList (i.e. "classOne classTwo").

Examples

bonzo("<p class='alert'>something went wrong</p>").toggleClass('alert')
  // the html is now <p class>something went wrong</p>
  // →  returns a Bonzo object

bonzo("<p class='alert'>something went wrong</p>").toggleClass('different')
  // the html is now <p class="alert different">something went wrong</p>
  // →  returns a Bonzo object

bonzo("<p class='one'>something went wrong</p>").toggleClass('three two one')
  // the html is now <p class="three two">something went wrong</p>
  // →  returns a Bonzo object

bonzo("<p class='large'>something went wrong</p>").toggleClass('small tiny')
  // the html is now <p class="small tiny large">something went wrong</p>
  // →  returns a Bonzo object

bonzo().show([type])

bonzo.show() sets a given element or set of elements' display style property. By passing in an optional type argument, you can specify the attribute of the display property Bonzo gives the element(s).

  • type is an optional argument. It is the display type you wish to utilize.

If you specify an unsupported type (i.e. something other than block, compact, inline-block, inline, inline-table, list-item, run-in, table, table-caption, table-cell, table-column, table-column-group, table-footer-group, table-header-group, table-row, or table-row-group), Bonzo will ignore the invalid type.

Examples

bonzo("<p style=\"display: none;\">I was hidden</p>").show()
  // html is now <p style>I was hidden</p>
  // →  returns a Bonzo object

bonzo("<p style=\"display: none;\">I was hidden</p>").show('inline-block')
  // html is now <p style="display: inline-block;">I was hidden</p>
  // →  returns a Bonzo object

bonzo().hide()

bonzo.hide() adds a display: none; to the specified element.

Examples

bonzo("<p>Hello, world</p>").hide()
  // html is now <p style="display: none;">Hello, world</p>
  // →  returns a Bonzo object

bonzo().toggle([callback[, type]])

toggle() ...


bonzo().first()

bonzo.first() returns a Bonzo object referencing the first element in a set of elements. If the set is empty, the a Bonzo object will still be returned, but it won't contain any children.

Examples

var firstItem = bonzo("<li>one</li><li>two</li><li>three</li>").first()
firstItem.text() // "one"
firstItem.length // 1
  // →  returns a Bonzo object

var el = bonzo("").first()
el.text() // ""
el.length // 0
  // →  returns a Bonzo object

bonzo().last()

bonzo.last() returns a Bonzo object referencing the last element in a set of elements. If the set is empty, the a Bonzo object will still be returned, but it won't contain any children.

Examples

var lastItem = bonzo("<li>one</li><li>two</li><li>three</li>").last()
lastItem.text() // "three"
lastItem.length // 1
  // →  returns a Bonzo object

var el = bonzo("").last()
el.text() // ""
el.length // 0
  // →  returns a Bonzo object

bonzo().next()

bonzo.next() returns a Bonzo object with a list of the next element siblings in the initial collection.


bonzo().previous()

bonzo.previous() returns a Bonzo object with a list of the previous element siblings in the initial collection.


bonzo().parent()

bonzo.parent() returns a Bonzo object with a list of the parentNode's of each item in the initial collection.


bonzo().focus()

bonzo.focus() will send the browser's focus event to an input element. This will only work on the first (zeroith index) item in the Bonzo collection.


bonzo().blur()

bonzo.blur() will send the browser's blur event to an input element. This will only work on the first (zeroith index) item in the Bonzo collection.


bonzo().append(html | element | collection)

bonzo.append() will insert the supplied html | element | collection at the initial supplied collection.

Examples

bonzo([element1, element2]).append('<p>hello</p><p>world</p>')
bonzo(document.createElement('div')).append(document.createElement('p'))
bonzo(element).append(document.getElementsByTagName('p'))

bonzo().appendTo(target)

bonzo.appendTo() will insert the initial collection at the supplied target. It's the backwards version of bonzo.append

Examples

bonzo('<p>hello</p>').appendTo(document.body)
bonzo(document.createElement('div')).appendTo(document.querySelectorAll('p'))

bonzo().prepend(html | element | collection)

bonzo.prepend() is similar to bonzo.append(), but inserts at the top of a collection (it prepends ;).


bonzo().prependTo(target)

prependTo() is similar to bonzo.appendTo(), but, you know, prepends.


bonzo().before(html | element | collection)

bonzo.before() inserts the supplied content before each item in the initial collection.

Examples

bonzo(document.querySelectorAll('p')).before('<b>hello</b>')

bonzo().insertBefore(target)

bonzo.insertBefore() will insert the items in the initial collection before the supplied targets.

Examples

bonzo('<p>hello</p>').insertBefore(document.querySelectorAll('div'))

bonzo().after(html | element | collection)

bonzo.after() will insert the supplied content after each item in the initial collection.

Examples

bonzo(document.querySelectorAll('p')).after('<b>, huh</b>')

bonzo().insertAfter(target)

insertAfter() ...


bonzo().replaceWith(html | element | collection)

bonzo.replaceWith() will replace each item in the initial collection with the supplied content.

Examples

bonzo(document.querySelectorAll('p')).replaceWith('<p>you have been replaced</p>')

bonzo().css(property | hash[, value])

Sets or returns CSS properties of the element. If a single string argument is passed, then the value of that CSS property is returned. If two string arguments are passed, the CSS property specified by the first is set to the value specified by the second. If a single hash argument is passed, then the CSS property corresponding to each property is set to the value designated by the hash property's value.

bonzo(elem).css({
  background: 'blue',
  color: green;
}).css('border', '2px solid red').css('color'); // "green"

bonzo().offset([ x, y ] | [ hash ])

bonzo.offset() is an overloaded setter and getter. When setting the offset of a collection, it will set an element to an explicit x/y coordinate position on the page. When getting the offset, the function returns an object containing the four properties top, left, width, and height.

Examples

bonzo(el).offset() // returns { top: n, left: n, width: n, height: n }
bonzo(el).offset(50, 100) // sets left to 50, and top to 100
bonzo(el).offset({ left: 50, top: 100 }) // sets left to 50, and top to 100

bonzo().dim()

bonzo.dim() returns the entire width and height dimensions of an element, including the scrollHeight.


bonzo().attr(key[, value] | hash)

Sets or returns attributes of the element. If the first argument is a hash, then each property of the hash is read and the corresponding attribute of the element is set to the hash property's value. If the first argument is a string and no second argument is provided, the value of the element's attribute with the same name is returned. If a second argument is supplied, then the element's attribute of the same name as the first argument is set to the value of the second argument.


bonzo().removeAttr(key)

bonzo.removeAttr() removes the supplied attribute from each item in the initial collection.


bonzo().val([ value ])

bonzo.val() is an overloaded setter and getter. it returns the content from the first element value attribute. When setting it sets the value attribute of each item in the initial collection.


bonzo().data([ key[, value ] ] | [ hash ])

bonzo.data() is an overloaded setter and getter. data can be set to any value and referenced further when getting the data.

Examples

bonzo(el).data('username', 'ded')
bonzo(el).data('username') // returns 'ded'

bonzo(another).data('userinfo', {
  id: 911,
  name: 'agent'
  age: 2
})

bonzo().remove()

bonzo.remove() removes the initial supplied collection from the DOM

Examples

bonzo(document.querySelectorAll('p')).remove()

bonzo().empty()

bonzo.empty() will empty out the content of the initial supplied collection, but not remove the nodes themselves.


bonzo().detach()

bonzo.detach() returns a Bonzo object containing the supplied collection, but detached from the DOM. This is useful if you wish to do heavy operations an offline Node, and then inserting it back into the DOM again.

Examples

bonzo(document.querySelectorAll('p')).detach().addClass('eyo').html('<p>stuff</p>').appendTo(document.body)

bonzo().scrollLeft([ x ])

bonzo.scrollLeft() is an overloaded setter and getter. it returns the scrollLeft of an element when no argument is supplied, otherwise, sets it.


bonzo().scrollTop([ y ]

bonzo.scrollTop() is an overloaded setter and getter. it returns the scrollTop of an element when no argument is supplied, otherwise, sets it.


bonzo.aug(hash)

bonzo.aug() will agument the Bonzo prototype so that you can customize and include additions to your liking.

Examples

bonzo.aug({
  color: function (c) {
    // `this` is the scope of the `Bonzo` instance
    return this.css('color', c)
  }
})

// you can now do the following
$('p').color('aqua')

bonzo.doc()

bonzo.doc() returns an object containing width and height information regarding the document size. This includes scrollWidth and scrollHeight.


bonzo.viewport()

bonzo.viewport() returns an object containing width and height information regarding the document viewport. This is usually the same as using bonzo.doc(), but can be smaller given it's only what you see in the viewport, and not the entire document (with scrolling).


bonzo.firstChild(element)

firstChild() ...


bonzo.isAncestor(container, element)

isAncestor() ...


bonzo.create(container, element)

create() ...


$().parents()

parents() ...


bonzo.setQueryEngine(engine)

bonzo.setQueryEngine() is a useful utility that allows you to pair Bonzo with a selector engine.

For the insertion methods you can set a query selector host:

Examples

// using Ender supported modules
bonzo.setQueryEngine(require('qwery'))
bonzo(bonzo.create('<div>')).insertAfter('.boosh a')

// or Sizzle
bonzo.setQueryEngine(Sizzle)

$().closest()

closest() ...


$().siblings()

siblings() ...


$().children()

children() ...


$().width([ value ])

width() ...


$().height([ value ])

height() ...


About the name "Bonzo"

Bonzo Madrid was a malicious battle school commander of whom eventually is killed by Ender Wiggin. Bonzo represents the DOM, of whom we'd all love to slay.

Contributing

You should only edit the files in the src/ directory. Bonzo is compiled into the bonzo.js and bonzo.min.js files contained in the root directory by the build command:

Building

$ npm install
$ make

Tests

Point your test browser(s) to tests/tests.html, or:

$ open tests/tests.html

Please try to include tests or adjustments to existing tests with all non-trivial contributions.

Browser support

  • IE9+
  • Chrome
  • Safari 5+
  • Firefox 10+
  • Opera

Ender integration

Bonzo is a registered npm package and fits in nicely with the Ender framework. If you don't have Ender, you should install now, and never look back, ever. As a side note the query engine host is set for you when you include it with Ender.

$ npm install ender -g

To combine Bonzo to your Ender build, you can add it as such:

$ ender build bonzo[ package-b[ package-c ...]]

or, add it to your existing ender package

$ ender add bonzo

Bonzo is included in The Jeesh, Ender's "starter-pack", when you ender build jeesh you'll get Bonzo and some other amazing libraries that'll make working in the browser a breeze. See the Ender documentation for more details.

Contributors

Licence & copyright

Bonzo is Copyright © 2014 Dustin Diaz @ded and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.