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
Conversation
bridge/polyfill/src/dom.ts
Outdated
@@ -23,3 +23,199 @@ Object.defineProperty(document, 'body', { | |||
enumerable: true, | |||
configurable: false | |||
}); | |||
|
|||
|
|||
// Some utility methods. |
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.
可以独立一个文件
bridge/polyfill/src/dom.ts
Outdated
|
||
|
||
// Some utility methods. | ||
let slice = function slice(arr: HTMLCollectionOf<Element>): Array<Element> { |
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.
这些工具方法在kraken下并不需要
bridge/polyfill/src/dom.ts
Outdated
let els = temp.selectors; | ||
selector = temp.ruleStr; | ||
|
||
// Get By ID |
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.
By -> by, 所有注释都不太规范
bridge/polyfill/src/dom.ts
Outdated
} | ||
|
||
// Get By Attributes | ||
if (attributes.length !== 0) { |
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.
没有对应的 testcase
bridge/polyfill/src/querySelector.ts
Outdated
@@ -0,0 +1,175 @@ | |||
function fetchSelector(str: string, regex: RegExp) { |
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.
querySelector.ts 文件名风格需要统一
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.
已修改
selectors: str.match(regex) || [], | ||
ruleStr: str.replace(regex, ' ') | ||
}; | ||
}; |
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.
FunctionDeclaration 不需要分号结束
|
||
elements = elements.concat(tempElements); | ||
return elements; | ||
}; |
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.
同上
} | ||
let elements = this.querySelectorAll(selector); | ||
return elements.length > 0 ? elements[0] : null; | ||
}; |
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.
1
div.style.backgroundColor = item; | ||
div.setAttribute('id', `id-${index}`); | ||
document.body.appendChild(div); | ||
}) |
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.
分号
div.style.backgroundColor = item; | ||
div.setAttribute('id', `id-${index}`); | ||
document.body.appendChild(div); | ||
}) |
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.
分号
div.style.backgroundColor = item; | ||
div.setAttribute('id', `id-${index}`); | ||
document.body.appendChild(div); | ||
}) |
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.
1
div.setAttribute('id', `id-${index}`); | ||
div.className = `class-${index} cc`; | ||
document.body.appendChild(div); | ||
}) |
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.
1
div.setAttribute('id', `id-${index}`); | ||
div.setAttribute('data-test', `attr-${index}`); | ||
document.body.appendChild(div); | ||
}) |
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.
1
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.
直接找个格式化工具吧
selector = temp.ruleStr; | ||
// TODO: now only support "equal". | ||
// Attributes. e.g. [rel=external]. | ||
temp = fetchSelector(selector, /\[.+?\]/g); |
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.
只支持 equal,正则是不是限定下 /[.+=.+]/g
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.
讨论了下这个正则没问题,去掉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', () => { |
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.
querySelector 和 querySelectorAll 的测试用例建议像 getElementByClassName 一样独立成文件,不用全部放在 document 中
selector = temp.ruleStr; | ||
|
||
// Elements. e.g. header, div. | ||
temp = fetchSelector(selector, /\w+/g); |
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.
讨论了下这个正则没问题。
close #41