拿掉 build files

This commit is contained in:
2022-07-18 16:33:23 +08:00
parent 41e2287bcb
commit 3707f158a3
31953 changed files with 0 additions and 4411796 deletions
-3
View File
@@ -1,3 +0,0 @@
test/
dist/
node_modules/
-142
View File
@@ -1,142 +0,0 @@
module.exports = {
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"@typescript-eslint/tslint"
],
"rules": {
"@typescript-eslint/prefer-includes": "off",
"@typescript-eslint/unbound-method": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/member-delimiter-style": "error",
"@typescript-eslint/no-misused-promises": "error",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/ban-types": "error",
"@typescript-eslint/class-name-casing": "error",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/indent": [
"error",
"tab",
{
"ArrayExpression": "first",
"ObjectExpression": "first"
}
],
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "error",
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/quotes": [
"error",
"single",
{
"avoidEscape": true
}
],
"@typescript-eslint/triple-slash-reference": "error",
"@typescript-eslint/unified-signatures": "off",
"camelcase": "off",
"comma-dangle": "error",
"complexity": "off",
"constructor-super": "error",
"dot-notation": "error",
"eqeqeq": [
"error",
"smart"
],
"guard-for-in": "off",
"id-blacklist": [
"error",
"any",
"Number",
"number",
"String",
"string",
"Boolean",
"boolean",
"Undefined"
],
"id-match": "error",
"max-classes-per-file": [
"error",
1
],
"max-len": "off",
"new-parens": "error",
"no-bitwise": "off",
"no-caller": "error",
"no-cond-assign": "off",
"no-console": "off",
"no-debugger": "error",
"no-empty": "off",
"no-eval": "error",
"no-fallthrough": "off",
"no-invalid-this": "off",
"no-multiple-empty-lines": "off",
"no-new-wrappers": "error",
"no-shadow": [
"error",
{
"hoist": "all"
}
],
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef-init": "error",
"no-underscore-dangle": "off",
"no-unsafe-finally": "error",
"no-unused-expressions": "error",
"no-unused-labels": "error",
"no-var": "error",
"object-shorthand": "error",
"one-var": [
"error",
"never"
],
"prefer-arrow/prefer-arrow-functions": "off",
"prefer-const": "error",
"radix": "error",
"spaced-comment": "error",
"use-isnan": "error",
"valid-typeof": "off",
"@typescript-eslint/tslint/config": [
"error",
{
"rules": {
"jsdoc-format": true,
"no-reference-import": true,
"no-unsafe-any": true,
"whitespace": [
true,
"check-branch",
"check-operator"
]
}
}
]
}
};
-226
View File
@@ -1,226 +0,0 @@
{
"env": {
"es6": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true,
"jsx": true
},
"project": "tsconfig.json",
"extraFileExtensions": [
".ts",
".tsx"
]
},
"plugins": [
"import",
"@typescript-eslint"
],
"rules": {
"no-prototype-builtins": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"arrow-parens": [
"off"
],
"brace-style": [
"off",
"stroustrup"
],
"consistent-return": "off",
"camelcase": "off",
"@typescript-eslint/camelcase": "off",
"curly": [
"error",
"multi-line",
"consistent"
],
"eol-last": "error",
"linebreak-style": [
"error",
"unix"
],
"new-parens": "error",
"no-console": "off",
"no-constant-condition": [
"warn",
{
"checkLoops": false
}
],
"no-caller": "error",
"no-debugger": "warn",
"no-dupe-class-members": "off",
"no-duplicate-imports": "error",
"no-else-return": "warn",
"no-empty": [
"warn",
{
"allowEmptyCatch": true
}
],
"no-eval": "error",
"no-ex-assign": "warn",
"no-extend-native": "error",
"no-extra-bind": "error",
"no-floating-decimal": "error",
"no-implicit-coercion": "error",
"no-implied-eval": "error",
// Turn off until fix for: https://github.com/typescript-eslint/typescript-eslint/issues/239
"no-inner-declarations": "off",
"no-lone-blocks": "error",
"no-lonely-if": "error",
"no-loop-func": "error",
"no-multi-spaces": "error",
"no-return-assign": "error",
"no-return-await": "warn",
"no-self-compare": "error",
"no-sequences": "error",
"no-template-curly-in-string": "warn",
"no-throw-literal": "error",
"no-unmodified-loop-condition": "warn",
"no-unneeded-ternary": "error",
"no-unused-expressions": [
"warn",
{
"allowShortCircuit": true
}
],
"no-use-before-define": "off",
"no-useless-call": "error",
"no-useless-catch": "error",
"no-useless-computed-key": "error",
"no-useless-concat": "error",
"no-useless-rename": "error",
"no-useless-return": "error",
"no-var": "error",
"no-with": "error",
"object-shorthand": "off",
"one-var": [
"error",
"never"
],
"prefer-arrow-callback": "error",
"prefer-const": "error",
"prefer-numeric-literals": "error",
"prefer-object-spread": "error",
"prefer-rest-params": "error",
"prefer-spread": "error",
"prefer-template": "error",
"quotes": "off",
// Turn off until fix for: https://github.com/eslint/eslint/issues/11899
"require-atomic-updates": "off",
"semi": [
"error",
"always"
],
"semi-style": [
"error",
"last"
],
"sort-imports": [
"error",
{
"ignoreCase": true,
"ignoreDeclarationSort": true,
"ignoreMemberSort": false,
"memberSyntaxSortOrder": [
"none",
"all",
"multiple",
"single"
]
}
],
"yoda": "error",
"import/export": "off",
"import/extensions": [
"error",
"never"
],
"import/named": "off",
"import/namespace": "off",
"import/newline-after-import": "warn",
"import/no-cycle": "off",
"import/no-dynamic-require": "error",
"import/no-default-export": "off",
"import/no-duplicates": "error",
"import/no-self-import": "error",
"import/no-unresolved": [
"warn",
{
"ignore": [
"vscode"
]
}
],
"import/order": [
"warn",
{
"groups": [
"builtin",
"external",
"internal",
[
"index",
"sibling",
"parent"
]
]
}
],
"@typescript-eslint/require-await": "off",
"@typescript-eslint/consistent-type-assertions": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-member-accessibility": "error",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/no-empty-function": [
"warn",
{
"allow": [
"constructors"
]
}
],
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-inferrable-types": [
"warn",
{
"ignoreParameters": true,
"ignoreProperties": true
}
],
"@typescript-eslint/no-misused-promises": [
"error",
{
"checksVoidReturn": false
}
],
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
"args": "none"
}
],
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/unbound-method": "off" // Too many bugs right now: https://github.com/typescript-eslint/typescript-eslint/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+unbound-method
}
}
-1
View File
@@ -1 +0,0 @@
require: blanket,should,spec
-7
View File
@@ -1,7 +0,0 @@
Copyright 2019 Tao Qiufeng
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-251
View File
@@ -1,251 +0,0 @@
# Fast HTML Parser [![NPM version](https://badge.fury.io/js/node-html-parser.png)](http://badge.fury.io/js/node-html-parser) [![Build Status](https://travis-ci.org/taoqf/node-html-parser.svg?branch=master)](https://travis-ci.org/taoqf/node-html-parser)
Fast HTML Parser is a _very fast_ HTML parser. Which will generate a simplified
DOM tree, with element query support.
Per the design, it intends to parse massive HTML files in lowest price, thus the
performance is the top priority. For this reason, some malformatted HTML may not
be able to parse correctly, but most usual errors are covered (eg. HTML4 style
no closing `<li>`, `<td>` etc).
## Install
```shell
npm install --save node-html-parser
```
> Note: when using Fast HTML Parser in a Typescript project the minimum Typescript version supported is `^4.1.2`.
## Performance
Faster than htmlparser2!
```shell
htmlparser :26.7111 ms/file ± 170.066
cheerio :24.2480 ms/file ± 17.1711
parse5 :13.7239 ms/file ± 8.68561
high5 :7.75466 ms/file ± 5.33549
htmlparser2 :5.27376 ms/file ± 8.68456
node-html-parser:2.85768 ms/file ± 2.87784
```
Tested with [htmlparser-benchmark](https://github.com/AndreasMadsen/htmlparser-benchmark).
## Usage
```ts
import { parse } from 'node-html-parser';
const root = parse('<ul id="list"><li>Hello World</li></ul>');
console.log(root.firstChild.structure);
// ul#list
// li
// #text
console.log(root.querySelector('#list'));
// { tagName: 'ul',
// rawAttrs: 'id="list"',
// childNodes:
// [ { tagName: 'li',
// rawAttrs: '',
// childNodes: [Object],
// classNames: [] } ],
// id: 'list',
// classNames: [] }
console.log(root.toString());
// <ul id="list"><li>Hello World</li></ul>
root.set_content('<li>Hello World</li>');
root.toString(); // <li>Hello World</li>
```
```js
var HTMLParser = require('node-html-parser');
var root = HTMLParser.parse('<ul id="list"><li>Hello World</li></ul>');
```
## Global Methods
### parse(data[, options])
Parse given data, and return root of the generated DOM.
- **data**, data to parse
- **options**, parse options
```js
{
lowerCaseTagName: false, // convert tag name to lower case (hurt performance heavily)
comment: false // retrieve comments (hurt performance slightly)
blockTextElements: {
script: true, // keep text content when parsing
noscript: true, // keep text content when parsing
style: true, // keep text content when parsing
pre: true // keep text content when parsing
}
}
```
### valid(data[, options])
Parse given data, return true if the givent data is valid, and return false if not.
## HTMLElement Methods
### HTMLElement#trimRight()
Trim element from right (in block) after seeing pattern in a TextNode.
### HTMLElement#removeWhitespace()
Remove whitespaces in this sub tree.
### HTMLElement#querySelectorAll(selector)
Query CSS selector to find matching nodes.
Note: Full css3 selector supported since v3.0.0.
### HTMLElement#querySelector(selector)
Query CSS Selector to find matching node.
### HTMLElement#closest(selector)
Query closest element by css selector.
### HTMLElement#appendChild(node)
Append a child node to childNodes
### HTMLElement#insertAdjacentHTML(where, html)
parses the specified text as HTML and inserts the resulting nodes into the DOM tree at a specified position.
### HTMLElement#setAttribute(key: string, value: string)
Set `value` to `key` attribute.
### HTMLElement#setAttributes(attrs: Record<string, string>)
Set attributes of the element.
### HTMLElement#removeAttribute(key: string)
Remove `key` attribute.
### HTMLElement#getAttribute(key: string)
Get `key` attribute.
### HTMLElement#exchangeChild(oldNode: Node, newNode: Node)
Exchanges given child with new child.
### HTMLElement#removeChild(node: Node)
Remove child node.
### HTMLElement#toString()
Same as [outerHTML](#htmlelementouterhtml)
### HTMLElement#set_content(content: string | Node | Node[])
Set content. **Notice**: Do not set content of the **root** node.
### HTMLElement#remove()
Remove current element.
### HTMLElement#replaceWith(...nodes: (string | Node)[])
Replace current element with other node(s).
### HTMLElement#classList
#### HTMLElement#classList.add
Add class name.
#### HTMLElement#classList.replace(old: string, new: string)
Replace class name with another one.
#### HTMLElement#classList.remove()
Remove class name.
#### HTMLElement#classList.toggle(className: string):void
Toggle class.
#### HTMLElement#classList.contains(className: string): boolean
Get if contains
#### HTMLElement#classList.values()
get class names
## HTMLElement Properties
### HTMLElement#text
Get unescaped text value of current node and its children. Like `innerText`.
(slow for the first time)
### HTMLElement#rawText
Get escpaed (as-it) text value of current node and its children. May have
`&amp;` in it. (fast)
### HTMLElement#tagName
Get tag name of HTMLElement. Notice: the returned value would be an uppercase string.
### HTMLElement#structuredText
Get structured Text
### HTMLElement#structure
Get DOM structure
### HTMLElement#firstChild
Get first child node
### HTMLElement#lastChild
Get last child node
### HTMLElement#innerHTML
Set or Get innerHTML.
### HTMLElement#outerHTML
Get outerHTML.
### HTMLElement#nextSibling
Returns a reference to the next child node of the current element's parent.
### HTMLElement#nextElementSibling
Returns a reference to the next child element of the current element's parent.
### HTMLElement#textContent
Get or Set textContent of current element, more efficient than [set_content](#htmlelementset_contentcontent-string--node--node).
### HTMLElement#attributes
Get all attributes of current element. **Notice: do not try to change the returned value.**
### HTMLElement#classList
Get all attributes of current element. **Notice: do not try to change the returned value.**
-1
View File
@@ -1 +0,0 @@
export default function arr_back<T>(arr: T[]): T;
-6
View File
@@ -1,6 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function arr_back(arr) {
return arr[arr.length - 1];
}
exports.default = arr_back;
-3
View File
@@ -1,3 +0,0 @@
export default function arr_back(arr) {
return arr[arr.length - 1];
}
-7
View File
@@ -1,7 +0,0 @@
export { default as CommentNode } from './nodes/comment';
export { default as HTMLElement } from './nodes/html';
export { default as parse, default } from './parse';
export { default as valid } from './valid';
export { default as Node } from './nodes/node';
export { default as TextNode } from './nodes/text';
export { default as NodeType } from './nodes/type';
-101
View File
@@ -1,101 +0,0 @@
import NodeType from './nodes/type';
function isTag(node) {
return node && node.nodeType === NodeType.ELEMENT_NODE;
}
function getAttributeValue(elem, name) {
return isTag(elem) ? elem.getAttribute(name) : undefined;
}
function getName(elem) {
return ((elem && elem.rawTagName) || '').toLowerCase();
}
function getChildren(node) {
return node && node.childNodes;
}
function getParent(node) {
return node ? node.parentNode : null;
}
function getText(node) {
return node.text;
}
function removeSubsets(nodes) {
let idx = nodes.length;
let node;
let ancestor;
let replace;
// Check if each node (or one of its ancestors) is already contained in the
// array.
while (--idx > -1) {
node = ancestor = nodes[idx];
// Temporarily remove the node under consideration
nodes[idx] = null;
replace = true;
while (ancestor) {
if (nodes.indexOf(ancestor) > -1) {
replace = false;
nodes.splice(idx, 1);
break;
}
ancestor = getParent(ancestor);
}
// If the node has been found to be unique, re-insert it.
if (replace) {
nodes[idx] = node;
}
}
return nodes;
}
function existsOne(test, elems) {
return elems.some((elem) => {
return isTag(elem) ? test(elem) || existsOne(test, getChildren(elem)) : false;
});
}
function getSiblings(node) {
const parent = getParent(node);
return parent && getChildren(parent);
}
function hasAttrib(elem, name) {
return getAttributeValue(elem, name) !== undefined;
}
function findOne(test, elems) {
let elem = null;
for (let i = 0, l = elems.length; i < l && !elem; i++) {
const el = elems[i];
if (test(el)) {
elem = el;
}
else {
const childs = getChildren(el);
if (childs && childs.length > 0) {
elem = findOne(test, childs);
}
}
}
return elem;
}
function findAll(test, nodes) {
let result = [];
for (let i = 0, j = nodes.length; i < j; i++) {
if (!isTag(nodes[i]))
continue;
if (test(nodes[i]))
result.push(nodes[i]);
const childs = getChildren(nodes[i]);
if (childs)
result = result.concat(findAll(test, childs));
}
return result;
}
export default {
isTag,
getAttributeValue,
getName,
getChildren,
getParent,
getText,
removeSubsets,
existsOne,
getSiblings,
hasAttrib,
findOne,
findAll
};
-23
View File
@@ -1,23 +0,0 @@
import Node from './node';
import NodeType from './type';
export default class CommentNode extends Node {
constructor(rawText, parentNode) {
super(parentNode);
this.rawText = rawText;
/**
* Node Type declaration.
* @type {Number}
*/
this.nodeType = NodeType.COMMENT_NODE;
}
/**
* Get unescaped text value of current node and its children.
* @return {string} text content
*/
get text() {
return this.rawText;
}
toString() {
return `<!--${this.rawText}-->`;
}
}
File diff suppressed because it is too large Load Diff
-18
View File
@@ -1,18 +0,0 @@
/**
* Node Class as base class for TextNode and HTMLElement.
*/
export default class Node {
constructor(parentNode = null) {
this.parentNode = parentNode;
this.childNodes = [];
}
get innerText() {
return this.rawText;
}
get textContent() {
return this.rawText;
}
set textContent(val) {
this.rawText = val;
}
}
-69
View File
@@ -1,69 +0,0 @@
import NodeType from './type';
import Node from './node';
/**
* TextNode to contain a text element in DOM tree.
* @param {string} value [description]
*/
export default class TextNode extends Node {
constructor(rawText, parentNode) {
super(parentNode);
this.rawText = rawText;
/**
* Node Type declaration.
* @type {Number}
*/
this.nodeType = NodeType.TEXT_NODE;
}
/**
* Returns text with all whitespace trimmed except single leading/trailing non-breaking space
*/
get trimmedText() {
if (this._trimmedText !== undefined)
return this._trimmedText;
const text = this.rawText;
let i = 0;
let startPos;
let endPos;
while (i >= 0 && i < text.length) {
if (/\S/.test(text[i])) {
if (startPos === undefined) {
startPos = i;
i = text.length;
}
else {
endPos = i;
i = void 0;
}
}
if (startPos === undefined)
i++;
else
i--;
}
if (startPos === undefined)
startPos = 0;
if (endPos === undefined)
endPos = text.length - 1;
const hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos - 1]);
const hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos + 1]);
this._trimmedText = (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');
return this._trimmedText;
}
/**
* Get unescaped text value of current node and its children.
* @return {string} text content
*/
get text() {
return this.rawText;
}
/**
* Detect if the node contains only white space.
* @return {bool}
*/
get isWhitespace() {
return /^(\s|&nbsp;)*$/.test(this.rawText);
}
toString() {
return this.text;
}
}
-7
View File
@@ -1,7 +0,0 @@
var NodeType;
(function (NodeType) {
NodeType[NodeType["ELEMENT_NODE"] = 1] = "ELEMENT_NODE";
NodeType[NodeType["TEXT_NODE"] = 3] = "TEXT_NODE";
NodeType[NodeType["COMMENT_NODE"] = 8] = "COMMENT_NODE";
})(NodeType || (NodeType = {}));
export default NodeType;
-1
View File
@@ -1 +0,0 @@
export { parse as default } from './nodes/html';
-9
View File
@@ -1,9 +0,0 @@
import { base_parse } from './nodes/html';
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
*/
export default function valid(data, options = { lowerCaseTagName: false, comment: false }) {
const stack = base_parse(data, options);
return Boolean(stack.length === 1);
}
-7
View File
@@ -1,7 +0,0 @@
export { default as CommentNode } from './nodes/comment';
export { default as HTMLElement, Options } from './nodes/html';
export { default as parse, default } from './parse';
export { default as valid } from './valid';
export { default as Node } from './nodes/node';
export { default as TextNode } from './nodes/text';
export { default as NodeType } from './nodes/type';
-21
View File
@@ -1,21 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeType = exports.TextNode = exports.Node = exports.valid = exports.default = exports.parse = exports.HTMLElement = exports.CommentNode = void 0;
var comment_1 = require("./nodes/comment");
Object.defineProperty(exports, "CommentNode", { enumerable: true, get: function () { return __importDefault(comment_1).default; } });
var html_1 = require("./nodes/html");
Object.defineProperty(exports, "HTMLElement", { enumerable: true, get: function () { return __importDefault(html_1).default; } });
var parse_1 = require("./parse");
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return __importDefault(parse_1).default; } });
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(parse_1).default; } });
var valid_1 = require("./valid");
Object.defineProperty(exports, "valid", { enumerable: true, get: function () { return __importDefault(valid_1).default; } });
var node_1 = require("./nodes/node");
Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return __importDefault(node_1).default; } });
var text_1 = require("./nodes/text");
Object.defineProperty(exports, "TextNode", { enumerable: true, get: function () { return __importDefault(text_1).default; } });
var type_1 = require("./nodes/type");
Object.defineProperty(exports, "NodeType", { enumerable: true, get: function () { return __importDefault(type_1).default; } });
-1543
View File
File diff suppressed because it is too large Load Diff
-6
View File
@@ -1,6 +0,0 @@
import { Adapter } from 'css-select/lib/types';
import HTMLElement from './nodes/html';
import Node from './nodes/node';
export declare type Predicate = (node: Node) => node is HTMLElement;
declare const _default: Adapter<Node, HTMLElement>;
export default _default;
-106
View File
@@ -1,106 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var type_1 = __importDefault(require("./nodes/type"));
function isTag(node) {
return node && node.nodeType === type_1.default.ELEMENT_NODE;
}
function getAttributeValue(elem, name) {
return isTag(elem) ? elem.getAttribute(name) : undefined;
}
function getName(elem) {
return ((elem && elem.rawTagName) || '').toLowerCase();
}
function getChildren(node) {
return node && node.childNodes;
}
function getParent(node) {
return node ? node.parentNode : null;
}
function getText(node) {
return node.text;
}
function removeSubsets(nodes) {
var idx = nodes.length;
var node;
var ancestor;
var replace;
// Check if each node (or one of its ancestors) is already contained in the
// array.
while (--idx > -1) {
node = ancestor = nodes[idx];
// Temporarily remove the node under consideration
nodes[idx] = null;
replace = true;
while (ancestor) {
if (nodes.indexOf(ancestor) > -1) {
replace = false;
nodes.splice(idx, 1);
break;
}
ancestor = getParent(ancestor);
}
// If the node has been found to be unique, re-insert it.
if (replace) {
nodes[idx] = node;
}
}
return nodes;
}
function existsOne(test, elems) {
return elems.some(function (elem) {
return isTag(elem) ? test(elem) || existsOne(test, getChildren(elem)) : false;
});
}
function getSiblings(node) {
var parent = getParent(node);
return parent && getChildren(parent);
}
function hasAttrib(elem, name) {
return getAttributeValue(elem, name) !== undefined;
}
function findOne(test, elems) {
var elem = null;
for (var i = 0, l = elems.length; i < l && !elem; i++) {
var el = elems[i];
if (test(el)) {
elem = el;
}
else {
var childs = getChildren(el);
if (childs && childs.length > 0) {
elem = findOne(test, childs);
}
}
}
return elem;
}
function findAll(test, nodes) {
var result = [];
for (var i = 0, j = nodes.length; i < j; i++) {
if (!isTag(nodes[i]))
continue;
if (test(nodes[i]))
result.push(nodes[i]);
var childs = getChildren(nodes[i]);
if (childs)
result = result.concat(findAll(test, childs));
}
return result;
}
exports.default = {
isTag: isTag,
getAttributeValue: getAttributeValue,
getName: getName,
getChildren: getChildren,
getParent: getParent,
getText: getText,
removeSubsets: removeSubsets,
existsOne: existsOne,
getSiblings: getSiblings,
hasAttrib: hasAttrib,
findOne: findOne,
findAll: findAll
};
-18
View File
@@ -1,18 +0,0 @@
import Node from './node';
import NodeType from './type';
import HTMLElement from './html';
export default class CommentNode extends Node {
rawText: string;
constructor(rawText: string, parentNode: HTMLElement);
/**
* Node Type declaration.
* @type {Number}
*/
nodeType: NodeType;
/**
* Get unescaped text value of current node and its children.
* @return {string} text content
*/
get text(): string;
toString(): `<!--${string}-->`;
}
-51
View File
@@ -1,51 +0,0 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var node_1 = __importDefault(require("./node"));
var type_1 = __importDefault(require("./type"));
var CommentNode = /** @class */ (function (_super) {
__extends(CommentNode, _super);
function CommentNode(rawText, parentNode) {
var _this = _super.call(this, parentNode) || this;
_this.rawText = rawText;
/**
* Node Type declaration.
* @type {Number}
*/
_this.nodeType = type_1.default.COMMENT_NODE;
return _this;
}
Object.defineProperty(CommentNode.prototype, "text", {
/**
* Get unescaped text value of current node and its children.
* @return {string} text content
*/
get: function () {
return this.rawText;
},
enumerable: false,
configurable: true
});
CommentNode.prototype.toString = function () {
return "<!--" + this.rawText + "-->";
};
return CommentNode;
}(node_1.default));
exports.default = CommentNode;
-206
View File
@@ -1,206 +0,0 @@
import Node from './node';
import NodeType from './type';
export interface KeyAttributes {
id?: string;
class?: string;
}
export interface Attributes {
[key: string]: string;
}
export interface RawAttributes {
[key: string]: string;
}
export declare type InsertPosition = 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';
declare class DOMTokenList {
private _set;
private _afterUpdate;
private _validate;
constructor(valuesInit?: string[], afterUpdate?: ((t: DOMTokenList) => void));
add(c: string): void;
replace(c1: string, c2: string): void;
remove(c: string): void;
toggle(c: string): void;
contains(c: string): boolean;
get length(): number;
values(): IterableIterator<string>;
get value(): string[];
toString(): string;
}
/**
* HTMLElement, which contains a set of children.
*
* Note: this is a minimalist implementation, no complete tree
* structure provided (no parentNode, nextSibling,
* previousSibling etc).
* @class HTMLElement
* @extends {Node}
*/
export default class HTMLElement extends Node {
private rawAttrs;
private _attrs;
private _rawAttrs;
rawTagName: string;
id: string;
classList: DOMTokenList;
/**
* Node Type declaration.
*/
nodeType: NodeType;
/**
* Quote attribute values
* @param attr attribute value
* @returns {string} quoted value
*/
private quoteAttribute;
/**
* Creates an instance of HTMLElement.
* @param keyAttrs id and class attribute
* @param [rawAttrs] attributes in string
*
* @memberof HTMLElement
*/
constructor(tagName: string, keyAttrs: KeyAttributes, rawAttrs: string, parentNode: HTMLElement | null);
/**
* Remove current element
*/
remove(): void;
/**
* Remove Child element from childNodes array
* @param {HTMLElement} node node to remove
*/
removeChild(node: Node): void;
/**
* Exchanges given child with new child
* @param {HTMLElement} oldNode node to exchange
* @param {HTMLElement} newNode new node
*/
exchangeChild(oldNode: Node, newNode: Node): void;
get tagName(): string;
get localName(): string;
/**
* Get escpaed (as-it) text value of current node and its children.
* @return {string} text content
*/
get rawText(): string;
get textContent(): string;
set textContent(val: string);
/**
* Get unescaped text value of current node and its children.
* @return {string} text content
*/
get text(): string;
/**
* Get structured Text (with '\n' etc.)
* @return {string} structured text
*/
get structuredText(): string;
toString(): string;
get innerHTML(): string;
set innerHTML(content: string);
set_content(content: string | Node | Node[], options?: Options): void;
replaceWith(...nodes: (string | Node)[]): void;
get outerHTML(): string;
/**
* Trim element from right (in block) after seeing pattern in a TextNode.
* @param {RegExp} pattern pattern to find
* @return {HTMLElement} reference to current node
*/
trimRight(pattern: RegExp): this;
/**
* Get DOM structure
* @return {string} strucutre
*/
get structure(): string;
/**
* Remove whitespaces in this sub tree.
* @return {HTMLElement} pointer to this
*/
removeWhitespace(): this;
/**
* Query CSS selector to find matching nodes.
* @param {string} selector Simplified CSS selector
* @return {HTMLElement[]} matching elements
*/
querySelectorAll(selector: string): HTMLElement[];
/**
* Query CSS Selector to find matching node.
* @param {string} selector Simplified CSS selector
* @return {HTMLElement} matching node
*/
querySelector(selector: string): HTMLElement;
/**
* traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
* @param selector a DOMString containing a selector list
*/
closest(selector: string): Node;
/**
* Append a child node to childNodes
* @param {Node} node node to append
* @return {Node} node appended
*/
appendChild<T extends Node = Node>(node: T): T;
/**
* Get first child node
* @return {Node} first child node
*/
get firstChild(): Node;
/**
* Get last child node
* @return {Node} last child node
*/
get lastChild(): Node;
/**
* Get attributes
* @access private
* @return {Object} parsed and unescaped attributes
*/
get attrs(): Attributes;
get attributes(): Record<string, string>;
/**
* Get escaped (as-it) attributes
* @return {Object} parsed attributes
*/
get rawAttributes(): RawAttributes;
removeAttribute(key: string): void;
hasAttribute(key: string): boolean;
/**
* Get an attribute
* @return {string} value of the attribute
*/
getAttribute(key: string): string | undefined;
/**
* Set an attribute value to the HTMLElement
* @param {string} key The attribute name
* @param {string} value The value to set, or null / undefined to remove an attribute
*/
setAttribute(key: string, value: string): void;
/**
* Replace all the attributes of the HTMLElement by the provided attributes
* @param {Attributes} attributes the new attribute set
*/
setAttributes(attributes: Attributes): void;
insertAdjacentHTML(where: InsertPosition, html: string): void;
get nextSibling(): Node;
get nextElementSibling(): HTMLElement;
get classNames(): string;
}
export interface Options {
lowerCaseTagName: boolean;
comment: boolean;
blockTextElements: {
[tag: string]: boolean;
};
}
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
* @param {string} data html
* @return {HTMLElement} root element
*/
export declare function base_parse(data: string, options?: Partial<Options>): HTMLElement[];
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
*/
export declare function parse(data: string, options?: Partial<Options>): HTMLElement;
export {};
-1228
View File
File diff suppressed because it is too large Load Diff
-17
View File
@@ -1,17 +0,0 @@
import NodeType from './type';
import HTMLElement from './html';
/**
* Node Class as base class for TextNode and HTMLElement.
*/
export default abstract class Node {
parentNode: HTMLElement;
abstract nodeType: NodeType;
childNodes: Node[];
abstract text: string;
abstract rawText: string;
abstract toString(): string;
constructor(parentNode?: HTMLElement);
get innerText(): string;
get textContent(): string;
set textContent(val: string);
}
-31
View File
@@ -1,31 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Node Class as base class for TextNode and HTMLElement.
*/
var Node = /** @class */ (function () {
function Node(parentNode) {
if (parentNode === void 0) { parentNode = null; }
this.parentNode = parentNode;
this.childNodes = [];
}
Object.defineProperty(Node.prototype, "innerText", {
get: function () {
return this.rawText;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Node.prototype, "textContent", {
get: function () {
return this.rawText;
},
set: function (val) {
this.rawText = val;
},
enumerable: false,
configurable: true
});
return Node;
}());
exports.default = Node;
-32
View File
@@ -1,32 +0,0 @@
import NodeType from './type';
import Node from './node';
import HTMLElement from './html';
/**
* TextNode to contain a text element in DOM tree.
* @param {string} value [description]
*/
export default class TextNode extends Node {
rawText: string;
constructor(rawText: string, parentNode: HTMLElement);
/**
* Node Type declaration.
* @type {Number}
*/
nodeType: NodeType;
private _trimmedText?;
/**
* Returns text with all whitespace trimmed except single leading/trailing non-breaking space
*/
get trimmedText(): string;
/**
* Get unescaped text value of current node and its children.
* @return {string} text content
*/
get text(): string;
/**
* Detect if the node contains only white space.
* @return {bool}
*/
get isWhitespace(): boolean;
toString(): string;
}
-105
View File
@@ -1,105 +0,0 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var type_1 = __importDefault(require("./type"));
var node_1 = __importDefault(require("./node"));
/**
* TextNode to contain a text element in DOM tree.
* @param {string} value [description]
*/
var TextNode = /** @class */ (function (_super) {
__extends(TextNode, _super);
function TextNode(rawText, parentNode) {
var _this = _super.call(this, parentNode) || this;
_this.rawText = rawText;
/**
* Node Type declaration.
* @type {Number}
*/
_this.nodeType = type_1.default.TEXT_NODE;
return _this;
}
Object.defineProperty(TextNode.prototype, "trimmedText", {
/**
* Returns text with all whitespace trimmed except single leading/trailing non-breaking space
*/
get: function () {
if (this._trimmedText !== undefined)
return this._trimmedText;
var text = this.rawText;
var i = 0;
var startPos;
var endPos;
while (i >= 0 && i < text.length) {
if (/\S/.test(text[i])) {
if (startPos === undefined) {
startPos = i;
i = text.length;
}
else {
endPos = i;
i = void 0;
}
}
if (startPos === undefined)
i++;
else
i--;
}
if (startPos === undefined)
startPos = 0;
if (endPos === undefined)
endPos = text.length - 1;
var hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos - 1]);
var hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos + 1]);
this._trimmedText = (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');
return this._trimmedText;
},
enumerable: false,
configurable: true
});
Object.defineProperty(TextNode.prototype, "text", {
/**
* Get unescaped text value of current node and its children.
* @return {string} text content
*/
get: function () {
return this.rawText;
},
enumerable: false,
configurable: true
});
Object.defineProperty(TextNode.prototype, "isWhitespace", {
/**
* Detect if the node contains only white space.
* @return {bool}
*/
get: function () {
return /^(\s|&nbsp;)*$/.test(this.rawText);
},
enumerable: false,
configurable: true
});
TextNode.prototype.toString = function () {
return this.text;
};
return TextNode;
}(node_1.default));
exports.default = TextNode;
-6
View File
@@ -1,6 +0,0 @@
declare enum NodeType {
ELEMENT_NODE = 1,
TEXT_NODE = 3,
COMMENT_NODE = 8
}
export default NodeType;
-9
View File
@@ -1,9 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var NodeType;
(function (NodeType) {
NodeType[NodeType["ELEMENT_NODE"] = 1] = "ELEMENT_NODE";
NodeType[NodeType["TEXT_NODE"] = 3] = "TEXT_NODE";
NodeType[NodeType["COMMENT_NODE"] = 8] = "COMMENT_NODE";
})(NodeType || (NodeType = {}));
exports.default = NodeType;
-1
View File
@@ -1 +0,0 @@
export { parse as default } from './nodes/html';
-5
View File
@@ -1,5 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = void 0;
var html_1 = require("./nodes/html");
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return html_1.parse; } });
-6
View File
@@ -1,6 +0,0 @@
import { Options } from './nodes/html';
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
*/
export default function valid(data: string, options?: Partial<Options>): boolean;
-13
View File
@@ -1,13 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var html_1 = require("./nodes/html");
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
*/
function valid(data, options) {
if (options === void 0) { options = { lowerCaseTagName: false, comment: false }; }
var stack = html_1.base_parse(data, options);
return Boolean(stack.length === 1);
}
exports.default = valid;
-1
View File
@@ -1 +0,0 @@
../he/bin/he
-20
View File
@@ -1,20 +0,0 @@
Copyright Mathias Bynens <https://mathiasbynens.be/>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-379
View File
@@ -1,379 +0,0 @@
# he [![Build status](https://travis-ci.org/mathiasbynens/he.svg?branch=master)](https://travis-ci.org/mathiasbynens/he) [![Code coverage status](https://codecov.io/github/mathiasbynens/he/coverage.svg?branch=master)](https://codecov.io/github/mathiasbynens/he?branch=master) [![Dependency status](https://gemnasium.com/mathiasbynens/he.svg)](https://gemnasium.com/mathiasbynens/he)
_he_ (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript. It supports [all standardized named character references as per HTML](https://html.spec.whatwg.org/multipage/syntax.html#named-character-references), handles [ambiguous ampersands](https://mathiasbynens.be/notes/ambiguous-ampersands) and other edge cases [just like a browser would](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references), has an extensive test suite, and — contrary to many other JavaScript solutions — _he_ handles astral Unicode symbols just fine. [An online demo is available.](https://mothereff.in/html-entities)
## Installation
Via [npm](https://www.npmjs.com/):
```bash
npm install he
```
Via [Bower](http://bower.io/):
```bash
bower install he
```
Via [Component](https://github.com/component/component):
```bash
component install mathiasbynens/he
```
In a browser:
```html
<script src="he.js"></script>
```
In [Node.js](https://nodejs.org/), [io.js](https://iojs.org/), [Narwhal](http://narwhaljs.org/), and [RingoJS](http://ringojs.org/):
```js
var he = require('he');
```
In [Rhino](http://www.mozilla.org/rhino/):
```js
load('he.js');
```
Using an AMD loader like [RequireJS](http://requirejs.org/):
```js
require(
{
'paths': {
'he': 'path/to/he'
}
},
['he'],
function(he) {
console.log(he);
}
);
```
## API
### `he.version`
A string representing the semantic version number.
### `he.encode(text, options)`
This function takes a string of text and encodes (by default) any symbols that arent printable ASCII symbols and `&`, `<`, `>`, `"`, `'`, and `` ` ``, replacing them with character references.
```js
he.encode('foo © bar ≠ baz 𝌆 qux');
// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
```
As long as the input string contains [allowed code points](https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream) only, the return value of this function is always valid HTML. Any [(invalid) code points that cannot be represented using a character reference](https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides) in the input are not encoded:
```js
he.encode('foo \0 bar');
// → 'foo \0 bar'
```
However, enabling [the `strict` option](https://github.com/mathiasbynens/he#strict) causes invalid code points to throw an exception. With `strict` enabled, `he.encode` either throws (if the input contains invalid code points) or returns a string of valid HTML.
The `options` object is optional. It recognizes the following properties:
#### `useNamedReferences`
The default value for the `useNamedReferences` option is `false`. This means that `encode()` will not use any named character references (e.g. `&copy;`) in the output — hexadecimal escapes (e.g. `&#xA9;`) will be used instead. Set it to `true` to enable the use of named references.
**Note that if compatibility with older browsers is a concern, this option should remain disabled.**
```js
// Using the global default setting (defaults to `false`):
he.encode('foo © bar ≠ baz 𝌆 qux');
// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
// Passing an `options` object to `encode`, to explicitly disallow named references:
he.encode('foo © bar ≠ baz 𝌆 qux', {
'useNamedReferences': false
});
// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
// Passing an `options` object to `encode`, to explicitly allow named references:
he.encode('foo © bar ≠ baz 𝌆 qux', {
'useNamedReferences': true
});
// → 'foo &copy; bar &ne; baz &#x1D306; qux'
```
#### `decimal`
The default value for the `decimal` option is `false`. If the option is enabled, `encode` will generally use decimal escapes (e.g. `&#169;`) rather than hexadecimal escapes (e.g. `&#xA9;`). Beside of this replacement, the basic behavior remains the same when combined with other options. For example: if both options `useNamedReferences` and `decimal` are enabled, named references (e.g. `&copy;`) are used over decimal escapes. HTML entities without a named reference are encoded using decimal escapes.
```js
// Using the global default setting (defaults to `false`):
he.encode('foo © bar ≠ baz 𝌆 qux');
// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
// Passing an `options` object to `encode`, to explicitly disable decimal escapes:
he.encode('foo © bar ≠ baz 𝌆 qux', {
'decimal': false
});
// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
// Passing an `options` object to `encode`, to explicitly enable decimal escapes:
he.encode('foo © bar ≠ baz 𝌆 qux', {
'decimal': true
});
// → 'foo &#169; bar &#8800; baz &#119558; qux'
// Passing an `options` object to `encode`, to explicitly allow named references and decimal escapes:
he.encode('foo © bar ≠ baz 𝌆 qux', {
'useNamedReferences': true,
'decimal': true
});
// → 'foo &copy; bar &ne; baz &#119558; qux'
```
#### `encodeEverything`
The default value for the `encodeEverything` option is `false`. This means that `encode()` will not use any character references for printable ASCII symbols that dont need escaping. Set it to `true` to encode every symbol in the input string. When set to `true`, this option takes precedence over `allowUnsafeSymbols` (i.e. setting the latter to `true` in such a case has no effect).
```js
// Using the global default setting (defaults to `false`):
he.encode('foo © bar ≠ baz 𝌆 qux');
// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
// Passing an `options` object to `encode`, to explicitly encode all symbols:
he.encode('foo © bar ≠ baz 𝌆 qux', {
'encodeEverything': true
});
// → '&#x66;&#x6F;&#x6F;&#x20;&#xA9;&#x20;&#x62;&#x61;&#x72;&#x20;&#x2260;&#x20;&#x62;&#x61;&#x7A;&#x20;&#x1D306;&#x20;&#x71;&#x75;&#x78;'
// This setting can be combined with the `useNamedReferences` option:
he.encode('foo © bar ≠ baz 𝌆 qux', {
'encodeEverything': true,
'useNamedReferences': true
});
// → '&#x66;&#x6F;&#x6F;&#x20;&copy;&#x20;&#x62;&#x61;&#x72;&#x20;&ne;&#x20;&#x62;&#x61;&#x7A;&#x20;&#x1D306;&#x20;&#x71;&#x75;&#x78;'
```
#### `strict`
The default value for the `strict` option is `false`. This means that `encode()` will encode any HTML text content you feed it, even if it contains any symbols that cause [parse errors](https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream). To throw an error when such invalid HTML is encountered, set the `strict` option to `true`. This option makes it possible to use _he_ as part of HTML parsers and HTML validators.
```js
// Using the global default setting (defaults to `false`, i.e. error-tolerant mode):
he.encode('\x01');
// → '&#x1;'
// Passing an `options` object to `encode`, to explicitly enable error-tolerant mode:
he.encode('\x01', {
'strict': false
});
// → '&#x1;'
// Passing an `options` object to `encode`, to explicitly enable strict mode:
he.encode('\x01', {
'strict': true
});
// → Parse error
```
#### `allowUnsafeSymbols`
The default value for the `allowUnsafeSymbols` option is `false`. This means that characters that are unsafe for use in HTML content (`&`, `<`, `>`, `"`, `'`, and `` ` ``) will be encoded. When set to `true`, only non-ASCII characters will be encoded. If the `encodeEverything` option is set to `true`, this option will be ignored.
```js
he.encode('foo © and & ampersand', {
'allowUnsafeSymbols': true
});
// → 'foo &#xA9; and & ampersand'
```
#### Overriding default `encode` options globally
The global default setting can be overridden by modifying the `he.encode.options` object. This saves you from passing in an `options` object for every call to `encode` if you want to use the non-default setting.
```js
// Read the global default setting:
he.encode.options.useNamedReferences;
// → `false` by default
// Override the global default setting:
he.encode.options.useNamedReferences = true;
// Using the global default setting, which is now `true`:
he.encode('foo © bar ≠ baz 𝌆 qux');
// → 'foo &copy; bar &ne; baz &#x1D306; qux'
```
### `he.decode(html, options)`
This function takes a string of HTML and decodes any named and numerical character references in it using [the algorithm described in section 12.2.4.69 of the HTML spec](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references).
```js
he.decode('foo &copy; bar &ne; baz &#x1D306; qux');
// → 'foo © bar ≠ baz 𝌆 qux'
```
The `options` object is optional. It recognizes the following properties:
#### `isAttributeValue`
The default value for the `isAttributeValue` option is `false`. This means that `decode()` will decode the string as if it were used in [a text context in an HTML document](https://html.spec.whatwg.org/multipage/syntax.html#data-state). HTML has different rules for [parsing character references in attribute values](https://html.spec.whatwg.org/multipage/syntax.html#character-reference-in-attribute-value-state) — set this option to `true` to treat the input string as if it were used as an attribute value.
```js
// Using the global default setting (defaults to `false`, i.e. HTML text context):
he.decode('foo&ampbar');
// → 'foo&bar'
// Passing an `options` object to `decode`, to explicitly assume an HTML text context:
he.decode('foo&ampbar', {
'isAttributeValue': false
});
// → 'foo&bar'
// Passing an `options` object to `decode`, to explicitly assume an HTML attribute value context:
he.decode('foo&ampbar', {
'isAttributeValue': true
});
// → 'foo&ampbar'
```
#### `strict`
The default value for the `strict` option is `false`. This means that `decode()` will decode any HTML text content you feed it, even if it contains any entities that cause [parse errors](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references). To throw an error when such invalid HTML is encountered, set the `strict` option to `true`. This option makes it possible to use _he_ as part of HTML parsers and HTML validators.
```js
// Using the global default setting (defaults to `false`, i.e. error-tolerant mode):
he.decode('foo&ampbar');
// → 'foo&bar'
// Passing an `options` object to `decode`, to explicitly enable error-tolerant mode:
he.decode('foo&ampbar', {
'strict': false
});
// → 'foo&bar'
// Passing an `options` object to `decode`, to explicitly enable strict mode:
he.decode('foo&ampbar', {
'strict': true
});
// → Parse error
```
#### Overriding default `decode` options globally
The global default settings for the `decode` function can be overridden by modifying the `he.decode.options` object. This saves you from passing in an `options` object for every call to `decode` if you want to use a non-default setting.
```js
// Read the global default setting:
he.decode.options.isAttributeValue;
// → `false` by default
// Override the global default setting:
he.decode.options.isAttributeValue = true;
// Using the global default setting, which is now `true`:
he.decode('foo&ampbar');
// → 'foo&ampbar'
```
### `he.escape(text)`
This function takes a string of text and escapes it for use in text contexts in XML or HTML documents. Only the following characters are escaped: `&`, `<`, `>`, `"`, `'`, and `` ` ``.
```js
he.escape('<img src=\'x\' onerror="prompt(1)">');
// → '&lt;img src=&#x27;x&#x27; onerror=&quot;prompt(1)&quot;&gt;'
```
### `he.unescape(html, options)`
`he.unescape` is an alias for `he.decode`. It takes a string of HTML and decodes any named and numerical character references in it.
### Using the `he` binary
To use the `he` binary in your shell, simply install _he_ globally using npm:
```bash
npm install -g he
```
After that you will be able to encode/decode HTML entities from the command line:
```bash
$ he --encode 'föo ♥ bår 𝌆 baz'
f&#xF6;o &#x2665; b&#xE5;r &#x1D306; baz
$ he --encode --use-named-refs 'föo ♥ bår 𝌆 baz'
f&ouml;o &hearts; b&aring;r &#x1D306; baz
$ he --decode 'f&ouml;o &hearts; b&aring;r &#x1D306; baz'
föo ♥ bår 𝌆 baz
```
Read a local text file, encode it for use in an HTML text context, and save the result to a new file:
```bash
$ he --encode < foo.txt > foo-escaped.html
```
Or do the same with an online text file:
```bash
$ curl -sL "http://git.io/HnfEaw" | he --encode > escaped.html
```
Or, the opposite — read a local file containing a snippet of HTML in a text context, decode it back to plain text, and save the result to a new file:
```bash
$ he --decode < foo-escaped.html > foo.txt
```
Or do the same with an online HTML snippet:
```bash
$ curl -sL "http://git.io/HnfEaw" | he --decode > decoded.txt
```
See `he --help` for the full list of options.
## Support
_he_ has been tested in at least:
* Chrome 27-50
* Firefox 3-45
* Safari 4-9
* Opera 10-12, 1537
* IE 611
* Edge
* Narwhal 0.3.2
* Node.js v0.10, v0.12, v4, v5
* PhantomJS 1.9.0
* Rhino 1.7RC4
* RingoJS 0.8-0.11
## Unit tests & code coverage
After cloning this repository, run `npm install` to install the dependencies needed for he development and testing. You may want to install Istanbul _globally_ using `npm install istanbul -g`.
Once thats done, you can run the unit tests in Node using `npm test` or `node tests/tests.js`. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use `grunt test`.
To generate the code coverage report, use `grunt cover`.
## Acknowledgements
Thanks to [Simon Pieters](https://simon.html5.org/) ([@zcorpan](https://twitter.com/zcorpan)) for the many suggestions.
## Author
| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
|---|
| [Mathias Bynens](https://mathiasbynens.be/) |
## License
_he_ is available under the [MIT](https://mths.be/mit) license.
-148
View File
@@ -1,148 +0,0 @@
#!/usr/bin/env node
(function() {
var fs = require('fs');
var he = require('../he.js');
var strings = process.argv.splice(2);
var stdin = process.stdin;
var data;
var timeout;
var action;
var options = {};
var log = console.log;
var main = function() {
var option = strings[0];
var count = 0;
if (/^(?:-h|--help|undefined)$/.test(option)) {
log(
'he v%s - https://mths.be/he',
he.version
);
log([
'\nUsage:\n',
'\the [--escape] string',
'\the [--encode] [--use-named-refs] [--everything] [--allow-unsafe] [--decimal] string',
'\the [--decode] [--attribute] [--strict] string',
'\the [-v | --version]',
'\the [-h | --help]',
'\nExamples:\n',
'\the --escape \\<img\\ src\\=\\\'x\\\'\\ onerror\\=\\"prompt\\(1\\)\\"\\>',
'\techo \'&copy; &#x1D306;\' | he --decode'
].join('\n'));
return process.exit(option ? 0 : 1);
}
if (/^(?:-v|--version)$/.test(option)) {
log('v%s', he.version);
return process.exit(0);
}
strings.forEach(function(string) {
// Process options
if (string == '--escape') {
action = 'escape';
return;
}
if (string == '--encode') {
action = 'encode';
return;
}
if (string == '--use-named-refs') {
action = 'encode';
options.useNamedReferences = true;
return;
}
if (string == '--everything') {
action = 'encode';
options.encodeEverything = true;
return;
}
if (string == '--allow-unsafe') {
action = 'encode';
options.allowUnsafeSymbols = true;
return;
}
if (string == '--decimal') {
action = 'encode';
options.decimal = true;
return;
}
if (string == '--decode') {
action = 'decode';
return;
}
if (string == '--attribute') {
action = 'decode';
options.isAttributeValue = true;
return;
}
if (string == '--strict') {
action = 'decode';
options.strict = true;
return;
}
// Process string(s)
var result;
if (!action) {
log('Error: he requires at least one option and a string argument.');
log('Try `he --help` for more information.');
return process.exit(1);
}
try {
result = he[action](string, options);
log(result);
count++;
} catch(error) {
log(error.message + '\n');
log('Error: failed to %s.', action);
log('If you think this is a bug in he, please report it:');
log('https://github.com/mathiasbynens/he/issues/new');
log(
'\nStack trace using he@%s:\n',
he.version
);
log(error.stack);
return process.exit(1);
}
});
if (!count) {
log('Error: he requires a string argument.');
log('Try `he --help` for more information.');
return process.exit(1);
}
// Return with exit status 0 outside of the `forEach` loop, in case
// multiple strings were passed in.
return process.exit(0);
};
if (stdin.isTTY) {
// handle shell arguments
main();
} else {
// Either the script is called from within a non-TTY context, or `stdin`
// content is being piped in.
if (!process.stdout.isTTY) {
// The script was called from a non-TTY context. This is a rather uncommon
// use case we dont actively support. However, we dont want the script
// to wait forever in such cases, so…
timeout = setTimeout(function() {
// …if no piped data arrived after a whole minute, handle shell
// arguments instead.
main();
}, 60000);
}
data = '';
stdin.on('data', function(chunk) {
clearTimeout(timeout);
data += chunk;
});
stdin.on('end', function() {
strings.push(data.trim());
main();
});
stdin.resume();
}
}());
File diff suppressed because one or more lines are too long
-78
View File
@@ -1,78 +0,0 @@
.Dd April 5, 2016
.Dt he 1
.Sh NAME
.Nm he
.Nd encode/decode HTML entities just like a browser would
.Sh SYNOPSIS
.Nm
.Op Fl -escape Ar string
.br
.Op Fl -encode Ar string
.br
.Op Fl -encode Fl -use-named-refs Fl -everything Fl -allow-unsafe Ar string
.br
.Op Fl -decode Ar string
.br
.Op Fl -decode Fl -attribute Ar string
.br
.Op Fl -decode Fl -strict Ar string
.br
.Op Fl v | -version
.br
.Op Fl h | -help
.Sh DESCRIPTION
.Nm
encodes/decodes HTML entities in strings just like a browser would.
.Sh OPTIONS
.Bl -ohang -offset
.It Sy "--escape"
Take a string of text and escape it for use in text contexts in XML or HTML documents. Only the following characters are escaped: `&`, `<`, `>`, `"`, and `'`.
.It Sy "--encode"
Take a string of text and encode any symbols that aren't printable ASCII symbols and that can be replaced with character references. For example, it would turn `©` into `&#xA9;`, but it wouldn't turn `+` into `&#x2B;` since there is no point in doing so. Additionally, it replaces any remaining non-ASCII symbols with a hexadecimal escape sequence (e.g. `&#x1D306;`). The return value of this function is always valid HTML.
.It Sy "--encode --use-named-refs"
Enable the use of named character references (like `&copy;`) in the output. If compatibility with older browsers is a concern, don't use this option.
.It Sy "--encode --everything"
Encode every symbol in the input string, even safe printable ASCII symbols.
.It Sy "--encode --allow-unsafe"
Encode non-ASCII characters only. This leaves unsafe HTML/XML symbols like `&`, `<`, `>`, `"`, and `'` intact.
.It Sy "--encode --decimal"
Use decimal digits rather than hexadecimal digits for encoded character references, e.g. output `&#169;` instead of `&#xA9;`.
.It Sy "--decode"
Takes a string of HTML and decode any named and numerical character references in it using the algorithm described in the HTML spec.
.It Sy "--decode --attribute"
Parse the input as if it was an HTML attribute value rather than a string in an HTML text content.
.It Sy "--decode --strict"
Throw an error if an invalid character reference is encountered.
.It Sy "-v, --version"
Print he's version.
.It Sy "-h, --help"
Show the help screen.
.El
.Sh EXIT STATUS
The
.Nm he
utility exits with one of the following values:
.Pp
.Bl -tag -width flag -compact
.It Li 0
.Nm
did what it was instructed to do successfully; either it encoded/decoded the input and printed the result, or it printed the version or usage message.
.It Li 1
.Nm
encountered an error.
.El
.Sh EXAMPLES
.Bl -ohang -offset
.It Sy "he --escape '<script>alert(1)</script>'"
Print an escaped version of the given string that is safe for use in HTML text contexts, escaping only `&`, `<`, `>`, `"`, and `'`.
.It Sy "he --decode '&copy;&#x1D306;'"
Print the decoded version of the given HTML string.
.It Sy "echo\ '&copy;&#x1D306;'\ |\ he --decode"
Print the decoded version of the HTML string that gets piped in.
.El
.Sh BUGS
he's bug tracker is located at <https://github.com/mathiasbynens/he/issues>.
.Sh AUTHOR
Mathias Bynens <https://mathiasbynens.be/>
.Sh WWW
<https://mths.be/he>
-91
View File
@@ -1,91 +0,0 @@
{
"_args": [
[
"he@1.1.1",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "he@1.1.1",
"_id": "he@1.1.1",
"_inBundle": false,
"_integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=",
"_location": "/node-html-parser/he",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "he@1.1.1",
"name": "he",
"escapedName": "he",
"rawSpec": "1.1.1",
"saveSpec": null,
"fetchSpec": "1.1.1"
},
"_requiredBy": [
"/node-html-parser"
],
"_resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz",
"_spec": "1.1.1",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"author": {
"name": "Mathias Bynens",
"url": "https://mathiasbynens.be/"
},
"bin": {
"he": "bin/he"
},
"bugs": {
"url": "https://github.com/mathiasbynens/he/issues"
},
"description": "A robust HTML entities encoder/decoder with full Unicode support.",
"devDependencies": {
"codecov.io": "^0.1.6",
"grunt": "^0.4.5",
"grunt-shell": "^1.1.1",
"grunt-template": "^0.2.3",
"istanbul": "^0.4.2",
"jsesc": "^1.0.0",
"lodash": "^4.8.2",
"qunit-extras": "^1.4.5",
"qunitjs": "~1.11.0",
"regenerate": "^1.2.1",
"requirejs": "^2.1.22",
"sort-object": "^3.0.2"
},
"directories": {
"bin": "bin",
"man": "man",
"test": "tests"
},
"files": [
"LICENSE-MIT.txt",
"he.js",
"bin/",
"man/"
],
"homepage": "https://mths.be/he",
"keywords": [
"string",
"entities",
"entity",
"html",
"encode",
"decode",
"unicode"
],
"license": "MIT",
"main": "he.js",
"man": [
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series/node_modules/node-html-parser/node_modules/he/man/he.1"
],
"name": "he",
"repository": {
"type": "git",
"url": "git+https://github.com/mathiasbynens/he.git"
},
"scripts": {
"build": "grunt build",
"test": "node tests/tests.js"
},
"version": "1.1.1"
}
-121
View File
@@ -1,121 +0,0 @@
{
"_args": [
[
"node-html-parser@3.3.6",
"/home/node/nuxt"
]
],
"_from": "node-html-parser@3.3.6",
"_id": "node-html-parser@3.3.6",
"_inBundle": false,
"_integrity": "sha512-VkWDHvNgFGB3mbQGMyzqRE1i/BG7TKX9wRXC8e/v8kL0kZR/Oy6RjYxXH91K6/+m3g8iQ8dTqRy75lTYoA2Cjg==",
"_location": "/node-html-parser",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "node-html-parser@3.3.6",
"name": "node-html-parser",
"escapedName": "node-html-parser",
"rawSpec": "3.3.6",
"saveSpec": null,
"fetchSpec": "3.3.6"
},
"_requiredBy": [
"/@nuxt/generator"
],
"_resolved": "https://registry.npmjs.org/node-html-parser/-/node-html-parser-3.3.6.tgz",
"_spec": "3.3.6",
"_where": "/home/node/nuxt",
"author": {
"name": "Xiaoyi Shi",
"email": "ashi009@gmail.com"
},
"bugs": {
"url": "https://github.com/taoqf/node-fast-html-parser/issues"
},
"config": {
"blanket": {
"pattern": "./dist/index.js",
"data-cover-never": [
"node_modules"
]
},
"travis-cov": {
"threshold": 70
}
},
"contributors": [
{
"name": "taoqf",
"email": "tao_qiufeng@126.com"
}
],
"dependencies": {
"css-select": "^4.1.3",
"he": "1.2.0"
},
"description": "A very fast HTML parser, generating a simplified DOM, with basic element query support.",
"devDependencies": {
"@types/entities": "latest",
"@types/he": "latest",
"@types/node": "latest",
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/eslint-plugin-tslint": "latest",
"@typescript-eslint/parser": "latest",
"blanket": "latest",
"cheerio": "^1.0.0-rc.5",
"del-cli": "latest",
"eslint": "latest",
"eslint-config-prettier": "latest",
"eslint-plugin-import": "latest",
"high5": "^1.0.0",
"htmlparser": "^1.7.7",
"htmlparser-benchmark": "^1.1.3",
"htmlparser2": "^6.0.0",
"mocha": "latest",
"np": "latest",
"parse5": "^6.0.1",
"should": "latest",
"spec": "latest",
"travis-cov": "latest",
"typescript": "next"
},
"directories": {
"test": "test"
},
"homepage": "https://github.com/taoqf/node-fast-html-parser",
"keywords": [
"parser",
"html",
"nodejs",
"typescript"
],
"license": "MIT",
"main": "dist/index.js",
"module": "dist/esm/index.js",
"name": "node-html-parser",
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"repository": {
"type": "git",
"url": "git+https://github.com/taoqf/node-fast-html-parser.git"
},
"scripts": {
"build": "npm run lint && npm run clean && npm run ts:cjs && npm run ts:amd && npm run ts:esm",
"clean": "del-cli ./dist/",
"dev": "tsc -w & mocha -w ./test/*.js",
"lint": "eslint ./src/*.ts ./src/**/*.ts",
"prepare": "npm run build",
"pretest": "tsc -m commonjs",
"release": "yarn build && np",
"test": "mocha",
"ts:amd": "tsc -t es5 -m amd -d false --outFile ./dist/main.js",
"ts:cjs": "tsc -m commonjs",
"ts:esm": "tsc -t esnext -m esnext -d false --outDir ./dist/esm/"
},
"sideEffects": false,
"types": "dist/index.d.ts",
"version": "3.3.6"
}
-2127
View File
File diff suppressed because it is too large Load Diff