#61 λ° #135 κ΄λ ¨.
μ§κΈκΉμ§ ν΄λΉ λ¬Έμ μ λν ν λ‘ μ κΈ°λ°μΌλ‘ μ΄κ²μ΄ κ°λ₯νμ§ νμ€νμ§ μμ§λ§ λλ κ·Έκ²μ λ²λ¦΄ κ²μ΄λΌκ³ μκ°νμ΅λλ€.
getBoundingClientRectλ μ΅μν κΈ°λ³Έκ°μ λ°ννλ λλ―Έ ν¨μλ‘ κ΅¬νλμ΄μΌ νλ€κ³ μκ°ν©λλ€.
ν©λ¦¬μ μΌλ‘ 보μ λλ€. λͺ¨λ κ²μ λν΄ 0μ λλ€. ν¨μΉλ₯Ό νμν©λλ€.
@F1LT3R , @domenic κ°μ¬ν©λλ€! (κ·Έ μ¬μ΄μ λλ μ€μ λ‘ μ΄λ―Έ μ΄λ¬ν ν μ€νΈλ₯Ό nodeunitμμ μ μ ν μΉ κΈ°λ° νλ μμν¬μΈ QUnitμΌλ‘ μ΄μνκΈ°λ‘ νμ΅λλ€.) :+1:
getClientRectsκ° jsdomμμ μλν©λκΉ? μ΄ jquery ν°μΌ μ λ°λ₯΄λ©΄,
getClientRectsλ μ§μλμ§ μκ³ getBoundingClientRectλ§ μ§μλ©λλ€. νμ λΉ λ°°μ΄(λλ λͺ¨λ κ°μ λν΄ 0μ΄ μλ λ¨μΌ rectκ° μλ λ°°μ΄??)μ λ°ννλ getClientRects()λ₯Ό μΆκ°νλ PRμ νλ₯ν κ²μ λλ€.
μ, jquery 1.12.0
λ getClientRects
λ₯Ό μ¬μ©νλλ‘ ν΅μ¬ μ€νμΌ κΈ°λ₯μ μ
λ°μ΄νΈνμ΅λλ€. μ°λ¦¬λ κ°μ λ¬Έμ μ μ§λ©΄νμ΅λλ€. μμμΌμ μ΄κ²μ μΆκ°νμ¬ PRμ ν μ μμμ§λ λͺ¨λ¦
λλ€. μ°λ¦¬μ κ²½μ° μ§κΈ λμ§κ³ μλ κ²μ .height()
ν¨μμμ΅λλ€.
el.getBoundingClientRect()μμ μ¬λ°λ₯Έ κ°μ κ°μ Έμ¬ μ μμ΅λλ€. λ°νλλ κ°μ²΄μλ λͺ¨λ μ¬λ°λ₯Έ ν€(λμ΄, λλΉ, μμͺ½ λ±)κ° μμ§λ§ λͺ¨λ κ°μ νμ 0μ λλ€. λ€λ₯Έ μ¬λμ΄ μ΄ λ¬Έμ λ₯Ό κ²ͺκ³ μλμ§ μκ³ μΆμ΅λλ€.
μ΄κ²μ νμ μ€ν¨ν©λλ€:
it('getBoundingClientRect produces correct height', () => {
container.style.height = '300px'
expect(container.getBoundingClientRect().height).eql(300)
})
ν μ€νΈλ₯Ό ν΅κ³ΌνκΈ° μν ν΄κ²° λ°©λ²(λ§νκΈ° νλ 문ꡬ)μ λ€μκ³Ό κ°μ΅λλ€.
getComputedElHeight(el) {
return Number(window.getComputedStyle(el).height.split('px')[0])
}
μ΄ λ¬Έμ κ° μμ§ μ΄λ € μλ λμ 곡μ ν μκ°μ λλ€.
μ¬λ°λ₯Έ κ°μ κ°λλ‘ κ΅¬νλμλ€κ³ μκ°νμ§ μμ΅λλ€. κ·Έλ¬λ κ² κ°μμ
μ¬λ°λ₯Έ μμ±λ§ κ°λλ‘ κ΅¬νλ©λλ€.
2016λ
3μ 14μΌ μ€μ 9μ 2λΆμ "lushfuture" [email protected]μ΄ μμ±νμ΅λλ€.
μ¬λ°λ₯Έ κ°μ κ°μ Έμ¬ μ μμ΅λλ€.
el.getBoundingClientRect(). λ°νλλ κ°μ²΄λ λͺ¨λ
ν€(λμ΄, λλΉ, μμͺ½ λ±)λ μ ννμ§λ§ λͺ¨λ κ°μ νμ 0μ λλ€.
λ€λ₯Έ μ¬λμ΄ μ΄κ²μ κ²½ννκ³ μλμ§ μκ³ μΆμ΅λλ€.μ΄κ²μ νμ μ€ν¨ν©λλ€:
it('getBoundingClientRect produces correct height', () => { container.style.height = '300px' expect(container.getBoundingClientRect().height).eql(300) })
ν μ€νΈλ₯Ό ν΅κ³ΌνκΈ° μν ν΄κ²° λ°©λ²(μ΄λ λλ₯Ό κ΄΄λ‘νλ 문ꡬμ λλ€.
λ§) λ€μκ³Ό κ°μ΅λλ€.getComputedElHeight(μ) {
λ°ν λ²νΈ(window.getComputedStyle(el).height.split('px')[0])
}μ΄ λ¬Έμ κ° μμ§ μ΄λ € μλ λμ 곡μ ν μκ°μ λλ€.
β
μ΄ μ΄λ©μΌμ μ§μ λ΅μ₯νκ±°λ GitHubμμ νμΈνμΈμ.
https://github.com/tmpvar/jsdom/issues/653#issuecomment -196300151.
μ, μ€μ κ°μ λ°ννλλ‘ νλ €λ©΄ λ³Έμ§μ μΌλ‘ λκ·λͺ¨ μμ μ΄ λ μ 체 λ μ΄μμ μμ§μ ꡬνν΄μΌ ν©λλ€. λ¬Έμ #1322μμ ν΄λΉ μ£Όμ λ₯Ό λ€λ£Ήλλ€.
ν μ€νΈμμ ν΄λΉ λ©μλ(λͺ¨λ μμμ λν΄)λ₯Ό μ¬μ μνμ¬ ν΄λΉ λ©μλλ₯Ό μ¬μ©νλ μ½λλ₯Ό ν μ€νΈνλ λμ μ¬μ© κ°λ₯ν κ°μ μ»μ μ μμ΅λκΉ?
μ, λΈλΌμ°μ μμμ κ°μ λ°©μμΌλ‘: Element.prototype.getBoundingClientRect = function () { ... }
λλ λλ΄μλ€
μ°½._μ½μ΄. HTMLDivElement.prototype.getBoundingClientRect
μ μλνμ΅λλ€.
μ΄λ jsdomμ ν₯ν 릴리μ€μμ μ€λ¨λ κ²μ
λλ€(곧, μ€μ λ‘). ._core
μ κ±°νλ©΄ λ©λλ€.
κ°μ¬ν©λλ€ μλν΄λ³΄κ² μ΅λλ€
2016λ
9μ 2μΌ μ€ν 9μ 3λΆμ "Domenic Denicola" [email protected]μ΄ μμ±νμ΅λλ€.
μ΄λ jsdomμ ν₯ν 릴리μ€μμ μ€λ¨λ κ²μ λλ€(곧, μ€μ λ‘). λ¨μ§
_coreλ₯Ό μ κ±°ν©λλ€.β
λΉμ μ΄ λκΈμ λ¬μκΈ° λλ¬Έμ μ΄κ²μ λ°λ κ²μ λλ€.
μ΄ μ΄λ©μΌμ μ§μ λ΅μ₯νκ³ GitHubμμ νμΈνμΈμ.
https://github.com/tmpvar/jsdom/issues/653#issuecomment -244520584 λλ μμκ±°
μ€λ λ
https://github.com/notifications/unsubscribe-auth/AEtD9luz1uJWmJ3JlgdwuO1i5MP04_BXks5qmNWCgaJpZM4Ayv3U
.
ν μ€νΈλ₯Ό μν΄ μμλ₯Ό μ μ΄νλ ββκ²½μ° μΈμ€ν΄μ€λ³λ‘ μ¬μ μν μλ μμ΅λλ€.
μμ:
function createMockDiv (width, height) {
const div = document.createElement("div");
Object.assign(div.style, {
width: width+"px",
height: height+"px",
});
// we have to mock this for jsdom.
div.getBoundingClientRect = () => ({
width,
height,
top: 0,
left: 0,
right: width,
bottom: height,
});
return div;
}
getboundingclientrect() λ©μλκ° ts νμΌμ if else 쑰건μ μλ κ²½μ° μΉ΄λ₯΄λ§ μ½λλ₯Ό μμ±νλ λ°©λ².
λꡬλ μ§ λμμ£ΌμΈμ.
window.HTMLElement.prototype.getBoundingClientRect = function () {
return {
width: parseFloat(this.style.width) || 0,
height: parseFloat(this.style.height) || 0,
top: parseFloat(this.style.marginTop) || 0,
left: parseFloat(this.style.marginLeft) || 0
}
}
κ·Έλ¦¬κ³ μ΄κ²μ μ€νμ μ μν κ²μ΄κΈ°λ ν©λλ€.
Object.defineProperties(window.HTMLElement.prototype, {
offsetWidth: {
get () { return parseFloat(this.style.width) || 0 }
},
offsetHeight: {
get () { return parseFloat(this.style.height) || 0 }
},
offsetTop: {
get () { return parseFloat(this.style.marginTop) || 0 }
},
offsetLeft: {
get () { return parseFloat(this.style.marginLeft) || 0 }
}
})
κ°μ₯ μ μ©ν λκΈ
ν μ€νΈλ₯Ό μν΄ μμλ₯Ό μ μ΄νλ ββκ²½μ° μΈμ€ν΄μ€λ³λ‘ μ¬μ μν μλ μμ΅λλ€.
μμ: