-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
202 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"id": "replaced ID", | ||
"title": "replaced title" | ||
} |
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,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title data-decor-attribute-id="param:id" data-decor-content="param:title"> | ||
Decor default template | ||
</title> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
</head> | ||
<body></body> | ||
</html> |
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,56 @@ | ||
import { Element, HTMLDocument } from './deps/deno-dom.ts' | ||
import { PartialTemplate, Template } from './template.ts' | ||
|
||
const attributeRegex = /^data-decor-attribute-(.+)$/ | ||
const parameterRegex = /^param:(.+)$/ | ||
|
||
function replaceElementAttributes( | ||
element: Element, | ||
parameters: Record<string, string>, | ||
): void { | ||
for (const attribute of element.attributes) { | ||
const parametersMatch = parameterRegex.exec(attribute.value) | ||
if (!parametersMatch) { | ||
continue | ||
} | ||
|
||
const parameterKey = parametersMatch[1] | ||
if (!parameters[parameterKey]) { | ||
continue | ||
} | ||
|
||
const attrebuteMatch = attributeRegex.exec(attribute.name) | ||
if (attrebuteMatch) { | ||
const attributeName = attrebuteMatch[1] | ||
element.setAttribute(attributeName, parameters[parameterKey]) | ||
continue | ||
} | ||
|
||
if (attribute.name === 'data-decor-content') { | ||
element.innerHTML = parameters[parameterKey] | ||
} | ||
} | ||
} | ||
|
||
export function replaceDocumentParameters( | ||
document: HTMLDocument, | ||
parameters: Record<string, string>, | ||
): void { | ||
document.querySelectorAll('*').forEach((element) => { | ||
replaceElementAttributes(element as Element, parameters) | ||
}) | ||
} | ||
|
||
export function replaceTemplateParameters( | ||
template: PartialTemplate, | ||
parameters: Record<string, string>, | ||
): void { | ||
for ( | ||
const key of Object.keys(template) as Array<keyof Template> | ||
) { | ||
const element = template[key] | ||
if (element) { | ||
replaceElementAttributes(element, parameters) | ||
} | ||
} | ||
} |
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,39 @@ | ||
import { DOMParser } from './deps/deno-dom.ts' | ||
import { assertEquals } from './deps/std/assert.ts' | ||
import { createPartialTemplate } from './template.ts' | ||
import { | ||
replaceDocumentParameters, | ||
replaceTemplateParameters, | ||
} from './replace_parameters.ts' | ||
|
||
Deno.test( | ||
'replaceDocumentParameters handles parameter replacement in a document', | ||
() => { | ||
const document = new DOMParser().parseFromString( | ||
'<div data-decor-attribute-foo="param:bar" data-decor-content="param:bar"></div>', | ||
'text/html', | ||
)! | ||
replaceDocumentParameters(document, { bar: 'baz' }) | ||
const element = document.querySelector('div')! | ||
assertEquals(element.getAttribute('foo'), 'baz') | ||
assertEquals(element.innerHTML, 'baz') | ||
}, | ||
) | ||
|
||
Deno.test( | ||
'replaceTemplateParameters handles parameter replacement in a template', | ||
() => { | ||
const document = new DOMParser().parseFromString( | ||
'<h1 data-decor-attribute-foo="param:bar" data-decor-content="param:bar"></h1>', | ||
'text/html', | ||
)! | ||
const template = { | ||
...createPartialTemplate(), | ||
heading1: document.querySelector('h1')!, | ||
} | ||
replaceTemplateParameters(template, { bar: 'baz' }) | ||
const element = template.heading1 | ||
assertEquals(element.getAttribute('foo'), 'baz') | ||
assertEquals(element.innerHTML, 'baz') | ||
}, | ||
) |
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