New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@begin/enhance

Package Overview
Dependencies
Maintainers
4
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@begin/enhance - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

timed.js

164

index.js
import path from 'path'
import { parse, parseFragment as fragment, serialize } from 'parse5'
import { parse, fragment, serialize } from '@begin/parse5'
import isCustomElement from './lib/is-custom-element.js'

@@ -33,13 +33,31 @@ const TEMPLATES = path.join('..', 'views', 'templates')

function findSlots(node) {
const state = {}
let place = 0
export function encode(value) {
if (typeof value !== 'string') {
const id = `__b_${place++}`
state[id] = value
return id
}
else {
return value
}
}
function processCustomElements(node, templates) {
const elements = []
const find = (node) => {
for (const child of node.childNodes) {
if (child.tagName === 'slot') {
if (isCustomElement(child.tagName)) {
elements.push(child)
const template = expandTemplate(child, templates)
fillSlots(child, template)
const nodeChildNodes = child.childNodes
nodeChildNodes.splice(
0,
nodeChildNodes.length,
...template.childNodes
)
}
if (!isCustomElement(child.tagName) &&
child.childNodes) {
find(child)
}
if (child.childNodes) find(child)
}

@@ -51,24 +69,28 @@ }

function findInserts(node) {
const elements = []
const find = (node) => {
for (const child of node.childNodes) {
const attrs = child.attrs
if (attrs) {
for (let i=0; i < attrs.length; i++) {
if (attrs[i].name === 'slot') {
elements.push(child)
}
}
}
if (!isCustomElement(child.tagName) &&
child.childNodes) {
find(child)
}
}
function expandTemplate(node, templates) {
return fragment(renderTemplate(node.tagName, templates, node.attrs) || '')
}
function renderTemplate(tagName, templates, attrs) {
const templatePath = `${templates}/${tagName}.js`
try {
return require(templatePath)
.default(attrs && attrsToState(attrs), render)
}
find(node)
return elements
catch {
console.warn(`🤷🏻‍♀️ Template file not found at: ${templatePath}`)
}
}
function attrsToState(attrs, state={}) {
[...attrs].forEach(attr => state[attr.name] = decode(attr.value))
return state
}
export function decode(value) {
return value.startsWith('__b_')
? state[value]
: value
}
function fillSlots(node, template) {

@@ -78,8 +100,10 @@ const slots = findSlots(template)

for (let i=0; i < slots.length; i++) {
const slotsLength = slots.length
for (let i=0; i<slotsLength; i++) {
let hasSlotName = false
const slot = slots[i]
const slotAttrs = slot.attrs || []
let hasSlotName = false
for (let i=0; i < slotAttrs.length; i++) {
const slotAttrsLength = slotAttrs.length
for (let i=0; i < slotAttrsLength; i++) {
const attr = slotAttrs[i]

@@ -90,7 +114,9 @@ if (attr.name === 'name') {

for (let i=0; i < inserts.length; i ++) {
const insertsLength = inserts.length
for (let i=0; i < insertsLength; i ++) {
const insert = inserts[i]
const insertAttrs = insert.attrs || []
for (let i=0; i < insertAttrs.length; i++) {
const insertAttrsLength = insertAttrs.length
for (let i=0; i < insertAttrsLength; i++) {
const attr = insertAttrs[i]

@@ -126,18 +152,13 @@ const insertSlot = attr.value

function processCustomElements(node, templates) {
function findSlots(node) {
const elements = []
const find = (node) => {
for (const child of node.childNodes) {
if (isCustomElement(child.tagName)) {
if (child.tagName === 'slot') {
elements.push(child)
const template = expandTemplate(child, templates)
fillSlots(child, template)
const nodeChildNodes = child.childNodes
nodeChildNodes.splice(
0,
nodeChildNodes.length,
...template.childNodes
)
}
if (child.childNodes) find(child)
if (!isCustomElement(child.tagName) &&
child.childNodes) {
find(child)
}
}

@@ -149,24 +170,22 @@ }

function expandTemplate(node, templates) {
return fragment(renderTemplate(node.tagName, templates, node.attrs) || '')
}
function addScriptTags(body, scripts) {
body.childNodes.push(...scripts.childNodes)
}
function attrsToState(attrs, state={}) {
[...attrs].forEach(attr => state[attr.name] = decode(attr.value))
return state
}
function renderTemplate(tagName, templates, attrs) {
const templatePath = `${templates}/${tagName}.js`
try {
return require(templatePath)
.default(attrs && attrsToState(attrs), render)
function findInserts(node) {
const elements = []
const find = (node) => {
for (const child of node.childNodes) {
const attrs = child.attrs
if (attrs) {
for (let i=0; i < attrs.length; i++) {
if (attrs[i].name === 'slot') {
elements.push(child)
}
}
}
if (!isCustomElement(child.tagName) &&
child.childNodes) {
find(child)
}
}
}
catch {
console.warn(`🤷🏻‍♀️ Template file not found at: ${templatePath}`)
}
find(node)
return elements
}

@@ -180,19 +199,4 @@

const state = {}
let place = 0
export function encode(value) {
if (typeof value !== 'string') {
const id = `__b_${place++}`
state[id] = value
return id
}
else {
return value
}
function addScriptTags(body, scripts) {
body.childNodes.push(...scripts.childNodes)
}
export function decode(value) {
return value.startsWith('__b_')
? state[value]
: value
}
{
"name": "@begin/enhance",
"version": "0.0.4",
"version": "0.0.5",
"description": "Custom Element Server-side rendering CESR",

@@ -17,5 +17,5 @@ "main": "index.js",

"dependencies": {
"parse5": "^6.0.1",
"@begin/parse5": "^0.0.2",
"esm": "^3.2.25"
}
}
import test from 'tape'
import enhance from '..'
// Timed version
//import enhance from '../timed.js'
const strip = str => str.replace(/\r?\n|\r|\s\s+/g, '')

@@ -13,8 +15,18 @@

test('enhance should exist', t => {
t.ok(html)
test('Enhance should', t => {
t.ok(true, 'it really should')
t.end()
})
test('should expand template', t=> {
test('exist', t => {
t.ok(enhance, 'it lives')
t.end()
})
test('return an html function', t => {
t.ok(html, 'ah yes, this might come in handy')
t.end()
})
test('expand template', t=> {
const actual = html`<my-paragraph></my-paragraph>`

@@ -30,3 +42,3 @@ const expected = doc(`

strip(expected),
'expands template with slot default content correctly'
'by gum, i do believe that it does expand that template with slotted default content'
)

@@ -36,3 +48,3 @@ t.end()

test('should fill named slot', t=> {
test('fill named slot', t=> {
const actual = html`

@@ -52,3 +64,3 @@ <my-paragraph>

strip(expected),
'Fills named slot'
'fills that named slot alright'
)

@@ -58,3 +70,3 @@ t.end()

test('should add authored children to unnamed slot.', t=> {
test('add authored children to unnamed slot', t=> {
const actual = html`

@@ -76,3 +88,3 @@ <my-content>

strip(expected),
'Adds unslotted children to unnamed slot'
'adds unslotted children to the unnamed slot'
)

@@ -82,3 +94,3 @@ t.end()

test('should pass attributes as state', t=> {
test('pass attributes as state', t=> {
const actual = html`

@@ -96,3 +108,3 @@ <my-link href='/yolo' text='sketchy'></my-link>

strip(expected),
'Passes attributes as state'
'passes attributes as a state object when executing template functions'
)

@@ -102,3 +114,3 @@ t.end()

test('should pass attribute array values correctly', t => {
test('pass attribute array values correctly', t => {
const things = [{ title: 'one' },{ title: 'two' },{ title: 'three' }]

@@ -124,3 +136,3 @@ const actual = html`

strip(expected),
'Passes complex attribute as state'
'this means that encoding and decoding arrays and objects works, exciting'
)

@@ -131,3 +143,3 @@ t.end()

test('should update deeply nested slots', t=> {
test('update deeply nested slots', t=> {
const actual = html`

@@ -164,3 +176,3 @@ <my-content>

strip(expected),
'Updates deeply nested slots'
'updates deeply nested slots SLOTS ON SLOTS ON SLOTS'
)

@@ -170,3 +182,3 @@ t.end()

test('should fill nested rendered slots', t=> {
test('fill nested rendered slots', t=> {
const items = [{ title: 'one' },{ title: 'two' },{ title: 'three' }]

@@ -199,3 +211,3 @@ const actual = html`

strip(expected),
'Renders nested custom elements by passing html function'
'Wow it renders nested custom elements by passing that handy render function when executing template functions'
)

@@ -205,8 +217,9 @@ t.end()

test('should not throw when template not found', t => {
test('not throw when template not found', t => {
t.ok(
html`<missing-template></missing-template>`,
'Warns instead of throwing.'
'well that\'s nice, it warns instead of throwing.'
)
t.end()
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc