GithubHelp home page GithubHelp logo

jquery.htmlparser's Introduction

jQuery.htmlParser plugin

This plugin can parse, clean or transform an HTML/XML document.

This code was originally written by Erik Arvidsson:
http://erik.eae.net/simplehtmlparser/simplehtmlparser.js
and then changed by John Resig:
http://ejohn.org/files/htmlparser.js
and then changed by Sam Blowes:
https://github.com/blowsie/Pure-JavaScript-HTML5-Parser
and then changed by me.

Installation

Download the jquery.htmlparser.js file in your project.

Examples

Examples 1: Clean a bad-formed HTML/XML document

// Example 1: fixes a bad-formed HTML document
$html = $.htmlParser('<p>Bad formed<br> html document');

Example 2: Parse an HTML/XML document

// Example 2: parses an HTML document
var html =
    '<p>Actually <strong>we do not exist</strong>.<br />' +
    'But before we can prove it, <em>we will have already disappeared.</em></p>';
$.htmlParser(html, {
        start: function () {
            // 'this' is a jQuery object representing the current node
            console.log('Start tag: <' + this.prop('tagName') + '>');
        },
        end: function () {
            console.log('End tag: </' + this.prop('tagName') + '>');
        },
        text: function () {
            console.log('Text: ' + this.text());
        },
        comment: function (text) {
            console.log('Comment: ' + this.text());
        }
});

Example 3: Transform an HTML/XML document to a new one

// Example 3: transforms an HTML document to another one
// This example replaces the following CSS properties:
//     1. 'font-weight: bold' is replaced by '<strong>'
//     2. 'font-style: italic' is replaced by '<em>'
//     3. 'text-decoration: underline' is replaced by '<u>'
var html =
    'The quick <span style="font-weight: bold; ">brown</span> fox jumps over the ' +
    '<span style="font-style: italic; ">lazy dog</span> and feels as if ' +
    '<span style="text-decoration: underline; font-weight: bold; ">he were in the </span> ' +
    'seventh <span style="font-weight: bold; font-style: italic; ">heaven of</span> ' +
    'typography together with Hermann Zapf, the most famous artist of the...';
var str = $.htmlParser(html, function () {
    var ret = this;
    var replacements = [
        {style: 'font-weight', value: 'bold', entity: 'strong'},
        {style: 'font-style', value: 'italic', entity: 'em'},
        {style: 'text-decoration', value: 'underline', entity: 'u'}
    ];
    
    // 'this' is an object representing the current node
    if (this.prop('tagName') == 'SPAN') {
        var target = this;
        
        $.each(replacements, function () {
            if (target.css(this.style) == this.value) {
                // wraps the result around the corresponding entity
                ret = $('<' + this.entity + ' />').append(ret);
                
                // removes the css style
                target.css(this.style, '');
                
                // removes the 'span' node if it doesn't have any attribute
                if (target[0].attributes.length == 0) {
                    target.replaceWith(target.contents());
                }
            }
        });
    }
    
    return ret;
});
console.log(str);

jquery.htmlparser's People

Contributors

blowsie avatar

Watchers

James Cloos avatar Ifeanyi Agu 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.