MCPcopy Index your code
hub / github.com/ded/bonzo

github.com/ded/bonzo @v2.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.0.0 ↗ · + Follow
223 symbols 552 edges 21 files 38 documented · 17% updated 10y ago★ 1,31017 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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('

the happs

')
  .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('

foo

');
bonzo(element).html(); // 

foo



bonzo().text([content])

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.

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("

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 | 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("

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 | 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("

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 | 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

``` 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

Core symbols most depended-on inside this repo

insertionTest
called by 160
tests/dommanip_insertions-test.js
verifyReturnType
called by 160
tests/dommanip_insertions-test.js
createElementMulti
called by 28
tests/dommanip_insertions-test.js
createElementSingle
called by 27
tests/dommanip_insertions-test.js
getOwn
called by 26
integration/external/require.js
createMulti
called by 20
tests/dommanip_insertions-test.js
createSingle
called by 19
tests/dommanip_insertions-test.js
each
called by 15
bonzo.js

Shape

Function 223

Languages

TypeScript100%

Modules by API surface

integration/ender.js40 symbols
integration/ender.min.js37 symbols
integration/external/require.js33 symbols
src/bonzo.js26 symbols
bonzo.js26 symbols
bonzo.min.js25 symbols
tests/dommanip_insertions-test.js14 symbols
tests/emptycollection-test.js7 symbols
tests/ender-js.js5 symbols
tests/attributes-test.js3 symbols
src/ender.js3 symbols
tests/selectorengine-test.js2 symbols

For agents

$ claude mcp add bonzo \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact