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('
the happs
')
.css({
color: 'red',
'background-color': 'white'
})
.show()
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.
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')
bonzo().get()bonzo().each()bonzo().deepEach()bonzo().map()bonzo().html()bonzo().text()bonzo().addClass()bonzo().removeClass()bonzo().hasClass()bonzo().toggleClass()bonzo().show()bonzo().hide()bonzo().toggle()bonzo().first()bonzo().last()bonzo().next()bonzo().previous()bonzo().parent()bonzo().focus()bonzo().blur()bonzo().append()bonzo().appendTo()bonzo().prepend()bonzo().prependTo()bonzo().before()bonzo().insertBefore()bonzo().after()bonzo().insertAfter()bonzo().replaceWith()bonzo().css()bonzo().offset()bonzo().dim()bonzo().attr()bonzo().removeAttr()bonzo().val()bonzo().data()bonzo().remove()bonzo().empty()bonzo().detach()bonzo().scrollLeft()bonzo().scrollTop()bonzo.aug()bonzo.doc()bonzo.viewport()bonzo.firstChild()bonzo.isAncestor()bonzo.create()bonzo.setQueryEngine()Added in the Ender bridge:
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...
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
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.
deepEach() ...
map() ...
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.bonzo(element).html('
foo
');
bonzo(element).html(); //
foo
bonzo.text() is very similar to .html, but uses the elementstextContentinstead ofinnerHTMLwhen setting thecontent. Thus, thecontent` 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.
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) 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").
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("
i want lots of classes
").addClass("one two three")
// the html is now
i want lots of classes
// → returns a Bonzo object
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").
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("
i have lots of classes
").removeClass("one two three")
// the html is now
i have lots of classes
// → 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) 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.
bonzo("
something went wrong
").hasClass('alert')
// → returns true
bonzo("
something went wrong
").hasClass('normal')
// → returns false
bonzo("
something went wrong
").hasClass('one two three')
// → returns true
bonzo("
something went wrong
").hasClass('three two one')
// → returns true
bonzo("
something went wrong
").hasClass('small tiny')
// → returns false
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").
``` js
bonzo("
something went wrong
").toggleClass('alert') // the html is now
something went wrong
// → returns a Bonzo object
bonzo("
something went wrong
").toggleClass('different') // the html is now
something went wrong
// → returns a Bonzo object
bonzo("
something went wrong
").toggleClass('three two one') // the html is now
something went wrong
// → returns a Bonzo object
bonzo("
something went wrong
").toggleClass('small tiny') // the html is
$ claude mcp add bonzo \
-- python -m otcore.mcp_server <graph>