Skip to content
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

Feat/support querySelector and querySelectorAll #672

Merged
merged 33 commits into from Sep 23, 2021

Conversation

answershuto
Copy link
Member

close #41

@@ -23,3 +23,199 @@ Object.defineProperty(document, 'body', {
enumerable: true,
configurable: false
});


// Some utility methods.
Copy link
Contributor

Choose a reason for hiding this comment

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

可以独立一个文件



// Some utility methods.
let slice = function slice(arr: HTMLCollectionOf<Element>): Array<Element> {
Copy link
Contributor

Choose a reason for hiding this comment

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

这些工具方法在kraken下并不需要

let els = temp.selectors;
selector = temp.ruleStr;

// Get By ID
Copy link
Contributor

Choose a reason for hiding this comment

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

By -> by, 所有注释都不太规范

}

// Get By Attributes
if (attributes.length !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

没有对应的 testcase

@answershuto answershuto changed the title Feat/support querySelector and querySelectorAll [WIP]Feat/support querySelector and querySelectorAll Sep 10, 2021
@answershuto answershuto changed the title [WIP]Feat/support querySelector and querySelectorAll Feat/support querySelector and querySelectorAll Sep 16, 2021
@answershuto answershuto changed the title Feat/support querySelector and querySelectorAll [WIP]Feat/support querySelector and querySelectorAll Sep 16, 2021
@answershuto answershuto changed the title [WIP]Feat/support querySelector and querySelectorAll Feat/support querySelector and querySelectorAll Sep 16, 2021
@@ -0,0 +1,175 @@
function fetchSelector(str: string, regex: RegExp) {
Copy link
Contributor

Choose a reason for hiding this comment

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

querySelector.ts 文件名风格需要统一

Copy link
Member Author

Choose a reason for hiding this comment

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

已修改

selectors: str.match(regex) || [],
ruleStr: str.replace(regex, ' ')
};
};
Copy link
Member

Choose a reason for hiding this comment

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

FunctionDeclaration 不需要分号结束


elements = elements.concat(tempElements);
return elements;
};
Copy link
Member

Choose a reason for hiding this comment

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

同上

}
let elements = this.querySelectorAll(selector);
return elements.length > 0 ? elements[0] : null;
};
Copy link
Member

Choose a reason for hiding this comment

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

1

div.style.backgroundColor = item;
div.setAttribute('id', `id-${index}`);
document.body.appendChild(div);
})
Copy link
Member

Choose a reason for hiding this comment

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

分号

div.style.backgroundColor = item;
div.setAttribute('id', `id-${index}`);
document.body.appendChild(div);
})
Copy link
Member

Choose a reason for hiding this comment

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

分号

div.style.backgroundColor = item;
div.setAttribute('id', `id-${index}`);
document.body.appendChild(div);
})
Copy link
Member

Choose a reason for hiding this comment

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

1

div.setAttribute('id', `id-${index}`);
div.className = `class-${index} cc`;
document.body.appendChild(div);
})
Copy link
Member

Choose a reason for hiding this comment

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

1

div.setAttribute('id', `id-${index}`);
div.setAttribute('data-test', `attr-${index}`);
document.body.appendChild(div);
})
Copy link
Member

Choose a reason for hiding this comment

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

1

Copy link
Member

Choose a reason for hiding this comment

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

直接找个格式化工具吧

wssgcg1213
wssgcg1213 previously approved these changes Sep 23, 2021
selector = temp.ruleStr;
// TODO: now only support "equal".
// Attributes. e.g. [rel=external].
temp = fetchSelector(selector, /\[.+?\]/g);
Copy link
Contributor

@temper357 temper357 Sep 23, 2021

Choose a reason for hiding this comment

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

只支持 equal,正则是不是限定下 /[.+=.+]/g

Copy link
Member Author

Choose a reason for hiding this comment

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

讨论了下这个正则没问题,去掉TODO。

@@ -32,4 +32,468 @@ describe('Document api', () => {
expect(document.all).not.toBeUndefined();
expect(document.all.length).toBeGreaterThan(0);
});

it('document querySelector cant find element', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

querySelector 和 querySelectorAll 的测试用例建议像 getElementByClassName 一样独立成文件,不用全部放在 document 中

selector = temp.ruleStr;

// Elements. e.g. header, div.
temp = fetchSelector(selector, /\w+/g);
Copy link
Member Author

Choose a reason for hiding this comment

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

讨论了下这个正则没问题。

@answershuto answershuto merged commit 28a5c77 into main Sep 23, 2021
@answershuto answershuto deleted the feat/support_querySelector branch September 23, 2021 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

支持 querySelector 与 querySelectorAll
4 participants