-
-
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
184 changed files
with
3,613 additions
and
3,739 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
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,34 @@ | ||
/** | ||
* @typedef {import('hast').Nodes} Nodes | ||
*/ | ||
|
||
// To do: next major: remove return result. | ||
/** | ||
* Set the plain-text value of a hast node. | ||
* This is like the DOMs `Node#textContent` setter. | ||
* The given node is returned. | ||
* | ||
* @template {Nodes} Thing | ||
* Node kind. | ||
* @param {Thing} node | ||
* Node to change. | ||
* @param {string | null | undefined} [value=''] | ||
* Value to use (default: `''`) | ||
* @returns {Thing} | ||
* Given node. | ||
*/ | ||
export function fromString(node, value) { | ||
const normalized = value === undefined || value === null ? '' : String(value) | ||
|
||
if ('children' in node) { | ||
node.children = [] | ||
|
||
if (value) { | ||
node.children.push({type: 'text', value: normalized}) | ||
} | ||
} else if (node.type !== 'doctype') { | ||
node.value = normalized | ||
} | ||
|
||
return 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
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,40 +1,56 @@ | ||
import assert from 'node:assert/strict' | ||
import test from 'node:test' | ||
import {u} from 'unist-builder' | ||
import {fromString} from './index.js' | ||
|
||
test('hast-util-from-string', async function (t) { | ||
await t.test('should set text nodes', async function () { | ||
assert.deepEqual( | ||
// @ts-expect-error: `value` missing. | ||
fromString(u('text'), 'foo'), | ||
u('text', 'foo') | ||
) | ||
}) | ||
|
||
await t.test('should reset text nodes (1)', async function () { | ||
assert.deepEqual( | ||
// @ts-expect-error: `value` missing. | ||
fromString(u('text')), | ||
u('text', '') | ||
) | ||
assert.deepEqual(fromString({type: 'text', value: 'alpha'}, 'bravo'), { | ||
type: 'text', | ||
value: 'bravo' | ||
}) | ||
}) | ||
|
||
await t.test('should reset text nodes (2)', async function () { | ||
assert.deepEqual(fromString(u('text', 'foo')), u('text', '')) | ||
await t.test('should reset text nodes', async function () { | ||
assert.deepEqual(fromString({type: 'text', value: 'alpha'}), { | ||
type: 'text', | ||
value: '' | ||
}) | ||
}) | ||
|
||
await t.test('should set parent nodes', async function () { | ||
assert.deepEqual( | ||
fromString(u('element', {tagName: 'p', properties: {}}, []), 'foo'), | ||
u('element', {tagName: 'p', properties: {}}, [u('text', 'foo')]) | ||
fromString( | ||
{ | ||
type: 'element', | ||
tagName: 'p', | ||
properties: {}, | ||
children: [{type: 'text', value: 'alpha'}] | ||
}, | ||
'bravo' | ||
), | ||
{ | ||
type: 'element', | ||
tagName: 'p', | ||
properties: {}, | ||
children: [{type: 'text', value: 'bravo'}] | ||
} | ||
) | ||
}) | ||
|
||
await t.test('should reset parent nodes', async function () { | ||
assert.deepEqual( | ||
fromString(u('element', {tagName: 'p', properties: {}}, [])), | ||
u('element', {tagName: 'p', properties: {}}, []) | ||
fromString({ | ||
type: 'element', | ||
tagName: 'p', | ||
properties: {}, | ||
children: [{type: 'text', value: 'alpha'}] | ||
}), | ||
{ | ||
type: 'element', | ||
tagName: 'p', | ||
properties: {}, | ||
children: [] | ||
} | ||
) | ||
}) | ||
}) |
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,38 @@ | ||
/** | ||
* @typedef {import('hast').Nodes} Nodes | ||
*/ | ||
|
||
const list = new Set(['pingback', 'prefetch', 'stylesheet']) | ||
|
||
/** | ||
* Checks whether a node is a “body OK” link. | ||
* | ||
* @param {Nodes} node | ||
* Node to check. | ||
* @returns {boolean} | ||
* Whether `node` is a “body OK” link. | ||
*/ | ||
export function isBodyOkLink(node) { | ||
if (node.type !== 'element' || node.tagName !== 'link') { | ||
return false | ||
} | ||
|
||
if (node.properties.itemProp) { | ||
return true | ||
} | ||
|
||
const rel = node.properties.rel | ||
let index = -1 | ||
|
||
if (!Array.isArray(rel) || rel.length === 0) { | ||
return false | ||
} | ||
|
||
while (++index < rel.length) { | ||
if (!list.has(String(rel[index]))) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
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
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,17 @@ | ||
const re = /^\[if[ \t\f\n\r]+[^\]]+]|<!\[endif]$/ | ||
|
||
/** | ||
* @typedef {import('hast').Nodes} Nodes | ||
*/ | ||
|
||
/** | ||
* Check if a node is a conditional comment. | ||
* | ||
* @param {Nodes} node | ||
* Node to check. | ||
* @returns {boolean} | ||
* Whether `node` is a conditional comment. | ||
*/ | ||
export function isConditionalComment(node) { | ||
return node.type === 'comment' && re.test(node.value) | ||
} |
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
Oops, something went wrong.