Comparing version 4.12.0 to 5.0.0
@@ -235,2 +235,6 @@ /*global window*/ | ||
return clone; | ||
} else if (typeof args[0] === 'string' && args.length === 1) { | ||
clone = this.clone(); | ||
clone.text(args[0]); | ||
return clone; | ||
} else if (typeof args[0] === 'string') { | ||
@@ -244,3 +248,4 @@ clone = this.clone(); | ||
'a callback appending content to a pen or\n' + | ||
'a style and arguments for that style'); | ||
'a style and arguments for that style or\n' + | ||
'just a string.'); | ||
} | ||
@@ -274,12 +279,12 @@ }; | ||
function isRawOutput(options) { | ||
return typeof options.width === 'number' && | ||
typeof options.height === 'number' && ( | ||
typeof options.content === 'function' || | ||
typeof options.content === 'string' | ||
); | ||
return options && | ||
typeof options === 'object' && | ||
typeof options.width === 'number' && | ||
typeof options.height === 'number' && ( | ||
typeof options.content === 'function' || | ||
typeof options.content === 'string' | ||
); | ||
} | ||
MagicPen.prototype.alt = | ||
MagicPen.prototype.raw = function (options) { | ||
MagicPen.prototype.alt = function (options) { | ||
var format = this.format; | ||
@@ -290,14 +295,26 @@ if (!format) { | ||
var outputProperty = options[format] || options.fallback; | ||
var outputProperty = options[format]; | ||
if (!outputProperty) { | ||
throw new Error('Output is not specified for format: ' + format + ' and no fallback method is given'); | ||
if (typeof outputProperty === 'undefined') { | ||
if (options.fallback) { | ||
return this.append(options.fallback); | ||
} else { | ||
throw new Error('Output is not specified for format: ' + format + ' and no fallback method is given'); | ||
} | ||
} | ||
var propertyType = typeof outputProperty; | ||
if (outputProperty && outputProperty.isMagicPen) { | ||
if (typeof outputProperty === 'string' || isRawOutput(outputProperty)) { | ||
return this.raw(outputProperty); | ||
} else { | ||
return this.append(outputProperty); | ||
} | ||
}; | ||
if (propertyType === 'string') { | ||
MagicPen.prototype.raw = function (options) { | ||
var format = this.format; | ||
if (!format) { | ||
throw new Error('The alt method is only supported on pen where the format has already been set'); | ||
} | ||
if (typeof options === 'string') { | ||
return this.write({ style: 'raw', args: { | ||
@@ -307,3 +324,3 @@ height: 0, | ||
content: function () { | ||
return outputProperty; | ||
return options; | ||
} | ||
@@ -313,22 +330,7 @@ }}); | ||
if (propertyType === 'function') { | ||
var pen = this.clone(); | ||
var originalAlt = pen.alt; | ||
pen.raw = pen.alt = function (arg) { | ||
if (isRawOutput(arg) || typeof arg !== 'object') { | ||
var content = arg; | ||
arg = {}; | ||
arg[format] = content; | ||
} | ||
return originalAlt.call(this, arg); | ||
}; | ||
outputProperty.call(pen, pen); | ||
return this.append(pen); | ||
} | ||
if (isRawOutput(outputProperty)) { | ||
if (typeof outputProperty.content === 'string') { | ||
outputProperty = extend({}, outputProperty); | ||
var content = outputProperty.content; | ||
outputProperty.content = function () { | ||
if (isRawOutput(options)) { | ||
if (typeof options.content === 'string') { | ||
options = extend({}, options); | ||
var content = options.content; | ||
options.content = function () { | ||
return content; | ||
@@ -338,8 +340,9 @@ }; | ||
return this.write({ style: 'raw', args: outputProperty }); | ||
return this.write({ style: 'raw', args: options }); | ||
} | ||
throw new Error('Arguments for the alt method must be an object with properties of the form:\n' + | ||
'a pen, a function that writes to a pen, a string or an object with the structure:\n' + | ||
'{ width: <number>, height: <number>, content: <string function() {}|string> }'); | ||
throw new Error('Raw ' + this.format + ' content needs to adhere to one of the following forms:\n' + | ||
'a string of raw content\n' + | ||
'a function returning a string of raw content or\n' + | ||
'an object with the following form { width: <number>, height: <number>, content: <string function() {}|string> }'); | ||
}; | ||
@@ -346,0 +349,0 @@ |
@@ -575,2 +575,6 @@ /*! | ||
return clone; | ||
} else if (typeof args[0] === 'string' && args.length === 1) { | ||
clone = this.clone(); | ||
clone.text(args[0]); | ||
return clone; | ||
} else if (typeof args[0] === 'string') { | ||
@@ -584,3 +588,4 @@ clone = this.clone(); | ||
'a callback appending content to a pen or\n' + | ||
'a style and arguments for that style'); | ||
'a style and arguments for that style or\n' + | ||
'just a string.'); | ||
} | ||
@@ -614,12 +619,12 @@ }; | ||
function isRawOutput(options) { | ||
return typeof options.width === 'number' && | ||
typeof options.height === 'number' && ( | ||
typeof options.content === 'function' || | ||
typeof options.content === 'string' | ||
); | ||
return options && | ||
typeof options === 'object' && | ||
typeof options.width === 'number' && | ||
typeof options.height === 'number' && ( | ||
typeof options.content === 'function' || | ||
typeof options.content === 'string' | ||
); | ||
} | ||
MagicPen.prototype.alt = | ||
MagicPen.prototype.raw = function (options) { | ||
MagicPen.prototype.alt = function (options) { | ||
var format = this.format; | ||
@@ -630,14 +635,26 @@ if (!format) { | ||
var outputProperty = options[format] || options.fallback; | ||
var outputProperty = options[format]; | ||
if (!outputProperty) { | ||
throw new Error('Output is not specified for format: ' + format + ' and no fallback method is given'); | ||
if (typeof outputProperty === 'undefined') { | ||
if (options.fallback) { | ||
return this.append(options.fallback); | ||
} else { | ||
throw new Error('Output is not specified for format: ' + format + ' and no fallback method is given'); | ||
} | ||
} | ||
var propertyType = typeof outputProperty; | ||
if (outputProperty && outputProperty.isMagicPen) { | ||
if (typeof outputProperty === 'string' || isRawOutput(outputProperty)) { | ||
return this.raw(outputProperty); | ||
} else { | ||
return this.append(outputProperty); | ||
} | ||
}; | ||
if (propertyType === 'string') { | ||
MagicPen.prototype.raw = function (options) { | ||
var format = this.format; | ||
if (!format) { | ||
throw new Error('The alt method is only supported on pen where the format has already been set'); | ||
} | ||
if (typeof options === 'string') { | ||
return this.write({ style: 'raw', args: { | ||
@@ -647,3 +664,3 @@ height: 0, | ||
content: function () { | ||
return outputProperty; | ||
return options; | ||
} | ||
@@ -653,22 +670,7 @@ }}); | ||
if (propertyType === 'function') { | ||
var pen = this.clone(); | ||
var originalAlt = pen.alt; | ||
pen.raw = pen.alt = function (arg) { | ||
if (isRawOutput(arg) || typeof arg !== 'object') { | ||
var content = arg; | ||
arg = {}; | ||
arg[format] = content; | ||
} | ||
return originalAlt.call(this, arg); | ||
}; | ||
outputProperty.call(pen, pen); | ||
return this.append(pen); | ||
} | ||
if (isRawOutput(outputProperty)) { | ||
if (typeof outputProperty.content === 'string') { | ||
outputProperty = extend({}, outputProperty); | ||
var content = outputProperty.content; | ||
outputProperty.content = function () { | ||
if (isRawOutput(options)) { | ||
if (typeof options.content === 'string') { | ||
options = extend({}, options); | ||
var content = options.content; | ||
options.content = function () { | ||
return content; | ||
@@ -678,8 +680,9 @@ }; | ||
return this.write({ style: 'raw', args: outputProperty }); | ||
return this.write({ style: 'raw', args: options }); | ||
} | ||
throw new Error('Arguments for the alt method must be an object with properties of the form:\n' + | ||
'a pen, a function that writes to a pen, a string or an object with the structure:\n' + | ||
'{ width: <number>, height: <number>, content: <string function() {}|string> }'); | ||
throw new Error('Raw ' + this.format + ' content needs to adhere to one of the following forms:\n' + | ||
'a string of raw content\n' + | ||
'a function returning a string of raw content or\n' + | ||
'an object with the following form { width: <number>, height: <number>, content: <string function() {}|string> }'); | ||
}; | ||
@@ -686,0 +689,0 @@ |
{ | ||
"name": "magicpen", | ||
"version": "4.12.0", | ||
"version": "5.0.0", | ||
"description": "Styled output in both consoles and browsers", | ||
@@ -5,0 +5,0 @@ "main": "./lib/MagicPen.js", |
@@ -112,3 +112,3 @@ # MagicPen | ||
Specifying the format is useful in combination with the `raw` method. | ||
Specifying the format is useful in combination with the `alt` and `raw` methods. | ||
@@ -421,4 +421,4 @@ Example: | ||
If a format is given, the pen cloned pen will have that format. This | ||
is useful in combination with the `raw` method. It will fail if the | ||
format of the pen has already been set. | ||
is useful in combination with the `alt` and `raw` methods. It will | ||
fail if the format of the pen has already been set. | ||
@@ -500,3 +500,3 @@ ### addStyle(style, handler) | ||
pen.addStyle('link', function (label, url) { | ||
this.raw({ | ||
this.alt({ | ||
fallback: function () { | ||
@@ -541,22 +541,39 @@ this.text(label).sp().text('(').blue(url).text(')'); | ||
If you're already inside an alt method, you can add more alt output by passing the | ||
content directly to the `alt` method: | ||
### raw | ||
In case you know the format of the pen you are working with, you can append | ||
custom output directly to the serializer without any escaping. | ||
Notice that you must specify the format of the pen before using this method | ||
either at construction time or when cloning. | ||
The raw content can have the following forms: | ||
* An raw object of the following structure | ||
{ | ||
width: <number>, | ||
height: <number>, | ||
content: <string function() {}|string> | ||
} | ||
If a method is given as the content it will be called at serialization time. | ||
* A string, that will be appended in it's raw form into output with a | ||
size of zero. | ||
* A function producing a string that will appended in it's raw form into output | ||
with a size of zero. The function will be called at serialization time. | ||
```javascript | ||
pen.alt({ | ||
html: function () { | ||
this.text('Hello'); | ||
this.alt({ | ||
height: 100, | ||
width: 100, | ||
content: '<canvas id="whoa"></canvas>' | ||
}).nl(); | ||
this.indentLines() | ||
this.i().block(function () { | ||
this.alt(function () { | ||
this.text('it even works in blocks'); | ||
}); | ||
}); | ||
}, | ||
fallback: 'foo' | ||
var pen = magicpen('html'); | ||
var label = 'magicpen'; | ||
var url = 'https://github.com/sunesimonsen/magicpen'; | ||
pen.raw({ | ||
height: 1, | ||
width: label.length, | ||
content: '<a href="' + url + '" alt="' + label + '">' + label + '</a>' | ||
}).nl(); | ||
pen.raw('<a href="' + url + '" alt="' + label + '">' + label + '</a>'); | ||
pen.raw(function () { | ||
return '<a href="' + url + '" alt="' + label + '">' + label + '</a>'; | ||
}); | ||
@@ -563,0 +580,0 @@ ``` |
Sorry, the diff of this file is too big to display
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
564110
4014
790