shortcode-tree
Advanced tools
Comparing version 1.4.7 to 1.4.8
@@ -14,8 +14,14 @@ 'use strict'; | ||
*/ | ||
stringify: function stringify(shortcode) { | ||
stringify: function stringify(shortcode, options) { | ||
if (!options) { | ||
options = ShortcodeFormatter.DEFAULT_OPTIONS; | ||
} else { | ||
options = Object.assign({}, ShortcodeFormatter.DEFAULT_OPTIONS, options); | ||
} | ||
var buffer = ""; | ||
// Open tag "[name" | ||
buffer += ShortcodeFormatter.T_TAG_START; | ||
buffer += shortcode.name; | ||
buffer += options.asHtml ? ShortcodeFormatter.T_TAG_START_HTML : ShortcodeFormatter.T_TAG_START; | ||
buffer += options.tagName ? options.tagName : shortcode.name; | ||
@@ -40,3 +46,3 @@ // Add any properties | ||
// Wrap in quotes as string literal if needed | ||
if (this.getShouldValueBeLiteral(value)) { | ||
if (options.asHtml || this.getShouldValueBeLiteral(value)) { | ||
buffer += ShortcodeFormatter.T_TAG_PROPERTY_VALUE_WRAPPER; | ||
@@ -58,3 +64,7 @@ buffer += this.escapeValueForLiteral(value); | ||
buffer += ShortcodeFormatter.T_TAG_END; | ||
if (options.asHtml) { | ||
buffer += ShortcodeFormatter.T_TAG_END_HTML; | ||
} else { | ||
buffer += ShortcodeFormatter.T_TAG_END; | ||
} | ||
@@ -68,6 +78,13 @@ // Add raw content | ||
if (!shortcode.isSelfClosing) { | ||
buffer += ShortcodeFormatter.T_TAG_START; | ||
buffer += ShortcodeFormatter.T_TAG_CLOSER; | ||
buffer += shortcode.name; | ||
buffer += ShortcodeFormatter.T_TAG_END; | ||
if (options.asHtml) { | ||
buffer += ShortcodeFormatter.T_TAG_START_HTML; | ||
buffer += ShortcodeFormatter.T_TAG_CLOSER; | ||
buffer += shortcode.name; | ||
buffer += ShortcodeFormatter.T_TAG_END_HTML; | ||
} else { | ||
buffer += ShortcodeFormatter.T_TAG_START; | ||
buffer += ShortcodeFormatter.T_TAG_CLOSER; | ||
buffer += shortcode.name; | ||
buffer += ShortcodeFormatter.T_TAG_END; | ||
} | ||
} | ||
@@ -100,2 +117,4 @@ | ||
ShortcodeFormatter.T_TAG_END = "]"; | ||
ShortcodeFormatter.T_TAG_START_HTML = "<"; | ||
ShortcodeFormatter.T_TAG_END_HTML = ">"; | ||
ShortcodeFormatter.T_TAG_CLOSER = "/"; | ||
@@ -107,2 +126,7 @@ ShortcodeFormatter.T_TAG_PROPERTY_ASSIGN = "="; | ||
ShortcodeFormatter.DEFAULT_OPTIONS = { | ||
asHtml: false, | ||
tagName: null | ||
}; | ||
module.exports = ShortcodeFormatter; |
@@ -142,3 +142,3 @@ "use strict"; | ||
/** | ||
* Renders this Shortcode as formatted code. | ||
* Renders this Shortcode as formatted code, in shortcode style. | ||
* | ||
@@ -155,2 +155,20 @@ * @returns {string} | ||
/** | ||
* Renders this Shortcode as formatted code, in HTML style. | ||
* | ||
* @param {string|null} tagName Optional HTML tag name override. | ||
* @return {*} | ||
*/ | ||
}, { | ||
key: "stringifyAsHtml", | ||
value: function stringifyAsHtml(tagName) { | ||
var config = { | ||
asHtml: true, | ||
tagName: tagName ? tagName : null | ||
}; | ||
return ShortcodeFormatter.stringify(this, config); | ||
} | ||
/** | ||
* Converts this item to string. | ||
@@ -157,0 +175,0 @@ * |
{ | ||
"name": "shortcode-tree", | ||
"version": "1.4.7", | ||
"version": "1.4.8", | ||
"description": "Parser library for reading short codes (BB codes) into a tree structure", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -32,3 +32,3 @@ # shortcode-tree | ||
- Supports tag properties (with or without string literals) | ||
- Stringify `Shortcode` objects to shortcode text | ||
- Stringify `Shortcode` objects to shortcode text or HTML equivalent | ||
- Extract only text / HTML from a document containg shortcodes | ||
@@ -85,2 +85,3 @@ | ||
| `stringify()` | string | Formats the data in the `Shortcode` object to shortcode text | | ||
| `stringifyHtml([string/null] tagName)` | string | Formats the data in the `Shortcode` object to HTML text | | ||
| `hasProperty([string] key)` | boolean | Gets whether property with name `key` exists. | | ||
@@ -87,0 +88,0 @@ | `getProperty([string] key)` | value or null | Gets value of property with name `key`, or NULL if not set. | |
@@ -70,2 +70,72 @@ let Shortcode = require('../src').Shortcode; | ||
}); | ||
}); | ||
describe('ShortcodeFormatter.stringify() in HTML mode', function () { | ||
let config = { | ||
asHtml: true | ||
}; | ||
it('stringifies a simple shortcode with content', function () { | ||
let testInput = new Shortcode("b", "Bold text"); | ||
let expectedOutput = "<b>Bold text</b>"; | ||
let actualOutput = ShortcodeFormatter.stringify(testInput, config); | ||
expect(actualOutput).to.equal(expectedOutput); | ||
}); | ||
it('stringifies a shortcode with content and text properties', function () { | ||
let testInput = new Shortcode("b", "Bold text", {"font-weight": "bolder"}); | ||
let expectedOutput = `<b font-weight="bolder">Bold text</b>`; | ||
let actualOutput = ShortcodeFormatter.stringify(testInput, config); | ||
expect(actualOutput).to.equal(expectedOutput); | ||
}); | ||
it('stringifies a simple shortcode with content and value-less properties', function () { | ||
let testInput = new Shortcode("b", "Bold text", {"checked": null}); | ||
let expectedOutput = "<b checked>Bold text</b>"; | ||
let actualOutput = ShortcodeFormatter.stringify(testInput, config); | ||
expect(actualOutput).to.equal(expectedOutput); | ||
}); | ||
it('stringifies a self-closing shortcode', function () { | ||
let testInput = new Shortcode("img", null, {}, true); | ||
let expectedOutput = "<img/>"; | ||
let actualOutput = ShortcodeFormatter.stringify(testInput, config); | ||
expect(actualOutput).to.equal(expectedOutput); | ||
}); | ||
it('stringifies a self-closing shortcode with text properties', function () { | ||
let testInput = new Shortcode("img", null, {"src": "sample.jpeg", "align": "center"}, true); | ||
let expectedOutput = `<img src="sample.jpeg" align="center"/>`; | ||
let actualOutput = ShortcodeFormatter.stringify(testInput, config); | ||
expect(actualOutput).to.equal(expectedOutput); | ||
}); | ||
it('correctly escapes quotes in string literals in shortcode properties', function () { | ||
let testInput = new Shortcode("b", "Bold text", {"name": `Mr. "Badass" McGee`}); | ||
let expectedOutput = `<b name="Mr. \\"Badass\\" McGee">Bold text</b>`; | ||
let actualOutput = ShortcodeFormatter.stringify(testInput, config); | ||
expect(actualOutput).to.equal(expectedOutput); | ||
}); | ||
it('does not format single number values as string literals in shortcode properties', function () { | ||
let testInput = new Shortcode("b", "Bold text", {"one": `100%`, "two": 123, "three": 123.45, "four": "12345"}); | ||
let expectedOutput = `<b one="100%" two="123" three="123.45" four="12345">Bold text</b>`; | ||
let actualOutput = ShortcodeFormatter.stringify(testInput, config); | ||
expect(actualOutput).to.equal(expectedOutput); | ||
}); | ||
it('shortcode.stringifyAsHtml() acts as an exact alias', function () { | ||
let testInput = new Shortcode("any", "Whatever", {sample: 123}); | ||
let actualOutput = ShortcodeFormatter.stringify(testInput, config); | ||
let actualOutputAlias = testInput.stringifyAsHtml(); | ||
expect(actualOutput).to.equal(actualOutputAlias); | ||
}); | ||
}); |
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
76069
1334
199