Git Product home page Git Product logo

prescribe's Introduction

THIS REPOSITORY IS NO LONGER MAINTAINED AND IS ARCHIVED.

Overview

Version License Build Status Code Climate Coverage Dependencies Commitizen friendly semantic-release Gitter

Tiny, forgiving HTML parser in JavaScript. This module started life as code from John Resig and has evolved and morphed over the years of hardening and use in Krux's postscribe library. This library doesn't aim for the greatest level of compliance, but rather to be as forgiving of real-life code encountered in scenarios like ad serving.

Installation

Browser

If you just want to use the script without installing anything:

<script src="dist/prescribe.min.js"></script>

NPM

You can include prescribe using npm:

npm install --save prescribe

This script runs in browsers, so this assumes you're using a module bundler like webpack, Browserify, JSPM or Rollup to consume CommonJS modules.

Accessing

ES6/ES2015

import HtmlParser from 'prescribe';

AMD

define(['prescribe'], function(HtmlParser) {

});

CommonJS

var HtmlParser = require('prescribe');

Browser Compatibility

This module is meant to parse any HTML that you can throw at it and do something meaningful, and we've taken care to make sur that it works on every browser we can get our hands on. We expect it to work on every browser built after 2009. There are over 30 unit tests that run on every commit. Prescribe is thoroughly tested and known to work well in the following browsers:

  • Firefox 4+
  • Chrome 10+
  • Safari 5.0+
  • Opera 10.0+
  • Internet Explorer 8+
  • iPhone/iPad and other WebKit-based browsers

Note that we do not provide any support for Internet Explorer versions earlier than IE8.

Help/Bugs/Requests

We β™₯ bug reports.

Have a problem? Need help? Would you like additional functionality added? We use GitHub's ticket system for keeping track of these requests.

Please check out the existing issues, and if you don't see that your problem is already being worked on, please file a new issue. The more information the better to describe your problem.

Contributing

We β™₯ forks and pull requests.

Please see CONTRIBUTING.md for full details.

Environment

The project requires nodejs (>=5.6) and npm (>=3.6.0) for development. It has no runtime dependencies.

Developing

Check the code out and install the development dependencies using:

npm install

Building

To build the code, run

npm run build

Linting

We use ESLint and JSCS to do static analysis of the JavaScript and keep things smelling good. To run both, use:

npm run lint

Testing

Using travis-ci, the Mocha unit tests are run on every commit using PhantomJS to run the tests with a real browser.

To test the code locally, you can use:

npm test

To run tests in Test-Driven-Development mode, where the test will be run after every change, use:

npm run tdd

To run the cross-browser tests, use:

npm run test:cross-browser

Issue Guidelines

Please either add a failing unit test or include a jsfiddle that distills and reproduces the issue.

License

We aim for you to use this inside your application, so we picked the least restrictive license we could find: the MIT license.

See LICENSE.

prescribe's People

Contributors

ayxos avatar dllopis-sfdc avatar greenkeeperio-bot avatar sfdc-aaronkulick avatar strugee avatar svc-scm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

prescribe's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because we are using your CI build statuses to figure out when to notify you about breaking changes.

Since we did not receive a CI status on the greenkeeper/initial branch, we assume that you still need to configure it.

If you have already set up a CI for this repository, you might need to check your configuration. Make sure it will run on all new branches. If you don’t want it to run on every branch, you can whitelist branches starting with greenkeeper/.

We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

Add to cdnjs

We should add prescribe to cdnjs as a parity feature w/ htmlParser

Can't handle slightly corrupted HTML

Browsers can handle this:

document.write('<img src"abc.jpg"><div>WORKS</div>')

Well Firefox prints <img src"abc.jpg"="">, but the div is still there.

Postscribe can't handle this.

<html>
<style>img { display: none }</style>
<div id="js">
<script>document.write('<img src"abc.jpg"><div>OK</div>')</script>
</div>
<div id="ps1"></div>
<div id="ps2"></div>
<script src="https://raw.github.com/krux/postscribe/master/dist/postscribe.js"></script>
<script>
postscribe('#ps1', '<script>document.write(\'<div>OK</div>\')</sc' + 'ript>');
postscribe('#ps2', '<script>document.write(\'<img src"abc.jpg"><div>OK</div>\')</sc' + 'ript>');
</script>
</html>

Expected behavior: 3 times OK. Actual behavior: 2 times OK

Error building with '@semantic-release', 'babel-runtime', 'webpack-core' and '@bahmutov' modules

In order to create a PR, I am trying to build the lib via npm run build, but i get the following error with '@semantic-release', 'babel-runtime', 'webpack-core' and '@bahmutov' modules. What am i doing wrong?

Thanks

[16:19:09] Requiring external module babel-register
node_modules/nan
resolve failed:  { Error: Cannot find module '@semantic-release'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.requireRelative.resolve (/Users/marco/projects/prescribe/node_modules/require-relative/index.js:30:17)
    at resolve (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:26:26)
    at findAndRemove (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:83:11)
    at /Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:126:13
    at Array.map (native)
    at loadPreset (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:118:29)
    at module.exports (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/index.js:95:19)
    at Object.<anonymous> (/Users/marco/projects/prescribe/node_modules/babel-preset-es2015-loose/index.js:5:18)
    at Module._compile (module.js:556:32)
    at Module._extensions..js (module.js:565:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/marco/projects/prescribe/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at /Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:302:17
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:270:20)
    at OptionManager.mergePresets (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:259:10)
    at OptionManager.mergeOptions (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:244:14) code: 'MODULE_NOT_FOUND' } @semantic-release
resolve failed:  { Error: Cannot find module 'babel-runtime'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.requireRelative.resolve (/Users/marco/projects/prescribe/node_modules/require-relative/index.js:30:17)
    at resolve (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:26:26)
    at findAndRemove (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:83:11)
    at /Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:126:13
    at Array.map (native)
    at loadPreset (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:118:29)
    at module.exports (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/index.js:95:19)
    at Object.<anonymous> (/Users/marco/projects/prescribe/node_modules/babel-preset-es2015-loose/index.js:5:18)
    at Module._compile (module.js:556:32)
    at Module._extensions..js (module.js:565:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/marco/projects/prescribe/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at /Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:302:17
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:270:20)
    at OptionManager.mergePresets (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:259:10)
    at OptionManager.mergeOptions (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:244:14) code: 'MODULE_NOT_FOUND' } babel-runtime
resolve failed:  { Error: Cannot find module 'webpack-core'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.requireRelative.resolve (/Users/marco/projects/prescribe/node_modules/require-relative/index.js:30:17)
    at resolve (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:26:26)
    at findAndRemove (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:83:11)
    at /Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:126:13
    at Array.map (native)
    at loadPreset (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:118:29)
    at module.exports (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/index.js:95:19)
    at Object.<anonymous> (/Users/marco/projects/prescribe/node_modules/babel-preset-es2015-loose/index.js:5:18)
    at Module._compile (module.js:556:32)
    at Module._extensions..js (module.js:565:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/marco/projects/prescribe/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at /Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:302:17
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:270:20)
    at OptionManager.mergePresets (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:259:10)
    at OptionManager.mergeOptions (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:244:14) code: 'MODULE_NOT_FOUND' } webpack-core
resolve failed:  { Error: Cannot find module '@bahmutov'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.requireRelative.resolve (/Users/marco/projects/prescribe/node_modules/require-relative/index.js:30:17)
    at resolve (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:26:26)
    at findAndRemove (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:83:11)
    at /Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:126:13
    at Array.map (native)
    at loadPreset (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/lib/serialize.js:118:29)
    at module.exports (/Users/marco/projects/prescribe/node_modules/modify-babel-preset/index.js:95:19)
    at Object.<anonymous> (/Users/marco/projects/prescribe/node_modules/babel-preset-es2015-loose/index.js:5:18)
    at Module._compile (module.js:556:32)
    at Module._extensions..js (module.js:565:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/marco/projects/prescribe/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at /Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:302:17
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:270:20)
    at OptionManager.mergePresets (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:259:10)
    at OptionManager.mergeOptions (/Users/marco/projects/prescribe/node_modules/babel-core/lib/transformation/file/options/option-manager.js:244:14) code: 'MODULE_NOT_FOUND' } @bahmutov
semantic-release ERR! post Command "build" not recognized. Use either "pre" or "post"

Can't handle nested tables and ul's (and probably some other tags)

The parser doesn't handle nesting very well. Here are two examples that are broken:


<table width="90%" cellspacing="0" cellpadding="10" border="2">
<tbody>
<tr>
    <td>
        <table width="80%" cellspacing="0" cellpadding="0" border="1">
        <tbody>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>&nbsp;
            </td>
        </tr>
        </tbody>
        </table>
    </td>
</tr>
</tbody>
</table>

And:

<ul>
<li>1 - one
<li>2 - two"
<ul><li>a - aaya<li>b - bee</ul>
<li>3 - three
</ul>

Doesn't render Facebook creatives

The version of prescribe.js doesn't render Facebook creatives as Facebook uses "custom" HTML tags for their ad placements.

These are not recognized by postscribe.js without a fix - I've did some changes to it already and found it to enable the rendering of those creatives, but there might be more code changes needed, so please check and adapt if necessary.

prescribe/src/streamReaders.js

   // Regular Expressions for parsing tags and attributes
-  var startTag = /^<([\-A-Za-z0-9_!]+)((?:\s+[\w\-]+(?:\s*=?\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/;
-  var endTag = /^<\/([\-A-Za-z0-9_]+)[^>]*>/;
+  var startTag = /^<([\-A-Za-z0-9_!:]+)((?:\s+[\w\-]+(?:\s*=?\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/;
+  var endTag = /^<\/([\-A-Za-z0-9_:]+)[^>]*>/;
   var attr = /(?:([\-A-Za-z0-9_]+)\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))|(?:([\-A-Za-z0-9_]+)(\s|$)+)/g;
   var fillAttr = /^(checked|compact|declare|defer|disabled|ismap|multiple|nohref|noresize|noshade|nowrap|readonly|selected)$/i;

I dont know if this is just for me or if its mergeable.

IFRAME is not handled as atomic token

If the IFRAME is splitted in 2 writes like:
document.write('<IFRAME SRC="...">');
document.write('</IFRAME>');
then it's not treated as an atomic token but the first write as start tag which is not fine.
An side effect is that the 'done' write function is not executed and the active stream is not reset.

The solution I found working is adding the iframe check to the peekTokenImpl function
like:
if (token.type === 'startTag' && /script|style|iframe/i.test(token.tagName)) {
return null;
}

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.