Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add recognizeNoValueAttribute option for issue #75 #89

Merged
merged 3 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,45 +81,50 @@ Tag objects can contain three keys. The `tag` key takes the name of the tag as t
## Options

### `directives`
Type: `Array`
Default: `[{name: '!doctype', start: '<', end: '>'}]`
Description: *Adds processing of custom directives. Note: The property ```name``` in custom directives can be ```String``` or ```RegExp``` type*
Type: `Array`
Default: `[{name: '!doctype', start: '<', end: '>'}]`
Description: *Adds processing of custom directives. Note: The property ```name``` in custom directives can be ```String``` or ```RegExp``` type*

### `xmlMode`
Type: `Boolean`
Default: `false`
Type: `Boolean`
Default: `false`
Description: *Indicates whether special tags (`<script>` and `<style>`) should get special treatment and if "empty" tags (eg. `<br>`) can have children. If false, the content of special tags will be text only. For feeds and other XML content (documents that don't consist of HTML), set this to true.*

### `decodeEntities`
Type: `Boolean`
Default: `false`
Type: `Boolean`
Default: `false`
Description: *If set to true, entities within the document will be decoded.*

### `lowerCaseTags`
Type: `Boolean`
Default: `false`
Type: `Boolean`
Default: `false`
Description: *If set to true, all tags will be lowercased. If `xmlMode` is disabled.*

### `lowerCaseAttributeNames`
Type: `Boolean`
Default: `false`
Type: `Boolean`
Default: `false`
Description: *If set to true, all attribute names will be lowercased. This has noticeable impact on speed.*

### `recognizeCDATA`
Type: `Boolean`
Default: `false`
Type: `Boolean`
Default: `false`
Description: *If set to true, CDATA sections will be recognized as text even if the `xmlMode` option is not enabled. NOTE: If `xmlMode` is set to `true` then CDATA sections will always be recognized as text.*

### `recognizeSelfClosing`
Type: `Boolean`
Default: `false`
Type: `Boolean`
Default: `false`
Description: *If set to true, self-closing tags will trigger the `onclosetag` event even if `xmlMode` is not set to `true`. NOTE: If `xmlMode` is set to `true` then self-closing tags will always be recognized.*

### `sourceLocations`
Type: `Boolean`
Default: `false`
### `sourceLocations`
Type: `Boolean`
Default: `false`
Description: *If set to true, AST nodes will have a `location` property containing the `start` and `end` line and column position of the node.*

### `recognizeNoValueAttribute`
Type: `Boolean`
Default: `false`
Description: *If set to true, AST nodes will recognize attribute with no value and mark as `true` which will be correctly rendered by `posthtml-render` package*

## License

[MIT](LICENSE)
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type Directive = {
export type Options = {
directives?: Directive[];
sourceLocations?: boolean;
recognizeNoValueAttribute?: boolean;
} & ParserOptions;

export type Tag = string | boolean;
Expand Down Expand Up @@ -45,6 +46,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
const bufArray: Node[] = [];
const results: Node[] = [];
let lastOpenTagEndIndex = 0;
let noValueAttributes: Record<string, true> = {};

function bufferArrayLast(): Node {
return bufArray[bufArray.length - 1];
Expand All @@ -70,6 +72,11 @@ export const parser = (html: string, options: Options = {}): Node[] => {
const object: Attributes = {};

object[key] = String(attrs[key]).replace(/&quot;/g, '"');

if (options.recognizeNoValueAttribute && noValueAttributes[key]) {
object[key] = true;
}

Object.assign(result, object);
});

Expand Down Expand Up @@ -122,6 +129,17 @@ export const parser = (html: string, options: Options = {}): Node[] => {
}
}

function onattribute(name: string, value: string, quote?: string | undefined | null) {
// Quote: Quotes used around the attribute.
// `null` if the attribute has no quotes around the value,
// `undefined` if the attribute has no value.
if (quote === undefined) {
// `true` is recognized by posthtml-render as attrubute without value
// See: https://github.com/posthtml/posthtml-render/blob/master/src/index.ts#L268
noValueAttributes[name] = true;
}
}

function onopentag(tag: string, attrs: Attributes) {
const buf: NodeTag = { tag };

Expand All @@ -137,6 +155,10 @@ export const parser = (html: string, options: Options = {}): Node[] => {
buf.attrs = normalizeArributes(attrs);
}

// Always reset after normalizeArributes
// Reason: onopentag callback will fire after all attrubutes have been processed
noValueAttributes = {};

bufArray.push(buf);
}

Expand Down Expand Up @@ -201,6 +223,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
const parser = new Parser({
onprocessinginstruction,
oncomment,
onattribute,
onopentag,
onclosetag,
ontext
Expand Down
27 changes: 27 additions & 0 deletions test/test-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,30 @@ test('should parse with input in button', t => {
];
t.deepEqual(tree, expected);
});

test('should parse no value attribute as `true` when `recognizeNoValueAttribute` is `true` ', t => {
const tree = parser(
'<div class="className" hasClass>Content</div>',
{ recognizeNoValueAttribute: true }
);
const expected = [
{
tag: 'div',
attrs: { class: 'className', hasClass: true },
content: ['Content']
}
];
t.deepEqual(tree, expected);
});

test('should parse no value attribute as empty string when `recognizeNoValueAttribute` is `false` or not set ', t => {
const tree = parser('<div class="className" hasClass>Content</div>');
const expected = [
{
tag: 'div',
attrs: { class: 'className', hasClass: '' },
content: ['Content']
}
];
t.deepEqual(tree, expected);
});