Giter Club home page Giter Club logo

hyperglue's Introduction

hyperglue

update html elements by mapping query selectors to attributes, text, and hypertext

browser support

build status

This module works in both node and the browser.

example

in the browser

var hyperglue = require('hyperglue');
var fs = require('fs');
var html = fs.readFileSync(__dirname + '/article.html');

function createArticle (doc) {
    var name = doc.title.replace(/[^A-Za-z0-9]+/g,'_');
    return hyperglue(html, {
        '.title a': {
            name: name,
            href: '#' + name,
            _text: doc.title
        },
        '.commit': doc.commit,
        '.author': doc.author,
        '.date': doc.date,
        '.body': { _html: doc.body }
    });
}

document.body.appendChild(createArticle({
    file: 'grobot.markdown',
    author: 'James Halliday',
    date: 'Mon Dec 24 15:31:27 2012 -0800',
    title: 'robots are pretty great',
    commit: '81c62aa62b6770a2f6bdf6865d393daf05930b4a',
    body: '<h1>robots!</h1>\n\n<p>Pretty great basically.</p>'
}));

document.body.appendChild(createArticle({
    file: 'test.markdown',
    author: 'James Halliday',
    date: 'Mon Dec 24 04:31:53 2012 -0800',
    title: 'testing title',
    commit: '2a516000d239bbfcf7cdbb4b5acf09486bdf9586',
    body: '<h1>title text</h1>\n\n<p>beep boop.</p>\n\n<p><em>rawr</em></p>'
}));

Compile this code with browserify and brfs:

$ npm install -g browserify; npm install brfs
$ browserify -t brfs browser.js > bundle.js

Then just do:

<script src="bundle.js"></script>

in node

var hyperglue = require('hyperglue');
var fs = require('fs');
var html = fs.readFileSync(__dirname + '/article.html');

function createArticle (doc) {
    var name = doc.title.replace(/[^A-Za-z0-9]+/g,'_');
    return hyperglue(html, {
        '.title a': {
            name: name,
            href: '#' + name,
            _text: doc.title
        },
        '.commit': doc.commit,
        '.author': doc.author,
        '.date': doc.date,
        '.body': { _html: doc.body }
    });
}

console.log(createArticle({
    file: 'grobot.markdown',
    author: 'James Halliday',
    date: 'Mon Dec 24 15:31:27 2012 -0800',
    title: 'robots are pretty great',
    commit: '81c62aa62b6770a2f6bdf6865d393daf05930b4a',
    body: '<h1>robots!</h1>\n\n<p>Pretty great basically.</p>'
}).innerHTML);

console.log(createArticle({
    file: 'test.markdown',
    author: 'James Halliday',
    date: 'Mon Dec 24 04:31:53 2012 -0800',
    title: 'testing title',
    commit: '2a516000d239bbfcf7cdbb4b5acf09486bdf9586',
    body: '<h1>title text</h1>\n\n<p>beep boop.</p>\n\n<p><em>rawr</em></p>'
}).innerHTML);

arrays

You can also duplicate existing elements in order to render arrays of results:

var hyperglue = require('hyperglue');

var html = [
    '<div id="rows">',
    '<div class="row">',
    '<span class="name"></span>',
    '<span class="message"></span>',
    '</div>',
    '<b>ahoy!</b>',
    '</div>'
].join('\n');

console.log(hyperglue(html, {
    '.row': [
        { '.name': 'T-REX', '.message': 'RAWR' },
        { '.name': 'robot', '.message': 'beep boop' },
        { '.name': 'Dr X', '.message': 'mwahaha' }
    ]
}).outerHTML);

output:

<div id="rows">
<div class="row">
<span class="name">T-REX</span>
<span class="message">RAWR</span>
</div>
<div class="row">
<span class="name">robot</span>
<span class="message">beep boop</span>
</div>
<div class="row">
<span class="name">Dr X</span>
<span class="message">mwahaha</span>
</div>
<b>ahoy!</b>
</div>

methods

var hyperglue = require('hyperglue')

hyperglue(src, updates)

Return an html element from the source string or element src with updates applied to it. In the browser you get a complete html element. In node you get an object with an innerHTML property populated with the string contents of the replacement.

updates should have query selectors as keys and target strings, numbers, or objects as values.

Each update query selector can have the special pseudo-class :first which causes the selector to only match the first value like querySelector() instead of all the matching elements like querySelectorAll(), the default.

If the target values in updates are strings or numbers, set the inner text content of the matching elements to that value.

When the target values are html elements, replace the inner content at the selected element with a clone of the value.

For target values of arrays, recursively apply hyperglue(node.cloneNode(), value) for each matching element in the array and then remove the original node. This feature makes rendering arrays of content super simple.

If the target values in updates are non-html element objects, update the attributes on all matching elements with the keys in the target values. Use '_text' to set the text content and '_html' to set the innerHTML in object form. If '_html' is an HTML element, replace the inner content at the selector elements with a clone of the '_html' value.

install

With npm do:

npm install hyperglue

license

MIT

kudos

Inspried by plates.

hyperglue's People

Contributors

juliangruber avatar ashnur avatar jcrugzz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.