This repository has been archived by the owner on Aug 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: - An AST is now generate from the JSDOM tree. - You can still access the originalNode using `node.originalNode`. - You now have to call `fromHtmlAttribute` and `fromHtmlElement` to replace a node.
- Loading branch information
Showing
16 changed files
with
1,357 additions
and
671 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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
import { JSDOM } from 'jsdom' | ||
import { fromHtmlElement } from 'h2x-types' | ||
|
||
function parse(code) { | ||
const { window } = new JSDOM() | ||
const container = window.document.createElement('div') | ||
container.innerHTML = code | ||
return container | ||
return fromHtmlElement(JSDOM.fragment(code)) | ||
} | ||
|
||
export default parse |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const ELEMENT_NODE = 1 | ||
export const TEXT_NODE = 3 | ||
export const COMMENT_NODE = 8 | ||
export const FRAGMENT_NODE = 11 |
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,20 @@ | ||
import { VISITOR_KEYS } from './symbols' | ||
|
||
const HTML_ELEMENT_PROPERTIES = ['name', 'value'] | ||
|
||
class HTMLAttributeNode { | ||
static [VISITOR_KEYS] = null | ||
|
||
constructor(originalNode) { | ||
this.originalNode = originalNode | ||
HTML_ELEMENT_PROPERTIES.forEach(property => { | ||
this[property] = originalNode[property] | ||
}) | ||
} | ||
} | ||
|
||
function fromHtmlAttribute(attribute) { | ||
return new HTMLAttributeNode(attribute) | ||
} | ||
|
||
export default fromHtmlAttribute |
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,31 @@ | ||
import { VISITOR_KEYS } from './symbols' | ||
import fromHtmlAttribute from './fromHtmlAttribute' | ||
|
||
const HTML_ELEMENT_PROPERTIES = ['tagName', 'ownerDocument', 'textContent'] | ||
|
||
class HTMLElementNode { | ||
static [VISITOR_KEYS] = ['childNodes', 'attributes'] | ||
|
||
constructor(originalNode) { | ||
this.originalNode = originalNode | ||
this.childNodes = originalNode.childNodes | ||
? Array.from(originalNode.childNodes).map(childNode => | ||
fromHtmlElement(childNode), | ||
) | ||
: null | ||
this.attributes = originalNode.attributes | ||
? Array.from(originalNode.attributes).map(attribute => | ||
fromHtmlAttribute(attribute), | ||
) | ||
: null | ||
HTML_ELEMENT_PROPERTIES.forEach(property => { | ||
this[property] = originalNode[property] | ||
}) | ||
} | ||
} | ||
|
||
function fromHtmlElement(htmlNode) { | ||
return new HTMLElementNode(htmlNode) | ||
} | ||
|
||
export default fromHtmlElement |
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 |
---|---|---|
@@ -1,13 +1,7 @@ | ||
import { VISITOR_KEYS } from './symbols' | ||
|
||
const getHTMLVisitorKeys = node => { | ||
if (node.constructor.name === 'Attr') return null | ||
return ['childNodes', 'attributes'] | ||
} | ||
|
||
function getNodeVisitorKeys(node) { | ||
if (node.constructor[VISITOR_KEYS]) return node.constructor[VISITOR_KEYS] | ||
return getHTMLVisitorKeys(node) | ||
return node.constructor[VISITOR_KEYS] || null | ||
} | ||
|
||
export default getNodeVisitorKeys |
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,3 +1,5 @@ | ||
export * from './symbols' | ||
export { default as getNodeType } from './getNodeType' | ||
export { default as getNodeVisitorKeys } from './getNodeVisitorKeys' | ||
export { default as fromHtmlElement } from './fromHtmlElement' | ||
export { default as fromHtmlAttribute } from './fromHtmlAttribute' |
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,2 +1,2 @@ | ||
export const NODE_TYPE = Symbol('NODE_TYPE') | ||
export const VISITOR_KEYS = Symbol('VISITOR_KEYS') | ||
export const NODE_TYPE = Symbol.for('NODE_TYPE') | ||
export const VISITOR_KEYS = Symbol.for('VISITOR_KEYS') |
Oops, something went wrong.