Skip to content

Commit bdd6c1e

Browse files
committed
Add support for legacy elements
1 parent dec8971 commit bdd6c1e

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,27 @@ function trimAndcollapseSpacesAndTabs(value, breakBefore, breakAfter) {
444444
}
445445
}
446446

447-
// We don’t support non-conforming legacy features:
448-
//
449-
// * `pre[wrap]` -> `pre-wrap`
450-
// * `listing`, `plaintext`, `xmp` -> `pre`
451-
// * `nobr`, `td[nowrap]`, `th[nowrap]` -> `nowrap`
452-
// * `nobr wbr` -> `normal`
447+
// We don’t support void elements here (so `nobr wbr` -> `normal` is ignored).
453448
function inferWhiteSpace(node, options) {
449+
var props = node.properties || {}
450+
var inherit = options.whiteSpace || 'normal'
451+
454452
switch (node.tagName) {
455-
case 'pre':
453+
case 'listing':
454+
case 'plaintext':
455+
case 'xmp':
456456
return 'pre'
457+
case 'nobr':
458+
return 'nowrap'
459+
case 'pre':
460+
return props.wrap ? 'pre-wrap' : 'pre'
461+
case 'td':
462+
case 'th':
463+
return props.noWrap ? 'nowrap' : inherit
457464
case 'textarea':
458465
return 'pre-wrap'
459466
default:
460-
return options.whiteSpace || 'normal'
467+
return inherit
461468
}
462469
}
463470

@@ -490,7 +497,6 @@ function row(node) {
490497
}
491498

492499
// See: <https://html.spec.whatwg.org/#the-css-user-agent-style-sheet-and-presentational-hints>
493-
// Note: Legacy elements and attributes are not supported.
494500
function blockOrCaption(node) {
495501
return is(node, [
496502
'caption', // `table-caption`
@@ -500,6 +506,7 @@ function blockOrCaption(node) {
500506
// Flow content
501507
'address',
502508
'blockquote',
509+
'center', // Legacy
503510
'dialog',
504511
'div',
505512
'figure',
@@ -509,9 +516,12 @@ function blockOrCaption(node) {
509516
'header',
510517
'hr',
511518
'legend',
519+
'listing', // Legacy
512520
'main',
513521
'p',
522+
'plaintext', // Legacy
514523
'pre',
524+
'xmp', // Legacy
515525
// Sections and headings
516526
'article',
517527
'aside',
@@ -525,6 +535,7 @@ function blockOrCaption(node) {
525535
'nav',
526536
'section',
527537
// Lists
538+
'dir', // Legacy
528539
'dd',
529540
'dl',
530541
'dt',

readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ Get the plain-text value of a [node][].
8080
* A line feed is collapsed instead of ignored in cases where Fullwidth, Wide,
8181
or Halfwidth East Asian Width characters are used, the same goes for a case
8282
with Chinese, Japanese, or Yi writing systems
83-
* Replaced elements and legacy elements (like `listing`, `plaintext`) are
84-
treated like normal elements
83+
* Replaced elements are treated like normal elements
8584

8685
## Related
8786

test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,32 @@ test('hast-util-to-text', function(t) {
253253
'should support a `pre` element'
254254
)
255255

256+
st.equal(
257+
toText(
258+
h('pre', {wrap: true}, ['\tAlpha \n\tbravo', h('br'), 'charlie()'])
259+
),
260+
'\tAlpha \n\tbravo\ncharlie()',
261+
'should support `[wrap]` on a `pre` element'
262+
)
263+
264+
st.equal(
265+
toText(h('listing', '\tAlpha \n\tbravo.')),
266+
'\tAlpha \n\tbravo.',
267+
'should support a `listing` element'
268+
)
269+
270+
st.equal(
271+
toText(h('td', {noWrap: true}, '\tAlpha \n\tbravo.')),
272+
'\tAlpha \n\tbravo.',
273+
'should support `[nowrap]` on a `td` element'
274+
)
275+
276+
st.equal(
277+
toText(h('nobr', '\tAlpha \n\tbravo.')),
278+
'\tAlpha \n\tbravo.',
279+
'should support a `nobr` element'
280+
)
281+
256282
st.equal(
257283
toText(h('textarea', '\tDelta \n\techo\t\n')),
258284
'\tDelta \n\techo\t\n',

0 commit comments

Comments
 (0)