-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,181 additions
and
16 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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'use strict' | ||
|
||
var repeat = require('repeat-string') | ||
var is = require('hast-util-is-element') | ||
var resolve = require('../util/resolve') | ||
var findSelectedOptions = require('../util/find-selected-options') | ||
|
||
module.exports = input | ||
|
||
// eslint-disable-next-line complexity | ||
function input(h, node) { | ||
var byId = h.nodeById | ||
var props = node.properties | ||
var value = props.value || props.placeholder | ||
var list = props.list | ||
var type = props.type | ||
var values = [] | ||
var length | ||
var index | ||
var results | ||
var url | ||
var text | ||
|
||
if (props.disabled || props.type === 'hidden' || props.type === 'file') { | ||
return | ||
} | ||
|
||
if (type === 'checkbox' || type === 'radio') { | ||
return {type: 'text', value: '[' + (props.checked ? 'x' : ' ') + ']'} | ||
} | ||
|
||
if (type === 'image' && props.alt) { | ||
values = [[props.alt]] | ||
} else if (value) { | ||
values = [[value]] | ||
} else if ( | ||
list && | ||
type !== 'password' && // `list` is not supported on `password` | ||
type !== 'file' && // …or `file` | ||
type !== 'submit' && // …or `submit` | ||
type !== 'reset' && // …or `reset` | ||
type !== 'button' && // …or `button` | ||
list in byId && | ||
is(byId[list], 'datalist') | ||
) { | ||
values = findSelectedOptions(byId[list], props) | ||
} | ||
|
||
if (values.length === 0) { | ||
return | ||
} | ||
|
||
// Passwords don’t support `list`. | ||
if (type === 'password') { | ||
values[0] = [repeat('•', values[0][0].length)] | ||
} | ||
|
||
// Images don’t support `list`. | ||
if (type === 'image') { | ||
return h(node, 'image', { | ||
url: resolve(h, props.src), | ||
title: props.title || null, | ||
alt: values[0][0] | ||
}) | ||
} | ||
|
||
length = values.length | ||
index = -1 | ||
results = [] | ||
|
||
if (type !== 'url' && type !== 'email') { | ||
while (++index < length) { | ||
value = values[index] | ||
results.push(value[1] ? value[1] + ' (' + value[0] + ')' : value[0]) | ||
} | ||
|
||
return h.augment(node, {type: 'text', value: results.join(', ')}) | ||
} | ||
|
||
while (++index < length) { | ||
value = values[index] | ||
text = resolve(h, value[0]) | ||
url = type === 'email' ? 'mailto:' + text : text | ||
|
||
results.push( | ||
h(node, 'link', {title: null, url: url}, [ | ||
{type: 'text', value: value[1] || text} | ||
]) | ||
) | ||
|
||
if (index !== length - 1) { | ||
results.push({type: 'text', value: ', '}) | ||
} | ||
} | ||
|
||
return results | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
var findSelectedOptions = require('../util/find-selected-options') | ||
|
||
module.exports = select | ||
|
||
function select(h, node) { | ||
var values = findSelectedOptions(node) | ||
var length = values.length | ||
var index = -1 | ||
var results = [] | ||
var value | ||
|
||
while (++index < length) { | ||
value = values[index] | ||
results.push(value[1] ? value[1] + ' (' + value[0] + ')' : value[0]) | ||
} | ||
|
||
if (results.length !== 0) { | ||
return h.augment(node, { | ||
type: 'text', | ||
value: results.join(', ') | ||
}) | ||
} | ||
} |
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,9 @@ | ||
'use strict' | ||
|
||
var toText = require('hast-util-to-text') | ||
|
||
module.exports = textarea | ||
|
||
function textarea(h, node) { | ||
return h.augment(node, {type: 'text', value: toText(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict' | ||
|
||
var is = require('hast-util-is-element') | ||
var has = require('hast-util-has-property') | ||
var toText = require('hast-util-to-text') | ||
|
||
module.exports = findSelectedOptions | ||
|
||
function findSelectedOptions(node, properties) { | ||
var props = properties || node.properties | ||
var multiple = props.multiple | ||
var size = Math.min(parseInt(props.size, 10), 0) || (multiple ? 4 : 1) | ||
var options = findOptions(node) | ||
var length = options.length | ||
var index = -1 | ||
var selectedOptions = [] | ||
var values = [] | ||
var option | ||
var list | ||
var content | ||
var label | ||
var value | ||
|
||
while (++index < length) { | ||
option = options[index] | ||
|
||
if (option.properties.selected) { | ||
selectedOptions.push(option) | ||
} | ||
} | ||
|
||
list = selectedOptions.length === 0 ? options : selectedOptions | ||
options = list.slice(0, size) | ||
length = options.length | ||
index = -1 | ||
|
||
while (++index < length) { | ||
option = options[index] | ||
content = toText(option) | ||
label = content || option.properties.label | ||
value = option.properties.value || content | ||
|
||
values.push([value, label === value ? null : label]) | ||
} | ||
|
||
return values | ||
} | ||
|
||
function findOptions(node) { | ||
var children = node.children | ||
var length = children.length | ||
var index = -1 | ||
var results = [] | ||
var child | ||
|
||
while (++index < length) { | ||
child = children[index] | ||
|
||
if (is(child, 'option')) { | ||
if (!has(child, 'disabled')) { | ||
results.push(child) | ||
} | ||
} else if ('children' in child) { | ||
results = results.concat(findOptions(child)) | ||
} | ||
} | ||
|
||
return results | ||
} |
Oops, something went wrong.