-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to wooorm/property-information#6. Related to GH-5.
- Loading branch information
Showing
8 changed files
with
521 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
|
||
// Characters. | ||
var NULL = '\0' | ||
var AMP = '&' | ||
var SP = ' ' | ||
var TB = '\t' | ||
var GR = '`' | ||
var DQ = '"' | ||
var SQ = "'" | ||
var EQ = '=' | ||
var LT = '<' | ||
var GT = '>' | ||
var SO = '/' | ||
var LF = '\n' | ||
var CR = '\r' | ||
var FF = '\f' | ||
|
||
var whitespace = [SP, TB, LF, CR, FF] | ||
// https://html.spec.whatwg.org/#attribute-name-state | ||
var name = whitespace.concat(AMP, SO, GT, EQ) | ||
// https://html.spec.whatwg.org/#attribute-value-(unquoted)-state | ||
var unquoted = whitespace.concat(AMP, GT) | ||
var unquotedSafe = unquoted.concat(NULL, DQ, SQ, LT, EQ, GR) | ||
// https://html.spec.whatwg.org/#attribute-value-(single-quoted)-state | ||
var singleQuoted = [AMP, SQ] | ||
// https://html.spec.whatwg.org/#attribute-value-(double-quoted)-state | ||
var doubleQuoted = [AMP, DQ] | ||
|
||
// Maps of subsets. Each value is a matrix of tuples. | ||
// The first value causes parse errors, the second is valid. | ||
// Of both values, the first value is unsafe, and the second is safe. | ||
module.exports = { | ||
name: [ | ||
[name, name.concat(DQ, SQ, GR)], | ||
[name.concat(NULL, DQ, SQ, LT), name.concat(NULL, DQ, SQ, LT, GR)] | ||
], | ||
unquoted: [[unquoted, unquotedSafe], [unquotedSafe, unquotedSafe]], | ||
single: [ | ||
[singleQuoted, singleQuoted.concat(DQ, GR)], | ||
[singleQuoted.concat(NULL), singleQuoted.concat(NULL, DQ, GR)] | ||
], | ||
double: [ | ||
[doubleQuoted, doubleQuoted.concat(SQ, GR)], | ||
[doubleQuoted.concat(NULL), doubleQuoted.concat(NULL, SQ, GR)] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,50 @@ | ||
'use strict' | ||
|
||
var html = require('property-information/html') | ||
var svg = require('property-information/svg') | ||
var voids = require('html-void-elements') | ||
var omission = require('./omission') | ||
var one = require('./one') | ||
|
||
module.exports = toHTML | ||
|
||
/* Characters. */ | ||
var NULL = '\0' | ||
var AMP = '&' | ||
var SPACE = ' ' | ||
var TAB = '\t' | ||
var GR = '`' | ||
var DQ = '"' | ||
var SQ = "'" | ||
var EQ = '=' | ||
var LT = '<' | ||
var GT = '>' | ||
var SO = '/' | ||
var LF = '\n' | ||
var CR = '\r' | ||
var FF = '\f' | ||
|
||
/* https://html.spec.whatwg.org/#attribute-name-state */ | ||
var NAME = [AMP, SPACE, TAB, LF, CR, FF, SO, GT, EQ] | ||
var CLEAN_NAME = NAME.concat(NULL, DQ, SQ, LT) | ||
|
||
/* In safe mode, all attribute values contain DQ (`"`), | ||
* SQ (`'`), and GR (`` ` ``), as those can create XSS | ||
* issues in older browsers: | ||
* - https://html5sec.org/#59 | ||
* - https://html5sec.org/#102 | ||
* - https://html5sec.org/#108 */ | ||
var QUOTES = [DQ, SQ, GR] | ||
|
||
/* https://html.spec.whatwg.org/#attribute-value-(unquoted)-state */ | ||
var UQ_VALUE = [AMP, SPACE, TAB, LF, CR, FF, GT] | ||
var UQ_VALUE_CLEAN = UQ_VALUE.concat(NULL, DQ, SQ, LT, EQ, GR) | ||
|
||
/* https://html.spec.whatwg.org/#attribute-value-(single-quoted)-state */ | ||
var SQ_VALUE = [AMP, SQ] | ||
var SQ_VALUE_CLEAN = SQ_VALUE.concat(NULL) | ||
|
||
/* https://html.spec.whatwg.org/#attribute-value-(double-quoted)-state */ | ||
var DQ_VALUE = [AMP, DQ] | ||
var DQ_VALUE_CLEAN = DQ_VALUE.concat(NULL) | ||
|
||
/* Stringify the given HAST node. */ | ||
function toHTML(node, options) { | ||
var settings = options || {} | ||
var quote = settings.quote || DQ | ||
var smart = settings.quoteSmart | ||
var errors = settings.allowParseErrors | ||
var characters = settings.allowDangerousCharacters | ||
var alternative = quote === DQ ? SQ : DQ | ||
var name = errors ? NAME : CLEAN_NAME | ||
var unquoted = errors ? UQ_VALUE : UQ_VALUE_CLEAN | ||
var singleQuoted = errors ? SQ_VALUE : SQ_VALUE_CLEAN | ||
var doubleQuoted = errors ? DQ_VALUE : DQ_VALUE_CLEAN | ||
var config | ||
var smart = settings.quoteSmart | ||
|
||
if (quote !== DQ && quote !== SQ) { | ||
throw new Error( | ||
'Invalid quote `' + quote + '`, expected `' + SQ + '` or `' + DQ + '`' | ||
) | ||
} | ||
|
||
config = { | ||
NAME: name.concat(characters ? [] : QUOTES), | ||
UNQUOTED: unquoted.concat(characters ? [] : QUOTES), | ||
DOUBLE_QUOTED: doubleQuoted.concat(characters ? [] : QUOTES), | ||
SINGLE_QUOTED: singleQuoted.concat(characters ? [] : QUOTES), | ||
omit: settings.omitOptionalTags && omission, | ||
quote: quote, | ||
alternative: smart ? alternative : null, | ||
unquoted: Boolean(settings.preferUnquoted), | ||
tight: settings.tightAttributes, | ||
tightDoctype: Boolean(settings.tightDoctype), | ||
tightLists: settings.tightCommaSeparatedLists, | ||
tightClose: settings.tightSelfClosing, | ||
collapseEmpty: settings.collapseEmptyAttributes, | ||
dangerous: settings.allowDangerousHTML, | ||
voids: settings.voids || voids.concat(), | ||
entities: settings.entities || {}, | ||
close: settings.closeSelfClosing | ||
} | ||
|
||
return one(config, node) | ||
return one( | ||
{ | ||
valid: settings.allowParseErrors ? 0 : 1, | ||
safe: settings.allowDangerousCharacters ? 0 : 1, | ||
schema: settings.space === 'svg' ? svg : html, | ||
omit: settings.omitOptionalTags && omission, | ||
quote: quote, | ||
alternative: smart ? alternative : null, | ||
unquoted: Boolean(settings.preferUnquoted), | ||
tight: settings.tightAttributes, | ||
tightDoctype: Boolean(settings.tightDoctype), | ||
tightLists: settings.tightCommaSeparatedLists, | ||
tightClose: settings.tightSelfClosing, | ||
collapseEmpty: settings.collapseEmptyAttributes, | ||
dangerous: settings.allowDangerousHTML, | ||
voids: settings.voids || voids.concat(), | ||
entities: settings.entities || {}, | ||
close: settings.closeSelfClosing, | ||
closeEmpty: settings.closeEmptyElements | ||
}, | ||
node | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.