Skip to content

Commit

Permalink
fix: get diff style error (closes #24 #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed May 14, 2023
1 parent 651b36f commit 69d11db
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/clone-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export async function cloneNode<T extends Node>(
) {
const cloned = await cloneElement(node, context)

// fix abnormal attribute
cloned.removeAttribute('"')

const diffStyle = copyCssStyles(node, cloned, isRoot, context)

if (isRoot) applyCssStyleWithOptions(cloned, context)
Expand Down
8 changes: 6 additions & 2 deletions src/copy-pseudo-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ export function copyPseudoClass<T extends HTMLElement | SVGElement>(

function copyBy(pseudoClass: string) {
const computedStyle = ownerWindow!.getComputedStyle(node, pseudoClass)
const content = computedStyle.getPropertyValue('content')
let content = computedStyle.getPropertyValue('content')

if (!content || content === 'none') return

content = content
// TODO support css.counter
.replace(/(')|(")|(counter\(.+\))/g, '')

const klasses = [uuid()]
const defaultStyle = getDefaultStyle(node, pseudoClass, context)
const cloneStyle = [
`content: '${ content.replace(/'|"/g, '') }';`,
`content: '${ content }';`,
]

const diffStyle = getDiffStyle(computedStyle, defaultStyle)
Expand Down
10 changes: 5 additions & 5 deletions src/get-diff-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export function getDiffStyle(
defaultStyle: Map<string, string>,
) {
const diffStyle = new Map<string, [string, string]>()
const diffStylePrefixs: string[] = []
const prefixs: string[] = []
const prefixTree = new Map<string, Map<string, [string, string]>>()

for (let len = style.length, i = 0; i < len; i++) {
Expand All @@ -12,7 +12,7 @@ export function getDiffStyle(
const priority = style.getPropertyPriority(name)

const subIndex = name.lastIndexOf('-')
const prefix = subIndex > -1 ? undefined : name.substring(0, subIndex)
const prefix = subIndex > -1 ? name.substring(0, subIndex) : undefined
if (prefix) {
let map = prefixTree.get(prefix)
if (!map) {
Expand All @@ -25,14 +25,14 @@ export function getDiffStyle(
if (defaultStyle.get(name) === value && !priority) continue

if (prefix) {
diffStylePrefixs.push(prefix)
prefixs.push(prefix)
} else {
diffStyle.set(name, [value, priority])
}
}

for (let len = diffStylePrefixs.length, i = 0; i < len; i++) {
prefixTree.get(diffStylePrefixs[i])
for (let len = prefixs.length, i = 0; i < len; i++) {
prefixTree.get(prefixs[i])
?.forEach((value, name) => diffStyle.set(name, value))
}

Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/css.counter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<style>
#root {
width: 100px;
height: 100px;
background-color: red;
}

span {
content: counter(number);
}
</style>

<template>
<div id="root">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</template>
Binary file added test/fixtures/css.counter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/fixtures/dom.attribute.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<style>
#root {
width: 100px;
height: 100px;
background-color: red;
}
</style>

<template>
<div id="root">
<div class="abnormal-attribute" " ></div>
</div>
</template>
Binary file added test/fixtures/dom.attribute.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

3 comments on commit 69d11db

@vercel
Copy link

@vercel vercel bot commented on 69d11db May 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

modern-screenshot – ./

modern-screenshot.vercel.app
modern-screenshot-git-main-qq15725.vercel.app
modern-screenshot-qq15725.vercel.app

@qq15725
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closes #25

@askdesigners
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woohoo! Thanks!! My issue is resolved 🙏🏻

Please sign in to comment.