-
Notifications
You must be signed in to change notification settings - Fork 96
feat: implemented JSX support as text/jsx+xml #96
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
const {Document} = require('../interface/document.js'); | ||
|
||
/** | ||
* @implements globalThis.JSXDocument | ||
*/ | ||
class JSXDocument extends Document { | ||
constructor() { super('text/jsx+xml'); } | ||
} | ||
exports.JSXDocument = JSXDocument |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import {ATTRIBUTE_NODE} from '../shared/constants.js'; | ||
import {CHANGED, VALUE} from '../shared/symbols.js'; | ||
import {CHANGED, VALUE, MIME} from '../shared/symbols.js'; | ||
import {String} from '../shared/utils.js'; | ||
import {attrAsJSON} from '../shared/jsdon.js'; | ||
import {emptyAttributes} from '../shared/attributes.js'; | ||
|
@@ -40,9 +40,10 @@ export class Attr extends Node { | |
} | ||
|
||
toString() { | ||
const {name, [VALUE]: value} = this; | ||
const {ownerDocument, name, [VALUE]: value} = this; | ||
const doubleQuote = ownerDocument[MIME].unquotedJsonAttributes && /^\{(.[\s\S]?)+\}$/.test(value) ? '' : '"' | ||
return emptyAttributes.has(name) && !value ? | ||
name : `${name}="${value.replace(QUOTE, '"')}"`; | ||
name : `${name}=${doubleQuote}${value.replace(QUOTE, doubleQuote ? '"' : '"')}${doubleQuote}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have instead: const useQuote = ownerDocument[MIME].unquotedJsonAttributes && /^\{(.[\s\S]?)+\}$/.test(value);
const quote = useQuote ? '"' : '';
// ...
`${name}=${quote}${value.replace(QUOTE, useQuote ? '"' : '"')}${quote}` There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is def. more readable. Good point. |
||
} | ||
|
||
toJSON() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {Document} from '../interface/document.js'; | ||
|
||
/** | ||
* @implements globalThis.JSXDocument | ||
*/ | ||
export class JSXDocument extends Document { | ||
constructor() { super('text/jsx+xml'); } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,26 +6,38 @@ export const Mime = { | |
'text/html': { | ||
docType: '<!DOCTYPE html>', | ||
ignoreCase: true, | ||
escapeHtmlEntities: true, | ||
voidElements: /^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i | ||
}, | ||
'image/svg+xml': { | ||
docType: '<?xml version="1.0" encoding="utf-8"?>', | ||
ignoreCase: false, | ||
escapeHtmlEntities: true, | ||
voidElements | ||
}, | ||
'text/xml': { | ||
docType: '<?xml version="1.0" encoding="utf-8"?>', | ||
ignoreCase: false, | ||
escapeHtmlEntities: true, | ||
voidElements | ||
}, | ||
'application/xml': { | ||
docType: '<?xml version="1.0" encoding="utf-8"?>', | ||
ignoreCase: false, | ||
escapeHtmlEntities: true, | ||
voidElements | ||
}, | ||
'application/xhtml+xml': { | ||
docType: '<?xml version="1.0" encoding="utf-8"?>', | ||
ignoreCase: false, | ||
escapeHtmlEntities: true, | ||
voidElements | ||
}, | ||
'text/jsx+xml': { | ||
docType: '<?xml version="1.0" encoding="utf-8"?>', | ||
ignoreCase: false, | ||
escapeHtmlEntities: false, | ||
unquotedJsonAttributes: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 things I'd rather change here, or discuss:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
voidElements | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const assert = require('../assert.js').for('XMLDocument'); | ||
|
||
const {DOMParser} = global[Symbol.for('linkedom')]; | ||
|
||
const document = (new DOMParser).parseFromString('<html></html>', 'text/jsx+xml'); | ||
|
||
assert(document.toString(), '<html />'); | ||
|
||
assert(document.documentElement.tagName, 'html'); | ||
assert(document.documentElement.nodeName, 'html'); | ||
|
||
document.documentElement.innerHTML = ` | ||
<Something> | ||
<Element someAttribute={foo}>{ bar }</Element> | ||
<Element>Text</Element> | ||
</Something> | ||
`.trim(); | ||
|
||
assert(document.querySelectorAll('Element').length, 2, 'case senstivive 2'); | ||
assert(document.querySelectorAll('element').length, 0, 'case senstivive 0'); | ||
|
||
assert(document.querySelector('Element').attributes.someAttribute.toString(), 'someAttribute={foo}', 'JSX must allow unquoted JSON attributes') | ||
assert(document.querySelector('Element').toString(), '<Element someAttribute={foo}>{ bar }</Element>', 'JSX must render case-sensitive') | ||
|
||
assert(document.toString(), `<html><Something> | ||
<Element someAttribute={foo}>{ bar }</Element> | ||
<Element>Text</Element> | ||
</Something></html>`, '1:1') | ||
|
||
const documentFullRerender = (new DOMParser).parseFromString(`<html />`, 'text/jsx+xml') | ||
|
||
// internally creates a html -> html -> ... structure because | ||
// documentElement cannot be replaced | ||
documentFullRerender.documentElement.innerHTML = `<html> | ||
<head> | ||
<title>{ Some.props.catName }</title> | ||
</head> | ||
<KittenHeader title={"foo"} /> | ||
... | ||
</html>` | ||
|
||
// accessing html (documentElement) -> html (actual root) -> ... here | ||
assert(documentFullRerender.documentElement.firstChild.toString(), `<html> | ||
<head> | ||
<title>{ Some.props.catName }</title> | ||
</head> | ||
<KittenHeader title={"foo"} /> | ||
... | ||
</html>`, 'Internal logic should not override mime-type decision set by the developer') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is
/^\{(.[\s\S]?)+\}$/
for? if it's about starting with{
and ending with}
I believe/^\{[\s\S]*\}$/
is what you are after?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I'll try that; should be more precise than my code there. It was late when I wrote that RegExp :)