dom-serializer
Advanced tools
Comparing version 1.0.1 to 1.1.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.attributeNames = exports.elementNames = void 0; | ||
exports.elementNames = new Map([ | ||
@@ -4,0 +5,0 @@ ["altglyph", "altGlyph"], |
@@ -1,2 +0,2 @@ | ||
import { Node } from "domhandler"; | ||
import type { Node } from "domhandler"; | ||
export interface DomSerializerOptions { | ||
@@ -3,0 +3,0 @@ emptyAttrs?: boolean; |
@@ -13,7 +13,19 @@ "use strict"; | ||
}; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
@@ -23,10 +35,11 @@ }; | ||
/* | ||
Module dependencies | ||
*/ | ||
* Module dependencies | ||
*/ | ||
var ElementType = __importStar(require("domelementtype")); | ||
var entities = __importStar(require("entities")); | ||
/* mixed-case SVG and MathML tags & attributes | ||
recognized by the HTML parser, see | ||
https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign | ||
*/ | ||
var entities_1 = require("entities"); | ||
/* | ||
* Mixed-case SVG and MathML tags & attributes | ||
* recognized by the HTML parser, see | ||
* https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign | ||
*/ | ||
var foreignNames_1 = require("./foreignNames"); | ||
@@ -43,5 +56,5 @@ var unencodedElements = new Set([ | ||
]); | ||
/* | ||
Format attributes | ||
*/ | ||
/** | ||
* Format attributes | ||
*/ | ||
function formatAttributes(attributes, opts) { | ||
@@ -52,22 +65,18 @@ if (!attributes) | ||
.map(function (key) { | ||
var _a; | ||
var value = attributes[key]; | ||
var _a, _b; | ||
var value = (_a = attributes[key]) !== null && _a !== void 0 ? _a : ""; | ||
if (opts.xmlMode === "foreign") { | ||
/* fix up mixed-case attribute names */ | ||
key = (_a = foreignNames_1.attributeNames.get(key)) !== null && _a !== void 0 ? _a : key; | ||
/* Fix up mixed-case attribute names */ | ||
key = (_b = foreignNames_1.attributeNames.get(key)) !== null && _b !== void 0 ? _b : key; | ||
} | ||
if (!opts.emptyAttrs && | ||
!opts.xmlMode && | ||
(value === null || value === "")) { | ||
if (!opts.emptyAttrs && !opts.xmlMode && value === "") { | ||
return key; | ||
} | ||
return key + "=\"" + (opts.decodeEntities | ||
? entities.encodeXML(value) | ||
: value.replace(/"/g, """)) + "\""; | ||
return key + "=\"" + (opts.decodeEntities ? entities_1.encodeXML(value) : value.replace(/"/g, """)) + "\""; | ||
}) | ||
.join(" "); | ||
} | ||
/* | ||
Self-enclosing tags | ||
*/ | ||
/** | ||
* Self-enclosing tags | ||
*/ | ||
var singleTag = new Set([ | ||
@@ -145,8 +154,9 @@ "area", | ||
if (opts.xmlMode === "foreign") { | ||
/* fix up mixed-case element names */ | ||
/* Fix up mixed-case element names */ | ||
elem.name = (_a = foreignNames_1.elementNames.get(elem.name)) !== null && _a !== void 0 ? _a : elem.name; | ||
/* exit foreign mode at integration points */ | ||
/* Exit foreign mode at integration points */ | ||
if (elem.parent && | ||
foreignModeIntegrationPoints.has(elem.parent.name)) | ||
foreignModeIntegrationPoints.has(elem.parent.name)) { | ||
opts = __assign(__assign({}, opts), { xmlMode: false }); | ||
} | ||
} | ||
@@ -161,7 +171,7 @@ if (!opts.xmlMode && foreignElements.has(elem.name)) { | ||
} | ||
if ((!elem.children || elem.children.length === 0) && | ||
if (elem.children.length === 0 && | ||
(opts.xmlMode | ||
? // in XML mode or foreign mode, and user hasn't explicitly turned off self-closing tags | ||
? // In XML mode or foreign mode, and user hasn't explicitly turned off self-closing tags | ||
opts.selfClosingTags !== false | ||
: // user explicitly asked for self-closing tags, even in HTML mode | ||
: // User explicitly asked for self-closing tags, even in HTML mode | ||
opts.selfClosingTags && singleTag.has(elem.name))) { | ||
@@ -174,3 +184,3 @@ if (!opts.xmlMode) | ||
tag += ">"; | ||
if (elem.children) { | ||
if (elem.children.length > 0) { | ||
tag += render(elem.children, opts); | ||
@@ -189,6 +199,6 @@ } | ||
var data = elem.data || ""; | ||
// if entities weren't decoded, no need to encode them back | ||
// If entities weren't decoded, no need to encode them back | ||
if (opts.decodeEntities && | ||
!(elem.parent && unencodedElements.has(elem.parent.name))) { | ||
data = entities.encodeXML(data); | ||
data = entities_1.encodeXML(data); | ||
} | ||
@@ -195,0 +205,0 @@ return data; |
@@ -18,4 +18,3 @@ "use strict"; | ||
var cheerio_1 = __importDefault(require("cheerio")); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
// @ts-expect-error Module does not have types for now | ||
var parse_1 = __importDefault(require("cheerio/lib/parse")); | ||
@@ -37,7 +36,9 @@ var index_1 = __importDefault(require("./index")); | ||
describe("render DOM parsed with htmlparser2", function () { | ||
// only test applicable to the default setup | ||
// Only test applicable to the default setup | ||
describe("(html)", function () { | ||
var htmlFunc = html.bind(null, { _useHtmlParser2: true }); | ||
// it doesn't really make sense for {decodeEntities: false} | ||
// since currently it will convert <hr class='blah'> into <hr class="blah"> anyway. | ||
/* | ||
* It doesn't really make sense for {decodeEntities: false} | ||
* since currently it will convert <hr class='blah'> into <hr class="blah"> anyway. | ||
*/ | ||
it("should handle double quotes within single quoted attributes properly", function () { | ||
@@ -48,5 +49,5 @@ var str = "<hr class='an \"edge\" case' />"; | ||
}); | ||
// run html with default options | ||
// Run html with default options | ||
describe("(html, {})", testBody.bind(null, html.bind(null, { _useHtmlParser2: true }))); | ||
// run html with turned off decodeEntities | ||
// Run html with turned off decodeEntities | ||
describe("(html, {decodeEntities: false})", testBody.bind(null, html.bind(null, { _useHtmlParser2: true, decodeEntities: false }))); | ||
@@ -53,0 +54,0 @@ describe("(xml)", function () { |
{ | ||
"name": "dom-serializer", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "render dom nodes to string", | ||
"author": "Felix Boehm <me@feedic.com>", | ||
"sideEffects": false, | ||
"keywords": [ | ||
@@ -27,6 +28,6 @@ "html", | ||
"@types/cheerio": "^0.22.18", | ||
"@types/jest": "^25.1.4", | ||
"@types/node": "^13.9.1", | ||
"@typescript-eslint/eslint-plugin": "^2.31.0", | ||
"@typescript-eslint/parser": "^2.31.0", | ||
"@types/jest": "^26.0.3", | ||
"@types/node": "^14.10.1", | ||
"@typescript-eslint/eslint-plugin": "^4.1.0", | ||
"@typescript-eslint/parser": "^4.1.0", | ||
"cheerio": "^1.0.0-rc.2", | ||
@@ -39,4 +40,4 @@ "coveralls": "^3.0.5", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^25.3.1", | ||
"typescript": "^3.5.3" | ||
"ts-jest": "^26.1.0", | ||
"typescript": "^4.0.2" | ||
}, | ||
@@ -46,3 +47,3 @@ "scripts": { | ||
"coverage": "cat coverage/lcov.info | coveralls", | ||
"lint": "eslint --ext=js,ts src", | ||
"lint": "eslint src", | ||
"format": "prettier --write '**/*.{ts,md,json}'", | ||
@@ -56,3 +57,4 @@ "build": "tsc", | ||
}, | ||
"funding": "https://github.com/cheeriojs/dom-serializer?sponsor=1", | ||
"license": "MIT" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23137
511